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