6e892c087b2a8dedb675bbf4206408873fe48637
[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_LOG_DEBUG(("Parsing ID payload"));
52
53   silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
54   newp = silc_calloc(1, sizeof(*newp));
55   if (!newp)
56     return NULL;
57
58   ret = silc_buffer_unformat(&buffer,
59                              SILC_STR_UI_SHORT(&newp->type),
60                              SILC_STR_UI_SHORT(&newp->len),
61                              SILC_STR_END);
62   if (ret == -1)
63     goto err;
64
65   if (newp->type > SILC_ID_CHANNEL)
66     goto err;
67
68   silc_buffer_pull(&buffer, 4);
69
70   if (newp->len > buffer.len || newp->len > SILC_PACKET_MAX_ID_LEN)
71     goto err;
72
73   ret = silc_buffer_unformat(&buffer,
74                              SILC_STR_UI_XNSTRING_ALLOC(&newp->id, newp->len),
75                              SILC_STR_END);
76   if (ret == -1)
77     goto err;
78
79   silc_buffer_push(&buffer, 4);
80
81   return newp;
82
83  err:
84   silc_free(newp);
85   return NULL;
86 }
87
88 /* Return the ID directly from the raw payload data. */
89
90 void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len,
91                                SilcIdType *ret_type)
92 {
93   SilcBufferStruct buffer;
94   SilcIdType type;
95   SilcUInt16 idlen;
96   unsigned char *id_data;
97   int ret;
98   void *id;
99
100   silc_buffer_set(&buffer, (unsigned char *)data, len);
101   ret = silc_buffer_unformat(&buffer,
102                              SILC_STR_UI_SHORT(&type),
103                              SILC_STR_UI_SHORT(&idlen),
104                              SILC_STR_END);
105   if (ret == -1)
106     return NULL;
107
108   if (type > SILC_ID_CHANNEL)
109     return NULL;
110
111   silc_buffer_pull(&buffer, 4);
112
113   if (idlen > buffer.len || idlen > SILC_PACKET_MAX_ID_LEN)
114     return NULL;
115
116   ret = silc_buffer_unformat(&buffer,
117                              SILC_STR_UI_XNSTRING(&id_data, idlen),
118                              SILC_STR_END);
119   if (ret == -1)
120     return NULL;
121
122   id = silc_id_str2id(id_data, idlen, type);
123
124   if (ret_type)
125     *ret_type = type;
126
127   return id;
128 }
129
130 /* Encodes ID Payload */
131
132 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type)
133 {
134   SilcBuffer buffer;
135   unsigned char *id_data;
136   SilcUInt32 len;
137
138   id_data = silc_id_id2str(id, type);
139   len = silc_id_get_len(id, type);
140   buffer = silc_id_payload_encode_data((const unsigned char *)id_data,
141                                        len, type);
142   silc_free(id_data);
143   return buffer;
144 }
145
146 SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
147                                        SilcUInt32 id_len, SilcIdType type)
148 {
149   SilcBuffer buffer;
150
151   SILC_LOG_DEBUG(("Encoding %s ID payload",
152                   type == SILC_ID_CLIENT ? "Client" :
153                   type == SILC_ID_SERVER ? "Server" : "Channel"));
154
155   buffer = silc_buffer_alloc_size(4 + id_len);
156   if (!buffer)
157     return NULL;
158   silc_buffer_format(buffer,
159                      SILC_STR_UI_SHORT(type),
160                      SILC_STR_UI_SHORT(id_len),
161                      SILC_STR_UI_XNSTRING(id, id_len),
162                      SILC_STR_END);
163   return buffer;
164 }
165
166 /* Free ID Payload */
167
168 void silc_id_payload_free(SilcIDPayload payload)
169 {
170   if (payload) {
171     silc_free(payload->id);
172     silc_free(payload);
173   }
174 }
175
176 /* Get ID type */
177
178 SilcIdType silc_id_payload_get_type(SilcIDPayload payload)
179 {
180   return payload ? payload->type : 0;
181 }
182
183 /* Get ID */
184
185 void *silc_id_payload_get_id(SilcIDPayload payload)
186 {
187   return payload ? silc_id_str2id(payload->id, payload->len,
188                                   payload->type) : NULL;
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 unsigned char *silc_id_id2str(const void *id, SilcIdType type)
211 {
212   unsigned char *ret_id;
213   SilcServerID *server_id;
214   SilcClientID *client_id;
215   SilcChannelID *channel_id;
216   SilcUInt32 id_len = silc_id_get_len(id, type);
217
218   if (id_len > SILC_PACKET_MAX_ID_LEN)
219     return NULL;
220
221   switch(type) {
222   case SILC_ID_SERVER:
223     server_id = (SilcServerID *)id;
224     ret_id = silc_calloc(id_len, sizeof(unsigned char));
225     if (!ret_id)
226       return NULL;
227     memcpy(ret_id, server_id->ip.data, server_id->ip.data_len);
228     SILC_PUT16_MSB(server_id->port, &ret_id[server_id->ip.data_len]);
229     SILC_PUT16_MSB(server_id->rnd, &ret_id[server_id->ip.data_len + 2]);
230     return ret_id;
231     break;
232   case SILC_ID_CLIENT:
233     client_id = (SilcClientID *)id;
234     ret_id = silc_calloc(id_len, sizeof(unsigned char));
235     if (!ret_id)
236       return NULL;
237     memcpy(ret_id, client_id->ip.data, client_id->ip.data_len);
238     ret_id[client_id->ip.data_len] = client_id->rnd;
239     memcpy(&ret_id[client_id->ip.data_len + 1], client_id->hash, 
240            CLIENTID_HASH_LEN);
241     return ret_id;
242     break;
243   case SILC_ID_CHANNEL:
244     channel_id = (SilcChannelID *)id;
245     ret_id = silc_calloc(id_len, sizeof(unsigned char));
246     if (!ret_id)
247       return NULL;
248     memcpy(ret_id, channel_id->ip.data, channel_id->ip.data_len);
249     SILC_PUT16_MSB(channel_id->port, &ret_id[channel_id->ip.data_len]);
250     SILC_PUT16_MSB(channel_id->rnd, &ret_id[channel_id->ip.data_len + 2]);
251     return ret_id;
252     break;
253   }
254
255   return NULL;
256 }
257
258 /* Converts string to a ID */
259
260 void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, 
261                      SilcIdType type)
262 {
263   if (id_len > SILC_PACKET_MAX_ID_LEN)
264     return NULL;
265
266   switch(type) {
267   case SILC_ID_SERVER:
268     {
269       SilcServerID *server_id;
270
271       if (id_len != ID_SERVER_LEN_PART + 4 &&
272           id_len != ID_SERVER_LEN_PART + 16)
273         return NULL;
274
275       server_id = silc_calloc(1, sizeof(*server_id));
276       if (!server_id)
277         return NULL;
278       memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 4 ?
279                                       16 : 4));
280       server_id->ip.data_len = (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4);
281       SILC_GET16_MSB(server_id->port, &id[server_id->ip.data_len]);
282       SILC_GET16_MSB(server_id->rnd, &id[server_id->ip.data_len + 2]);
283       return server_id;
284     }
285     break;
286   case SILC_ID_CLIENT:
287     {
288       SilcClientID *client_id;
289
290       if (id_len != ID_CLIENT_LEN_PART + 4 &&
291           id_len != ID_CLIENT_LEN_PART + 16)
292         return NULL;
293
294       client_id = silc_calloc(1, sizeof(*client_id));
295       if (!client_id)
296         return NULL;
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 client_id;
304     }
305     break;
306   case SILC_ID_CHANNEL:
307     {
308       SilcChannelID *channel_id;
309
310       if (id_len != ID_CHANNEL_LEN_PART + 4 &&
311           id_len != ID_CHANNEL_LEN_PART + 16)
312         return NULL;
313
314       channel_id = silc_calloc(1, sizeof(*channel_id));
315       if (!channel_id)
316         return NULL;
317       memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ?
318                                        16 : 4));
319       channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4);
320       SILC_GET16_MSB(channel_id->port, &id[channel_id->ip.data_len]);
321       SILC_GET16_MSB(channel_id->rnd, &id[channel_id->ip.data_len + 2]);
322       return channel_id;
323     }
324     break;
325   }
326
327   return NULL;
328 }
329
330 /* Returns length of the ID */
331
332 SilcUInt32 silc_id_get_len(const void *id, SilcIdType type)
333 {
334   switch(type) {
335   case SILC_ID_SERVER:
336     {
337       SilcServerID *server_id = (SilcServerID *)id;
338       return ID_SERVER_LEN_PART + server_id->ip.data_len;
339     }
340     break;
341   case SILC_ID_CLIENT:
342     {
343       SilcClientID *client_id = (SilcClientID *)id;
344       return ID_CLIENT_LEN_PART + client_id->ip.data_len;
345     }
346     break;
347   case SILC_ID_CHANNEL:
348     {
349       SilcChannelID *channel_id = (SilcChannelID *)id;
350       return ID_CHANNEL_LEN_PART + channel_id->ip.data_len;
351     }
352     break;
353   }
354
355   return 0;
356 }
357
358 /* Duplicate ID data */
359
360 void *silc_id_dup(const void *id, SilcIdType type)
361 {
362   switch(type) {
363   case SILC_ID_SERVER:
364     {
365       SilcServerID *server_id = (SilcServerID *)id;
366       return silc_memdup(server_id, sizeof(*server_id));
367     }
368     break;
369   case SILC_ID_CLIENT:
370     {
371       SilcClientID *client_id = (SilcClientID *)id;
372       return silc_memdup(client_id, sizeof(*client_id));
373     }
374     break;
375   case SILC_ID_CHANNEL:
376     {
377       SilcChannelID *channel_id = (SilcChannelID *)id;
378       return silc_memdup(channel_id, sizeof(*channel_id));
379     }
380     break;
381   }
382
383   return NULL;
384 }