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