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