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;
46 SILC_LOG_DEBUG(("Parsing Set Mode payload"));
48 new = silc_calloc(1, sizeof(*new));
50 ret = silc_buffer_unformat(buffer,
51 SILC_STR_UI_SHORT(&new->mode_type),
52 SILC_STR_UI_SHORT(&len),
53 SILC_STR_UI_INT(&new->mode_mask),
54 SILC_STR_UI_CHAR(&new->argc),
59 if (len > buffer->len)
63 silc_buffer_pull(buffer, 5);
64 new->args = silc_argument_payload_parse(buffer, new->argc);
65 silc_buffer_push(buffer, 5);
75 /* Encode Set Mode payload with variable argument list. If `argc' is > 0
76 argument payloads will be associated to the Set Mode payload. Variable
77 arguments must be {usigned char *, unsigned int (len)}. */
79 SilcBuffer silc_set_mode_payload_encode(unsigned short mode_type,
80 unsigned int mode_mask,
85 SilcBuffer args = NULL;
87 unsigned int *argv_lens = NULL, *argv_types = NULL;
93 argv = silc_calloc(argc, sizeof(unsigned char *));
94 argv_lens = silc_calloc(argc, sizeof(unsigned int));
95 argv_types = silc_calloc(argc, sizeof(unsigned int));
97 for (i = 0; i < argc; i++) {
98 x = va_arg(ap, unsigned char *);
99 x_len = va_arg(ap, unsigned int);
101 argv[i] = silc_calloc(x_len + 1, sizeof(unsigned char));
102 memcpy(argv[i], x, x_len);
103 argv_lens[i] = x_len;
104 argv_types[i] = i + 1;
107 args = silc_argument_payload_encode(argc, argv, argv_lens, argv_types);
110 for (i = 0; i < argc; i++)
113 silc_free(argv_lens);
114 silc_free(argv_types);
118 buffer = silc_buffer_alloc(len);
119 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
120 silc_buffer_format(buffer,
121 SILC_STR_UI_SHORT(mode_type),
122 SILC_STR_UI_SHORT(len),
123 SILC_STR_UI_INT(mode_mask),
124 SILC_STR_UI_CHAR(argc),
128 silc_buffer_pull(buffer, 5);
129 silc_buffer_format(buffer,
130 SILC_STR_UI_XNSTRING(args->data, args->len),
132 silc_buffer_push(buffer, 5);
133 silc_buffer_free(args);
139 /* Free's Set Mode payload */
141 void silc_set_mode_payload_free(SilcSetModePayload payload)
144 silc_argument_payload_free(payload->args);
149 /* Return mode type */
151 unsigned short silc_set_mode_get_type(SilcSetModePayload payload)
153 return payload->mode_type;
156 /* Return mode mask */
158 unsigned int silc_set_mode_get_mode(SilcSetModePayload payload)
160 return payload->mode_mask;
163 /* Return argument nums */
165 unsigned int silc_set_mode_get_arg_num(SilcSetModePayload payload)
167 return payload->argc;
170 /* Return argument payload */
172 SilcArgumentPayload silc_set_mode_get_args(SilcSetModePayload payload)
174 return payload->args;