updates.
[silc.git] / apps / silcd / idlist.h
1 /*
2
3   idlist.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20
21 #ifndef IDLIST_H
22 #define IDLIST_H
23
24 /* Forward declarations */
25 typedef struct SilcServerEntryStruct *SilcServerEntry;
26 typedef struct SilcClientEntryStruct *SilcClientEntry;
27 typedef struct SilcChannelEntryStruct *SilcChannelEntry;
28
29 /* Context for holding cache information to periodically purge
30    the cache. */
31 typedef struct {
32   SilcIDCache cache;
33   void *timeout_queue;
34 } *SilcIDListPurge;
35
36 /*
37    Generic ID list data structure.
38
39    This structure is included in all ID list entries and it includes data
40    pointers that are common to all ID entries.  This structure is always
41    defined to the first field in the ID entries and is used to explicitly
42    cast to this type without first explicitly casting to correct ID entry
43    type.  Hence, the ID list entry is casted to this type to get this data
44    from the ID entry (which is usually opaque pointer).
45
46    Note that some of the fields may be NULL.
47
48 */
49 typedef struct {
50   /* Send and receive symmetric keys */
51   SilcCipher send_key;
52   SilcCipher receive_key;
53
54   /* Hash selected in the SKE protocol, NULL if not needed at all */
55   SilcHash hash;
56
57   /* HMAC and raw key data */
58   SilcHmac hmac;
59   unsigned char *hmac_key;
60   unsigned int hmac_key_len;
61
62   /* PKCS and public key */
63   SilcPKCS pkcs;
64   SilcPublicKey public_key;
65
66   long last_receive;         /* Time last received data */
67   long last_sent;            /* Time last sent data */
68   unsigned char registered;  /* Boolean whether connection is registered */
69 } *SilcIDListData, SilcIDListDataStruct;
70
71 /* 
72    SILC Server entry object.
73
74    This entry holds information about servers in SILC network. However, 
75    contents of this entry is highly dependent of what kind of server we are 
76    (normal server or router server) and whether the entry is used as a local 
77    list or a global list. These factors dictates the contents of this entry.
78
79    This entry is defined as follows:
80
81    Server type   List type      Contents
82    =======================================================================
83    server        local list     Server itself
84    server        global list    NULL
85    router        local list     All servers is the cell
86    router        global list    All servers in the SILC network
87
88    Following short description of the fields:
89
90    SilcIDListDataStruct data
91
92        Generic data structure to hold data common to all ID entries.
93
94    char *server_name
95
96        Logical name of the server. There is no limit of the length of the
97        server name. This is usually the same name as defined in DNS.
98
99    int server_type
100
101        Type of the server. SILC_SERVER or SILC_ROUTER are the possible
102        choices for this.
103
104    SilcServerID *id
105
106        ID of the server. This includes all the relevant information about
107        the server SILC will ever need. These are also the informations
108        that is broadcasted between servers and routers in the SILC network.
109
110    char *server_info
111    char *motd
112
113        Server info (from INFO command) saved temporarily and motd (from
114        MOTD command) saved temporarily.
115
116    SilcServerEntry router
117
118        This is a pointer back to the server list. This is the router server 
119        where this server is connected to. If this is the router itself and 
120        it doesn't have a route this is NULL.
121
122    SilcCipher send_key
123    SilcCipher receive_key
124
125        Data sending and receiving keys.
126
127    void *connection
128
129        A pointer, usually, to the socket list for fast referencing to
130        the data used in connection with this server.  This may be anything
131        but as just said, this is usually pointer to the socket connection
132        list.
133    
134 */
135 struct SilcServerEntryStruct {
136   /* Generic data structure. DO NOT add anything before this! */
137   SilcIDListDataStruct data;
138
139   char *server_name;
140   int server_type;
141   SilcServerID *id;
142   char *server_info;
143   char *motd;
144
145   /* Pointer to the router */
146   SilcServerEntry router;
147
148   /* Connection data */
149   void *connection;
150 };
151
152 /* 
153    SILC Channel Client entry structure.
154
155    This entry used only by the SilcChannelEntry object and it holds
156    information about current clients (ie. users) on channel. Following
157    short description  of the fields:
158
159    SilcClientEntry client
160
161        Pointer to the client list. This is the client currently on channel.
162
163    unsigned int mode
164
165        Client's current mode on the channel.
166
167    SilcChannelEntry channel
168
169        Back pointer back to channel. As this structure is also used by
170        SilcClientEntry we have this here for fast access to the channel when
171        used by SilcClientEntry.
172
173   struct SilcChannelClientEntryStruct *client_list
174   struct SilcChannelClientEntryStruct *channel_list
175
176        List member pointers. This structure is used by channel entry and
177        client entry thus we must have separate list member pointers for
178        them since we are using same entry for both lists (the entry is not
179        duplicated). SilcList requires this.
180
181 */
182 typedef struct SilcChannelClientEntryStruct {
183   SilcClientEntry client;
184   unsigned int mode;
185   SilcChannelEntry channel;
186   struct SilcChannelClientEntryStruct *client_list;
187   struct SilcChannelClientEntryStruct *channel_list;
188 } *SilcChannelClientEntry;
189
190 /* 
191    SILC Client entry object.
192
193    This entry holds information about connected clients ie. users in the SILC
194    network. The contents of this entrt is depended on whether we are normal 
195    server or router server and whether the list is a local or global list.
196
197    This entry is defined as follows:
198
199    Server type   List type      Contents
200    =======================================================================
201    server        local list     All clients in server
202    server        global list    NULL
203    router        local list     All clients in cell
204    router        global list    All clients in SILC
205
206    Following short description of the fields:
207
208    SilcIDListDataStruct data
209
210        Generic data structure to hold data common to all ID entries.
211
212    char username
213
214        Client's usename. This is defined in the following manner:
215
216        Server type   List type      Contents
217        ====================================================
218        server        local list     User's name
219        router        local list     NULL
220        router        global list    NULL
221
222        Router doesn't hold this information since it is not vital data 
223        for the router. If this information is needed by the client it is
224        fetched when it is needed.
225
226    char userinfo
227
228        Information about user. This is free information and can be virtually
229        anything. This is defined in following manner:
230        
231        Server type   List type      Contents
232        ====================================================
233        server        local list     User's information
234        router        local list     NULL
235        router        global list    NULL
236
237        Router doesn't hold this information since it is not vital data 
238        for the router. If this information is needed by the client it is
239        fetched when it is needed.
240
241    SilcClientID *id
242
243        ID of the client. This includes all the information SILC will ever
244        need. Notice that no nickname of the user is saved anywhere. This is
245        beacuse of SilcClientID includes 88 bit hash value of the user's 
246        nickname which can be used to track down specific user by their 
247        nickname. Nickname is not relevant information that would need to be 
248        saved as plain.
249
250    int mode
251
252        Client's mode.  Client maybe for example server operator or
253        router operator (SILC operator).
254
255    long last_command
256
257        Time of last time client executed command. We are strict and will
258        not allow any command to be exeucted more than once in about
259        2 seconds. This is result of normal time().
260
261    char fast_command
262
263        Counter to check command bursts.  By default, up to 5 commands
264        are allowed before limiting the execution.  See command flags
265        for more detail.
266
267    SilcServerEntry router
268
269        This is a pointer to the server list. This is the router server whose 
270        cell this client is coming from. This is used to route messages to 
271        this client.
272
273    SilcList channels
274
275        List of channels this client has joined.
276
277    void *connection
278
279        A pointer, usually, to the socket list for fast referencing to
280        the data used in connection with this client.  This may be anything
281        but as just said, this is usually pointer to the socket connection
282        list.
283
284 */
285 struct SilcClientEntryStruct {
286   /* Generic data structure. DO NOT add anything before this! */
287   SilcIDListDataStruct data;
288
289   unsigned char *nickname;
290   char *username;
291   char *userinfo;
292   SilcClientID *id;
293   int mode;
294
295   long last_command;
296   char fast_command;
297
298   /* Pointer to the router */
299   SilcServerEntry router;
300
301   /* List of channels client has joined to */
302   SilcList channels;
303
304   /* Connection data */
305   void *connection;
306 };
307
308 /* 
309    SILC Channel entry object.
310
311    This entry holds information about channels in SILC network. The contents 
312    of this entry is depended on whether we are normal server or router server 
313    and whether the list is a local or global list.
314
315    This entry is defined as follows:
316
317    Server type   List type      Contents
318    =======================================================================
319    server        local list     All channels in server
320    server        global list    NULL
321    router        local list     All channels in cell
322    router        global list    All channels in SILC
323
324    Following short description of the fields:
325
326    char *channel_name
327
328        Logical name of the channel.
329
330    unsigned int mode
331
332        Current mode of the channel.  See lib/silccore/silcchannel.h for
333        all modes.
334
335    SilcChannelID *id
336
337        ID of the channel. This includes all the information SILC will ever
338        need.
339
340    int global_users
341  
342        Boolean value to tell whether there are users outside this server
343        on this channel. This is set to TRUE if router sends message to
344        the server that there are users outside your server on your
345        channel as well. This way server knows that messages needs to be
346        sent to the router for further routing. If this is a normal 
347        server and this channel is not created on this server this field
348        is always TRUE. If this server is a router this field is ignored.
349
350    char *topic
351
352        Current topic of the channel.
353
354    char *cipher
355
356        Default cipher of the channel. If this is NULL then server picks
357        the cipher to be used. This can be set at SILC_COMMAND_JOIN.
358
359    char *hmac_name
360
361        Default hmac of the channel. If this is NULL then server picks
362        the cipher to be used. This can be set at SILC_COMMAND_JOIN.
363
364    SilcPublicKey founder_key
365    SilcAuthMethod founder_method
366    unsigned char *founder_passwd
367    unsigned int founder_passwd_len
368
369        If the SILC_CMODE_FOUNDER_AUTH has been set then these will include
370        the founder's public key, authentication method and the password
371        if the method is SILC_AUTH_PASSWORD.  If it is SILC_AUTH_PUBLIC_KEY
372        then the `founder_passwd' is NULL.
373
374    SilcServerEntry router
375
376        This is a pointer to the server list. This is the router server 
377        whose cell this channel belongs to. This is used to route messages 
378        to this channel.
379
380    SilcCipher channel_key
381
382        The key of the channel (the cipher actually).
383
384    unsigned char *key
385    unsigned int key_len
386
387        Raw key data of the channel key.
388
389    unsigned char iv[SILC_CIPHER_MAX_IV_SIZE]
390
391        Current initial vector. Initial vector is received always along
392        with the channel packet. By default this is filled with NULL.
393
394    SilcHmac hmac;
395
396        HMAC of the channel.
397
398 */
399 struct SilcChannelEntryStruct {
400   char *channel_name;
401   unsigned int mode;
402   SilcChannelID *id;
403   int global_users;
404   char *topic;
405   char *cipher;
406   char *hmac_name;
407
408   SilcPublicKey founder_key;
409   SilcAuthMethod founder_method;
410   unsigned char *founder_passwd;
411   unsigned int founder_passwd_len;
412
413   unsigned int user_limit;
414   unsigned char *passphrase;
415   char *invite_list;
416   char *ban_list;
417
418   /* List of users on channel */
419   SilcList user_list;
420
421   /* Pointer to the router */
422   SilcServerEntry router;
423
424   /* Channel keys */
425   SilcCipher channel_key;
426   unsigned char *key;
427   unsigned int key_len;
428   unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
429   SilcHmac hmac;
430 };
431
432 /* 
433    SILC ID List object.
434
435    As for remainder these lists are defined as follows:
436
437    Entry list (cache)  Server type   List type      Contents
438    =======================================================================
439    servers             server        local list     Server itself
440    servers             server        global list    NULL
441    servers             router        local list     All servers in cell
442    servers             router        global list    All servers in SILC
443
444    clients             server        local list     All clients in server
445    clients             server        global list    NULL
446    clients             router        local list     All clients in cell
447    clients             router        global list    All clients in SILC
448
449    channels            server        local list     All channels in server
450    channels            server        global list    NULL
451    channels            router        local list     All channels in cell
452    channels            router        global list    All channels in SILC
453
454    As seen on the list normal server never defines a global list. This is
455    because of normal server don't know anything about anything global data,
456    they get it from the router if and when they need it. Routers, on the
457    other hand, always define local and global lists because routers really
458    know all the relevant data in the SILC network.
459
460    This object is used as local and global list by the server/router.
461    Above table shows how this is defined on different conditions.
462
463    This object holds pointers to the ID cache system. Every ID cache entry
464    has a specific context pointer to allocated entry (server, client or
465    channel entry).
466
467 */
468 typedef struct SilcIDListStruct {
469   SilcIDCache servers;
470   SilcIDCache clients;
471   SilcIDCache channels;
472 } *SilcIDList;
473
474 /*
475    ID Entry for Unknown connections.
476
477    This is used during authentication phases where we still don't know 
478    what kind of connection remote connection is, hence, we will use this
479    structure instead until we know what type of connection remote end is.
480
481    This is not in any list. This is always individually allocated and
482    used as such.
483
484 */
485 typedef struct {
486   /* Generic data structure. DO NOT add anything before this! */
487   SilcIDListDataStruct data;
488 } *SilcUnknownEntry;
489
490 /* Prototypes */
491 void silc_idlist_add_data(void *entry, SilcIDListData idata);
492 void silc_idlist_del_data(void *entry);
493 SILC_TASK_CALLBACK_GLOBAL(silc_idlist_purge);
494 SilcServerEntry 
495 silc_idlist_add_server(SilcIDList id_list, 
496                        char *server_name, int server_type,
497                        SilcServerID *id, SilcServerEntry router,
498                        void *connection);
499 SilcServerEntry
500 silc_idlist_find_server_by_id(SilcIDList id_list, SilcServerID *id,
501                               SilcIDCacheEntry *ret_entry);
502 SilcServerEntry
503 silc_idlist_find_server_by_name(SilcIDList id_list, char *name,
504                                 SilcIDCacheEntry *ret_entry);
505 SilcServerEntry
506 silc_idlist_find_server_by_conn(SilcIDList id_list, char *hostname,
507                                 int port, SilcIDCacheEntry *ret_entry);
508 SilcServerEntry
509 silc_idlist_replace_server_id(SilcIDList id_list, SilcServerID *old_id,
510                               SilcServerID *new_id);
511 void silc_idlist_del_server(SilcIDList id_list, SilcServerEntry entry);
512 SilcClientEntry
513 silc_idlist_add_client(SilcIDList id_list, unsigned char *nickname, 
514                        unsigned int nickname_len, char *username, 
515                        char *userinfo, SilcClientID *id, 
516                        SilcServerEntry router, void *connection);
517 int silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry);
518 SilcClientEntry *
519 silc_idlist_get_clients_by_nickname(SilcIDList id_list, char *nickname,
520                                     char *server, unsigned int *clients_count);
521 SilcClientEntry *
522 silc_idlist_get_clients_by_hash(SilcIDList id_list, char *nickname,
523                                 SilcHash md5hash,
524                                 unsigned int *clients_count);
525 SilcClientEntry
526 silc_idlist_find_client_by_hash(SilcIDList id_list, char *nickname,
527                                 SilcHash md5hash, SilcIDCacheEntry *ret_entry);
528 SilcClientEntry
529 silc_idlist_find_client_by_id(SilcIDList id_list, SilcClientID *id,
530                               SilcIDCacheEntry *ret_entry);
531 SilcClientEntry
532 silc_idlist_replace_client_id(SilcIDList id_list, SilcClientID *old_id,
533                               SilcClientID *new_id);
534 void silc_idlist_client_destructor(SilcIDCache cache,
535                                    SilcIDCacheEntry entry);
536 SilcChannelEntry
537 silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
538                         SilcChannelID *id, SilcServerEntry router,
539                         SilcCipher channel_key, SilcHmac hmac);
540 int silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry);
541 SilcChannelEntry
542 silc_idlist_find_channel_by_name(SilcIDList id_list, char *name,
543                                  SilcIDCacheEntry *ret_entry);
544 SilcChannelEntry
545 silc_idlist_find_channel_by_id(SilcIDList id_list, SilcChannelID *id,
546                                SilcIDCacheEntry *ret_entry);
547 SilcChannelEntry
548 silc_idlist_replace_channel_id(SilcIDList id_list, SilcChannelID *old_id,
549                                SilcChannelID *new_id);
550 SilcChannelEntry *
551 silc_idlist_get_channels(SilcIDList id_list, SilcChannelID *channel_id,
552                          unsigned int *channels_count);
553
554 #endif