Major rewrite of ID Cache system. Support added for the new
[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    SilcServerEntry router
65
66        This is a pointer back to the server list. This is the router server 
67        where this server is connected to. If this is the router itself and 
68        it doesn't have a route this is NULL.
69
70    SilcCipher send_key
71    
72    SilcCipher receive_key
73
74    void *connection
75
76        A pointer, usually, to the socket list for fast referencing to
77        the data used in connection with this server.  This may be anything
78        but as just said, this is usually pointer to the socket connection
79        list.
80    
81 */
82 struct SilcServerEntryStruct {
83   char *server_name;
84   int server_type;
85   SilcServerID *id;
86
87   /* TRUE when server is registered to server */
88   int registered;
89
90   /* Pointer to the router */
91   SilcServerEntry router;
92
93   /* Keys */
94   SilcCipher send_key;
95   SilcCipher receive_key;
96   SilcPKCS pkcs;
97   SilcPublicKey public_key;
98   SilcHmac hmac;
99   unsigned char *hmac_key;
100   unsigned int hmac_key_len;
101
102   /* Connection data */
103   void *connection;
104 };
105
106 /* 
107    SILC Client entry object.
108
109    This entry holds information about connected clients ie. users in the SILC
110    network. The contents of this entrt is depended on whether we are normal 
111    server or router server and whether the list is a local or global list.
112
113    This entry is defined as follows:
114
115    Server type   List type      Contents
116    =======================================================================
117    server        local list     All clients in server
118    server        global list    NULL
119    router        local list     All clients in cell
120    router        global list    All clients in SILC
121
122    Following short description of the fields:
123
124    char username
125
126        Client's (meaning user's) real name. This is defined in following 
127        manner:
128
129        Server type   List type      Contents
130        ====================================================
131        server        local list     User's name
132        router        local list     NULL
133        router        global list    NULL
134
135        Router doesn't hold this information since it is not vital data 
136        for the router. If this information is needed by the client it is
137        fetched when it is needed.
138
139    char userinfo
140
141        Information about user. This is free information and can be virtually
142        anything. This is defined in following manner:
143        
144        Server type   List type      Contents
145        ====================================================
146        server        local list     User's information
147        router        local list     NULL
148        router        global list    NULL
149
150        Router doesn't hold this information since it is not vital data 
151        for the router. If this information is needed by the client it is
152        fetched when it is needed.
153
154    SilcClientID *id
155
156        ID of the client. This includes all the information SILC will ever
157        need. Notice that no nickname of the user is saved anywhere. This is
158        beacuse of SilcClientID includes 88 bit hash value of the user's 
159        nickname which can be used to track down specific user by their 
160        nickname. Nickname is not relevant information that would need to be 
161        saved as plain.
162
163    int mode
164
165        Client's mode.  Client maybe for example server operator or
166        router operator (SILC operator).
167
168    SilcServerEntry router
169
170        This is a pointer to the server list. This is the router server whose 
171        cell this client is coming from. This is used to route messages to 
172        this client.
173
174    SilcCipher session_key
175
176        The actual session key established by key exchange protcol between
177        connecting parties. This is used for both encryption and decryption.
178
179    SilcPKCS pkcs
180
181        PKCS of the client. This maybe NULL.
182
183    SilcHmac hmac
184    unsigned char *hmac_key
185    unsigned int hmac_key_len
186
187        MAC key used to compute MAC's for packets. 
188
189    void *connection
190
191        A pointer, usually, to the socket list for fast referencing to
192        the data used in connection with this client.  This may be anything
193        but as just said, this is usually pointer to the socket connection
194        list.
195
196 */
197 struct SilcClientEntryStruct {
198   char *nickname;
199   char *username;
200   char *userinfo;
201   SilcClientID *id;
202   int mode;
203
204   /* TRUE when client is registered to server */
205   int registered;
206
207   /* Pointer to the router */
208   SilcServerEntry router;
209
210   /* Pointers to channels this client has joined */
211   SilcChannelEntry *channel;
212   unsigned int channel_count;
213
214   /* Keys */
215   SilcCipher send_key;
216   SilcCipher receive_key;
217   SilcPKCS pkcs;
218   SilcHmac hmac;
219   SilcPublicKey public_key;
220   unsigned char *hmac_key;
221   unsigned int hmac_key_len;
222
223   /* Connection data */
224   void *connection;
225 };
226
227 /* 
228    SILC Channel Client entry structure.
229
230    This entry used only by the SilcChannelEntry object and it holds
231    information about current clients (ie. users) on channel. Following
232    short description  of the fields:
233
234    SilcClientEntry client
235
236        Pointer to the client list. This is the client currently on channel.
237
238    int mode
239
240        Client's current mode on the channel.
241
242 */
243 typedef struct SilcChannelClientEntryStruct {
244   SilcClientEntry client;
245   int mode;
246 } *SilcChannelClientEntry;
247
248 /* 
249    SILC Channel entry object.
250
251    This entry holds information about channels in SILC network. The contents 
252    of this entry is depended on whether we are normal server or router server 
253    and whether the list is a local or global list.
254
255    This entry is defined as follows:
256
257    Server type   List type      Contents
258    =======================================================================
259    server        local list     All channels in server
260    server        global list    NULL
261    router        local list     All channels in cell
262    router        global list    All channels in SILC
263
264    Following short description of the fields:
265
266    char *channel_name
267
268        Logical name of the channel.
269
270    int mode
271
272        Current mode of the channel.
273
274    SilcChannelID *id
275
276        ID of the channel. This includes all the information SILC will ever
277        need.
278
279    int global_users
280  
281        Boolean value to tell whether there are users outside this server
282        on this channel. This is set to TRUE if router sends message to
283        the server that there are users outside your server on your
284        channel as well. This way server knows that messages needs to be
285        sent to the router for further routing. If this is a normal 
286        server and this channel is not created on this server this field
287        is always TRUE. If this server is a router this field is ignored.
288
289    char *topic
290
291        Current topic of the channel.
292
293    SilcServerEntry router
294
295        This is a pointer to the server list. This is the router server 
296        whose cell this channel belongs to. This is used to route messages 
297        to this channel.
298
299    SilcCipher channel_key
300
301        The key of the channel (the cipher actually).
302
303    unsigned char *key
304    unsigned int key_len
305
306        Raw key data of the channel key.
307
308    unsigned char iv[SILC_CIPHER_MAX_IV_SIZE]
309
310        Current initial vector. Initial vector is received always along
311        with the channel packet. By default this is filled with NULL.
312
313 */
314 struct SilcChannelEntryStruct {
315   char *channel_name;
316   int mode;
317   SilcChannelID *id;
318   int global_users;
319   char *topic;
320
321   /* List of users on channel */
322   SilcChannelClientEntry user_list;
323   unsigned int user_list_count;
324
325   /* Pointer to the router */
326   SilcServerEntry router;
327
328   /* Channel keys */
329   SilcCipher channel_key;
330   unsigned char *key;
331   unsigned int key_len;
332   unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
333 };
334
335 /* 
336    SILC ID List object.
337
338    As for remainder these lists are defined as follows:
339
340    Entry list (cache)  Server type   List type      Contents
341    =======================================================================
342    servers             server        local list     Server itself
343    servers             server        global list    NULL
344    servers             router        local list     All servers in cell
345    servers             router        global list    All servers in SILC
346
347    clients             server        local list     All clients in server
348    clients             server        global list    NULL
349    clients             router        local list     All clients in cell
350    clients             router        global list    All clients in SILC
351
352    channels            server        local list     All channels in server
353    channels            server        global list    NULL
354    channels            router        local list     All channels in cell
355    channels            router        global list    All channels in SILC
356
357    As seen on the list normal server never defines a global list. This is
358    because of normal server don't know anything about anything global data,
359    they get it from the router if and when they need it. Routers, on the
360    other hand, always define local and global lists because routers really
361    know all the relevant data in the SILC network.
362
363    This object is used as local and global list by the server/router.
364    Above table shows how this is defined on different conditions.
365
366    This object holds pointers to the ID cache system. Every ID cache entry
367    has a specific context pointer to allocated entry (server, client or
368    channel entry).
369
370 */
371 typedef struct SilcIDListStruct {
372   SilcIDCache servers;
373   SilcIDCache clients;
374   SilcIDCache channels;
375 } *SilcIDList;
376
377 /*
378    Temporary ID Entry object.
379
380    This is used during authentication phases where we still don't know 
381    what kind of connection remote connection is, hence, we will use this
382    structure instead until we know what type of connection remote end is.
383
384    This is not in any list. This is always individually allocated and
385    used as such.
386
387 */
388 typedef struct {
389   SilcCipher send_key;
390   SilcCipher receive_key;
391   SilcPKCS pkcs;
392   SilcPublicKey public_key;
393
394   SilcHmac hmac;
395   unsigned char *hmac_key;
396   unsigned int hmac_key_len;
397
398   /* SilcComp comp */
399 } *SilcUnknownEntry;
400
401 /* Prototypes */
402 SilcServerEntry 
403 silc_idlist_add_server(SilcIDList id_list, 
404                        char *server_name, int server_type,
405                        SilcServerID *id, SilcServerEntry router,
406                        SilcCipher send_key, SilcCipher receive_key,
407                        SilcPKCS pkcs, SilcHmac hmac, 
408                        SilcPublicKey public_key, void *connection);
409 SilcClientEntry
410 silc_idlist_add_client(SilcIDList id_list, char *nickname, char *username,
411                        char *userinfo, SilcClientID *id, 
412                        SilcServerEntry router,
413                        SilcCipher send_key, SilcCipher receive_key,
414                        SilcPKCS pkcs, SilcHmac hmac, 
415                        SilcPublicKey public_key, void *connection);
416 void silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry);
417 SilcClientEntry
418 silc_idlist_find_client_by_nickname(SilcIDList id_list, char *nickname,
419                                     char *server);
420 SilcClientEntry
421 silc_idlist_find_client_by_hash(SilcIDList id_list, unsigned char *hash,
422                                 SilcHash md5hash);
423 SilcClientEntry
424 silc_idlist_find_client_by_id(SilcIDList id_list, SilcClientID *id);
425 SilcChannelEntry
426 silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
427                         SilcChannelID *id, SilcServerEntry router,
428                         SilcCipher channel_key);
429 void silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry);
430 SilcChannelEntry
431 silc_idlist_find_channel_by_name(SilcIDList id_list, char *name);
432 SilcChannelEntry
433 silc_idlist_find_channel_by_id(SilcIDList id_list, SilcChannelID *id);
434
435 #endif