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