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