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