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