5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 2000 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.
20 /* Implementation of generic payloads described in the protocol
21 specification drafts. */
24 #include "silcincludes.h"
25 #include "silcpayload.h"
27 /******************************************************************************
31 ******************************************************************************/
33 struct SilcIDPayloadStruct {
39 /* Parses buffer and return ID payload into payload structure */
41 SilcIDPayload silc_id_payload_parse(SilcBuffer buffer)
46 SILC_LOG_DEBUG(("Parsing ID payload"));
48 new = silc_calloc(1, sizeof(*new));
50 ret = silc_buffer_unformat(buffer,
51 SILC_STR_UI_SHORT(&new->type),
52 SILC_STR_UI_SHORT(&new->len),
57 silc_buffer_pull(buffer, 4);
59 if (new->len > buffer->len)
62 ret = silc_buffer_unformat(buffer,
63 SILC_STR_UI_XNSTRING_ALLOC(&new->id, new->len),
68 silc_buffer_push(buffer, 4);
77 /* Parses data and return ID payload into payload structure. */
79 SilcIDPayload silc_id_payload_parse_data(unsigned char *data,
83 SilcBufferStruct buffer;
86 SILC_LOG_DEBUG(("Parsing ID payload"));
88 silc_buffer_set(&buffer, data, len);
90 new = silc_calloc(1, sizeof(*new));
92 ret = silc_buffer_unformat(&buffer,
93 SILC_STR_UI_SHORT(&new->type),
94 SILC_STR_UI_SHORT(&new->len),
99 silc_buffer_pull(&buffer, 4);
101 if (new->len > buffer.len)
104 ret = silc_buffer_unformat(&buffer,
105 SILC_STR_UI_XNSTRING_ALLOC(&new->id, new->len),
117 /* Return the ID directly from the raw payload data. */
119 void *silc_id_payload_parse_id(unsigned char *data, uint32 len)
121 SilcBufferStruct buffer;
124 unsigned char *id_data = NULL;
128 silc_buffer_set(&buffer, data, len);
130 ret = silc_buffer_unformat(&buffer,
131 SILC_STR_UI_SHORT(&type),
132 SILC_STR_UI_SHORT(&idlen),
137 silc_buffer_pull(&buffer, 4);
139 if (idlen > buffer.len)
142 ret = silc_buffer_unformat(&buffer,
143 SILC_STR_UI_XNSTRING_ALLOC(&id_data, idlen),
148 id = silc_id_str2id(id_data, idlen, type);
156 /* Encodes ID Payload */
158 SilcBuffer silc_id_payload_encode(void *id, SilcIdType type)
161 unsigned char *id_data;
164 id_data = silc_id_id2str(id, type);
165 len = silc_id_get_len(id, type);
166 buffer = silc_id_payload_encode_data((const unsigned char *)id_data,
172 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
173 uint32 id_len, SilcIdType type)
177 SILC_LOG_DEBUG(("Encoding %s ID payload",
178 type == SILC_ID_CLIENT ? "Client" :
179 type == SILC_ID_SERVER ? "Server" : "Channel"));
181 buffer = silc_buffer_alloc(4 + id_len);
182 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
183 silc_buffer_format(buffer,
184 SILC_STR_UI_SHORT(type),
185 SILC_STR_UI_SHORT(id_len),
186 SILC_STR_UI_XNSTRING(id, id_len),
191 /* Free ID Payload */
193 void silc_id_payload_free(SilcIDPayload payload)
196 silc_free(payload->id);
203 SilcIdType silc_id_payload_get_type(SilcIDPayload payload)
205 return payload ? payload->type : 0;
210 void *silc_id_payload_get_id(SilcIDPayload payload)
212 return payload ? silc_id_str2id(payload->id, payload->len,
213 payload->type) : NULL;
216 /* Get raw ID data. Data is duplicated. */
218 unsigned char *silc_id_payload_get_data(SilcIDPayload payload)
225 ret = silc_calloc(payload->len, sizeof(*ret));
226 memcpy(ret, payload->id, payload->len);
230 /* Get length of ID */
232 uint32 silc_id_payload_get_len(SilcIDPayload payload)
234 return payload ? payload->len : 0;
237 /******************************************************************************
241 ******************************************************************************/
243 struct SilcArgumentPayloadStruct {
245 unsigned char **argv;
251 /* Parses arguments and returns them into Argument Payload structure. */
253 SilcArgumentPayload silc_argument_payload_parse(SilcBuffer buffer,
256 SilcArgumentPayload new;
257 uint16 payload_len = 0;
258 unsigned char arg_num = 0;
259 unsigned char arg_type = 0;
263 SILC_LOG_DEBUG(("Parsing argument payload"));
265 new = silc_calloc(1, sizeof(*new));
266 new->argv = silc_calloc(argc, sizeof(unsigned char *));
267 new->argv_lens = silc_calloc(argc, sizeof(uint32));
268 new->argv_types = silc_calloc(argc, sizeof(uint32));
272 for (i = 0; i < argc; i++) {
273 ret = silc_buffer_unformat(buffer,
274 SILC_STR_UI_SHORT(&payload_len),
275 SILC_STR_UI_CHAR(&arg_type),
280 new->argv_lens[i] = payload_len;
281 new->argv_types[i] = arg_type;
283 if (payload_len > buffer->len - 3)
286 /* Get argument data */
287 silc_buffer_pull(buffer, 3);
288 ret = silc_buffer_unformat(buffer,
289 SILC_STR_UI_XNSTRING_ALLOC(&new->argv[i],
295 silc_buffer_pull(buffer, payload_len);
296 pull_len += 3 + payload_len;
299 if (buffer->len != 0)
305 silc_buffer_push(buffer, pull_len);
313 for (k = 0; k < i; k++)
314 silc_free(new->argv[k]);
317 silc_free(new->argv);
318 silc_free(new->argv_lens);
319 silc_free(new->argv_types);
327 /* Encodes arguments in to Argument Paylods returning them to SilcBuffer. */
329 SilcBuffer silc_argument_payload_encode(uint32 argc,
330 unsigned char **argv,
338 SILC_LOG_DEBUG(("Encoding Argument payload"));
341 for (i = 0; i < argc; i++)
342 len += 3 + argv_lens[i];
344 buffer = silc_buffer_alloc(len);
345 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
348 for (i = 0; i < argc; i++) {
349 silc_buffer_format(buffer,
350 SILC_STR_UI_SHORT(argv_lens[i]),
351 SILC_STR_UI_CHAR(argv_types[i]),
352 SILC_STR_UI_XNSTRING(argv[i], argv_lens[i]),
354 silc_buffer_pull(buffer, 3 + argv_lens[i]);
357 silc_buffer_push(buffer, len);
362 /* Same as above but encode the buffer from SilcArgumentPayload structure
363 instead of raw data. */
365 SilcBuffer silc_argument_payload_encode_payload(SilcArgumentPayload payload)
371 SILC_LOG_DEBUG(("Encoding Argument payload"));
374 for (i = 0; i < payload->argc; i++)
375 len += 3 + payload->argv_lens[i];
377 buffer = silc_buffer_alloc(len);
378 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
381 for (i = 0; i < payload->argc; i++) {
382 silc_buffer_format(buffer,
383 SILC_STR_UI_SHORT(payload->argv_lens[i]),
384 SILC_STR_UI_CHAR(payload->argv_types[i]),
385 SILC_STR_UI_XNSTRING(payload->argv[i],
386 payload->argv_lens[i]),
388 silc_buffer_pull(buffer, 3 + payload->argv_lens[i]);
391 silc_buffer_push(buffer, len);
396 /* Frees Argument Payload */
398 void silc_argument_payload_free(SilcArgumentPayload payload)
403 for (i = 0; i < payload->argc; i++)
404 silc_free(payload->argv[i]);
406 silc_free(payload->argv);
407 silc_free(payload->argv_lens);
408 silc_free(payload->argv_types);
413 /* Returns number of arguments in payload */
415 uint32 silc_argument_get_arg_num(SilcArgumentPayload payload)
417 return payload ? payload->argc : 0;
420 /* Returns first argument from payload. */
422 unsigned char *silc_argument_get_first_arg(SilcArgumentPayload payload,
431 *ret_len = payload->argv_lens[payload->pos];
433 return payload->argv[payload->pos++];
436 /* Returns next argument from payload or NULL if no more arguments. */
438 unsigned char *silc_argument_get_next_arg(SilcArgumentPayload payload,
444 if (payload->pos >= payload->argc)
448 *ret_len = payload->argv_lens[payload->pos];
450 return payload->argv[payload->pos++];
453 /* Returns argument which type is `type'. */
455 unsigned char *silc_argument_get_arg_type(SilcArgumentPayload payload,
464 for (i = 0; i < payload->argc; i++)
465 if (payload->argv_types[i] == type)
468 if (i >= payload->argc)
472 *ret_len = payload->argv_lens[i];
474 return payload->argv[i];