Added SILC Server library.
[silc.git] / lib / silcserver / server_params.c
1 /*
2
3   server_params.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2005 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; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #include "silc.h"
21 #include "silcserver.h"
22 #include "server_internal.h"
23
24 /************************** Types and definitions ***************************/
25
26 /* Default values */
27 #define SILC_SERVER_PARAM_RETRY_COUNT        7    /* Max retry count */
28 #define SILC_SERVER_PARAM_RETRY_MULTIPLIER   2    /* Interval growth */
29 #define SILC_SERVER_PARAM_RETRY_RANDOMIZER   2    /* timeout += rnd % 2 */
30 #define SILC_SERVER_PARAM_RETRY_INTERVAL_MIN 10   /* Min retry timeout */
31 #define SILC_SERVER_PARAM_RETRY_INTERVAL_MAX 600  /* Max generated timeout */
32 #define SILC_SERVER_PARAM_REKEY              3600 /* Session rekey interval */
33 #define SILC_SERVER_PARAM_KEEPALIVE          300  /* Heartbeat interval */
34 #define SILC_SERVER_PARAM_MAX_CONNS          1000 /* Max connections */
35 #define SILC_SERVER_PARAM_MAX_CONNS_SINGLE   1000 /* Max conns per host */
36 #define SILC_SERVER_PARAM_CONN_PER_THREAD    30   /* Connections per thread */
37 #define SILC_SERVER_PARAM_CHANNEL_REKEY      3600 /* Channel rekey interval */
38 #define SILC_SERVER_PARAM_SKE_TIMEOUT        60   /* SKE timeout */
39 #define SILC_SERVER_PARAM_CONNAUTH_TIMEOUT   60   /* CONN_AUTH timeout */
40 #define SILC_SERVER_PARAM_QOS_RATE_LIMIT     10   /* QoS rate limit */
41 #define SILC_SERVER_PARAM_QOS_BYTES_LIMIT    2048 /* QoS bytes limit */
42 #define SILC_SERVER_PARAM_QOS_LIMIT_SEC      0    /* QoS limit sec */
43 #define SILC_SERVER_PARAM_QOS_LIMIT_USEC     500000 /* QoS limit usec */
44 #define SILC_SERVER_PARAM_CH_JOIN_LIMIT      50   /* Join limit */
45
46
47 /************************ Static utility functions **************************/
48
49 /* Sets connection parameter defaults */
50
51 static void
52 silc_server_param_set_param_defaults(SilcServerParamConnParams params,
53                                      SilcServerParamConnParams defaults)
54 {
55 #define SET_PARAM_DEFAULT(p, d) params->p =                             \
56   (params->p ? params->p : (defaults && defaults->p ? defaults->p : d))
57
58   SET_PARAM_DEFAULT(connections_max, SILC_SERVER_PARAM_MAX_CONNS);
59   SET_PARAM_DEFAULT(connections_max_per_host,
60                     SILC_SERVER_PARAM_MAX_CONNS_SINGLE);
61   SET_PARAM_DEFAULT(keepalive_secs, SILC_SERVER_PARAM_KEEPALIVE);
62   SET_PARAM_DEFAULT(reconnect_count, SILC_SERVER_PARAM_RETRY_COUNT);
63   SET_PARAM_DEFAULT(reconnect_interval, SILC_SERVER_PARAM_RETRY_INTERVAL_MIN);
64   SET_PARAM_DEFAULT(reconnect_interval_max,
65                     SILC_SERVER_PARAM_RETRY_INTERVAL_MAX);
66   SET_PARAM_DEFAULT(key_exchange_rekey, SILC_SERVER_PARAM_REKEY);
67   SET_PARAM_DEFAULT(qos_rate_limit, SILC_SERVER_PARAM_QOS_RATE_LIMIT);
68   SET_PARAM_DEFAULT(qos_bytes_limit, SILC_SERVER_PARAM_QOS_BYTES_LIMIT);
69   SET_PARAM_DEFAULT(qos_limit_sec, SILC_SERVER_PARAM_QOS_LIMIT_SEC);
70   SET_PARAM_DEFAULT(qos_limit_usec, SILC_SERVER_PARAM_QOS_LIMIT_USEC);
71   SET_PARAM_DEFAULT(chlimit, SILC_SERVER_PARAM_CH_JOIN_LIMIT);
72
73 #undef SET_PARAM_DEFAULT
74 }
75
76
77 /***************************** Retrieval API ********************************/
78
79 /* Returns the denied connection configuration entry by host. */
80
81 SilcServerParamDeny
82 silc_server_params_find_denied(SilcServer server, char *ip, char *host)
83 {
84   SilcServerParams params = server->params;
85   SilcServerParamDeny deny;
86
87   if (ip) {
88     silc_list_start(params->denied);
89     while ((deny = silc_list_get(params->denied)) != SILC_LIST_END) {
90       if (deny->host && !silc_string_compare(deny->host, ip))
91         continue;
92       return deny;
93     }
94   }
95
96   if (host) {
97     silc_list_start(params->denied);
98     while ((deny = silc_list_get(params->denied)) != SILC_LIST_END) {
99       if (deny->host && !silc_string_compare(deny->host, host))
100         continue;
101       return deny;
102     }
103   }
104
105   return NULL;
106 }
107
108 /* Returns client connection information from configuration file by host
109    (name or ip) */
110
111 SilcServerParamClient
112 silc_server_params_find_client(SilcServer server, char *ip, char *host)
113 {
114   SilcServerParams params = server->params;
115   SilcServerParamClient client;
116
117   if (ip) {
118     silc_list_start(params->clients);
119     while ((client = silc_list_get(params->clients)) != SILC_LIST_END) {
120       if (client->host && !silc_string_compare(client->host, ip))
121         continue;
122       return client;
123     }
124   }
125
126   if (host) {
127     silc_list_start(params->clients);
128     while ((client = silc_list_get(params->clients)) != SILC_LIST_END) {
129       if (client->host && !silc_string_compare(client->host, host))
130         continue;
131       return client;
132     }
133   }
134
135   return NULL;
136 }
137
138 /* Returns server connection info from server configuartion by host
139    (name or ip). */
140
141 SilcServerParamServer
142 silc_server_params_find_server(SilcServer server, char *ip, char *host)
143 {
144   SilcServerParams params = server->params;
145   SilcServerParamServer serv;
146
147   if (ip) {
148     silc_list_start(params->servers);
149     while ((serv = silc_list_get(params->servers)) != SILC_LIST_END) {
150       if (serv->host && !silc_string_compare(serv->host, ip))
151         continue;
152       return serv;
153     }
154   }
155
156   if (host) {
157     silc_list_start(params->servers);
158     while ((serv = silc_list_get(params->servers)) != SILC_LIST_END) {
159       if (serv->host && !silc_string_compare(serv->host, host))
160         continue;
161       return serv;
162     }
163   }
164
165   return NULL;
166 }
167
168 /* Returns router connection info from server configuration by
169    host (name or ip). */
170
171 SilcServerParamRouter
172 silc_server_params_find_router(SilcServer server, char *ip,
173                                char *host, int port)
174 {
175   SilcServerParams params = server->params;
176   SilcServerParamRouter serv;
177
178   if (ip) {
179     silc_list_start(params->routers);
180     while ((serv = silc_list_get(params->routers)) != SILC_LIST_END) {
181       if (serv->host && !silc_string_compare(serv->host, ip))
182         continue;
183       if (port && serv->port && serv->port != port)
184         continue;
185       return serv;
186     }
187   }
188
189   if (host) {
190     silc_list_start(params->routers);
191     while ((serv = silc_list_get(params->routers)) != SILC_LIST_END) {
192       if (serv->host && !silc_string_compare(serv->host, host))
193         continue;
194       if (port && serv->port && serv->port != port)
195         continue;
196       return serv;
197     }
198   }
199
200   return NULL;
201 }
202
203 /* Find backup router connection by host (name or ip) */
204
205 SilcServerParamRouter
206 silc_server_params_find_backup(SilcServer server, char *host, char *ip)
207 {
208   SilcServerParams params = server->params;
209   SilcServerParamRouter serv;
210
211   if (ip) {
212     silc_list_start(params->routers);
213     while ((serv = silc_list_get(params->routers)) != SILC_LIST_END) {
214       if (!serv->backup_router)
215         continue;
216       if (!silc_string_compare(serv->host, ip))
217         continue;
218       return serv;
219     }
220   }
221
222   if (host) {
223     silc_list_start(params->routers);
224     while ((serv = silc_list_get(params->routers)) != SILC_LIST_END) {
225       if (!serv->backup_router)
226         continue;
227       if (!silc_string_compare(serv->host, host))
228         continue;
229       return serv;
230     }
231   }
232
233   return NULL;
234 }
235
236
237 /******************************* Public API *********************************/
238
239 /* Allocate parameters context */
240
241 SilcServerParams silc_server_params_alloc(void)
242 {
243   SilcServerParams params;
244
245   params = silc_calloc(1, sizeof(*params));
246   if (!params)
247     return NULL;
248
249   /* Init lists */
250   silc_list_init(params->cipher, struct SilcServerParamCipherStruct, next);
251   silc_list_init(params->hash, struct SilcServerParamHashStruct, next);
252   silc_list_init(params->hmac, struct SilcServerParamHmacStruct, next);
253   silc_list_init(params->pkcs, struct SilcServerParamPkcsStruct, next);
254   silc_list_init(params->clients, struct SilcServerParamClientStruct, next);
255   silc_list_init(params->servers, struct SilcServerParamServerStruct, next);
256   silc_list_init(params->routers, struct SilcServerParamRouterStruct, next);
257   silc_list_init(params->conn_params, SilcServerParamConnParamsStruct, next);
258   silc_list_init(params->denied, struct SilcServerParamDenyStruct, next);
259   silc_list_init(params->admins, struct SilcServerParamAdminStruct, next);
260
261   /* Set default values */
262   silc_server_param_set_param_defaults(&params->param, NULL);
263   params->channel_rekey_secs = SILC_SERVER_PARAM_CHANNEL_REKEY;
264   params->key_exchange_timeout = SILC_SERVER_PARAM_SKE_TIMEOUT;
265   params->conn_auth_timeout = SILC_SERVER_PARAM_CONNAUTH_TIMEOUT;
266   params->connections_per_thread = SILC_SERVER_PARAM_CONN_PER_THREAD;
267
268   return params;
269 }
270
271 /* Frees parameters context */
272
273 void silc_server_params_free(SilcServerParams params)
274 {
275   silc_free(params);
276 }
277
278 /* Allocate server info context */
279
280 SilcServerParamServerInfo silc_server_params_serverinfo_alloc(void)
281 {
282   SilcServerParamServerInfo server_info;
283
284   server_info = silc_calloc(1, sizeof(*server_info));
285   if (!server_info)
286     return NULL;
287
288   silc_list_init(server_info->interfaces,
289                  struct SilcServerParamInterfaceStruct, next);
290
291   return server_info;
292 }
293
294 /* Set server info */
295
296 void silc_server_params_set_serverinfo(SilcServerParams params,
297                                        SilcServerParamServerInfo server_info)
298 {
299   params->server_info = server_info;
300 }
301
302 /* Add interface */
303
304 void silc_server_params_serverinfo_add_iface(SilcServerParamServerInfo info,
305                                              SilcServerParamInterface iface)
306 {
307   silc_list_add(info->interfaces, iface);
308 }
309
310 /* Add cipher */
311
312 void silc_server_params_add_cipher(SilcServerParams params,
313                                    SilcServerParamCipher cipher)
314 {
315   silc_list_add(params->cipher, cipher);
316 }
317
318 /* Add hash */
319
320 void silc_server_params_add_hash(SilcServerParams params,
321                                  SilcServerParamHash hash)
322 {
323   silc_list_add(params->hash, hash);
324 }
325
326 /* Add HMAC */
327
328 void silc_server_params_add_hmac(SilcServerParams params,
329                                  SilcServerParamHmac hmac)
330 {
331   silc_list_add(params->hmac, hmac);
332 }
333
334 /* Add PKCS */
335
336 void silc_server_params_add_pkcs(SilcServerParams params,
337                                  SilcServerParamPkcs pkcs)
338 {
339   silc_list_add(params->pkcs, pkcs);
340 }
341
342 /* Add client */
343
344 void silc_server_params_add_client(SilcServerParams params,
345                                    SilcServerParamClient client)
346 {
347   silc_list_add(params->clients, client);
348 }
349
350 /* Add server */
351
352 void silc_server_params_add_server(SilcServerParams params,
353                                    SilcServerParamServer server)
354 {
355   silc_list_add(params->servers, server);
356 }
357
358 /* Add router */
359
360 void silc_server_params_add_router(SilcServerParams params,
361                                    SilcServerParamRouter router)
362 {
363   silc_list_add(params->routers, router);
364 }
365
366 /* Add connection parameters */
367
368 void silc_server_params_add_connparam(SilcServerParams params,
369                                       SilcServerParamConnParams param)
370 {
371   silc_list_add(params->conn_params, param);
372 }
373
374 /* Add deny */
375
376 void silc_server_params_add_deny(SilcServerParams params,
377                                  SilcServerParamDeny deny)
378 {
379   silc_list_add(params->denied, deny);
380 }
381
382 /* Add admin */
383
384 void silc_server_params_add_admin(SilcServerParams params,
385                                   SilcServerParamAdmin admin)
386 {
387   silc_list_add(params->admins, admin);
388 }