updates.
[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 - 2001 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 SilcServerEntryStruct *SilcServerEntry;
26 typedef struct SilcClientEntryStruct *SilcClientEntry;
27 typedef struct SilcChannelEntryStruct *SilcChannelEntry;
28
29 /* Context for holding cache information to periodically purge
30    the cache. */
31 typedef struct {
32   SilcIDCache cache;
33   void *timeout_queue;
34 } *SilcIDListPurge;
35
36 /*
37    Generic ID list data structure.
38
39    This structure is included in all ID list entries and it includes data
40    pointers that are common to all ID entries.  This structure is always
41    defined to the first field in the ID entries and is used to explicitly
42    cast to this type without first explicitly casting to correct ID entry
43    type.  Hence, the ID list entry is casted to this type to get this data
44    from the ID entry (which is usually opaque pointer).
45
46    Note that some of the fields may be NULL.
47
48 */
49 typedef struct {
50   /* Send and receive symmetric keys */
51   SilcCipher send_key;
52   SilcCipher receive_key;
53
54   /* Hash selected in the SKE protocol, NULL if not needed at all */
55   SilcHash hash;
56
57   /* HMAC and raw key data */
58   SilcHmac hmac;
59   unsigned char *hmac_key;
60   unsigned int hmac_key_len;
61
62   /* public key */
63   SilcPublicKey public_key;
64
65   long last_receive;         /* Time last received data */
66   long last_sent;            /* Time last sent data */
67   unsigned char registered;  /* Boolean whether connection is registered */
68 } *SilcIDListData, SilcIDListDataStruct;
69
70 /* 
71    SILC Server entry object.
72
73    This entry holds information about servers in SILC network. However, 
74    contents of this entry is highly dependent of what kind of server we are 
75    (normal server or router server) and whether the entry is used as a local 
76    list or a global list. These factors dictates the contents of this entry.
77
78    This entry is defined as follows:
79
80    Server type   List type      Contents
81    =======================================================================
82    server        local list     Server itself
83    server        global list    NULL
84    router        local list     All servers is the cell
85    router        global list    All servers in the SILC network
86
87    Following short description of the fields:
88
89    SilcIDListDataStruct data
90
91        Generic data structure to hold data common to all ID entries.
92
93    char *server_name
94
95        Logical name of the server. There is no limit of the length of the
96        server name. This is usually the same name as defined in DNS.
97
98    int server_type
99
100        Type of the server. SILC_SERVER or SILC_ROUTER are the possible
101        choices for this.
102
103    SilcServerID *id
104
105        ID of the server. This includes all the relevant information about
106        the server SILC will ever need. These are also the informations
107        that is broadcasted between servers and routers in the SILC network.
108
109    char *server_info
110    char *motd
111
112        Server info (from INFO command) saved temporarily and motd (from
113        MOTD command) saved temporarily.
114
115    SilcServerEntry router
116
117        This is a pointer back to the server list. This is the router server 
118        where this server is connected to. If this is the router itself and 
119        it doesn't have a route this is NULL.
120
121    SilcCipher send_key
122    SilcCipher receive_key
123
124        Data sending and receiving keys.
125
126    void *connection
127
128        A pointer, usually, to the socket list for fast referencing to
129        the data used in connection with this server.  This may be anything
130        but as just said, this is usually pointer to the socket connection
131        list.
132    
133 */
134 struct SilcServerEntryStruct {
135   /* Generic data structure. DO NOT add anything before this! */
136   SilcIDListDataStruct data;
137
138   char *server_name;
139   int server_type;
140   SilcServerID *id;
141   char *server_info;
142   char *motd;
143
144   /* Pointer to the router */
145   SilcServerEntry router;
146
147   /* Connection data */
148   void *connection;
149 };
150
151 /* 
152    SILC Channel Client entry structure.
153
154    This entry used only by the SilcChannelEntry object and it holds
155    information about current clients (ie. users) on channel. Following
156    short description  of the fields:
157
158    SilcClientEntry client
159
160        Pointer to the client list. This is the client currently on channel.
161
162    unsigned int mode
163
164        Client's current mode on the channel.
165
166    SilcChannelEntry channel
167
168        Back pointer back to channel. As this structure is also used by
169        SilcClientEntry we have this here for fast access to the channel when
170        used by SilcClientEntry.
171
172   struct SilcChannelClientEntryStruct *client_list
173   struct SilcChannelClientEntryStruct *channel_list
174
175        List member pointers. This structure is used by channel entry and
176        client entry thus we must have separate list member pointers for
177        them since we are using same entry for both lists (the entry is not
178        duplicated). SilcList requires this.
179
180 */
181 typedef struct SilcChannelClientEntryStruct {
182   SilcClientEntry client;
183   unsigned int mode;
184   SilcChannelEntry channel;
185   struct SilcChannelClientEntryStruct *client_list;
186   struct SilcChannelClientEntryStruct *channel_list;
187 } *SilcChannelClientEntry;
188
189 /* 
190    SILC Client entry object.
191
192    This entry holds information about connected clients ie. users in the SILC
193    network. The contents of this entrt is depended on whether we are normal 
194    server or router server and whether the list is a local or global list.
195
196    This entry is defined as follows:
197
198    Server type   List type      Contents
199    =======================================================================
200    server        local list     All clients in server
201    server        global list    NULL
202    router        local list     All clients in cell
203    router        global list    All clients in SILC
204
205    Following short description of the fields:
206
207    SilcIDListDataStruct data
208
209        Generic data structure to hold data common to all ID entries.
210
211    char username
212
213        Client's usename. This is defined in the following manner:
214
215        Server type   List type      Contents
216        ====================================================
217        server        local list     User's name
218        router        local list     NULL
219        router        global list    NULL
220
221        Router doesn't hold this information since it is not vital data 
222        for the router. If this information is needed by the client it is
223        fetched when it is needed.
224
225    char userinfo
226
227        Information about user. This is free information and can be virtually
228        anything. This is defined in following manner:
229        
230        Server type   List type      Contents
231        ====================================================
232        server        local list     User's information
233        router        local list     NULL
234        router        global list    NULL
235
236        Router doesn't hold this information since it is not vital data 
237        for the router. If this information is needed by the client it is
238        fetched when it is needed.
239
240    SilcClientID *id
241
242        ID of the client. This includes all the information SILC will ever
243        need. Notice that no nickname of the user is saved anywhere. This is
244        beacuse of SilcClientID includes 88 bit hash value of the user's 
245        nickname which can be used to track down specific user by their 
246        nickname. Nickname is not relevant information that would need to be 
247        saved as plain.
248
249    int mode
250
251        Client's mode.  Client maybe for example server operator or
252        router operator (SILC operator).
253
254    long last_command
255
256        Time of last time client executed command. We are strict and will
257        not allow any command to be exeucted more than once in about
258        2 seconds. This is result of normal time().
259
260    char fast_command
261
262        Counter to check command bursts.  By default, up to 5 commands
263        are allowed before limiting the execution.  See command flags
264        for more detail.
265
266    SilcServerEntry router
267
268        This is a pointer to the server list. This is the router server whose 
269        cell this client is coming from. This is used to route messages to 
270        this client.
271
272    SilcList channels
273
274        List of channels this client has joined.
275
276    void *connection
277
278        A pointer, usually, to the socket list for fast referencing to
279        the data used in connection with this client.  This may be anything
280        but as just said, this is usually pointer to the socket connection
281        list.
282
283 */
284 struct SilcClientEntryStruct {
285   /* Generic data structure. DO NOT add anything before this! */
286   SilcIDListDataStruct data;
287
288   unsigned char *nickname;
289   char *username;
290   char *userinfo;
291   SilcClientID *id;
292   int mode;
293
294   long last_command;
295   char fast_command;
296
297   /* Pointer to the router */
298   SilcServerEntry router;
299
300   /* List of channels client has joined to */
301   SilcList channels;
302
303   /* Connection data */
304   void *connection;
305 };
306
307 /* 
308    SILC Channel entry object.
309
310    This entry holds information about channels in SILC network. The contents 
311    of this entry is depended on whether we are normal server or router server 
312    and whether the list is a local or global list.
313
314    This entry is defined as follows:
315
316    Server type   List type      Contents
317    =======================================================================
318    server        local list     All channels in server
319    server        global list    NULL
320    router        local list     All channels in cell
321    router        global list    All channels in SILC
322
323    Following short description of the fields:
324
325    char *channel_name
326
327        Logical name of the channel.
328
329    unsigned int mode
330
331        Current mode of the channel.  See lib/silccore/silcchannel.h for
332        all modes.
333
334    SilcChannelID *id
335
336        ID of the channel. This includes all the information SILC will ever
337        need.
338
339    int global_users
340  
341        Boolean value to tell whether there are users outside this server
342        on this channel. This is set to TRUE if router sends message to
343        the server that there are users outside your server on your
344        channel as well. This way server knows that messages needs to be
345        sent to the router for further routing. If this is a normal 
346        server and this channel is not created on this server this field
347        is always TRUE. If this server is a router this field is ignored.
348
349    char *topic
350
351        Current topic of the channel.
352
353    char *cipher
354
355        Default cipher of the channel. If this is NULL then server picks
356        the cipher to be used. This can be set at SILC_COMMAND_JOIN.
357
358    char *hmac_name
359
360        Default hmac of the channel. If this is NULL then server picks
361        the cipher to be used. This can be set at SILC_COMMAND_JOIN.
362
363    SilcPublicKey founder_key
364    SilcAuthMethod founder_method
365    unsigned char *founder_passwd
366    unsigned int founder_passwd_len
367
368        If the SILC_CMODE_FOUNDER_AUTH has been set then these will include
369        the founder's public key, authentication method and the password
370        if the method is SILC_AUTH_PASSWORD.  If it is SILC_AUTH_PUBLIC_KEY
371        then the `founder_passwd' is NULL.
372
373    SilcServerEntry router
374
375        This is a pointer to the server list. This is the router server 
376        whose cell this channel belongs to. This is used to route messages 
377        to this channel.
378
379    SilcCipher channel_key
380
381        The key of the channel (the cipher actually).
382
383    unsigned char *key
384    unsigned int key_len
385
386        Raw key data of the channel key.
387
388    unsigned char iv[SILC_CIPHER_MAX_IV_SIZE]
389
390        Current initial vector. Initial vector is received always along
391        with the channel packet. By default this is filled with NULL.
392
393    SilcHmac hmac;
394
395        HMAC of the channel.
396
397 */
398 struct SilcChannelEntryStruct {
399   char *channel_name;
400   unsigned int mode;
401   SilcChannelID *id;
402   int global_users;
403   char *topic;
404   char *cipher;
405   char *hmac_name;
406
407   SilcPublicKey founder_key;
408   SilcAuthMethod founder_method;
409   unsigned char *founder_passwd;
410   unsigned int founder_passwd_len;
411
412   unsigned int user_limit;
413   unsigned char *passphrase;
414   char *invite_list;
415   char *ban_list;
416
417   /* List of users on channel */
418   SilcList user_list;
419
420   /* Pointer to the router */
421   SilcServerEntry router;
422
423   /* Channel keys */
424   SilcCipher channel_key;
425   unsigned char *key;
426   unsigned int key_len;
427   unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
428   SilcHmac hmac;
429 };
430
431 /* 
432    SILC ID List object.
433
434    As for remainder these lists are defined as follows:
435
436    Entry list (cache)  Server type   List type      Contents
437    =======================================================================
438    servers             server        local list     Server itself
439    servers             server        global list    NULL
440    servers             router        local list     All servers in cell
441    servers             router        global list    All servers in SILC
442
443    clients             server        local list     All clients in server
444    clients             server        global list    NULL
445    clients             router        local list     All clients in cell
446    clients             router        global list    All clients in SILC
447
448    channels            server        local list     All channels in server
449    channels            server        global list    NULL
450    channels            router        local list     All channels in cell
451    channels            router        global list    All channels in SILC
452
453    As seen on the list normal server never defines a global list. This is
454    because of normal server don't know anything about anything global data,
455    they get it from the router if and when they need it. Routers, on the
456    other hand, always define local and global lists because routers really
457    know all the relevant data in the SILC network.
458
459    This object is used as local and global list by the server/router.
460    Above table shows how this is defined on different conditions.
461
462    This object holds pointers to the ID cache system. Every ID cache entry
463    has a specific context pointer to allocated entry (server, client or
464    channel entry).
465
466 */
467 typedef struct SilcIDListStruct {
468   SilcIDCache servers;
469   SilcIDCache clients;
470   SilcIDCache channels;
471 } *SilcIDList;
472
473 /*
474    ID Entry for Unknown connections.
475
476    This is used during authentication phases where we still don't know 
477    what kind of connection remote connection is, hence, we will use this
478    structure instead until we know what type of connection remote end is.
479
480    This is not in any list. This is always individually allocated and
481    used as such.
482
483 */
484 typedef struct {
485   /* Generic data structure. DO NOT add anything before this! */
486   SilcIDListDataStruct data;
487 } *SilcUnknownEntry;
488
489 /* Prototypes */
490 void silc_idlist_add_data(void *entry, SilcIDListData idata);
491 void silc_idlist_del_data(void *entry);
492 SILC_TASK_CALLBACK_GLOBAL(silc_idlist_purge);
493 SilcServerEntry 
494 silc_idlist_add_server(SilcIDList id_list, 
495                        char *server_name, int server_type,
496                        SilcServerID *id, SilcServerEntry router,
497                        void *connection);
498 SilcServerEntry
499 silc_idlist_find_server_by_id(SilcIDList id_list, SilcServerID *id,
500                               SilcIDCacheEntry *ret_entry);
501 SilcServerEntry
502 silc_idlist_find_server_by_name(SilcIDList id_list, char *name,
503                                 SilcIDCacheEntry *ret_entry);
504 SilcServerEntry
505 silc_idlist_find_server_by_conn(SilcIDList id_list, char *hostname,
506                                 int port, SilcIDCacheEntry *ret_entry);
507 SilcServerEntry
508 silc_idlist_replace_server_id(SilcIDList id_list, SilcServerID *old_id,
509                               SilcServerID *new_id);
510 void silc_idlist_del_server(SilcIDList id_list, SilcServerEntry entry);
511 SilcClientEntry
512 silc_idlist_add_client(SilcIDList id_list, unsigned char *nickname, 
513                        unsigned int nickname_len, char *username, 
514                        char *userinfo, SilcClientID *id, 
515                        SilcServerEntry router, void *connection);
516 int silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry);
517 SilcClientEntry *
518 silc_idlist_get_clients_by_nickname(SilcIDList id_list, char *nickname,
519                                     char *server, unsigned int *clients_count);
520 SilcClientEntry *
521 silc_idlist_get_clients_by_hash(SilcIDList id_list, char *nickname,
522                                 SilcHash md5hash,
523                                 unsigned int *clients_count);
524 SilcClientEntry
525 silc_idlist_find_client_by_hash(SilcIDList id_list, char *nickname,
526                                 SilcHash md5hash, SilcIDCacheEntry *ret_entry);
527 SilcClientEntry
528 silc_idlist_find_client_by_id(SilcIDList id_list, SilcClientID *id,
529                               SilcIDCacheEntry *ret_entry);
530 SilcClientEntry
531 silc_idlist_replace_client_id(SilcIDList id_list, SilcClientID *old_id,
532                               SilcClientID *new_id);
533 void silc_idlist_client_destructor(SilcIDCache cache,
534                                    SilcIDCacheEntry entry);
535 SilcChannelEntry
536 silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
537                         SilcChannelID *id, SilcServerEntry router,
538                         SilcCipher channel_key, SilcHmac hmac);
539 int silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry);
540 SilcChannelEntry
541 silc_idlist_find_channel_by_name(SilcIDList id_list, char *name,
542                                  SilcIDCacheEntry *ret_entry);
543 SilcChannelEntry
544 silc_idlist_find_channel_by_id(SilcIDList id_list, SilcChannelID *id,
545                                SilcIDCacheEntry *ret_entry);
546 SilcChannelEntry
547 silc_idlist_replace_channel_id(SilcIDList id_list, SilcChannelID *old_id,
548                                SilcChannelID *new_id);
549 SilcChannelEntry *
550 silc_idlist_get_channels(SilcIDList id_list, SilcChannelID *channel_id,
551                          unsigned int *channels_count);
552
553 #endif