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