5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 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 /* ID lengths (in bytes) without the IP address part */
26 #define ID_SERVER_LEN_PART 4
27 #define ID_CLIENT_LEN_PART CLIENTID_HASH_LEN + 1
28 #define ID_CHANNEL_LEN_PART 4
30 /******************************************************************************
34 ******************************************************************************/
36 struct SilcIDPayloadStruct {
42 /* Parses buffer and return ID payload into payload structure */
44 SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
47 SilcBufferStruct buffer;
51 SILC_LOG_DEBUG(("Parsing ID payload"));
53 silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
54 new = silc_calloc(1, sizeof(*new));
56 ret = silc_buffer_unformat(&buffer,
57 SILC_STR_UI_SHORT(&new->type),
58 SILC_STR_UI_SHORT(&new->len),
63 silc_buffer_pull(&buffer, 4);
65 if (new->len > buffer.len)
68 ret = silc_buffer_unformat(&buffer,
69 SILC_STR_UI_XNSTRING_ALLOC(&new->id, new->len),
74 silc_buffer_push(&buffer, 4);
83 /* Return the ID directly from the raw payload data. */
85 void *silc_id_payload_parse_id(const unsigned char *data, uint32 len)
87 SilcBufferStruct buffer;
90 unsigned char *id_data = NULL;
94 silc_buffer_set(&buffer, (unsigned char *)data, len);
95 ret = silc_buffer_unformat(&buffer,
96 SILC_STR_UI_SHORT(&type),
97 SILC_STR_UI_SHORT(&idlen),
102 silc_buffer_pull(&buffer, 4);
104 if (idlen > buffer.len)
107 ret = silc_buffer_unformat(&buffer,
108 SILC_STR_UI_XNSTRING_ALLOC(&id_data, idlen),
113 id = silc_id_str2id(id_data, idlen, type);
121 /* Encodes ID Payload */
123 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type)
126 unsigned char *id_data;
129 id_data = silc_id_id2str(id, type);
130 len = silc_id_get_len(id, type);
131 buffer = silc_id_payload_encode_data((const unsigned char *)id_data,
137 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
138 uint32 id_len, SilcIdType type)
142 SILC_LOG_DEBUG(("Encoding %s ID payload",
143 type == SILC_ID_CLIENT ? "Client" :
144 type == SILC_ID_SERVER ? "Server" : "Channel"));
146 buffer = silc_buffer_alloc(4 + id_len);
147 silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
148 silc_buffer_format(buffer,
149 SILC_STR_UI_SHORT(type),
150 SILC_STR_UI_SHORT(id_len),
151 SILC_STR_UI_XNSTRING(id, id_len),
156 /* Free ID Payload */
158 void silc_id_payload_free(SilcIDPayload payload)
161 silc_free(payload->id);
168 SilcIdType silc_id_payload_get_type(SilcIDPayload payload)
170 return payload ? payload->type : 0;
175 void *silc_id_payload_get_id(SilcIDPayload payload)
177 return payload ? silc_id_str2id(payload->id, payload->len,
178 payload->type) : NULL;
181 /* Get raw ID data. Data is duplicated. */
183 unsigned char *silc_id_payload_get_data(SilcIDPayload payload)
190 ret = silc_calloc(payload->len, sizeof(*ret));
191 memcpy(ret, payload->id, payload->len);
195 /* Get length of ID */
197 uint32 silc_id_payload_get_len(SilcIDPayload payload)
199 return payload ? payload->len : 0;
202 /* Converts ID to string. */
204 unsigned char *silc_id_id2str(const void *id, SilcIdType type)
206 unsigned char *ret_id;
207 SilcServerID *server_id;
208 SilcClientID *client_id;
209 SilcChannelID *channel_id;
210 uint32 id_len = silc_id_get_len(id, type);
214 server_id = (SilcServerID *)id;
215 ret_id = silc_calloc(id_len, sizeof(unsigned char));
216 memcpy(ret_id, server_id->ip.data, server_id->ip.data_len);
217 SILC_PUT16_MSB(server_id->port, &ret_id[4]);
218 SILC_PUT16_MSB(server_id->rnd, &ret_id[6]);
222 client_id = (SilcClientID *)id;
223 ret_id = silc_calloc(id_len, sizeof(unsigned char));
224 memcpy(ret_id, client_id->ip.data, client_id->ip.data_len);
225 ret_id[4] = client_id->rnd;
226 memcpy(&ret_id[5], client_id->hash, CLIENTID_HASH_LEN);
229 case SILC_ID_CHANNEL:
230 channel_id = (SilcChannelID *)id;
231 ret_id = silc_calloc(id_len, sizeof(unsigned char));
232 memcpy(ret_id, channel_id->ip.data, channel_id->ip.data_len);
233 SILC_PUT16_MSB(channel_id->port, &ret_id[4]);
234 SILC_PUT16_MSB(channel_id->rnd, &ret_id[6]);
242 /* Converts string to a ID */
244 void *silc_id_str2id(const unsigned char *id, uint32 id_len, SilcIdType type)
250 SilcServerID *server_id;
252 if (id_len != ID_SERVER_LEN_PART + 4 &&
253 id_len != ID_SERVER_LEN_PART + 16)
256 server_id = silc_calloc(1, sizeof(*server_id));
257 memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 4 ?
259 server_id->ip.data_len = (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4);
260 SILC_GET16_MSB(server_id->port, &id[4]);
261 SILC_GET16_MSB(server_id->rnd, &id[6]);
267 SilcClientID *client_id;
269 if (id_len != ID_CLIENT_LEN_PART + 4 &&
270 id_len != ID_CLIENT_LEN_PART + 16)
273 client_id = silc_calloc(1, sizeof(*client_id));
274 memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 4 ?
276 client_id->ip.data_len = (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4);
277 client_id->rnd = id[4];
278 memcpy(client_id->hash, &id[5], CLIENTID_HASH_LEN);
282 case SILC_ID_CHANNEL:
284 SilcChannelID *channel_id;
286 if (id_len != ID_CHANNEL_LEN_PART + 4 &&
287 id_len != ID_CHANNEL_LEN_PART + 16)
290 channel_id = silc_calloc(1, sizeof(*channel_id));
291 memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ?
293 channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4);
294 SILC_GET16_MSB(channel_id->port, &id[4]);
295 SILC_GET16_MSB(channel_id->rnd, &id[6]);
304 /* Returns length of the ID */
306 uint32 silc_id_get_len(const void *id, SilcIdType type)
311 SilcServerID *server_id = (SilcServerID *)id;
312 return ID_SERVER_LEN_PART + server_id->ip.data_len;
317 SilcClientID *client_id = (SilcClientID *)id;
318 return ID_CLIENT_LEN_PART + client_id->ip.data_len;
321 case SILC_ID_CHANNEL:
323 SilcChannelID *channel_id = (SilcChannelID *)id;
324 return ID_CHANNEL_LEN_PART + channel_id->ip.data_len;
332 /* Duplicate ID data */
334 void *silc_id_dup(const void *id, SilcIdType type)
339 SilcServerID *server_id = (SilcServerID *)id, *new;
340 new = silc_calloc(1, sizeof(*server_id));
341 memcpy(new, server_id, sizeof(*server_id));
347 SilcClientID *client_id = (SilcClientID *)id, *new;
348 new = silc_calloc(1, sizeof(*client_id));
349 memcpy(new, client_id, sizeof(*client_id));
353 case SILC_ID_CHANNEL:
355 SilcChannelID *channel_id = (SilcChannelID *)id, *new;
356 new = silc_calloc(1, sizeof(*channel_id));
357 memcpy(new, channel_id, sizeof(*channel_id));