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