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    unsigned char *nickname
233
234        The nickname of the client.
235
236    char *servername
237
238        The name of the server where the client is from. MAy be NULL.
239
240    char username
241
242        Client's usename. This is defined in the following manner:
243
244        Server type   List type      Contents
245        ====================================================
246        server        local list     User's name
247        router        local list     NULL
248        router        global list    NULL
249
250        Router doesn't hold this information since it is not vital data 
251        for the router. If this information is needed by the client it is
252        fetched when it is needed.
253
254    char userinfo
255
256        Information about user. This is free information and can be virtually
257        anything. This is defined in following manner:
258        
259        Server type   List type      Contents
260        ====================================================
261        server        local list     User's information
262        router        local list     NULL
263        router        global list    NULL
264
265        Router doesn't hold this information since it is not vital data 
266        for the router. If this information is needed by the client it is
267        fetched when it is needed.
268
269    SilcClientID *id
270
271        ID of the client. This includes all the information SILC will ever
272        need. Notice that no nickname of the user is saved anywhere. This is
273        beacuse of SilcClientID includes 88 bit hash value of the user's 
274        nickname which can be used to track down specific user by their 
275        nickname. Nickname is not relevant information that would need to be 
276        saved as plain.
277
278    uint32 mode
279
280        Client's mode.  Client maybe for example server operator or
281        router operator (SILC operator).
282
283    long last_command
284
285        Time of last time client executed command. We are strict and will
286        not allow any command to be exeucted more than once in about
287        2 seconds. This is result of normal time().
288
289    uint8 fast_command
290
291        Counter to check command bursts.  By default, up to 5 commands
292        are allowed before limiting the execution.  See command flags
293        for more detail.
294
295    SilcServerEntry router
296
297        This is a pointer to the server list. This is the router server whose 
298        cell this client is coming from. This is used to route messages to 
299        this client.
300
301    SilcList channels
302
303        List of channels this client has joined.
304
305    void *connection
306
307        A pointer, usually, to the socket list for fast referencing to
308        the data used in connection with this client.  This may be anything
309        but as just said, this is usually pointer to the socket connection
310        list.
311
312 */
313 struct SilcClientEntryStruct {
314   /* Generic data structure. DO NOT add anything before this! */
315   SilcIDListDataStruct data;
316
317   unsigned char *nickname;
318   char *servername;
319   char *username;
320   char *userinfo;
321   SilcClientID *id;
322   uint32 mode;
323
324   long last_command;
325   uint8 fast_command;
326
327   /* Pointer to the router */
328   SilcServerEntry router;
329
330   /* List of channels client has joined to */
331   SilcList channels;
332
333   /* Connection data */
334   void *connection;
335 };
336
337 /* 
338    SILC Channel entry object.
339
340    This entry holds information about channels in SILC network. The contents 
341    of this entry is depended on whether we are normal server or router server 
342    and whether the list is a local or global list.
343
344    This entry is defined as follows:
345
346    Server type   List type      Contents
347    =======================================================================
348    server        local list     All channels in server
349    server        global list    NULL
350    router        local list     All channels in cell
351    router        global list    All channels in SILC
352
353    Following short description of the fields:
354
355    char *channel_name
356
357        Logical name of the channel.
358
359    uint32 mode
360
361        Current mode of the channel.  See lib/silccore/silcchannel.h for
362        all modes.
363
364    SilcChannelID *id
365
366        ID of the channel. This includes all the information SILC will ever
367        need.
368
369    bool global_users
370  
371        Boolean value to tell whether there are users outside this server
372        on this channel. This is set to TRUE if router sends message to
373        the server that there are users outside your server on your
374        channel as well. This way server knows that messages needs to be
375        sent to the router for further routing. If this is a normal 
376        server and this channel is not created on this server this field
377        is always TRUE. If this server is a router this field is ignored.
378
379    char *topic
380
381        Current topic of the channel.
382
383    char *cipher
384
385        Default cipher of the channel. If this is NULL then server picks
386        the cipher to be used. This can be set at SILC_COMMAND_JOIN.
387
388    char *hmac_name
389
390        Default hmac of the channel. If this is NULL then server picks
391        the cipher to be used. This can be set at SILC_COMMAND_JOIN.
392
393    SilcPublicKey founder_key
394    SilcAuthMethod founder_method
395    unsigned char *founder_passwd
396    uint32 founder_passwd_len
397
398        If the SILC_CMODE_FOUNDER_AUTH has been set then these will include
399        the founder's public key, authentication method and the password
400        if the method is SILC_AUTH_PASSWORD.  If it is SILC_AUTH_PUBLIC_KEY
401        then the `founder_passwd' is NULL.
402
403    SilcServerEntry router
404
405        This is a pointer to the server list. This is the router server 
406        whose cell this channel belongs to. This is used to route messages 
407        to this channel.
408
409    SilcCipher channel_key
410
411        The key of the channel (the cipher actually).
412
413    unsigned char *key
414    uint32 key_len
415
416        Raw key data of the channel key.
417
418    unsigned char iv[SILC_CIPHER_MAX_IV_SIZE]
419
420        Current initial vector. Initial vector is received always along
421        with the channel packet. By default this is filled with NULL.
422
423    SilcHmac hmac;
424
425        HMAC of the channel.
426
427    SilcServerChannelRekey rekey
428
429        Channel key re-key context.
430
431 */
432 struct SilcChannelEntryStruct {
433   char *channel_name;
434   uint32 mode;
435   SilcChannelID *id;
436   bool global_users;
437   char *topic;
438   char *cipher;
439   char *hmac_name;
440
441   SilcPublicKey founder_key;
442   SilcAuthMethod founder_method;
443   unsigned char *founder_passwd;
444   uint32 founder_passwd_len;
445
446   uint32 user_limit;
447   unsigned char *passphrase;
448   char *invite_list;
449   char *ban_list;
450
451   /* List of users on channel */
452   SilcList user_list;
453
454   /* Pointer to the router */
455   SilcServerEntry router;
456
457   /* Channel keys */
458   SilcCipher channel_key;
459   unsigned char *key;
460   uint32 key_len;
461   unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
462   SilcHmac hmac;
463
464   SilcServerChannelRekey rekey;
465 };
466
467 /* 
468    SILC ID List object.
469
470    As for remainder these lists are defined as follows:
471
472    Entry list (cache)  Server type   List type      Contents
473    =======================================================================
474    servers             server        local list     Server itself
475    servers             server        global list    NULL
476    servers             router        local list     All servers in cell
477    servers             router        global list    All servers in SILC
478
479    clients             server        local list     All clients in server
480    clients             server        global list    NULL
481    clients             router        local list     All clients in cell
482    clients             router        global list    All clients in SILC
483
484    channels            server        local list     All channels in server
485    channels            server        global list    NULL
486    channels            router        local list     All channels in cell
487    channels            router        global list    All channels in SILC
488
489    As seen on the list normal server never defines a global list. This is
490    because of normal server don't know anything about anything global data,
491    they get it from the router if and when they need it. Routers, on the
492    other hand, always define local and global lists because routers really
493    know all the relevant data in the SILC network.
494
495    This object is used as local and global list by the server/router.
496    Above table shows how this is defined on different conditions.
497
498    This object holds pointers to the ID cache system. Every ID cache entry
499    has a specific context pointer to allocated entry (server, client or
500    channel entry).
501
502 */
503 typedef struct SilcIDListStruct {
504   SilcIDCache servers;
505   SilcIDCache clients;
506   SilcIDCache channels;
507 } *SilcIDList;
508
509 /*
510    ID Entry for Unknown connections.
511
512    This is used during authentication phases where we still don't know 
513    what kind of connection remote connection is, hence, we will use this
514    structure instead until we know what type of connection remote end is.
515
516    This is not in any list. This is always individually allocated and
517    used as such.
518
519 */
520 typedef struct {
521   /* Generic data structure. DO NOT add anything before this! */
522   SilcIDListDataStruct data;
523 } *SilcUnknownEntry;
524
525 /* Prototypes */
526 void silc_idlist_add_data(void *entry, SilcIDListData idata);
527 void silc_idlist_del_data(void *entry);
528 SILC_TASK_CALLBACK_GLOBAL(silc_idlist_purge);
529 SilcServerEntry 
530 silc_idlist_add_server(SilcIDList id_list, 
531                        char *server_name, int server_type,
532                        SilcServerID *id, SilcServerEntry router,
533                        void *connection);
534 SilcServerEntry
535 silc_idlist_find_server_by_id(SilcIDList id_list, SilcServerID *id,
536                               SilcIDCacheEntry *ret_entry);
537 SilcServerEntry
538 silc_idlist_find_server_by_name(SilcIDList id_list, char *name,
539                                 SilcIDCacheEntry *ret_entry);
540 SilcServerEntry
541 silc_idlist_find_server_by_conn(SilcIDList id_list, char *hostname,
542                                 int port, SilcIDCacheEntry *ret_entry);
543 SilcServerEntry
544 silc_idlist_replace_server_id(SilcIDList id_list, SilcServerID *old_id,
545                               SilcServerID *new_id);
546 int silc_idlist_del_server(SilcIDList id_list, SilcServerEntry entry);
547 SilcClientEntry
548 silc_idlist_add_client(SilcIDList id_list, char *nickname, char *username, 
549                        char *userinfo, SilcClientID *id, 
550                        SilcServerEntry router, void *connection);
551 int silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry);
552 int silc_idlist_get_clients_by_nickname(SilcIDList id_list, char *nickname,
553                                         char *server, 
554                                         SilcClientEntry **clients,
555                                         uint32 *clients_count);
556 int silc_idlist_get_clients_by_hash(SilcIDList id_list, char *nickname,
557                                     SilcHash md5hash,
558                                     SilcClientEntry **clients,
559                                     uint32 *clients_count);
560 SilcClientEntry
561 silc_idlist_find_client_by_id(SilcIDList id_list, SilcClientID *id,
562                               SilcIDCacheEntry *ret_entry);
563 SilcClientEntry
564 silc_idlist_replace_client_id(SilcIDList id_list, SilcClientID *old_id,
565                               SilcClientID *new_id);
566 void silc_idlist_client_destructor(SilcIDCache cache,
567                                    SilcIDCacheEntry entry);
568 SilcChannelEntry
569 silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
570                         SilcChannelID *id, SilcServerEntry router,
571                         SilcCipher channel_key, SilcHmac hmac);
572 int silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry);
573 SilcChannelEntry
574 silc_idlist_find_channel_by_name(SilcIDList id_list, char *name,
575                                  SilcIDCacheEntry *ret_entry);
576 SilcChannelEntry
577 silc_idlist_find_channel_by_id(SilcIDList id_list, SilcChannelID *id,
578                                SilcIDCacheEntry *ret_entry);
579 SilcChannelEntry
580 silc_idlist_replace_channel_id(SilcIDList id_list, SilcChannelID *old_id,
581                                SilcChannelID *new_id);
582 SilcChannelEntry *
583 silc_idlist_get_channels(SilcIDList id_list, SilcChannelID *channel_id,
584                          uint32 *channels_count);
585
586 #endif