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, server_id, sizeof(*server_id));
170       return new;
171     }
172     break;
173   case SILC_ID_CLIENT:
174     {
175       SilcClientID *client_id = (SilcClientID *)id, *new;
176       new = silc_calloc(1, sizeof(*client_id));
177       memcpy(new, client_id, sizeof(*client_id));
178       return new;
179     }
180     break;
181   case SILC_ID_CHANNEL:
182     {
183       SilcChannelID *channel_id = (SilcChannelID *)id, *new;
184       new = silc_calloc(1, sizeof(*channel_id));
185       memcpy(new, channel_id, sizeof(*channel_id));
186       return new;
187     }
188     break;
189   }
190
191   return NULL;
192 }