Code auditing weekend results and fixes committing.
[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
24 /* Creates a Server ID. Newly created Server ID is returned to the
25    new_id argument. */
26
27 void silc_id_create_server_id(int sock, SilcRng rng, SilcServerID **new_id)
28 {
29   struct sockaddr_in server;
30   int rval, len;
31
32   SILC_LOG_DEBUG(("Creating new Server ID"));
33
34   *new_id = silc_calloc(1, sizeof(**new_id));
35
36   /* Get IP address */
37   len = sizeof(server);
38   rval = getsockname(sock, (struct sockaddr *)&server, &len);
39   if (rval < 0) {
40     SILC_LOG_ERROR(("Could not get IP address: %s", strerror(errno)));
41     silc_free(*new_id);
42     *new_id = NULL;
43     return;
44   }
45
46   /* Create the ID */
47   (*new_id)->ip = server.sin_addr;
48   (*new_id)->port = server.sin_port;
49   (*new_id)->rnd = silc_rng_get_rn16(rng);
50
51   SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_SERVER)));
52 }
53
54 /* Creates Client ID */
55
56 void silc_id_create_client_id(SilcServerID *server_id, SilcRng rng,
57                               SilcHash md5hash, char *nickname, 
58                               SilcClientID **new_id)
59 {
60   unsigned char hash[16];
61
62   SILC_LOG_DEBUG(("Creating new Client ID"));
63
64   *new_id = silc_calloc(1, sizeof(**new_id));
65
66   /* Create hash of the nickanem */
67   silc_hash_make(md5hash, nickname, strlen(nickname), hash);
68
69   /* Create the ID */
70   (*new_id)->ip.s_addr = server_id->ip.s_addr;
71   (*new_id)->rnd = silc_rng_get_byte(rng);
72   memcpy((*new_id)->hash, hash, CLIENTID_HASH_LEN);
73
74   SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_CLIENT)));
75 }
76
77 /* Creates Channel ID */
78
79 void silc_id_create_channel_id(SilcServerID *router_id, SilcRng rng,
80                                SilcChannelID **new_id)
81 {
82   SILC_LOG_DEBUG(("Creating new Channel ID"));
83
84   *new_id = silc_calloc(1, sizeof(**new_id));
85
86   /* Create the ID */
87   (*new_id)->ip.s_addr = router_id->ip.s_addr;
88   (*new_id)->port = router_id->port;
89   (*new_id)->rnd = silc_rng_get_rn16(rng);
90
91   SILC_LOG_DEBUG(("New ID (%s)", silc_id_render(*new_id, SILC_ID_CHANNEL)));
92 }