5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
22 #include "silcincludes.h"
25 /******************************************************************************
29 ******************************************************************************/
31 struct SilcSetModePayloadStruct {
32 unsigned short mode_type;
33 unsigned int mode_mask;
35 SilcArgumentPayload args;
38 /* Parse Set Mode payload buffer and return data into payload structure */
40 SilcSetModePayload silc_set_mode_payload_parse(SilcBuffer buffer)
42 SilcSetModePayload new;
45 SILC_LOG_DEBUG(("Parsing Set Mode payload"));
47 new = silc_calloc(1, sizeof(*new));
49 silc_buffer_unformat(buffer,
50 SILC_STR_UI_SHORT(&new->mode_type),
51 SILC_STR_UI_SHORT(&len),
52 SILC_STR_UI_INT(&new->mode_mask),
53 SILC_STR_UI_CHAR(&new->argc),
56 if (len > buffer->len)
60 silc_buffer_pull(buffer, 5);
61 new->args = silc_argument_payload_parse(buffer, new->argc);
62 silc_buffer_push(buffer, 5);
72 /* Encode Set Mode payload with variable argument list. If `argc' is > 0
73 argument payloads will be associated to the Set Mode payload. Variable
74 arguments must be {usigned char *, unsigned int (len)}. */
76 SilcBuffer silc_set_mode_payload_encode(unsigned short mode_type,
77 unsigned int mode_mask,
82 SilcBuffer args = NULL;
84 unsigned int *argv_lens = NULL, *argv_types = NULL;
90 argv = silc_calloc(argc, sizeof(unsigned char *));
91 argv_lens = silc_calloc(argc, sizeof(unsigned int));
92 argv_types = silc_calloc(argc, sizeof(unsigned int));
94 for (i = 0; i < argc; i++) {
95 x = va_arg(ap, unsigned char *);
96 x_len = va_arg(ap, unsigned int);
98 argv[i] = silc_calloc(x_len + 1, sizeof(unsigned char));
99 memcpy(argv[i], x, x_len);
100 argv_lens[i] = x_len;
101 argv_types[i] = i + 1;
104 args = silc_argument_payload_encode(argc, argv, argv_lens, argv_types);
107 for (i = 0; i < argc; i++)
110 silc_free(argv_lens);
111 silc_free(argv_types);
115 buffer = silc_buffer_alloc(len);
116 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
117 silc_buffer_format(buffer,
118 SILC_STR_UI_SHORT(mode_type),
119 SILC_STR_UI_SHORT(len),
120 SILC_STR_UI_INT(mode_mask),
121 SILC_STR_UI_CHAR(argc),
125 silc_buffer_pull(buffer, 5);
126 silc_buffer_format(buffer,
127 SILC_STR_UI_XNSTRING(args->data, args->len),
129 silc_buffer_push(buffer, 5);
130 silc_buffer_free(args);
136 /* Free's Set Mode payload */
138 void silc_set_mode_payload_free(SilcSetModePayload payload)
141 silc_argument_payload_free(payload->args);
146 /* Return mode type */
148 unsigned short silc_set_mode_get_type(SilcSetModePayload payload)
150 return payload->mode_type;
153 /* Return mode mask */
155 unsigned int silc_set_mode_get_mode(SilcSetModePayload payload)
157 return payload->mode_mask;
160 /* Return argument nums */
162 unsigned int silc_set_mode_get_arg_num(SilcSetModePayload payload)
164 return payload->argc;
167 /* Return argument payload */
169 SilcArgumentPayload silc_set_mode_get_args(SilcSetModePayload payload)
171 return payload->args;