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