5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 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 "serverincludes.h"
23 #include "server_internal.h"
25 /* Creates a Server ID. Newly created Server ID is returned to the
28 void silc_id_create_server_id(const char *ip, SilcUInt16 port, SilcRng rng,
29 SilcServerID **new_id)
31 SILC_LOG_DEBUG(("Creating new Server ID"));
33 *new_id = silc_calloc(1, sizeof(**new_id));
37 if (!silc_net_addr2bin(ip, (*new_id)->ip.data,
38 sizeof((*new_id)->ip.data))) {
44 (*new_id)->ip.data_len = silc_net_is_ip4(ip) ? 4 : 16;
45 (*new_id)->port = SILC_SWAB_16(port);
46 (*new_id)->rnd = 0xff;
48 SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_SERVER)));
51 /* Creates Client ID. This assures that there are no collisions in the
52 created Client IDs. If the collision would occur (meaning that there
53 are 2^8 occurences of the `nickname' this will return FALSE, and the
54 caller must recall the function with different nickname. If this returns
55 TRUE the new ID was created successfully. */
57 SilcBool silc_id_create_client_id(SilcServer server,
58 SilcServerID *server_id, SilcRng rng,
60 unsigned char *nickname, SilcUInt32 nick_len,
61 SilcClientID **new_id)
63 unsigned char hash[16];
64 SilcBool finding = FALSE;
66 SILC_LOG_DEBUG(("Creating new Client ID"));
68 *new_id = silc_calloc(1, sizeof(**new_id));
70 /* Create hash of the nickname (it's already checked as valid identifier
72 silc_hash_make(md5hash, nickname, nick_len, hash);
75 memcpy((*new_id)->ip.data, server_id->ip.data, server_id->ip.data_len);
76 (*new_id)->ip.data_len = server_id->ip.data_len;
77 (*new_id)->rnd = silc_rng_get_byte(rng);
78 memcpy((*new_id)->hash, hash, CLIENTID_HASH_LEN);
80 /* Assure that the ID does not exist already */
82 if (!silc_idlist_find_client_by_id(server->local_list,
83 *new_id, FALSE, NULL))
84 if (!silc_idlist_find_client_by_id(server->global_list,
85 *new_id, FALSE, NULL))
88 /* The ID exists, start increasing the rnd from 0 until we find a
89 ID that does not exist. If we wrap and it still exists then we
90 will return FALSE and the caller must send some other nickname
91 since this cannot be used anymore. */
94 if (finding && (*new_id)->rnd == 0)
103 SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_CLIENT)));
108 /* Creates Channel ID */
110 SilcBool silc_id_create_channel_id(SilcServer server,
111 SilcServerID *router_id, SilcRng rng,
112 SilcChannelID **new_id)
114 SilcBool finding = TRUE;
116 SILC_LOG_DEBUG(("Creating new Channel ID"));
118 *new_id = silc_calloc(1, sizeof(**new_id));
121 memcpy((*new_id)->ip.data, router_id->ip.data, router_id->ip.data_len);
122 (*new_id)->ip.data_len = router_id->ip.data_len;
123 (*new_id)->port = router_id->port;
124 (*new_id)->rnd = silc_rng_get_rn16(rng);
126 /* Assure that the ID does not exist already */
128 if (!silc_idlist_find_channel_by_id(server->local_list,
134 if (finding && (*new_id)->rnd == 0)
143 SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_CHANNEL)));
148 /* Checks whether the `server_id' is valid. It must be based to the
149 IP address provided in the `remote' socket connection. */
151 SilcBool silc_id_is_valid_server_id(SilcServer server,
152 SilcServerID *server_id,
153 SilcPacketStream remote)
155 unsigned char ip[16];
156 const char *remote_ip;
157 SilcStream stream = silc_packet_stream_get_stream(remote);
159 silc_socket_stream_get_info(stream, NULL, NULL, &remote_ip, NULL);
160 if (!silc_net_addr2bin(remote_ip, ip, sizeof(ip)))
163 if (silc_net_is_ip4(remote_ip)) {
164 if (!memcmp(server_id->ip.data, ip, 4))
167 if (!memcmp(server_id->ip.data, ip, 16))