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