Merged silc_1_0_branch to trunk.
[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 > buffer.len || 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 > buffer.len || 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 {
210   unsigned char *ret_id;
211   SilcServerID *server_id;
212   SilcClientID *client_id;
213   SilcChannelID *channel_id;
214   SilcUInt32 id_len = silc_id_get_len(id, type);
215
216   if (id_len > SILC_PACKET_MAX_ID_LEN)
217     return NULL;
218
219   switch(type) {
220   case SILC_ID_SERVER:
221     server_id = (SilcServerID *)id;
222     ret_id = silc_calloc(id_len, sizeof(unsigned char));
223     if (!ret_id)
224       return NULL;
225     memcpy(ret_id, server_id->ip.data, server_id->ip.data_len);
226     SILC_PUT16_MSB(server_id->port, &ret_id[server_id->ip.data_len]);
227     SILC_PUT16_MSB(server_id->rnd, &ret_id[server_id->ip.data_len + 2]);
228     return ret_id;
229     break;
230   case SILC_ID_CLIENT:
231     client_id = (SilcClientID *)id;
232     ret_id = silc_calloc(id_len, sizeof(unsigned char));
233     if (!ret_id)
234       return NULL;
235     memcpy(ret_id, client_id->ip.data, client_id->ip.data_len);
236     ret_id[client_id->ip.data_len] = client_id->rnd;
237     memcpy(&ret_id[client_id->ip.data_len + 1], client_id->hash, 
238            CLIENTID_HASH_LEN);
239     return ret_id;
240     break;
241   case SILC_ID_CHANNEL:
242     channel_id = (SilcChannelID *)id;
243     ret_id = silc_calloc(id_len, sizeof(unsigned char));
244     if (!ret_id)
245       return NULL;
246     memcpy(ret_id, channel_id->ip.data, channel_id->ip.data_len);
247     SILC_PUT16_MSB(channel_id->port, &ret_id[channel_id->ip.data_len]);
248     SILC_PUT16_MSB(channel_id->rnd, &ret_id[channel_id->ip.data_len + 2]);
249     return ret_id;
250     break;
251   }
252
253   return NULL;
254 }
255
256 /* Converts string to a ID */
257
258 void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, 
259                      SilcIdType type)
260 {
261   if (id_len > SILC_PACKET_MAX_ID_LEN)
262     return NULL;
263
264   switch(type) {
265   case SILC_ID_SERVER:
266     {
267       SilcServerID *server_id;
268
269       if (id_len != ID_SERVER_LEN_PART + 4 &&
270           id_len != ID_SERVER_LEN_PART + 16)
271         return NULL;
272
273       server_id = silc_calloc(1, sizeof(*server_id));
274       if (!server_id)
275         return NULL;
276       memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 4 ?
277                                       16 : 4));
278       server_id->ip.data_len = (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4);
279       SILC_GET16_MSB(server_id->port, &id[server_id->ip.data_len]);
280       SILC_GET16_MSB(server_id->rnd, &id[server_id->ip.data_len + 2]);
281       return server_id;
282     }
283     break;
284   case SILC_ID_CLIENT:
285     {
286       SilcClientID *client_id;
287
288       if (id_len != ID_CLIENT_LEN_PART + 4 &&
289           id_len != ID_CLIENT_LEN_PART + 16)
290         return NULL;
291
292       client_id = silc_calloc(1, sizeof(*client_id));
293       if (!client_id)
294         return NULL;
295       memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 4 ?
296                                       16 : 4));
297       client_id->ip.data_len = (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4);
298       client_id->rnd = id[client_id->ip.data_len];
299       memcpy(client_id->hash, &id[client_id->ip.data_len + 1], 
300              CLIENTID_HASH_LEN);
301       return client_id;
302     }
303     break;
304   case SILC_ID_CHANNEL:
305     {
306       SilcChannelID *channel_id;
307
308       if (id_len != ID_CHANNEL_LEN_PART + 4 &&
309           id_len != ID_CHANNEL_LEN_PART + 16)
310         return NULL;
311
312       channel_id = silc_calloc(1, sizeof(*channel_id));
313       if (!channel_id)
314         return NULL;
315       memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ?
316                                        16 : 4));
317       channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4);
318       SILC_GET16_MSB(channel_id->port, &id[channel_id->ip.data_len]);
319       SILC_GET16_MSB(channel_id->rnd, &id[channel_id->ip.data_len + 2]);
320       return channel_id;
321     }
322     break;
323   }
324
325   return NULL;
326 }
327
328 /* Returns length of the ID */
329
330 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type)
331 {
332   switch(type) {
333   case SILC_ID_SERVER:
334     {
335       SilcServerID *server_id = (SilcServerID *)id;
336       return ID_SERVER_LEN_PART + server_id->ip.data_len;
337     }
338     break;
339   case SILC_ID_CLIENT:
340     {
341       SilcClientID *client_id = (SilcClientID *)id;
342       return ID_CLIENT_LEN_PART + client_id->ip.data_len;
343     }
344     break;
345   case SILC_ID_CHANNEL:
346     {
347       SilcChannelID *channel_id = (SilcChannelID *)id;
348       return ID_CHANNEL_LEN_PART + channel_id->ip.data_len;
349     }
350     break;
351   }
352
353   return 0;
354 }
355
356 /* Duplicate ID data */
357
358 void *silc_id_dup(const void *id, SilcIdType type)
359 {
360   switch(type) {
361   case SILC_ID_SERVER:
362     {
363       SilcServerID *server_id = (SilcServerID *)id;
364       return silc_memdup(server_id, sizeof(*server_id));
365     }
366     break;
367   case SILC_ID_CLIENT:
368     {
369       SilcClientID *client_id = (SilcClientID *)id;
370       return silc_memdup(client_id, sizeof(*client_id));
371     }
372     break;
373   case SILC_ID_CHANNEL:
374     {
375       SilcChannelID *channel_id = (SilcChannelID *)id;
376       return silc_memdup(channel_id, sizeof(*channel_id));
377     }
378     break;
379   }
380
381   return NULL;
382 }