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