1d6b59b9fafdbe09e8f7885b0c606e7d0705cf6e
[silc.git] / lib / silccore / silcid.c
1 /*
2
3   id.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2005 Pekka Riikonen
8
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; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19 /* $Id$ */
20
21 #include "silcincludes.h"
22 #include "silcid.h"
23
24 /* ID lengths (in bytes) without the IP address part */
25 #define ID_SERVER_LEN_PART      4
26 #define ID_CLIENT_LEN_PART      CLIENTID_HASH_LEN + 1
27 #define ID_CHANNEL_LEN_PART     4
28
29 /******************************************************************************
30
31                                 ID Payload
32
33 ******************************************************************************/
34
35 struct SilcIDPayloadStruct {
36   SilcIdType type;
37   SilcUInt16 len;
38   unsigned char *id;
39 };
40
41 /* Parses buffer and return ID payload into payload structure */
42
43 SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
44                                     SilcUInt32 payload_len)
45 {
46   SilcBufferStruct buffer;
47   SilcIDPayload newp;
48   int ret;
49
50   silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
51   newp = silc_calloc(1, sizeof(*newp));
52   if (!newp)
53     return NULL;
54
55   ret = silc_buffer_unformat(&buffer,
56                              SILC_STR_UI_SHORT(&newp->type),
57                              SILC_STR_UI_SHORT(&newp->len),
58                              SILC_STR_END);
59   if (ret == -1)
60     goto err;
61
62   if (newp->type > SILC_ID_CHANNEL)
63     goto err;
64
65   silc_buffer_pull(&buffer, 4);
66
67   if (newp->len > silc_buffer_len(&buffer) || newp->len > SILC_PACKET_MAX_ID_LEN)
68     goto err;
69
70   ret = silc_buffer_unformat(&buffer,
71                              SILC_STR_UI_XNSTRING_ALLOC(&newp->id, newp->len),
72                              SILC_STR_END);
73   if (ret == -1)
74     goto err;
75
76   silc_buffer_push(&buffer, 4);
77
78   return newp;
79
80  err:
81   SILC_LOG_DEBUG(("Error parsing ID payload"));
82   silc_free(newp);
83   return NULL;
84 }
85
86 /* Return the ID directly from the raw payload data. */
87
88 void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len,
89                                SilcIdType *ret_type)
90 {
91   SilcBufferStruct buffer;
92   SilcIdType type;
93   SilcUInt16 idlen;
94   unsigned char *id_data;
95   int ret;
96   void *id;
97
98   silc_buffer_set(&buffer, (unsigned char *)data, len);
99   ret = silc_buffer_unformat(&buffer,
100                              SILC_STR_UI_SHORT(&type),
101                              SILC_STR_UI_SHORT(&idlen),
102                              SILC_STR_END);
103   if (ret == -1)
104     goto err;
105
106   if (type > SILC_ID_CHANNEL)
107     goto err;
108
109   silc_buffer_pull(&buffer, 4);
110
111   if (idlen > silc_buffer_len(&buffer) || idlen > SILC_PACKET_MAX_ID_LEN)
112     goto err;
113
114   ret = silc_buffer_unformat(&buffer,
115                              SILC_STR_UI_XNSTRING(&id_data, idlen),
116                              SILC_STR_END);
117   if (ret == -1)
118     goto err;
119
120   id = silc_id_str2id(id_data, idlen, type);
121
122   if (ret_type)
123     *ret_type = type;
124
125   return id;
126
127  err:
128   SILC_LOG_DEBUG(("Error parsing ID payload"));
129   return NULL;
130 }
131
132 /* Encodes ID Payload */
133
134 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type)
135 {
136   SilcBuffer buffer;
137   unsigned char *id_data;
138   SilcUInt32 len;
139
140   id_data = silc_id_id2str(id, type);
141   len = silc_id_get_len(id, type);
142   buffer = silc_id_payload_encode_data((const unsigned char *)id_data,
143                                        len, type);
144   silc_free(id_data);
145   return buffer;
146 }
147
148 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
149                                        SilcUInt32 id_len, SilcIdType type)
150 {
151   SilcBuffer buffer;
152
153   buffer = silc_buffer_alloc_size(4 + id_len);
154   if (!buffer)
155     return NULL;
156   silc_buffer_format(buffer,
157                      SILC_STR_UI_SHORT(type),
158                      SILC_STR_UI_SHORT(id_len),
159                      SILC_STR_UI_XNSTRING(id, id_len),
160                      SILC_STR_END);
161   return buffer;
162 }
163
164 /* Free ID Payload */
165
166 void silc_id_payload_free(SilcIDPayload payload)
167 {
168   if (payload) {
169     silc_free(payload->id);
170     silc_free(payload);
171   }
172 }
173
174 /* Get ID type */
175
176 SilcIdType silc_id_payload_get_type(SilcIDPayload payload)
177 {
178   return payload ? payload->type : 0;
179 }
180
181 /* Get ID */
182
183 void *silc_id_payload_get_id(SilcIDPayload payload)
184 {
185   return payload ? silc_id_str2id(payload->id, payload->len,
186                                   payload->type) : NULL;
187 }
188
189 /* Get raw ID data. Data is duplicated. */
190
191 unsigned char *silc_id_payload_get_data(SilcIDPayload payload)
192 {
193   if (!payload)
194     return NULL;
195
196   return silc_memdup(payload->id, payload->len);
197 }
198
199 /* Get length of ID */
200
201 SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload)
202 {
203   return payload ? payload->len : 0;
204 }
205
206 /* Converts ID to string. */
207
208 unsigned char *silc_id_id2str(const void *id, SilcIdType type,
209                               SilcUInt32 *ret_id_len)
210 {
211   unsigned char *ret_id;
212   SilcServerID *server_id;
213   SilcClientID *client_id;
214   SilcChannelID *channel_id;
215   SilcUInt32 id_len = silc_id_get_len(id, type);
216
217   if (ret_id_len)
218     *ret_id_len = id_len;
219
220   if (id_len > SILC_PACKET_MAX_ID_LEN)
221     return NULL;
222
223   switch(type) {
224   case SILC_ID_SERVER:
225     server_id = (SilcServerID *)id;
226     ret_id = silc_calloc(id_len, sizeof(unsigned char));
227     if (!ret_id)
228       return NULL;
229     memcpy(ret_id, server_id->ip.data, server_id->ip.data_len);
230     SILC_PUT16_MSB(server_id->port, &ret_id[server_id->ip.data_len]);
231     SILC_PUT16_MSB(server_id->rnd, &ret_id[server_id->ip.data_len + 2]);
232     return ret_id;
233     break;
234   case SILC_ID_CLIENT:
235     client_id = (SilcClientID *)id;
236     ret_id = silc_calloc(id_len, sizeof(unsigned char));
237     if (!ret_id)
238       return NULL;
239     memcpy(ret_id, client_id->ip.data, client_id->ip.data_len);
240     ret_id[client_id->ip.data_len] = client_id->rnd;
241     memcpy(&ret_id[client_id->ip.data_len + 1], client_id->hash,
242            CLIENTID_HASH_LEN);
243     return ret_id;
244     break;
245   case SILC_ID_CHANNEL:
246     channel_id = (SilcChannelID *)id;
247     ret_id = silc_calloc(id_len, sizeof(unsigned char));
248     if (!ret_id)
249       return NULL;
250     memcpy(ret_id, channel_id->ip.data, channel_id->ip.data_len);
251     SILC_PUT16_MSB(channel_id->port, &ret_id[channel_id->ip.data_len]);
252     SILC_PUT16_MSB(channel_id->rnd, &ret_id[channel_id->ip.data_len + 2]);
253     return ret_id;
254     break;
255   }
256
257   return NULL;
258 }
259
260 /* Converts string to a ID */
261
262 bool silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
263                     SilcIdType type, void *ret_id)
264 {
265   if (id_len > SILC_PACKET_MAX_ID_LEN)
266     return FALSE;
267
268   switch(type) {
269   case SILC_ID_SERVER:
270     {
271       SilcServerID *server_id = ret_id;
272
273       if (id_len != ID_SERVER_LEN_PART + 4 &&
274           id_len != ID_SERVER_LEN_PART + 16)
275         return FALSE;
276
277       memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 4 ?
278                                       16 : 4));
279       server_id->ip.data_len = (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4);
280       SILC_GET16_MSB(server_id->port, &id[server_id->ip.data_len]);
281       SILC_GET16_MSB(server_id->rnd, &id[server_id->ip.data_len + 2]);
282       return TRUE;
283     }
284     break;
285   case SILC_ID_CLIENT:
286     {
287       SilcClientID *client_id = ret_id;
288
289       if (id_len != ID_CLIENT_LEN_PART + 4 &&
290           id_len != ID_CLIENT_LEN_PART + 16)
291         return FALSE;
292
293       memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 4 ?
294                                       16 : 4));
295       client_id->ip.data_len = (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4);
296       client_id->rnd = id[client_id->ip.data_len];
297       memcpy(client_id->hash, &id[client_id->ip.data_len + 1],
298              CLIENTID_HASH_LEN);
299       return TRUE;
300     }
301     break;
302   case SILC_ID_CHANNEL:
303     {
304       SilcChannelID *channel_id = ret_id;
305
306       if (id_len != ID_CHANNEL_LEN_PART + 4 &&
307           id_len != ID_CHANNEL_LEN_PART + 16)
308         return FALSE;
309
310       memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ?
311                                        16 : 4));
312       channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4);
313       SILC_GET16_MSB(channel_id->port, &id[channel_id->ip.data_len]);
314       SILC_GET16_MSB(channel_id->rnd, &id[channel_id->ip.data_len + 2]);
315       return TRUE;
316     }
317     break;
318   }
319
320   return FALSE;
321 }
322
323 /* Returns length of the ID */
324
325 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type)
326 {
327   switch(type) {
328   case SILC_ID_SERVER:
329     {
330       SilcServerID *server_id = (SilcServerID *)id;
331       return ID_SERVER_LEN_PART + server_id->ip.data_len;
332     }
333     break;
334   case SILC_ID_CLIENT:
335     {
336       SilcClientID *client_id = (SilcClientID *)id;
337       return ID_CLIENT_LEN_PART + client_id->ip.data_len;
338     }
339     break;
340   case SILC_ID_CHANNEL:
341     {
342       SilcChannelID *channel_id = (SilcChannelID *)id;
343       return ID_CHANNEL_LEN_PART + channel_id->ip.data_len;
344     }
345     break;
346   }
347
348   return 0;
349 }
350
351 /* Duplicate ID data */
352
353 void *silc_id_dup(const void *id, SilcIdType type)
354 {
355   switch(type) {
356   case SILC_ID_SERVER:
357     {
358       SilcServerID *server_id = (SilcServerID *)id;
359       return silc_memdup(server_id, sizeof(*server_id));
360     }
361     break;
362   case SILC_ID_CLIENT:
363     {
364       SilcClientID *client_id = (SilcClientID *)id;
365       return silc_memdup(client_id, sizeof(*client_id));
366     }
367     break;
368   case SILC_ID_CHANNEL:
369     {
370       SilcChannelID *channel_id = (SilcChannelID *)id;
371       return silc_memdup(channel_id, sizeof(*channel_id));
372     }
373     break;
374   }
375
376   return NULL;
377 }