updates.
[silc.git] / lib / silccore / silcid.h
1 /****h* silccore/silcid.h
2  *
3  * NAME
4  *
5  * silcid.h
6  *
7  * COPYRIGHT
8  *
9  * Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
10  *
11  * Copyright (C) 1997 - 2000 Pekka Riikonen
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * DESCRIPTION
24  *
25  * These are important ID types used in SILC. SILC server creates these
26  * but SILC client has to handle these as well since these are used in
27  * packet sending and reception. However, client never creates these
28  * but it receives the correct ID's from server. Clients, servers and
29  * channels are identified by the these ID's.
30  *
31  * The ID's are based on IP addresses. The IP address provides a good
32  * way to distinguish the ID's from other ID's. The ID's support both
33  * IPv4 and IPv6.
34  *
35  ***/
36
37 #ifndef SILCID_H
38 #define SILCID_H
39
40 /****d* silccore/SilcIDAPI/SilcIdType
41  *
42  * NAME
43  * 
44  *    typedef uint16 SilcIdType;
45  *
46  * DESCRIPTION
47  *
48  *    SILC ID type definitions and the ID types.
49  *
50  * SOURCE
51  */
52 typedef uint16 SilcIdType;
53
54 /* The SILC ID Types */
55 #define SILC_ID_NONE 0
56 #define SILC_ID_SERVER 1
57 #define SILC_ID_CLIENT 2
58 #define SILC_ID_CHANNEL 3
59 /***/
60
61 /* The ID Lenghts. These are IPv4 based and should be noted if used directly
62    that these cannot be used with IPv6. */
63 #define SILC_ID_SERVER_LEN      (64 / 8)
64 #define SILC_ID_CLIENT_LEN      (128 / 8)
65 #define SILC_ID_CHANNEL_LEN     (64 / 8)
66
67 #define CLIENTID_HASH_LEN       (88 / 8) /* Client ID's 88 bit MD5 hash */
68
69 /****s* silccore/SilcIDAPI/SilcIDIP
70  *
71  * NAME
72  * 
73  *    typedef struct { ... } SilcIDIP;
74  *
75  * DESCRIPTION
76  *
77  *    Generic IP address structure to indicate either IPv4 or IPv6 address.
78  *    This structure is used inside all SILC ID's. The true length of the
79  *    ID depends of the length of the IP address.
80  *
81  * SOURCE
82  */
83 typedef struct {
84   unsigned char data[16];       /* IP data (in MSB first order) */
85   uint8 data_len;               /* Length of the data (4 or 16) */
86 } SilcIDIP;
87 /***/
88
89 /****s* silccore/SilcIDAPI/SilcServerID
90  *
91  * NAME
92  * 
93  *    typedef struct { ... } SilcServerID;
94  *
95  * DESCRIPTION
96  *
97  *    64 or 160 bit SilcServerID structure:
98  *  
99  *     n bit IP address
100  *    16 bit port
101  *    16 bit random number
102  *
103  * SOURCE
104  */
105 typedef struct {
106   SilcIDIP ip;                  /* n bit IP address */
107   uint16 port;                  /* 16 bit port */
108   uint16 rnd;                   /* 16 bit random number */
109 } SilcServerID;
110 /***/
111
112 /****s* silccore/SilcIDAPI/SilcClientID
113  *
114  * NAME
115  * 
116  *    typedef struct { ... } SilcClientID;
117  *
118  * DESCRIPTION
119  *
120  *    128 or 224 bit SilcClientID structure:
121  *
122  *      n bit ServerID IP address [bits 1-32 or bits 1-128]
123  *      8 bit random number
124  *     88 bit hash value from nickname
125  *
126  * SOURCE
127  */
128 typedef struct {
129   SilcIDIP ip;                                  /* n bit IP address */
130   unsigned char rnd;                            /* 8 bit random number */
131   unsigned char hash[CLIENTID_HASH_LEN];        /* 88 bit MD5 hash */
132 } SilcClientID;
133 /***/
134
135 /****s* silccore/SilcIDAPI/SilcChannelID
136  *
137  * NAME
138  * 
139  *    typedef struct { ... } SilcChannelID;
140  *
141  * DESCRIPTION
142  *
143  *    64 or 160 bit SilcChannel ID structure:
144  *
145  *     n bit Router's ServerID IP address [bits 1-32 or bits 1-128]
146  *    16 bit Router's ServerID port [bits 33-48 or bits 129-144]
147  *    16 bit random number
148  *
149  * SOURCE
150  */
151 typedef struct {
152   SilcIDIP ip;                                  /* n bit IP address */
153   uint16 port;                                  /* 16 bit port */
154   uint16 rnd;                                   /* 16 bit random number */
155 } SilcChannelID;
156 /***/
157
158 /* Macros */
159
160 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE
161  *
162  * NAME
163  * 
164  *    #define SILC_ID_COMPARE ...
165  *
166  * DESCRIPTION
167  *
168  *    Compares two ID's. Returns TRUE if they match and FALSE if they do
169  *    not.
170  *
171  * SOURCE
172  */
173 #define SILC_ID_COMPARE(id1, id2, len) (!memcmp(id1, id2, len))
174 /***/
175
176 /****d* silccore/SilcIDAPI/SILC_ID_CLIENT_COMPARE
177  *
178  * NAME
179  * 
180  *    #define SILC_ID_CLIENT_COMPARE ...
181  *
182  * DESCRIPTION
183  *
184  *    Compares Client ID's. Returns TRUE if they match.
185  *
186  * SOURCE
187  */
188 #define SILC_ID_CLIENT_COMPARE(id1, id2) \
189   SILC_ID_COMPARE(id1, id2, sizeof(SilcClientID))
190 /***/
191
192 /****d* silccore/SilcIDAPI/SILC_ID_SERVER_COMPARE
193  *
194  * NAME
195  * 
196  *    #define SILC_ID_SERVER_COMPARE ...
197  *
198  * DESCRIPTION
199  *
200  *    Compares Server ID's. Returns TRUE if they match.
201  *
202  * SOURCE
203  */
204 #define SILC_ID_SERVER_COMPARE(id1, id2) \
205   SILC_ID_COMPARE(id1, id2, sizeof(SilcServerID))
206 /***/
207
208 /****d* silccore/SilcIDAPI/SILC_ID_CHANNEL_COMPARE
209  *
210  * NAME
211  * 
212  *    #define SILC_ID_CHANNEL_COMPARE ...
213  *
214  * DESCRIPTION
215  *
216  *    Compares Channel ID's. Returns TRUE if they match.
217  *
218  * SOURCE
219  */
220 #define SILC_ID_CHANNEL_COMPARE(id1, id2) \
221   SILC_ID_COMPARE(id1, id2, sizeof(SilcChannelID))
222 /***/
223
224 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_TYPE
225  *
226  * NAME
227  * 
228  *    #define SILC_ID_COMPARE_TYPE ...
229  *
230  * DESCRIPTION
231  *
232  *    Compares two ID's by type. Returns TRUE if they match.
233  *
234  * SOURCE
235  */
236 #define SILC_ID_COMPARE_TYPE(id1, id2, type)                    \
237   (type == SILC_ID_SERVER ? SILC_ID_SERVER_COMPARE(id1, id2) :  \
238    type == SILC_ID_CLIENT ? SILC_ID_CLIENT_COMPARE(id1, id2) :  \
239    SILC_ID_CHANNEL_COMPARE(id1, id2))
240 /***/
241
242 /****d* silccore/SilcIDAPI/SILC_ID_COMPARE_HASH
243  *
244  * NAME
245  * 
246  *    #define SILC_ID_COMPARE_HASH ...
247  *
248  * DESCRIPTION
249  *
250  *    Compares the nickname hash of the Client ID. Returns TRUE if
251  *    they match. Since the nickname hash is based on the nickname of
252  *    the client this can be used to search the ID by nickname (taking
253  *    the hash out of it) or using the hash from the ID.
254  *
255  * SOURCE
256  */
257 #define SILC_ID_COMPARE_HASH(id1, id2) \
258   (!memcmp((id1)->hash, (id2)->hash, CLIENTID_HASH_LEN))
259 /***/
260
261 /* Prototypes */
262
263 /****f* silccore/SilcIDAPI/silc_id_id2str
264  *
265  * SYNOPSIS
266  *
267  *    unsigned char *silc_id_id2str(void *id, SilcIdType type);
268  *
269  * DESCRIPTION
270  *
271  *    Converts an ID of type `type' to data. This can be used to
272  *    convert the ID's to data for inclusion in the packets.
273  *
274  ***/
275 unsigned char *silc_id_id2str(void *id, SilcIdType type);
276
277 /****f* silccore/SilcIDAPI/silc_id_str2id
278  *
279  * SYNOPSIS
280  *
281  *    void *silc_id_str2id(unsigned char *id, uint32 id_len, SilcIdType type);
282  *
283  * DESCRIPTION
284  *
285  *    Converts ID data string to an ID. This can be used to get the
286  *    ID out of data that has been taken for example from packet.
287  *
288  ***/
289 void *silc_id_str2id(unsigned char *id, uint32 id_len, SilcIdType type);
290
291 /****f* silccore/SilcIDAPI/silc_id_get_len
292  *
293  * SYNOPSIS
294  *
295  *    uint32 silc_id_get_len(void *id, SilcIdType type);
296  *
297  * DESCRIPTION
298  *
299  *    Returns the true length of the ID of the type `type'.
300  *
301  ***/
302 uint32 silc_id_get_len(void *id, SilcIdType type);
303
304 /****f* silccore/SilcIDAPI/silc_id_dup
305  *
306  * SYNOPSIS
307  *
308  *    void *silc_id_dup(void *id, SilcIdType type);
309  *
310  * DESCRIPTION
311  *
312  *    Duplicates the ID of the type `type'. The caller must free the
313  *    duplicated ID.
314  *
315  ***/
316 void *silc_id_dup(void *id, SilcIdType type);
317
318 #endif