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