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