Added SilcPublicKey's
[silc.git] / apps / silcd / idlist.h
1 /*
2
3   idlist.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
21 #ifndef IDLIST_H
22 #define IDLIST_H
23
24 /* Forward declarations */
25 typedef struct SilcServerListStruct SilcServerList;
26 typedef struct SilcClientListStruct SilcClientList;
27 typedef struct SilcChannelListStruct SilcChannelList;
28
29 /* 
30    SILC Server list object.
31
32    This list holds information about servers in SILC network. However, 
33    contents of this list is highly dependent of what kind of server we are 
34    (normal server or router server) and whether the list is used as a local 
35    list or a global list. These factors dictates the contents of this list.
36
37    This list is defined as follows:
38
39    Server type   List type      Contents
40    =======================================================================
41    server        local list     Server itself
42    server        global list    NULL
43    router        local list     All servers is the cell
44    router        global list    All servers in the SILC network
45
46    Following short description of the fields:
47
48    char *server_name
49
50        Logical name of the server. There is no limit of the length of the
51        server name. This is usually the same name as defined in DNS.
52
53    int server_type
54
55        Type of the server. SILC_SERVER or SILC_ROUTER are the possible
56        choices for this.
57
58    SilcServerID *id
59
60        ID of the server. This includes all the relevant information about
61        the server SILC will ever need. These are also the informations
62        that is broadcasted between servers and routers in the SILC network.
63
64    struct SilcServerListStruct *router
65
66        This is a pointer back to the server list. This is the router server 
67        where this server is connected to. If this is the router itself and 
68        it doesn't have a route this is NULL.
69
70    SilcCipher send_key
71    
72    SilcCipher receive_key
73
74    void *connection
75
76        A pointer, usually, to the socket list for fast referencing to
77        the data used in connection with this server.  This may be anything
78        but as just said, this is usually pointer to the socket connection
79        list.
80    
81 */
82 struct SilcServerListStruct {
83   char *server_name;
84   int server_type;
85   SilcServerID *id;
86
87   /* Pointer to the router */
88   struct SilcServerListStruct *router;
89
90   /* Keys */
91   SilcCipher send_key;
92   SilcCipher receive_key;
93   SilcPKCS pkcs;
94   SilcPublicKey public_key;
95   SilcHmac hmac;
96   unsigned char *hmac_key;
97   unsigned int hmac_key_len;
98
99   /* Connection data */
100   void *connection;
101
102   struct SilcServerListStruct *next;
103   struct SilcServerListStruct *prev;
104 };
105
106 /* 
107    SILC Client list object.
108
109    This list holds information about connected clients ie. users in the SILC
110    network. The contents of this list is depended on whether we are normal 
111    server or router server and whether the list is a local or global list.
112
113    This list is defined as follows:
114
115    Server type   List type      Contents
116    =======================================================================
117    server        local list     All clients in server
118    server        global list    NULL
119    router        local list     All clients in cell
120    router        global list    All clients in SILC
121
122    Following short description of the fields:
123
124    char username
125
126        Client's (meaning user's) real name. This is defined in following 
127        manner:
128
129        Server type   List type      Contents
130        ====================================================
131        server        local list     User's name
132        router        local list     NULL
133        router        global list    NULL
134
135        Router doesn't hold this information since it is not vital data 
136        for the router. If this information is needed by the client it is
137        fetched when it is needed.
138
139    char userinfo
140
141        Information about user. This is free information and can be virtually
142        anything. This is defined in following manner:
143        
144        Server type   List type      Contents
145        ====================================================
146        server        local list     User's information
147        router        local list     NULL
148        router        global list    NULL
149
150        Router doesn't hold this information since it is not vital data 
151        for the router. If this information is needed by the client it is
152        fetched when it is needed.
153
154    SilcClientID *id
155
156        ID of the client. This includes all the information SILC will ever
157        need. Notice that no nickname of the user is saved anywhere. This is
158        beacuse of SilcClientID includes 88 bit hash value of the user's 
159        nickname which can be used to track down specific user by their 
160        nickname. Nickname is not relevant information that would need to be 
161        saved as plain.
162
163    int mode
164
165        Client's mode.  Client maybe for example server operator or
166        router operator (SILC operator).
167
168    SilcServerList *router
169
170        This is a pointer to the server list. This is the router server whose 
171        cell this client is coming from. This is used to route messages to 
172        this client.
173
174    SilcCipher session_key
175
176        The actual session key established by key exchange protcol between
177        connecting parties. This is used for both encryption and decryption.
178
179    SilcPKCS pkcs
180
181        PKCS of the client. This maybe NULL.
182
183    SilcHmac hmac
184    unsigned char *hmac_key
185    unsigned int hmac_key_len
186
187        MAC key used to compute MAC's for packets. 
188
189    void *connection
190
191        A pointer, usually, to the socket list for fast referencing to
192        the data used in connection with this client.  This may be anything
193        but as just said, this is usually pointer to the socket connection
194        list.
195
196 */
197 struct SilcClientListStruct {
198   char *nickname;
199   char *username;
200   char *userinfo;
201   SilcClientID *id;
202   int mode;
203
204   /* Pointer to the router */
205   SilcServerList *router;
206
207   /* Pointers to channels this client has joined */
208   SilcChannelList **channel;
209   unsigned int channel_count;
210
211   /* Keys */
212   SilcCipher send_key;
213   SilcCipher receive_key;
214   SilcPKCS pkcs;
215   SilcHmac hmac;
216   unsigned char *hmac_key;
217   unsigned int hmac_key_len;
218
219   /* Connection data */
220   void *connection;
221
222   struct SilcClientListStruct *next;
223   struct SilcClientListStruct *prev;
224 };
225
226 /* 
227    SILC Channel Client list structure.
228
229    This list used only by the SilcChannelList object and it holds information 
230    about current clients (ie. users) on channel. Following short description 
231    of the fields:
232
233    SilcClientList client
234
235        Pointer to the client list. This is the client currently on channel.
236
237    int mode
238
239        Client's current mode on the channel.
240
241 */
242 typedef struct SilcChannelClientListStruct {
243   SilcClientList *client;
244   int mode;
245 } SilcChannelClientList;
246
247 /* 
248    SILC Channel list object.
249
250    This list holds information about channels in SILC network. The contents 
251    of this list is depended on whether we are normal server or router server 
252    and whether the list is a local or global list.
253
254    This list is defined as follows:
255
256    Server type   List type      Contents
257    =======================================================================
258    server        local list     All channels in server
259    server        global list    NULL
260    router        local list     All channels in cell
261    router        global list    All channels in SILC
262
263    Following short description of the fields:
264
265    char *channel_name
266
267        Logical name of the channel.
268
269    int mode
270
271        Current mode of the channel.
272
273    SilcChannelID *id
274
275        ID of the channel. This includes all the information SILC will ever
276        need.
277
278    int global_users
279  
280        Boolean value to tell whether there are users outside this server
281        on this channel. This is set to TRUE if router sends message to
282        the server that there are users outside your server on your
283        channel as well. This way server knows that messages needs to be
284        sent to the router for further routing. If this is a normal 
285        server and this channel is not created on this server this field
286        is always TRUE. If this server is a router this field is ignored.
287
288    char *topic
289
290        Current topic of the channel.
291
292    SilcServerList *router
293
294        This is a pointer to the server list. This is the router server 
295        whose cell this channel belongs to. This is used to route messages 
296        to this channel.
297
298    SilcCipher send_key
299
300
301    SilcCipher receive_key
302
303 */
304 struct SilcChannelListStruct {
305   char *channel_name;
306   int mode;
307   SilcChannelID *id;
308   int global_users;
309   char *topic;
310
311   /* List of users on channel */
312   SilcChannelClientList *user_list;
313   unsigned int user_list_count;
314
315   /* Pointer to the router */
316   SilcServerList *router;
317
318   /* Channel keys */
319   SilcCipher channel_key;
320   unsigned char *key;
321   unsigned int key_len;
322   unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
323
324   struct SilcChannelListStruct *next;
325   struct SilcChannelListStruct *prev;
326 };
327
328 /* 
329    SILC ID List object.
330
331    As for remainder these lists are defined as follows:
332
333    List        Server type   List type      Contents
334    =======================================================================
335    servers     server        local list     Server itself
336    servers     server        global list    NULL
337    servers     router        local list     All servers in cell
338    servers     router        global list    All servers in SILC
339
340    clients     server        local list     All clients in server
341    clients     server        global list    NULL
342    clients     router        local list     All clients in cell
343    clients     router        global list    All clients in SILC
344
345    channels    server        local list     All channels in server
346    channels    server        global list    NULL
347    channels    router        local list     All channels in cell
348    channels    router        global list    All channels in SILC
349
350    As seen on the list normal server never defines a global list. This is
351    because of normal server don't know anything about anything global data,
352    they get it from the router if and when they need it. Routers, on the
353    other hand, always define local and global lists because routers really
354    know all the relevant data in the SILC network.
355
356 */
357 typedef struct SilcIDListStruct {
358   SilcServerList *servers;
359   SilcClientList *clients;
360   SilcChannelList *channels;
361
362   /* ID Caches. Caches are used to perform fast search on the ID's. */
363   SilcIDCache *server_cache[96];
364   unsigned int server_cache_count[96];
365   SilcIDCache *client_cache[96];
366   unsigned int client_cache_count[96];
367   SilcIDCache *channel_cache[96];
368   unsigned int channel_cache_count[96];
369 } SilcIDListObject;
370
371 typedef SilcIDListObject *SilcIDList;
372
373 /*
374    Temporary ID List object.
375
376    This is used during authentication phases where we still don't
377    know what kind of connection remote connection is, hence, we
378    will use this structure instead until we know what type of
379    connection remote end is.
380
381    This is not in any list. This is always individually allocated
382    and used as such.
383
384 */
385 typedef struct {
386   SilcCipher send_key;
387   SilcCipher receive_key;
388   SilcPKCS pkcs;
389   SilcPublicKey public_key;
390
391   SilcHmac hmac;
392   unsigned char *hmac_key;
393   unsigned int hmac_key_len;
394
395   /* SilcComp comp */
396 } SilcIDListUnknown;
397
398 /* Prototypes */
399 void silc_idlist_add_server(SilcServerList **list, 
400                             char *server_name, int server_type,
401                             SilcServerID *id, SilcServerList *router,
402                             SilcCipher send_key, SilcCipher receive_key,
403                             SilcPKCS public_key, SilcHmac hmac, 
404                             SilcServerList **new_idlist);
405 void silc_idlist_add_client(SilcClientList **list, char *nickname,
406                             char *username, char *userinfo,
407                             SilcClientID *id, SilcServerList *router,
408                             SilcCipher send_key, SilcCipher receive_key,
409                             SilcPKCS public_key, SilcHmac hmac, 
410                             SilcClientList **new_idlist);
411 void silc_idlist_del_client(SilcClientList **list, SilcClientList *entry);
412 SilcClientList *
413 silc_idlist_find_client_by_nickname(SilcClientList *list,
414                                     char *nickname,
415                                     char *server);
416 SilcClientList *
417 silc_idlist_find_client_by_hash(SilcClientList *list,
418                                 char *nickname, SilcHash hash);
419 SilcClientList *
420 silc_idlist_find_client_by_id(SilcClientList *list, SilcClientID *id);
421 void silc_idlist_add_channel(SilcChannelList **list, 
422                              char *channel_name, int mode,
423                              SilcChannelID *id, SilcServerList *router,
424                              SilcCipher channel_key,
425                              SilcChannelList **new_idlist);
426 SilcChannelList *
427 silc_idlist_find_channel_by_id(SilcChannelList *list, SilcChannelID *id);
428 void silc_idlist_del_channel(SilcChannelList **list, SilcChannelList *entry);
429
430 #endif