updates.
[silc.git] / lib / silccore / id.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 "id.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 /* Converts ID to string. */
31
32 unsigned char *silc_id_id2str(void *id, SilcIdType type)
33 {
34   unsigned char *ret_id;
35   SilcServerID *server_id;
36   SilcClientID *client_id;
37   SilcChannelID *channel_id;
38   uint32 id_len = silc_id_get_len(id, type);
39
40   switch(type) {
41   case SILC_ID_SERVER:
42     server_id = (SilcServerID *)id;
43     ret_id = silc_calloc(id_len, sizeof(unsigned char));
44     memcpy(ret_id, server_id->ip.data, server_id->ip.data_len);
45     SILC_PUT16_MSB(server_id->port, &ret_id[4]);
46     SILC_PUT16_MSB(server_id->rnd, &ret_id[6]);
47     return ret_id;
48     break;
49   case SILC_ID_CLIENT:
50     client_id = (SilcClientID *)id;
51     ret_id = silc_calloc(id_len, sizeof(unsigned char));
52     memcpy(ret_id, client_id->ip.data, client_id->ip.data_len);
53     ret_id[4] = client_id->rnd;
54     memcpy(&ret_id[5], client_id->hash, CLIENTID_HASH_LEN);
55     return ret_id;
56     break;
57   case SILC_ID_CHANNEL:
58     channel_id = (SilcChannelID *)id;
59     ret_id = silc_calloc(id_len, sizeof(unsigned char));
60     memcpy(ret_id, channel_id->ip.data, channel_id->ip.data_len);
61     SILC_PUT16_MSB(channel_id->port, &ret_id[4]);
62     SILC_PUT16_MSB(channel_id->rnd, &ret_id[6]);
63     return ret_id;
64     break;
65   }
66
67   return NULL;
68 }
69
70 /* Converts string to a ID */
71
72 void *silc_id_str2id(unsigned char *id, uint32 id_len, SilcIdType type)
73 {
74
75   switch(type) {
76   case SILC_ID_SERVER:
77     {
78       SilcServerID *server_id;
79
80       if (id_len != ID_SERVER_LEN_PART + 4 &&
81           id_len != ID_SERVER_LEN_PART + 16)
82         return NULL;
83
84       server_id = silc_calloc(1, sizeof(*server_id));
85       memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 4 ?
86                                       16 : 4));
87       server_id->ip.data_len = (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4);
88       SILC_GET16_MSB(server_id->port, &id[4]);
89       SILC_GET16_MSB(server_id->rnd, &id[6]);
90       return server_id;
91     }
92     break;
93   case SILC_ID_CLIENT:
94     {
95       SilcClientID *client_id;
96
97       if (id_len != ID_CLIENT_LEN_PART + 4 &&
98           id_len != ID_CLIENT_LEN_PART + 16)
99         return NULL;
100
101       client_id = silc_calloc(1, sizeof(*client_id));
102       memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 4 ?
103                                       16 : 4));
104       client_id->ip.data_len = (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4);
105       client_id->rnd = id[4];
106       memcpy(client_id->hash, &id[5], CLIENTID_HASH_LEN);
107       return client_id;
108     }
109     break;
110   case SILC_ID_CHANNEL:
111     {
112       SilcChannelID *channel_id;
113
114       if (id_len != ID_CHANNEL_LEN_PART + 4 &&
115           id_len != ID_CHANNEL_LEN_PART + 16)
116         return NULL;
117
118       channel_id = silc_calloc(1, sizeof(*channel_id));
119       memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ?
120                                        16 : 4));
121       channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4);
122       SILC_GET16_MSB(channel_id->port, &id[4]);
123       SILC_GET16_MSB(channel_id->rnd, &id[6]);
124       return channel_id;
125     }
126     break;
127   }
128
129   return NULL;
130 }
131
132 /* Returns length of the ID */
133
134 uint32 silc_id_get_len(void *id, SilcIdType type)
135 {
136   switch(type) {
137   case SILC_ID_SERVER:
138     {
139       SilcServerID *server_id = (SilcServerID *)id;
140       return ID_SERVER_LEN_PART + server_id->ip.data_len;
141     }
142     break;
143   case SILC_ID_CLIENT:
144     {
145       SilcClientID *client_id = (SilcClientID *)id;
146       return ID_CLIENT_LEN_PART + client_id->ip.data_len;
147     }
148     break;
149   case SILC_ID_CHANNEL:
150     {
151       SilcChannelID *channel_id = (SilcChannelID *)id;
152       return ID_CHANNEL_LEN_PART + channel_id->ip.data_len;
153     }
154     break;
155   }
156
157   return 0;
158 }
159
160 /* Duplicate ID data */
161
162 void *silc_id_dup(void *id, SilcIdType type)
163 {
164   switch(type) {
165   case SILC_ID_SERVER:
166     {
167       SilcServerID *server_id = (SilcServerID *)id, *new;
168       new = silc_calloc(1, sizeof(*server_id));
169       memcpy(&new->ip, &server_id->ip, sizeof(server_id->ip)); 
170       new->port = server_id->port;
171       new->rnd = server_id->rnd;
172       return new;
173     }
174     break;
175   case SILC_ID_CLIENT:
176     {
177       SilcClientID *client_id = (SilcClientID *)id, *new;
178       new = silc_calloc(1, sizeof(*client_id));
179       memcpy(&new->ip, &client_id->ip, sizeof(client_id->ip)); 
180       new->rnd = client_id->rnd;
181       memcpy(new->hash, client_id->hash, CLIENTID_HASH_LEN);
182       return new;
183     }
184     break;
185   case SILC_ID_CHANNEL:
186     {
187       SilcChannelID *channel_id = (SilcChannelID *)id, *new;
188       new = silc_calloc(1, sizeof(*channel_id));
189       memcpy(&new->ip, &channel_id->ip, sizeof(channel_id->ip)); 
190       new->port = channel_id->port;
191       new->rnd = channel_id->rnd;
192       return new;
193     }
194     break;
195   }
196
197   return NULL;
198 }
199
200 /* Returns TRUE if the `id2' is equal to `id1'. */
201
202 bool silc_id_compare(void *id1, void *id2, SilcIdType type)
203 {
204   switch(type) {
205   case SILC_ID_SERVER:
206     {
207       SilcServerID *server_id1 = (SilcServerID *)id1;
208       SilcServerID *server_id2 = (SilcServerID *)id2;
209       if (!memcmp(server_id1->ip.data, server_id2->ip.data, 
210                   server_id1->ip.data_len) &&
211           server_id1->port == server_id2->port &&
212           server_id1->rnd == server_id2->rnd)
213         return TRUE;
214       return FALSE;
215     }
216     break;
217   case SILC_ID_CLIENT:
218     {
219       SilcClientID *client_id1 = (SilcClientID *)id1;
220       SilcClientID *client_id2 = (SilcClientID *)id2;
221       if (!memcmp(client_id1->ip.data, client_id2->ip.data, 
222                   client_id1->ip.data_len) &&
223           client_id1->rnd == client_id2->rnd &&
224           !memcmp(client_id1->hash, client_id2->hash, CLIENTID_HASH_LEN))
225         return TRUE;
226       return FALSE;
227     }
228     break;
229   case SILC_ID_CHANNEL:
230     {
231       SilcChannelID *channel_id1 = (SilcChannelID *)id1;
232       SilcChannelID *channel_id2 = (SilcChannelID *)id2;
233       if (!memcmp(channel_id1->ip.data, channel_id2->ip.data, 
234                   channel_id1->ip.data_len) &&
235           channel_id1->port == channel_id2->port &&
236           channel_id1->rnd == channel_id2->rnd)
237         return TRUE;
238       return FALSE;
239     }
240     break;
241   }
242
243   return FALSE;
244 }