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