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