Integer type name change.
[silc.git] / apps / silcd / serverid.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 "serverincludes.h"
23 #include "server_internal.h"
24
25 /* Creates a Server ID. Newly created Server ID is returned to the
26    new_id argument. */
27
28 void silc_id_create_server_id(const char *ip, SilcUInt16 port, SilcRng rng, 
29                               SilcServerID **new_id)
30 {
31   SILC_LOG_DEBUG(("Creating new Server ID"));
32
33   *new_id = silc_calloc(1, sizeof(**new_id));
34
35   /* Create the ID */
36
37   if (!silc_net_addr2bin(ip, (*new_id)->ip.data, 
38                          sizeof((*new_id)->ip.data))) {
39     silc_free(*new_id);
40     *new_id = NULL;
41     return;
42   }
43
44   (*new_id)->ip.data_len = silc_net_is_ip4(ip) ? 4 : 16;
45   (*new_id)->port = htons(port);
46   (*new_id)->rnd = silc_rng_get_rn16(rng);
47
48   SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_SERVER)));
49 }
50
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. */
56
57 bool silc_id_create_client_id(SilcServer server,
58                               SilcServerID *server_id, SilcRng rng,
59                               SilcHash md5hash, char *nickname, 
60                               SilcClientID **new_id)
61 {
62   unsigned char hash[16];
63   bool finding = FALSE;
64
65   SILC_LOG_DEBUG(("Creating new Client ID"));
66
67   *new_id = silc_calloc(1, sizeof(**new_id));
68
69   /* Create hash of the nickanem */
70   silc_hash_make(md5hash, nickname, strlen(nickname), hash);
71
72   /* Create the ID */
73   memcpy((*new_id)->ip.data, server_id->ip.data, server_id->ip.data_len);
74   (*new_id)->ip.data_len = server_id->ip.data_len;
75   (*new_id)->rnd = silc_rng_get_byte(rng);
76   memcpy((*new_id)->hash, hash, CLIENTID_HASH_LEN);
77
78   /* Assure that the ID does not exist already */
79   while (1) {
80     if (!silc_idlist_find_client_by_id(server->local_list, 
81                                        *new_id, FALSE, NULL))
82       if (!silc_idlist_find_client_by_id(server->global_list, 
83                                          *new_id, FALSE, NULL))
84         break;
85
86     /* The ID exists, start increasing the rnd from 0 until we find a
87        ID that does not exist. If we wrap and it still exists then we
88        will return FALSE and the caller must send some other nickname
89        since this cannot be used anymore. */
90     (*new_id)->rnd++;
91
92     if (finding && (*new_id)->rnd == 0)
93       return FALSE;
94
95     if (!finding) {
96       (*new_id)->rnd = 0;
97       finding = TRUE;
98     }
99   }
100
101   SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_CLIENT)));
102
103   return TRUE;
104 }
105
106 /* Creates Channel ID */
107
108 bool silc_id_create_channel_id(SilcServer server,
109                                SilcServerID *router_id, SilcRng rng,
110                                SilcChannelID **new_id)
111 {
112   bool finding = TRUE;
113
114   SILC_LOG_DEBUG(("Creating new Channel ID"));
115
116   *new_id = silc_calloc(1, sizeof(**new_id));
117
118   /* Create the ID */
119   memcpy((*new_id)->ip.data, router_id->ip.data, router_id->ip.data_len);
120   (*new_id)->ip.data_len = router_id->ip.data_len;
121   (*new_id)->port = router_id->port;
122   (*new_id)->rnd = silc_rng_get_rn16(rng);
123
124   /* Assure that the ID does not exist already */
125   while (1) {
126     if (!silc_idlist_find_channel_by_id(server->local_list, 
127                                         *new_id, NULL))
128       break;
129
130     (*new_id)->rnd++;
131     
132     if (finding && (*new_id)->rnd == 0)
133       return FALSE;
134     
135     if (!finding) {
136       (*new_id)->rnd = 0;
137       finding = TRUE;
138     }
139   }
140
141   SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_CHANNEL)));
142
143   return TRUE;
144 }