5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 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"
24 /* Converts ID to string. */
26 unsigned char *silc_id_id2str(void *id, SilcIdType type)
28 unsigned char *ret_id;
29 SilcServerID *server_id;
30 SilcClientID *client_id;
31 SilcChannelID *channel_id;
35 server_id = (SilcServerID *)id;
36 ret_id = silc_calloc(8, sizeof(unsigned char));
37 SILC_PUT32_MSB(server_id->ip.s_addr, ret_id);
38 SILC_PUT16_MSB(server_id->port, &ret_id[4]);
39 SILC_PUT16_MSB(server_id->rnd, &ret_id[6]);
43 client_id = (SilcClientID *)id;
44 ret_id = silc_calloc(16, sizeof(unsigned char));
45 SILC_PUT32_MSB(client_id->ip.s_addr, ret_id);
46 ret_id[4] = client_id->rnd;
47 memcpy(&ret_id[5], client_id->hash, CLIENTID_HASH_LEN);
51 channel_id = (SilcChannelID *)id;
52 ret_id = silc_calloc(8, sizeof(unsigned char));
53 SILC_PUT32_MSB(channel_id->ip.s_addr, ret_id);
54 SILC_PUT16_MSB(channel_id->port, &ret_id[4]);
55 SILC_PUT16_MSB(channel_id->rnd, &ret_id[6]);
63 /* Converts string to a ID */
65 void *silc_id_str2id(unsigned char *id, unsigned int id_len, SilcIdType type)
71 SilcServerID *server_id;
73 if (id_len != SILC_ID_SERVER_LEN)
76 server_id = silc_calloc(1, sizeof(*server_id));
77 SILC_GET32_MSB(server_id->ip.s_addr, id);
78 SILC_GET16_MSB(server_id->port, &id[4]);
79 SILC_GET16_MSB(server_id->rnd, &id[6]);
85 SilcClientID *client_id;
87 if (id_len != SILC_ID_CLIENT_LEN)
90 client_id = silc_calloc(1, sizeof(*client_id));
91 SILC_GET32_MSB(client_id->ip.s_addr, id);
92 client_id->rnd = id[4];
93 memcpy(client_id->hash, &id[5], CLIENTID_HASH_LEN);
99 SilcChannelID *channel_id;
101 if (id_len != SILC_ID_CHANNEL_LEN)
104 channel_id = silc_calloc(1, sizeof(*channel_id));
105 SILC_GET32_MSB(channel_id->ip.s_addr, id);
106 SILC_GET16_MSB(channel_id->port, &id[4]);
107 SILC_GET16_MSB(channel_id->rnd, &id[6]);
116 /* Returns length of the ID */
118 unsigned int silc_id_get_len(SilcIdType type)
122 return SILC_ID_SERVER_LEN;
125 return SILC_ID_CLIENT_LEN;
127 case SILC_ID_CHANNEL:
128 return SILC_ID_CHANNEL_LEN;