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