5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
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.
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.
22 #include "silcincludes.h"
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
30 /* Converts ID to string. */
32 unsigned char *silc_id_id2str(void *id, SilcIdType type)
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);
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]);
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);
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]);
70 /* Converts string to a ID */
72 void *silc_id_str2id(unsigned char *id, uint32 id_len, SilcIdType type)
78 SilcServerID *server_id;
80 if (id_len != ID_SERVER_LEN_PART + 4 &&
81 id_len != ID_SERVER_LEN_PART + 16)
84 server_id = silc_calloc(1, sizeof(*server_id));
85 memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 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]);
95 SilcClientID *client_id;
97 if (id_len != ID_CLIENT_LEN_PART + 4 &&
98 id_len != ID_CLIENT_LEN_PART + 16)
101 client_id = silc_calloc(1, sizeof(*client_id));
102 memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 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);
110 case SILC_ID_CHANNEL:
112 SilcChannelID *channel_id;
114 if (id_len != ID_CHANNEL_LEN_PART + 4 &&
115 id_len != ID_CHANNEL_LEN_PART + 16)
118 channel_id = silc_calloc(1, sizeof(*channel_id));
119 memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 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]);
132 /* Returns length of the ID */
134 uint32 silc_id_get_len(void *id, SilcIdType type)
139 SilcServerID *server_id = (SilcServerID *)id;
140 return ID_SERVER_LEN_PART + server_id->ip.data_len;
145 SilcClientID *client_id = (SilcClientID *)id;
146 return ID_CLIENT_LEN_PART + client_id->ip.data_len;
149 case SILC_ID_CHANNEL:
151 SilcChannelID *channel_id = (SilcChannelID *)id;
152 return ID_CHANNEL_LEN_PART + channel_id->ip.data_len;
160 /* Duplicate ID data */
162 void *silc_id_dup(void *id, SilcIdType type)
167 SilcServerID *server_id = (SilcServerID *)id, *new;
168 new = silc_calloc(1, sizeof(*server_id));
169 memcpy(new, server_id, sizeof(*server_id));
175 SilcClientID *client_id = (SilcClientID *)id, *new;
176 new = silc_calloc(1, sizeof(*client_id));
177 memcpy(new, client_id, sizeof(*client_id));
181 case SILC_ID_CHANNEL:
183 SilcChannelID *channel_id = (SilcChannelID *)id, *new;
184 new = silc_calloc(1, sizeof(*channel_id));
185 memcpy(new, channel_id, sizeof(*channel_id));