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