Don't decrement my_clients when killing or expiring a detached client.
[silc.git] / apps / silcd / idlist.h
1 /*
2
3   idlist.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2005, 2007 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 #include "serverconfig.h"
25
26 /* Context for holding cache information to periodically purge
27    the cache. */
28 typedef struct {
29   SilcIDCache cache;
30   SilcUInt32 timeout;
31 } *SilcIDListPurge;
32
33 /* Channel key re-key context. */
34 typedef struct {
35   SilcChannelEntry channel;
36   SilcUInt32 key_len;
37   SilcTask task;
38 } *SilcServerChannelRekey;
39
40 /* ID List Entry status flags. */
41 typedef SilcUInt8 SilcIDListStatus;
42 #define SILC_IDLIST_STATUS_NONE         0x00  /* No status */
43 #define SILC_IDLIST_STATUS_REGISTERED   0x01  /* Entry is registered */
44 #define SILC_IDLIST_STATUS_RESOLVED     0x02  /* Entry info is resolved */
45 #define SILC_IDLIST_STATUS_RESOLVING    0x04  /* Entry is being resolved
46                                                  with WHOIS or IDENTIFY */
47 #define SILC_IDLIST_STATUS_DISABLED     0x08  /* Entry is disabled */
48 #define SILC_IDLIST_STATUS_RESUMED      0x10  /* Entry is resumed */
49 #define SILC_IDLIST_STATUS_LOCAL        0x20  /* Entry locally connected */
50 #define SILC_IDLIST_STATUS_RESUME_RES   0x40  /* Entry resolved while
51                                                  resuming */
52 #define SILC_IDLIST_STATUS_NOATTR       0x80  /* Entry does not support
53                                                  attributes in WHOIS */
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    type cast to this type without first explicitly casting to correct ID
62    entry type.  Hence, the ID list entry is type casted to this type to
63    get this data from the ID entry (which is usually opaque pointer).
64
65    Note that some of the fields may be NULL.
66
67 */
68 struct SilcIDListDataObject {
69   SilcConnectionType conn_type;      /* Connection type */
70   SilcServerConnection sconn;        /* Connection context */
71   SilcSKERekeyMaterial rekey;        /* Rekey material */
72   SilcHash hash;
73
74   /* Public key */
75   SilcPublicKey public_key;
76   unsigned char fingerprint[20];
77
78   long last_receive;            /* Time last received data */
79   long last_sent;               /* Time last sent data */
80
81   unsigned long created;        /* Time when entry was created */
82
83   SilcIDListStatus status;      /* Status mask of the entry */
84 };
85
86 /*
87    SILC Server entry object.
88
89    This entry holds information about servers in SILC network. However,
90    contents of this entry is highly dependent of what kind of server we are
91    (normal server or router server) and whether the entry is used as a local
92    list or a global list. These factors dictates the contents of this entry.
93
94    This entry is defined as follows:
95
96    Server type   List type      Contents
97    =======================================================================
98    server        local list     Server itself
99    server        global list    NULL
100    router        local list     All servers is the cell
101    router        global list    All servers in the SILC network
102
103    Following short description of the fields:
104
105    SilcIDListDataStruct data
106
107        Generic data structure to hold data common to all ID entries.
108
109    char *server_name
110
111        Logical name of the server. There is no limit of the length of the
112        server name. This is usually the same name as defined in DNS.
113
114    SilcUInt8 server_type
115
116        Type of the server. SILC_SERVER or SILC_ROUTER are the possible
117        choices for this.
118
119    SilcServerID *id
120
121        ID of the server. This includes all the relevant information about
122        the server SILC will ever need. These are also the informations
123        that is broadcasted between servers and routers in the SILC network.
124
125    char *server_info
126    char *motd
127
128        Server info (from INFO command) saved temporarily and motd (from
129        MOTD command) saved temporarily.
130
131    SilcServerEntry router
132
133        This is a pointer back to the server list. This is the router server
134        where this server is connected to. If this is the router itself and
135        it doesn't have a route this is NULL.
136
137    SilcCipher send_key
138    SilcCipher receive_key
139
140        Data sending and receiving keys.
141
142    void *connection
143
144        A pointer, usually, to the socket list for fast referencing to
145        the data used in connection with this server.  This may be anything
146        but as just said, this is usually pointer to the socket connection
147        list.
148
149 */
150 struct SilcServerEntryStruct {
151   /* Generic data structure. DO NOT add anything before this! */
152   SilcIDListDataStruct data;
153
154   char *server_name;
155   SilcUInt8 server_type;
156   SilcServerID *id;
157   char *server_info;
158   char *motd;
159
160   /* Pointer to the router */
161   SilcServerEntry router;
162
163   /* Connection data */
164   void *connection;
165
166   void *backup_proto;
167   unsigned int backup  : 1;     /* Set when executing backup protocol */
168 };
169
170 /*
171    SILC Channel Client entry structure.
172
173    This entry used only by the SilcChannelEntry object and it holds
174    information about current clients (ie. users) on channel. Following
175    short description of the fields:
176
177    SilcClientEntry client
178
179        Pointer to the client list. This is the client currently on channel.
180
181    SilcUInt32 mode
182
183        Client's current mode on the channel.
184
185    SilcChannelEntry channel
186
187        Back pointer back to channel. As this structure is also used by
188        SilcClientEntry we have this here for fast access to the channel when
189        used by SilcClientEntry.
190
191 */
192 typedef struct SilcChannelClientEntryStruct {
193   SilcClientEntry client;
194   SilcUInt32 mode;
195   SilcChannelEntry channel;
196 } *SilcChannelClientEntry;
197
198 /*
199    SILC Client entry object.
200
201    This entry holds information about connected clients ie. users in the SILC
202    network. The contents of this entrt is depended on whether we are normal
203    server or router server and whether the list is a local or global list.
204
205    This entry is defined as follows:
206
207    Server type   List type      Contents
208    =======================================================================
209    server        local list     All clients in server
210    server        global list    NULL
211    router        local list     All clients in cell
212    router        global list    All clients in SILC
213
214    Following short description of the fields:
215
216    SilcIDListDataStruct data
217
218        Generic data structure to hold data common to all ID entries.
219
220    unsigned char *nickname
221
222        The nickname of the client.  This is nickname in original format,
223        not casefolded or normalized.  However, it is checked to assure
224        that prohibited characters do not exist.  The casefolded version
225        is in the ID Cache.
226
227    char *servername
228
229        The name of the server where the client is from. MAy be NULL.
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    SilcUInt32 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    SilcUInt8 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    SilcHashTable channels;
293
294        All the channels this client has joined.  The context saved in the
295        hash table shares memory with the channel entrys `user_list' hash
296        table.
297
298    void *connection
299
300        A pointer, usually, to the socket list for fast referencing to
301        the data used in connection with this client.  This may be anything
302        but as just said, this is usually pointer to the socket connection
303        list.
304
305    SilcUInt16 resolve_cmd_ident
306
307        Command identifier for the entry when the entry's data.status
308        is SILC_IDLIST_STATUS_RESOLVING.  If this entry is asked to be
309        resolved when the status is set then the resolver may attach to
310        this command identifier and handle the process after the resolving
311        is over.
312
313    SilcClientEntry resuming_client
314
315    unsigned locally_detached : 1
316
317        Set to indicate that the client is locally owned but detached for
318        purposes of tracking user counts.
319
320 */
321 struct SilcClientEntryStruct {
322   /* Generic data structure. DO NOT add anything before this! */
323   SilcIDListDataStruct data;
324
325   unsigned char *nickname;
326   char *servername;
327   char *username;
328   char *userinfo;
329   SilcClientID *id;
330   SilcUInt32 mode;
331
332   long last_command;
333   SilcUInt8 fast_command;
334
335   /* Requested Attributes */
336   unsigned char *attrs;
337   SilcUInt16 attrs_len;
338
339   /* Pointer to the router */
340   SilcServerEntry router;
341
342   /* All channels this client has joined */
343   SilcHashTable channels;
344
345   /* Connection data */
346   void *connection;
347
348   /* Last time updated/accessed */
349   unsigned long updated;
350
351   /* data.status is RESOLVING and this includes the resolving command
352      reply identifier. */
353   SilcUInt16 resolve_cmd_ident;
354
355   /* we need this so nobody can resume more than once at the same time -
356    * server crashes, really odd behaviour, ... */
357   SilcClientEntry resuming_client;
358
359   /* Client is locally owned but detached and not counted in the user
360    * count for local clients (server->stat.my_clients). */
361   unsigned local_detached : 1;
362 };
363
364 /*
365    SILC Channel entry object.
366
367    This entry holds information about channels in SILC network. The contents
368    of this entry is depended on whether we are normal server or router server
369    and whether the list is a local or global list.
370
371    This entry is defined as follows:
372
373    Server type   List type      Contents
374    =======================================================================
375    server        local list     All channels in server
376    server        global list    NULL
377    router        local list     All channels in cell
378    router        global list    All channels in SILC
379
380    Following short description of the fields:
381
382    char *channel_name
383
384        Logical name of the channel.  This is the original format, not
385        the casefolded or normalized.  However, this is checked to assure
386        that prohibited characters do not exist.  The casefolded version
387        is in the ID Cache.
388
389    SilcUInt32 mode
390
391        Current mode of the channel.  See lib/silccore/silcchannel.h for
392        all modes.
393
394    SilcChannelID *id
395
396        ID of the channel. This includes all the information SILC will ever
397        need.
398
399    SilcBool global_users
400
401        Boolean value to tell whether there are users outside this server
402        on this channel. This is set to TRUE if router sends message to
403        the server that there are users outside your server on your
404        channel as well. This way server knows that messages needs to be
405        sent to the router for further routing. If this is a normal
406        server and this channel is not created on this server this field
407        is always TRUE. If this server is a router this field is ignored.
408
409    char *topic
410
411        Current topic of the channel.
412
413    char *cipher
414
415        Default cipher 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    char *hmac_name
419
420        Default hmac of the channel. If this is NULL then server picks
421        the cipher to be used. This can be set at SILC_COMMAND_JOIN.
422
423    SilcPublicKey founder_key
424
425        If the SILC_CMODE_FOUNDER_AUTH has been set then this will include
426        the founder's public key.  When the mode and this key is set the
427        channel is also permanent channel and cannot be destroyed.
428
429    SilcHashTable user_list
430
431        All users joined on this channel.  Note that the context saved to
432        this entry shares memory with the client entrys `channels' hash
433        table.
434
435    SilcServerEntry router
436
437        This is a pointer to the server list. This is the router server
438        whose cell this channel belongs to. This is used to route messages
439        to this channel.
440
441    SilcCipher send_key
442    SilcCipher receive_key
443
444        The key of the channel (the cipher actually).
445
446    unsigned char *key
447    SilcUInt32 key_len
448
449        Raw key data of the channel key.
450
451    unsigned char iv[SILC_CIPHER_MAX_IV_SIZE]
452
453        Current initial vector. Initial vector is received always along
454        with the channel packet. By default this is filled with NULL.
455
456    SilcHmac hmac;
457
458        HMAC of the channel.
459
460    SilcServerChannelRekey rekey
461
462        Channel key re-key context.
463
464 */
465 struct SilcChannelEntryStruct {
466   char *channel_name;
467   SilcUInt32 mode;
468   SilcChannelID *id;
469   char *topic;
470   char *cipher;
471   char *hmac_name;
472   SilcPublicKey founder_key;
473   SilcHashTable channel_pubkeys;
474
475   SilcUInt32 user_limit;
476   unsigned char *passphrase;
477   SilcHashTable invite_list;
478   SilcHashTable ban_list;
479
480   /* All users on this channel */
481   SilcHashTable user_list;
482   SilcUInt32 user_count;
483
484   /* Pointer to the router */
485   SilcServerEntry router;
486
487   /* Channel keys */
488   SilcCipher send_key;
489   SilcCipher receive_key;
490   unsigned char *key;
491   SilcUInt32 key_len;
492   SilcHmac hmac;
493
494   SilcServerChannelRekey rekey;
495   unsigned long created;
496   unsigned long updated;
497
498   /* Flags */
499   unsigned int global_users : 1;
500   unsigned int disabled : 1;
501   unsigned int users_resolved : 1;
502 };
503
504 /*
505    SILC ID List object.
506
507    As for remainder these lists are defined as follows:
508
509    Entry list (cache)  Server type   List type      Contents
510    =======================================================================
511    servers             server        local list     Server itself
512    servers             server        global list    NULL
513    servers             router        local list     All servers in cell
514    servers             router        global list    All servers in SILC
515
516    clients             server        local list     All clients in server
517    clients             server        global list    NULL
518    clients             router        local list     All clients in cell
519    clients             router        global list    All clients in SILC
520
521    channels            server        local list     All channels in server
522    channels            server        global list    NULL
523    channels            router        local list     All channels in cell
524    channels            router        global list    All channels in SILC
525
526    As seen on the list normal server never defines a global list. This is
527    because of normal server don't know anything about anything global data,
528    they get it from the router if and when they need it. Routers, on the
529    other hand, always define local and global lists because routers really
530    know all the relevant data in the SILC network.
531
532    This object is used as local and global list by the server/router.
533    Above table shows how this is defined on different conditions.
534
535    This object holds pointers to the ID cache system. Every ID cache entry
536    has a specific context pointer to allocated entry (server, client or
537    channel entry).
538
539 */
540 struct SilcIDListStruct {
541   SilcIDCache servers;
542   SilcIDCache clients;
543   SilcIDCache channels;
544 };
545
546 /*
547    ID Entry for Unknown connections.
548
549    This is used during authentication phases where we still don't know
550    what kind of connection remote connection is, hence, we will use this
551    structure instead until we know what type of connection remote end is.
552
553    This is not in any list. This is always individually allocated and
554    used as such.
555
556 */
557 typedef struct {
558   /* Generic data structure. DO NOT add anything before this! */
559   SilcIDListDataStruct data;
560   SilcAsyncOperation op;
561   SilcServerConfigRef cconfig;
562   SilcServerConfigRef sconfig;
563   SilcServerConfigRef rconfig;
564   SilcServer server;
565   const char *hostname;
566   const char *ip;
567   SilcUInt16 port;
568   SilcConnectionType conn_type;
569 } *SilcUnknownEntry;
570
571 /* Prototypes */
572 void silc_idlist_add_data(void *entry, SilcIDListData idata);
573 void silc_idlist_del_data(void *entry);
574 SILC_TASK_CALLBACK(silc_idlist_purge);
575 SilcServerEntry
576 silc_idlist_add_server(SilcIDList id_list,
577                        char *server_name, int server_type,
578                        SilcServerID *id, SilcServerEntry router,
579                        void *connection);
580 SilcServerEntry
581 silc_idlist_find_server_by_id(SilcIDList id_list, SilcServerID *id,
582                               SilcBool registered, SilcIDCacheEntry *ret_entry);
583 SilcServerEntry
584 silc_idlist_find_server_by_name(SilcIDList id_list, char *name,
585                                 SilcBool registered, SilcIDCacheEntry *ret_entry);
586 SilcServerEntry
587 silc_idlist_find_server_by_conn(SilcIDList id_list, char *hostname,
588                                 int port, SilcBool registered,
589                                 SilcIDCacheEntry *ret_entry);
590 SilcServerEntry
591 silc_idlist_replace_server_id(SilcIDList id_list, SilcServerID *old_id,
592                               SilcServerID *new_id);
593 int silc_idlist_del_server(SilcIDList id_list, SilcServerEntry entry);
594 void silc_idlist_server_destructor(SilcIDCache cache,
595                                    SilcIDCacheEntry entry,
596                                    void *dest_context,
597                                    void *app_context);
598 SilcClientEntry
599 silc_idlist_add_client(SilcIDList id_list, char *nickname, char *username,
600                        char *userinfo, SilcClientID *id,
601                        SilcServerEntry router, void *connection);
602 int silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry);
603 int silc_idlist_get_clients_by_nickname(SilcIDList id_list, char *nickname,
604                                         char *server,
605                                         SilcClientEntry **clients,
606                                         SilcUInt32 *clients_count);
607 int silc_idlist_get_clients_by_hash(SilcIDList id_list,
608                                     char *nickname, char *server,
609                                     SilcHash md5hash,
610                                     SilcClientEntry **clients,
611                                     SilcUInt32 *clients_count);
612 SilcClientEntry
613 silc_idlist_find_client_by_id(SilcIDList id_list, SilcClientID *id,
614                               SilcBool registered, SilcIDCacheEntry *ret_entry);
615 SilcClientEntry
616 silc_idlist_replace_client_id(SilcServer server,
617                               SilcIDList id_list, SilcClientID *old_id,
618                               SilcClientID *new_id, const char *nickname);
619 void silc_idlist_client_destructor(SilcIDCache cache,
620                                    SilcIDCacheEntry entry,
621                                    void *dest_context,
622                                    void *app_context);
623 SilcChannelEntry
624 silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
625                         SilcChannelID *id, SilcServerEntry router,
626                         SilcCipher send_key, SilcCipher receive_key,
627                         SilcHmac hmac);
628 void silc_idlist_channel_destructor(SilcIDCache cache,
629                                     SilcIDCacheEntry entry,
630                                     void *dest_context,
631                                     void *app_context);
632 int silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry);
633 SilcChannelEntry
634 silc_idlist_find_channel_by_name(SilcIDList id_list, char *name,
635                                  SilcIDCacheEntry *ret_entry);
636 SilcChannelEntry
637 silc_idlist_find_channel_by_id(SilcIDList id_list, SilcChannelID *id,
638                                SilcIDCacheEntry *ret_entry);
639 SilcChannelEntry
640 silc_idlist_replace_channel_id(SilcIDList id_list, SilcChannelID *old_id,
641                                SilcChannelID *new_id);
642 SilcChannelEntry *
643 silc_idlist_get_channels(SilcIDList id_list, SilcChannelID *channel_id,
644                          SilcUInt32 *channels_count);
645
646 #endif