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;
45 SILC_LOG_DEBUG(("Parsing Notify payload"));
47 new = silc_calloc(1, sizeof(*new));
49 ret = silc_buffer_unformat(buffer,
50 SILC_STR_UI_SHORT(&new->type),
51 SILC_STR_UI_SHORT(&len),
52 SILC_STR_UI_CHAR(&new->argc),
57 if (len > buffer->len)
61 silc_buffer_pull(buffer, 5);
62 new->args = silc_argument_payload_parse(buffer, new->argc);
63 silc_buffer_push(buffer, 5);
73 /* Encode notify payload with variable argument list. If `argc' is > 0
74 argument payloads will be associated to the notify payload. Variable
75 arguments must be {usigned char *, unsigned int (len)}. */
77 SilcBuffer silc_notify_payload_encode(SilcNotifyType type, unsigned int argc,
81 SilcBuffer args = NULL;
83 unsigned int *argv_lens = NULL, *argv_types = NULL;
89 argv = silc_calloc(argc, sizeof(unsigned char *));
90 argv_lens = silc_calloc(argc, sizeof(unsigned int));
91 argv_types = silc_calloc(argc, sizeof(unsigned int));
93 for (i = 0; i < argc; i++) {
94 x = va_arg(ap, unsigned char *);
95 x_len = va_arg(ap, unsigned int);
97 argv[i] = silc_calloc(x_len + 1, sizeof(unsigned char));
98 memcpy(argv[i], x, x_len);
100 argv_types[i] = i + 1;
103 args = silc_argument_payload_encode(argc, argv, argv_lens, argv_types);
106 for (i = 0; i < argc; i++)
109 silc_free(argv_lens);
110 silc_free(argv_types);
114 buffer = silc_buffer_alloc(len);
115 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
116 silc_buffer_format(buffer,
117 SILC_STR_UI_SHORT(type),
118 SILC_STR_UI_SHORT(len),
119 SILC_STR_UI_CHAR(argc),
123 silc_buffer_pull(buffer, 5);
124 silc_buffer_format(buffer,
125 SILC_STR_UI_XNSTRING(args->data, args->len),
127 silc_buffer_push(buffer, 5);
128 silc_buffer_free(args);
134 /* Free's notify payload */
136 void silc_notify_payload_free(SilcNotifyPayload payload)
139 silc_argument_payload_free(payload->args);
144 /* Return notify type */
146 SilcNotifyType silc_notify_get_type(SilcNotifyPayload payload)
148 return payload->type;
151 /* Return argument nums */
153 unsigned int silc_notify_get_arg_num(SilcNotifyPayload payload)
155 return payload->argc;
158 /* Return argument payload */
160 SilcArgumentPayload silc_notify_get_args(SilcNotifyPayload payload)
162 return payload->args;