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[server_id->ip.data_len]);
219 SILC_PUT16_MSB(server_id->rnd, &ret_id[server_id->ip.data_len + 2]);
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[client_id->ip.data_len] = client_id->rnd;
229 memcpy(&ret_id[client_id->ip.data_len + 1], client_id->hash,
233 case SILC_ID_CHANNEL:
234 channel_id = (SilcChannelID *)id;
235 ret_id = silc_calloc(id_len, sizeof(unsigned char));
238 memcpy(ret_id, channel_id->ip.data, channel_id->ip.data_len);
239 SILC_PUT16_MSB(channel_id->port, &ret_id[channel_id->ip.data_len]);
240 SILC_PUT16_MSB(channel_id->rnd, &ret_id[channel_id->ip.data_len + 2]);
248 /* Converts string to a ID */
250 void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type)
256 SilcServerID *server_id;
258 if (id_len != ID_SERVER_LEN_PART + 4 &&
259 id_len != ID_SERVER_LEN_PART + 16)
262 server_id = silc_calloc(1, sizeof(*server_id));
265 memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 4 ?
267 server_id->ip.data_len = (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4);
268 SILC_GET16_MSB(server_id->port, &id[server_id->ip.data_len]);
269 SILC_GET16_MSB(server_id->rnd, &id[server_id->ip.data_len + 2]);
275 SilcClientID *client_id;
277 if (id_len != ID_CLIENT_LEN_PART + 4 &&
278 id_len != ID_CLIENT_LEN_PART + 16)
281 client_id = silc_calloc(1, sizeof(*client_id));
284 memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 4 ?
286 client_id->ip.data_len = (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4);
287 client_id->rnd = id[client_id->ip.data_len];
288 memcpy(client_id->hash, &id[client_id->ip.data_len + 1],
293 case SILC_ID_CHANNEL:
295 SilcChannelID *channel_id;
297 if (id_len != ID_CHANNEL_LEN_PART + 4 &&
298 id_len != ID_CHANNEL_LEN_PART + 16)
301 channel_id = silc_calloc(1, sizeof(*channel_id));
304 memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ?
306 channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4);
307 SILC_GET16_MSB(channel_id->port, &id[channel_id->ip.data_len]);
308 SILC_GET16_MSB(channel_id->rnd, &id[channel_id->ip.data_len + 2]);
317 /* Returns length of the ID */
319 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type)
324 SilcServerID *server_id = (SilcServerID *)id;
325 return ID_SERVER_LEN_PART + server_id->ip.data_len;
330 SilcClientID *client_id = (SilcClientID *)id;
331 return ID_CLIENT_LEN_PART + client_id->ip.data_len;
334 case SILC_ID_CHANNEL:
336 SilcChannelID *channel_id = (SilcChannelID *)id;
337 return ID_CHANNEL_LEN_PART + channel_id->ip.data_len;
345 /* Duplicate ID data */
347 void *silc_id_dup(const void *id, SilcIdType type)
352 SilcServerID *server_id = (SilcServerID *)id;
353 return silc_memdup(server_id, sizeof(*server_id));
358 SilcClientID *client_id = (SilcClientID *)id;
359 return silc_memdup(client_id, sizeof(*client_id));
362 case SILC_ID_CHANNEL:
364 SilcChannelID *channel_id = (SilcChannelID *)id;
365 return silc_memdup(channel_id, sizeof(*channel_id));