Initial revision
[silc.git] / lib / silccore / id.h
1 /*
2
3   id.h
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 /* These are important ID types used in SILC. SILC server creates these
21    but SILC client has to handle these as well since these are used in
22    packet sending and reception. However, client never creates these
23    but it receives the correct ID's from server. Clients, servers and
24    channels are identified by the these ID's.
25
26    Note that these are currently IPv4 specific, although adding IPv6
27    support is not a bad task and SILC protocol already supports IPv6.
28 */
29
30 #ifndef ID_H
31 #define ID_H
32
33 #define SILC_ID_SERVER_LEN      (64 / 8)
34 #define SILC_ID_CLIENT_LEN      (128 / 8)
35 #define SILC_ID_CHANNEL_LEN     (64 / 8)
36 #define CLIENTID_HASH_LEN       (88 / 8) /* Client ID's 88 bit MD5 hash */
37
38 /* SILC ID Types */
39 #define SILC_ID_NONE 0
40 #define SILC_ID_SERVER 1
41 #define SILC_ID_CLIENT 2
42 #define SILC_ID_CHANNEL 3
43
44 /* Type definition for the ID types. */
45 typedef unsigned char SilcIdType;
46
47 /* 
48    64 bit SilcServerID structure:
49    
50    32 bit IP address
51    16 bit port
52    16 bit random number
53 */
54 typedef struct {
55   struct in_addr ip;                            /* 32 bit IP */
56   unsigned short port;                          /* 16 bit port */
57   unsigned short rnd;                           /* 16 bit random number */
58 } SilcServerID;
59
60 /* 
61    128 bit SilcClientID structure:
62
63    32 bit ServerID IP address [bits 1-32]
64     8 bit random number
65    88 bit hash value from nickname
66 */
67 typedef struct {
68   struct in_addr ip;                            /* 32 bit IP */
69   unsigned char rnd;                            /* 8 bit random number */
70   unsigned char hash[CLIENTID_HASH_LEN];        /* 88 bit MD5 hash */
71 } SilcClientID;
72
73 /* 
74    64 bit SilcChannel ID structure:
75
76    32 bit Router's ServerID IP address [bits 1-32]
77    16 bit Router's ServerID port [bits 33-48]
78    16 bit random number
79 */
80 typedef struct {
81   struct in_addr ip;                            /* 32 bit IP */
82   unsigned short port;                          /* 16 bit port */
83   unsigned short rnd;                           /* 16 bit random number */
84 } SilcChannelID;
85
86 /* Macros */
87
88 /* Compares two ID's */
89 #define SILC_ID_COMPARE(id1, id2, len) (memcmp(id1, id2, len))
90
91 /* Compares Channel ID's */
92 #define SILC_ID_CHANNEL_COMPARE(id1, id2) \
93   SILC_ID_COMPARE(id1, id2, SILC_ID_CHANNEL_LEN)
94
95 /* Compares Client ID's */
96 #define SILC_ID_CLIENT_COMPARE(id1, id2) \
97   SILC_ID_COMPARE(id1, id2, SILC_ID_CLIENT_LEN)
98
99 /* Compares Server ID's */
100 #define SILC_ID_SERVER_COMPARE(id1, id2) \
101   SILC_ID_COMPARE(id1, id2, SILC_ID_SERVER_LEN)
102
103 /* Compares IP addresses from the ID's. */
104 #define SILC_ID_COMPARE_IP(id1, id2) \
105   SILC_ID_COMPARE(id1, id2, 4)
106
107 /* Compare nickname hash from Client ID */
108 #define SILC_ID_COMPARE_HASH(id, hash) \
109   memcmp(id->hash, hash, CLIENTID_HASH_LEN)
110
111 /* Prototypes */
112 unsigned char *silc_id_id2str(void *id, SilcIdType type);
113 void *silc_id_str2id(unsigned char *id, SilcIdType type);
114 unsigned int silc_id_get_len(SilcIdType type);
115
116 #endif