Added SILC Server library.
[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 "silc.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) ||
68       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   return newp;
78
79  err:
80   SILC_LOG_DEBUG(("Error parsing ID payload"));
81   silc_free(newp);
82   return NULL;
83 }
84
85 /* Return the ID directly from the raw payload data. */
86
87 SilcBool silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len,
88                                   SilcIdType *ret_type, void *ret_id,
89                                   SilcUInt32 ret_id_size)
90 {
91   SilcBufferStruct buffer;
92   SilcIdType type;
93   SilcUInt16 idlen;
94   unsigned char *id_data;
95   int ret;
96
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),
101                              SILC_STR_END);
102   if (ret == -1)
103     goto err;
104
105   if (type > SILC_ID_CHANNEL)
106     goto err;
107
108   silc_buffer_pull(&buffer, 4);
109
110   if (idlen > silc_buffer_len(&buffer) || idlen > SILC_PACKET_MAX_ID_LEN)
111     goto err;
112
113   ret = silc_buffer_unformat(&buffer,
114                              SILC_STR_UI_XNSTRING(&id_data, idlen),
115                              SILC_STR_END);
116   if (ret == -1)
117     goto err;
118
119   if (!silc_id_str2id(id_data, idlen, type, ret_id, ret_id_size))
120     goto err;
121
122   if (ret_type)
123     *ret_type = type;
124
125   return TRUE;
126
127  err:
128   SILC_LOG_DEBUG(("Error parsing ID payload"));
129   return FALSE;
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[32];
138   SilcUInt32 len;
139
140   if (!silc_id_id2str(id, type, id_data, sizeof(id_data), &len))
141     return NULL;
142   buffer = silc_id_payload_encode_data((const unsigned char *)id_data,
143                                        len, type);
144   return buffer;
145 }
146
147 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
148                                        SilcUInt32 id_len, SilcIdType type)
149 {
150   SilcBuffer buffer;
151
152   buffer = silc_buffer_alloc_size(4 + id_len);
153   if (!buffer)
154     return NULL;
155   silc_buffer_format(buffer,
156                      SILC_STR_UI_SHORT(type),
157                      SILC_STR_UI_SHORT(id_len),
158                      SILC_STR_UI_XNSTRING(id, id_len),
159                      SILC_STR_END);
160   return buffer;
161 }
162
163 /* Free ID Payload */
164
165 void silc_id_payload_free(SilcIDPayload payload)
166 {
167   if (payload) {
168     silc_free(payload->id);
169     silc_free(payload);
170   }
171 }
172
173 /* Get ID type */
174
175 SilcIdType silc_id_payload_get_type(SilcIDPayload payload)
176 {
177   return payload ? payload->type : 0;
178 }
179
180 /* Get ID */
181
182 SilcBool silc_id_payload_get_id(SilcIDPayload payload, void *ret_id,
183                                 SilcUInt32 ret_id_len)
184 {
185   if (!payload)
186     return FALSE;
187   return silc_id_str2id(payload->id, payload->len, payload->type,
188                         ret_id, ret_id_len);
189 }
190
191 /* Get raw ID data. Data is duplicated. */
192
193 unsigned char *silc_id_payload_get_data(SilcIDPayload payload)
194 {
195   if (!payload)
196     return NULL;
197
198   return silc_memdup(payload->id, payload->len);
199 }
200
201 /* Get length of ID */
202
203 SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload)
204 {
205   return payload ? payload->len : 0;
206 }
207
208 /* Converts ID to string. */
209
210 SilcBool silc_id_id2str(const void *id, SilcIdType type,
211                         unsigned char *ret_id, SilcUInt32 ret_id_size,
212                         SilcUInt32 *ret_id_len)
213 {
214   SilcServerID *server_id;
215   SilcClientID *client_id;
216   SilcChannelID *channel_id;
217   SilcUInt32 id_len = silc_id_get_len(id, type);
218
219   if (id_len > ret_id_size)
220     return FALSE;
221
222   if (ret_id_len)
223     *ret_id_len = id_len;
224
225   if (id_len > SILC_PACKET_MAX_ID_LEN)
226     return FALSE;
227
228   switch(type) {
229   case SILC_ID_SERVER:
230     server_id = (SilcServerID *)id;
231     memcpy(ret_id, server_id->ip.data, server_id->ip.data_len);
232     SILC_PUT16_MSB(server_id->port, &ret_id[server_id->ip.data_len]);
233     SILC_PUT16_MSB(server_id->rnd, &ret_id[server_id->ip.data_len + 2]);
234     return TRUE;
235     break;
236   case SILC_ID_CLIENT:
237     client_id = (SilcClientID *)id;
238     memcpy(ret_id, client_id->ip.data, client_id->ip.data_len);
239     ret_id[client_id->ip.data_len] = client_id->rnd;
240     memcpy(&ret_id[client_id->ip.data_len + 1], client_id->hash,
241            CLIENTID_HASH_LEN);
242     return TRUE;
243     break;
244   case SILC_ID_CHANNEL:
245     channel_id = (SilcChannelID *)id;
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 TRUE;
250     break;
251   }
252
253   return FALSE;
254 }
255
256 /* Converts string to a ID */
257
258 SilcBool silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
259                         SilcIdType type, void *ret_id, SilcUInt32 ret_id_size)
260 {
261   if (id_len > SILC_PACKET_MAX_ID_LEN)
262     return FALSE;
263
264   switch(type) {
265   case SILC_ID_SERVER:
266     {
267       SilcServerID *server_id = ret_id;
268
269       if (id_len != ID_SERVER_LEN_PART + 4 &&
270           id_len != ID_SERVER_LEN_PART + 16)
271         return FALSE;
272
273       if (ret_id_size < sizeof(SilcServerID))
274         return FALSE;
275
276       memset(ret_id, 0, ret_id_size);
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       if (ret_id_size < sizeof(SilcClientID))
294         return FALSE;
295
296       memset(ret_id, 0, ret_id_size);
297       memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 4 ?
298                                       16 : 4));
299       client_id->ip.data_len = (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4);
300       client_id->rnd = id[client_id->ip.data_len];
301       memcpy(client_id->hash, &id[client_id->ip.data_len + 1],
302              CLIENTID_HASH_LEN);
303       return TRUE;
304     }
305     break;
306   case SILC_ID_CHANNEL:
307     {
308       SilcChannelID *channel_id = ret_id;
309
310       if (id_len != ID_CHANNEL_LEN_PART + 4 &&
311           id_len != ID_CHANNEL_LEN_PART + 16)
312         return FALSE;
313
314       if (ret_id_size < sizeof(SilcChannelID))
315         return FALSE;
316
317       memset(ret_id, 0, ret_id_size);
318       memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ?
319                                        16 : 4));
320       channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4);
321       SILC_GET16_MSB(channel_id->port, &id[channel_id->ip.data_len]);
322       SILC_GET16_MSB(channel_id->rnd, &id[channel_id->ip.data_len + 2]);
323       return TRUE;
324     }
325     break;
326   }
327
328   return FALSE;
329 }
330
331 /* Returns length of the ID */
332
333 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type)
334 {
335   switch(type) {
336   case SILC_ID_SERVER:
337     {
338       SilcServerID *server_id = (SilcServerID *)id;
339       return ID_SERVER_LEN_PART + server_id->ip.data_len;
340     }
341     break;
342   case SILC_ID_CLIENT:
343     {
344       SilcClientID *client_id = (SilcClientID *)id;
345       return ID_CLIENT_LEN_PART + client_id->ip.data_len;
346     }
347     break;
348   case SILC_ID_CHANNEL:
349     {
350       SilcChannelID *channel_id = (SilcChannelID *)id;
351       return ID_CHANNEL_LEN_PART + channel_id->ip.data_len;
352     }
353     break;
354   }
355
356   return 0;
357 }
358
359 /* Duplicate ID data */
360
361 void *silc_id_dup(const void *id, SilcIdType type)
362 {
363   switch(type) {
364   case SILC_ID_SERVER:
365     {
366       SilcServerID *server_id = (SilcServerID *)id;
367       return silc_memdup(server_id, sizeof(*server_id));
368     }
369     break;
370   case SILC_ID_CLIENT:
371     {
372       SilcClientID *client_id = (SilcClientID *)id;
373       return silc_memdup(client_id, sizeof(*client_id));
374     }
375     break;
376   case SILC_ID_CHANNEL:
377     {
378       SilcChannelID *channel_id = (SilcChannelID *)id;
379       return silc_memdup(channel_id, sizeof(*channel_id));
380     }
381     break;
382   }
383
384   return NULL;
385 }