Code auditing weekend results and fixes committing.
[silc.git] / lib / silccore / id.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 /* $Id$ */
21
22 #include "silcincludes.h"
23
24 /* Converts ID to string. */
25
26 unsigned char *silc_id_id2str(void *id, SilcIdType type)
27 {
28   unsigned char *ret_id;
29   SilcServerID *server_id;
30   SilcClientID *client_id;
31   SilcChannelID *channel_id;
32
33   switch(type) {
34   case SILC_ID_SERVER:
35     server_id = (SilcServerID *)id;
36     ret_id = silc_calloc(8, sizeof(unsigned char));
37     SILC_PUT32_MSB(server_id->ip.s_addr, ret_id);
38     SILC_PUT16_MSB(server_id->port, &ret_id[4]);
39     SILC_PUT16_MSB(server_id->rnd, &ret_id[6]);
40     return ret_id;
41     break;
42   case SILC_ID_CLIENT:
43     client_id = (SilcClientID *)id;
44     ret_id = silc_calloc(16, sizeof(unsigned char));
45     SILC_PUT32_MSB(client_id->ip.s_addr, ret_id);
46     ret_id[4] = client_id->rnd;
47     memcpy(&ret_id[5], client_id->hash, CLIENTID_HASH_LEN);
48     return ret_id;
49     break;
50   case SILC_ID_CHANNEL:
51     channel_id = (SilcChannelID *)id;
52     ret_id = silc_calloc(8, sizeof(unsigned char));
53     SILC_PUT32_MSB(channel_id->ip.s_addr, ret_id);
54     SILC_PUT16_MSB(channel_id->port, &ret_id[4]);
55     SILC_PUT16_MSB(channel_id->rnd, &ret_id[6]);
56     return ret_id;
57     break;
58   }
59
60   return NULL;
61 }
62
63 /* Converts string to a ID */
64
65 void *silc_id_str2id(unsigned char *id, unsigned int id_len, SilcIdType type)
66 {
67
68   switch(type) {
69   case SILC_ID_SERVER:
70     {
71       SilcServerID *server_id;
72
73       if (id_len != SILC_ID_SERVER_LEN)
74         return NULL;
75
76       server_id = silc_calloc(1, sizeof(*server_id));
77       SILC_GET32_MSB(server_id->ip.s_addr, id);
78       SILC_GET16_MSB(server_id->port, &id[4]);
79       SILC_GET16_MSB(server_id->rnd, &id[6]);
80       return server_id;
81     }
82     break;
83   case SILC_ID_CLIENT:
84     {
85       SilcClientID *client_id;
86
87       if (id_len != SILC_ID_CLIENT_LEN)
88         return NULL;
89
90       client_id = silc_calloc(1, sizeof(*client_id));
91       SILC_GET32_MSB(client_id->ip.s_addr, id);
92       client_id->rnd = id[4];
93       memcpy(client_id->hash, &id[5], CLIENTID_HASH_LEN);
94       return client_id;
95     }
96     break;
97   case SILC_ID_CHANNEL:
98     {
99       SilcChannelID *channel_id;
100
101       if (id_len != SILC_ID_CHANNEL_LEN)
102         return NULL;
103
104       channel_id = silc_calloc(1, sizeof(*channel_id));
105       SILC_GET32_MSB(channel_id->ip.s_addr, id);
106       SILC_GET16_MSB(channel_id->port, &id[4]);
107       SILC_GET16_MSB(channel_id->rnd, &id[6]);
108       return channel_id;
109     }
110     break;
111   }
112
113   return NULL;
114 }
115
116 /* Returns length of the ID */
117
118 unsigned int silc_id_get_len(SilcIdType type)
119 {
120   switch(type) {
121   case SILC_ID_SERVER:
122     return SILC_ID_SERVER_LEN;
123     break;
124   case SILC_ID_CLIENT:
125     return SILC_ID_CLIENT_LEN;
126     break;
127   case SILC_ID_CHANNEL:
128     return SILC_ID_CHANNEL_LEN;
129     break;
130   }
131
132   return 0;
133 }