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