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.
22 #include "silcincludes.h"
23 #include "silcnotify.h"
25 /******************************************************************************
29 ******************************************************************************/
31 struct SilcNotifyPayloadStruct {
34 SilcArgumentPayload args;
37 /* Parse notify payload buffer and return data into payload structure */
39 SilcNotifyPayload silc_notify_payload_parse(SilcBuffer buffer)
41 SilcNotifyPayload new;
44 SILC_LOG_DEBUG(("Parsing Notify payload"));
46 new = silc_calloc(1, sizeof(*new));
48 silc_buffer_unformat(buffer,
49 SILC_STR_UI_SHORT(&new->type),
50 SILC_STR_UI_SHORT(&len),
51 SILC_STR_UI_CHAR(&new->argc),
54 if (len > buffer->len)
58 silc_buffer_pull(buffer, 5);
59 new->args = silc_argument_payload_parse(buffer, new->argc);
60 silc_buffer_push(buffer, 5);
70 /* Encode notify payload with variable argument list. If `argc' is > 0
71 argument payloads will be associated to the notify payload. Variable
72 arguments must be {usigned char *, unsigned int (len)}. */
74 SilcBuffer silc_notify_payload_encode(SilcNotifyType type, unsigned int argc,
78 SilcBuffer args = NULL;
80 unsigned int *argv_lens = NULL, *argv_types = NULL;
86 argv = silc_calloc(argc, sizeof(unsigned char *));
87 argv_lens = silc_calloc(argc, sizeof(unsigned int));
88 argv_types = silc_calloc(argc, sizeof(unsigned int));
90 for (i = 0; i < argc; i++) {
91 x = va_arg(ap, unsigned char *);
92 x_len = va_arg(ap, unsigned int);
94 argv[i] = silc_calloc(x_len + 1, sizeof(unsigned char));
95 memcpy(argv[i], x, x_len);
97 argv_types[i] = i + 1;
100 args = silc_argument_payload_encode(argc, argv, argv_lens, argv_types);
103 for (i = 0; i < argc; i++)
106 silc_free(argv_lens);
107 silc_free(argv_types);
111 buffer = silc_buffer_alloc(len);
112 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
113 silc_buffer_format(buffer,
114 SILC_STR_UI_SHORT(type),
115 SILC_STR_UI_SHORT(len),
116 SILC_STR_UI_CHAR(argc),
120 silc_buffer_pull(buffer, 5);
121 silc_buffer_format(buffer,
122 SILC_STR_UI_XNSTRING(args->data, args->len),
124 silc_buffer_push(buffer, 5);
125 silc_buffer_free(args);
131 /* Free's notify payload */
133 void silc_notify_payload_free(SilcNotifyPayload payload)
136 silc_argument_payload_free(payload->args);
141 /* Return notify type */
143 SilcNotifyType silc_notify_get_type(SilcNotifyPayload payload)
145 return payload->type;
148 /* Return argument nums */
150 unsigned int silc_notify_get_arg_num(SilcNotifyPayload payload)
152 return payload->argc;
155 /* Return argument payload */
157 SilcArgumentPayload silc_notify_get_args(SilcNotifyPayload payload)
159 return payload->args;