updates.
[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
27 #ifndef ID_H
28 #define ID_H
29
30 /* The SILC ID Types */
31 #define SILC_ID_NONE 0
32 #define SILC_ID_SERVER 1
33 #define SILC_ID_CLIENT 2
34 #define SILC_ID_CHANNEL 3
35
36 /* Type definition for the ID types. */
37 typedef uint16 SilcIdType;
38
39 /* The ID Lenghts. These are IPv4 based and should be noted if used directly
40    that these cannot be used with IPv6. */
41 #define SILC_ID_SERVER_LEN      (64 / 8)
42 #define SILC_ID_CLIENT_LEN      (128 / 8)
43 #define SILC_ID_CHANNEL_LEN     (64 / 8)
44
45 #define CLIENTID_HASH_LEN       (88 / 8) /* Client ID's 88 bit MD5 hash */
46
47 /*
48    SILC ID IP structure.
49
50    Generic IP address structure to indicate either IPv4 or IPv6 address.
51    This structure is used inside all SILC ID's. The true length of the
52    ID depends of the length of the IP address.
53 */
54 typedef struct {
55   unsigned char data[16];       /* IP data */
56   uint8 data_len;               /* Length of the data (4 or 16) */
57 } SilcIDIP;
58
59 /* 
60    64 or 160 bit SilcServerID structure:
61    
62     n bit IP address
63    16 bit port
64    16 bit random number
65 */
66 typedef struct {
67   SilcIDIP ip;                  /* n bit IP address */
68   uint16 port;                  /* 16 bit port */
69   uint16 rnd;                   /* 16 bit random number */
70 } SilcServerID;
71
72 /* 
73    128 or 224 bit SilcClientID structure:
74
75     n bit ServerID IP address [bits 1-32 or bits 1-128]
76     8 bit random number
77    88 bit hash value from nickname
78 */
79 typedef struct {
80   SilcIDIP ip;                                  /* n bit IP address */
81   unsigned char rnd;                            /* 8 bit random number */
82   unsigned char hash[CLIENTID_HASH_LEN];        /* 88 bit MD5 hash */
83 } SilcClientID;
84
85 /* 
86    64 or 160 bit SilcChannel ID structure:
87
88     n bit Router's ServerID IP address [bits 1-32 or bits 1-128]
89    16 bit Router's ServerID port [bits 33-48 or bits 129-144]
90    16 bit random number
91 */
92 typedef struct {
93   SilcIDIP ip;                                  /* n bit IP address */
94   uint16 port;                                  /* 16 bit port */
95   uint16 rnd;                                   /* 16 bit random number */
96 } SilcChannelID;
97
98 /* Macros */
99
100 /* Compares two ID's. */
101 #define SILC_ID_COMPARE(id1, id2, len) (!memcmp(id1, id2, len))
102
103 /* Compares Client ID's */
104 #define SILC_ID_CLIENT_COMPARE(id1, id2) \
105   SILC_ID_COMPARE(id1, id2, sizeof(SilcClientID))
106
107 /* Compares Server ID's */
108 #define SILC_ID_SERVER_COMPARE(id1, id2) \
109   SILC_ID_COMPARE(id1, id2, sizeof(SilcServerID))
110
111 /* Compares Channel ID's */
112 #define SILC_ID_CHANNEL_COMPARE(id1, id2) \
113   SILC_ID_COMPARE(id1, id2, sizeof(SilcChannelID))
114
115 /* Compares two ID's by type */
116 #define SILC_ID_COMPARE_TYPE(id1, id2, type)                    \
117   (type == SILC_ID_SERVER ? SILC_ID_SERVER_COMPARE(id1, id2) :  \
118    type == SILC_ID_CLIENT ? SILC_ID_CLIENT_COMPARE(id1, id2) :  \
119    SILC_ID_CHANNEL_COMPARE(id1, id2))
120
121 /* Compare nickname hash from Client ID */
122 #define SILC_ID_COMPARE_HASH(id, _hash) \
123   memcmp(id->hash, _hash, CLIENTID_HASH_LEN)
124
125 /* Prototypes */
126 unsigned char *silc_id_id2str(void *id, SilcIdType type);
127 void *silc_id_str2id(unsigned char *id, uint32 id_len, SilcIdType type);
128 uint32 silc_id_get_len(void *id, SilcIdType type);
129 void *silc_id_dup(void *id, SilcIdType type);
130
131 #endif