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,
45 SilcUInt32 payload_len)
47 SilcBufferStruct buffer;
51 SILC_LOG_DEBUG(("Parsing ID payload"));
53 silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
54 newp = silc_calloc(1, sizeof(*newp));
58 ret = silc_buffer_unformat(&buffer,
59 SILC_STR_UI_SHORT(&newp->type),
60 SILC_STR_UI_SHORT(&newp->len),
65 silc_buffer_pull(&buffer, 4);
67 if (newp->len > buffer.len)
70 ret = silc_buffer_unformat(&buffer,
71 SILC_STR_UI_XNSTRING_ALLOC(&newp->id, newp->len),
76 silc_buffer_push(&buffer, 4);
85 /* Return the ID directly from the raw payload data. */
87 void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len)
89 SilcBufferStruct buffer;
92 unsigned char *id_data = NULL;
96 silc_buffer_set(&buffer, (unsigned char *)data, len);
97 ret = silc_buffer_unformat(&buffer,
98 SILC_STR_UI_SHORT(&type),
99 SILC_STR_UI_SHORT(&idlen),
104 silc_buffer_pull(&buffer, 4);
106 if (idlen > buffer.len)
109 ret = silc_buffer_unformat(&buffer,
110 SILC_STR_UI_XNSTRING_ALLOC(&id_data, idlen),
115 id = silc_id_str2id(id_data, idlen, type);
123 /* Encodes ID Payload */
125 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type)
128 unsigned char *id_data;
131 id_data = silc_id_id2str(id, type);
132 len = silc_id_get_len(id, type);
133 buffer = silc_id_payload_encode_data((const unsigned char *)id_data,
139 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
140 SilcUInt32 id_len, SilcIdType type)
144 SILC_LOG_DEBUG(("Encoding %s ID payload",
145 type == SILC_ID_CLIENT ? "Client" :
146 type == SILC_ID_SERVER ? "Server" : "Channel"));
148 buffer = silc_buffer_alloc_size(4 + id_len);
151 silc_buffer_format(buffer,
152 SILC_STR_UI_SHORT(type),
153 SILC_STR_UI_SHORT(id_len),
154 SILC_STR_UI_XNSTRING(id, id_len),
159 /* Free ID Payload */
161 void silc_id_payload_free(SilcIDPayload payload)
164 silc_free(payload->id);
171 SilcIdType silc_id_payload_get_type(SilcIDPayload payload)
173 return payload ? payload->type : 0;
178 void *silc_id_payload_get_id(SilcIDPayload payload)
180 return payload ? silc_id_str2id(payload->id, payload->len,
181 payload->type) : NULL;
184 /* Get raw ID data. Data is duplicated. */
186 unsigned char *silc_id_payload_get_data(SilcIDPayload payload)
191 return silc_memdup(payload->id, payload->len);
194 /* Get length of ID */
196 SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload)
198 return payload ? payload->len : 0;
201 /* Converts ID to string. */
203 unsigned char *silc_id_id2str(const void *id, SilcIdType type)
205 unsigned char *ret_id;
206 SilcServerID *server_id;
207 SilcClientID *client_id;
208 SilcChannelID *channel_id;
209 SilcUInt32 id_len = silc_id_get_len(id, type);
213 server_id = (SilcServerID *)id;
214 ret_id = silc_calloc(id_len, sizeof(unsigned char));
217 memcpy(ret_id, server_id->ip.data, server_id->ip.data_len);
218 SILC_PUT16_MSB(server_id->port, &ret_id[4]);
219 SILC_PUT16_MSB(server_id->rnd, &ret_id[6]);
223 client_id = (SilcClientID *)id;
224 ret_id = silc_calloc(id_len, sizeof(unsigned char));
227 memcpy(ret_id, client_id->ip.data, client_id->ip.data_len);
228 ret_id[4] = client_id->rnd;
229 memcpy(&ret_id[5], client_id->hash, CLIENTID_HASH_LEN);
232 case SILC_ID_CHANNEL:
233 channel_id = (SilcChannelID *)id;
234 ret_id = silc_calloc(id_len, sizeof(unsigned char));
237 memcpy(ret_id, channel_id->ip.data, channel_id->ip.data_len);
238 SILC_PUT16_MSB(channel_id->port, &ret_id[4]);
239 SILC_PUT16_MSB(channel_id->rnd, &ret_id[6]);
247 /* Converts string to a ID */
249 void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type)
255 SilcServerID *server_id;
257 if (id_len != ID_SERVER_LEN_PART + 4 &&
258 id_len != ID_SERVER_LEN_PART + 16)
261 server_id = silc_calloc(1, sizeof(*server_id));
264 memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 4 ?
266 server_id->ip.data_len = (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4);
267 SILC_GET16_MSB(server_id->port, &id[4]);
268 SILC_GET16_MSB(server_id->rnd, &id[6]);
274 SilcClientID *client_id;
276 if (id_len != ID_CLIENT_LEN_PART + 4 &&
277 id_len != ID_CLIENT_LEN_PART + 16)
280 client_id = silc_calloc(1, sizeof(*client_id));
283 memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 4 ?
285 client_id->ip.data_len = (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4);
286 client_id->rnd = id[4];
287 memcpy(client_id->hash, &id[5], CLIENTID_HASH_LEN);
291 case SILC_ID_CHANNEL:
293 SilcChannelID *channel_id;
295 if (id_len != ID_CHANNEL_LEN_PART + 4 &&
296 id_len != ID_CHANNEL_LEN_PART + 16)
299 channel_id = silc_calloc(1, sizeof(*channel_id));
302 memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ?
304 channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4);
305 SILC_GET16_MSB(channel_id->port, &id[4]);
306 SILC_GET16_MSB(channel_id->rnd, &id[6]);
315 /* Returns length of the ID */
317 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type)
322 SilcServerID *server_id = (SilcServerID *)id;
323 return ID_SERVER_LEN_PART + server_id->ip.data_len;
328 SilcClientID *client_id = (SilcClientID *)id;
329 return ID_CLIENT_LEN_PART + client_id->ip.data_len;
332 case SILC_ID_CHANNEL:
334 SilcChannelID *channel_id = (SilcChannelID *)id;
335 return ID_CHANNEL_LEN_PART + channel_id->ip.data_len;
343 /* Duplicate ID data */
345 void *silc_id_dup(const void *id, SilcIdType type)
350 SilcServerID *server_id = (SilcServerID *)id;
351 return silc_memdup(server_id, sizeof(*server_id));
356 SilcClientID *client_id = (SilcClientID *)id;
357 return silc_memdup(client_id, sizeof(*client_id));
360 case SILC_ID_CHANNEL:
362 SilcChannelID *channel_id = (SilcChannelID *)id;
363 return silc_memdup(channel_id, sizeof(*channel_id));