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 || newp->len > SILC_PACKET_MAX_ID_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,
90 SilcBufferStruct buffer;
93 unsigned char *id_data;
97 silc_buffer_set(&buffer, (unsigned char *)data, len);
98 ret = silc_buffer_unformat(&buffer,
99 SILC_STR_UI_SHORT(&type),
100 SILC_STR_UI_SHORT(&idlen),
105 silc_buffer_pull(&buffer, 4);
107 if (idlen > buffer.len || idlen > SILC_PACKET_MAX_ID_LEN)
110 ret = silc_buffer_unformat(&buffer,
111 SILC_STR_UI_XNSTRING(&id_data, idlen),
116 id = silc_id_str2id(id_data, idlen, type);
124 /* Encodes ID Payload */
126 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type)
129 unsigned char *id_data;
132 id_data = silc_id_id2str(id, type);
133 len = silc_id_get_len(id, type);
134 buffer = silc_id_payload_encode_data((const unsigned char *)id_data,
140 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
141 SilcUInt32 id_len, SilcIdType type)
145 SILC_LOG_DEBUG(("Encoding %s ID payload",
146 type == SILC_ID_CLIENT ? "Client" :
147 type == SILC_ID_SERVER ? "Server" : "Channel"));
149 buffer = silc_buffer_alloc_size(4 + id_len);
152 silc_buffer_format(buffer,
153 SILC_STR_UI_SHORT(type),
154 SILC_STR_UI_SHORT(id_len),
155 SILC_STR_UI_XNSTRING(id, id_len),
160 /* Free ID Payload */
162 void silc_id_payload_free(SilcIDPayload payload)
165 silc_free(payload->id);
172 SilcIdType silc_id_payload_get_type(SilcIDPayload payload)
174 return payload ? payload->type : 0;
179 void *silc_id_payload_get_id(SilcIDPayload payload)
181 return payload ? silc_id_str2id(payload->id, payload->len,
182 payload->type) : NULL;
185 /* Get raw ID data. Data is duplicated. */
187 unsigned char *silc_id_payload_get_data(SilcIDPayload payload)
192 return silc_memdup(payload->id, payload->len);
195 /* Get length of ID */
197 SilcUInt32 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 SilcUInt32 id_len = silc_id_get_len(id, type);
214 server_id = (SilcServerID *)id;
215 ret_id = silc_calloc(id_len, sizeof(unsigned char));
218 memcpy(ret_id, server_id->ip.data, server_id->ip.data_len);
219 SILC_PUT16_MSB(server_id->port, &ret_id[server_id->ip.data_len]);
220 SILC_PUT16_MSB(server_id->rnd, &ret_id[server_id->ip.data_len + 2]);
224 client_id = (SilcClientID *)id;
225 ret_id = silc_calloc(id_len, sizeof(unsigned char));
228 memcpy(ret_id, client_id->ip.data, client_id->ip.data_len);
229 ret_id[client_id->ip.data_len] = client_id->rnd;
230 memcpy(&ret_id[client_id->ip.data_len + 1], client_id->hash,
234 case SILC_ID_CHANNEL:
235 channel_id = (SilcChannelID *)id;
236 ret_id = silc_calloc(id_len, sizeof(unsigned char));
239 memcpy(ret_id, channel_id->ip.data, channel_id->ip.data_len);
240 SILC_PUT16_MSB(channel_id->port, &ret_id[channel_id->ip.data_len]);
241 SILC_PUT16_MSB(channel_id->rnd, &ret_id[channel_id->ip.data_len + 2]);
249 /* Converts string to a ID */
251 void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
258 SilcServerID *server_id;
260 if (id_len != ID_SERVER_LEN_PART + 4 &&
261 id_len != ID_SERVER_LEN_PART + 16)
264 server_id = silc_calloc(1, sizeof(*server_id));
267 memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 4 ?
269 server_id->ip.data_len = (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4);
270 SILC_GET16_MSB(server_id->port, &id[server_id->ip.data_len]);
271 SILC_GET16_MSB(server_id->rnd, &id[server_id->ip.data_len + 2]);
277 SilcClientID *client_id;
279 if (id_len != ID_CLIENT_LEN_PART + 4 &&
280 id_len != ID_CLIENT_LEN_PART + 16)
283 client_id = silc_calloc(1, sizeof(*client_id));
286 memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 4 ?
288 client_id->ip.data_len = (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4);
289 client_id->rnd = id[client_id->ip.data_len];
290 memcpy(client_id->hash, &id[client_id->ip.data_len + 1],
295 case SILC_ID_CHANNEL:
297 SilcChannelID *channel_id;
299 if (id_len != ID_CHANNEL_LEN_PART + 4 &&
300 id_len != ID_CHANNEL_LEN_PART + 16)
303 channel_id = silc_calloc(1, sizeof(*channel_id));
306 memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ?
308 channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4);
309 SILC_GET16_MSB(channel_id->port, &id[channel_id->ip.data_len]);
310 SILC_GET16_MSB(channel_id->rnd, &id[channel_id->ip.data_len + 2]);
319 /* Returns length of the ID */
321 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type)
326 SilcServerID *server_id = (SilcServerID *)id;
327 return ID_SERVER_LEN_PART + server_id->ip.data_len;
332 SilcClientID *client_id = (SilcClientID *)id;
333 return ID_CLIENT_LEN_PART + client_id->ip.data_len;
336 case SILC_ID_CHANNEL:
338 SilcChannelID *channel_id = (SilcChannelID *)id;
339 return ID_CHANNEL_LEN_PART + channel_id->ip.data_len;
347 /* Duplicate ID data */
349 void *silc_id_dup(const void *id, SilcIdType type)
354 SilcServerID *server_id = (SilcServerID *)id;
355 return silc_memdup(server_id, sizeof(*server_id));
360 SilcClientID *client_id = (SilcClientID *)id;
361 return silc_memdup(client_id, sizeof(*client_id));
364 case SILC_ID_CHANNEL:
366 SilcChannelID *channel_id = (SilcChannelID *)id;
367 return silc_memdup(channel_id, sizeof(*channel_id));