89ea6b82a5807e40970eb343d62d7afabf138efa
[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 - 2000 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 /*
21  * $Id$
22  * $Log$
23  * Revision 1.2  2001/02/02 13:34:45  priikone
24  *      updates.
25  *
26  * Revision 1.1.1.1  2000/06/27 11:36:56  priikone
27  *      Importet from internal CVS/Added Log headers.
28  *
29  *
30  */
31
32 #include "serverincludes.h"
33
34 /* Creates a Server ID. Newly created Server ID is returned to the
35    new_id argument. */
36
37 void silc_id_create_server_id(int sock, SilcRng rng, SilcServerID **new_id)
38 {
39   struct sockaddr_in server;
40   int rval, len;
41
42   SILC_LOG_DEBUG(("Creating new Server ID"));
43
44   *new_id = silc_calloc(1, sizeof(**new_id));
45   if (*new_id == NULL) {
46     SILC_LOG_ERROR(("Could not allocate new Server ID"));
47     return;
48   }
49
50   /* Get IP address */
51   len = sizeof(server);
52   rval = getsockname(sock, (struct sockaddr *)&server, &len);
53   if (rval < 0) {
54     SILC_LOG_ERROR(("Could not get IP address: %s", strerror(errno)));
55     silc_free(*new_id);
56     *new_id = NULL;
57     return;
58   }
59
60   /* Create the ID */
61   (*new_id)->ip = server.sin_addr;
62   (*new_id)->port = server.sin_port;
63   (*new_id)->rnd = silc_rng_get_rn16(rng);
64
65   SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_SERVER)));
66 }
67
68 /* Creates Client ID */
69
70 void silc_id_create_client_id(SilcServerID *server_id, SilcRng rng,
71                               SilcHash md5hash, char *nickname, 
72                               SilcClientID **new_id)
73 {
74   unsigned char hash[16];
75
76   SILC_LOG_DEBUG(("Creating new Client ID"));
77
78   *new_id = silc_calloc(1, sizeof(**new_id));
79   if (*new_id == NULL) {
80     SILC_LOG_ERROR(("Could not allocate new Client ID"));
81     return;
82   }
83
84   /* Create hash of the nickanem */
85   silc_hash_make(md5hash, nickname, strlen(nickname), hash);
86
87   /* Create the ID */
88   (*new_id)->ip.s_addr = server_id->ip.s_addr;
89   (*new_id)->rnd = silc_rng_get_byte(rng);
90   memcpy((*new_id)->hash, hash, CLIENTID_HASH_LEN);
91
92   SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_CLIENT)));
93 }
94
95 /* Creates Channel ID */
96
97 void silc_id_create_channel_id(SilcServerID *router_id, SilcRng rng,
98                                SilcChannelID **new_id)
99 {
100   SILC_LOG_DEBUG(("Creating new Channel ID"));
101
102   *new_id = silc_calloc(1, sizeof(**new_id));
103   if (*new_id == NULL) {
104     SILC_LOG_ERROR(("Could not allocate new Channel ID"));
105     return;
106   }
107
108   /* Create the ID */
109   (*new_id)->ip.s_addr = router_id->ip.s_addr;
110   (*new_id)->port = router_id->port;
111   (*new_id)->rnd = silc_rng_get_rn16(rng);
112
113   SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_CHANNEL)));
114 }