Added statistics updating and statisics dumping with SIGUSR1.
[silc.git] / apps / silcd / server.c
1 /*
2
3   server.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2002 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  * This is the actual SILC server than handles everything relating to
22  * servicing the SILC connections. This is also a SILC router as a router
23  * is also normal server.
24  */
25 /* $Id$ */
26
27 #include "serverincludes.h"
28 #include "server_internal.h"
29
30 /* Static prototypes */
31 SILC_TASK_CALLBACK(silc_server_connect_to_router_retry);
32 SILC_TASK_CALLBACK(silc_server_connect_router);
33 SILC_TASK_CALLBACK(silc_server_connect_to_router);
34 SILC_TASK_CALLBACK(silc_server_connect_to_router_second);
35 SILC_TASK_CALLBACK(silc_server_connect_to_router_final);
36 SILC_TASK_CALLBACK(silc_server_accept_new_connection);
37 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second);
38 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final);
39 SILC_TASK_CALLBACK(silc_server_packet_process);
40 SILC_TASK_CALLBACK(silc_server_packet_parse_real);
41 SILC_TASK_CALLBACK(silc_server_close_connection_final);
42 SILC_TASK_CALLBACK(silc_server_free_client_data_timeout);
43 SILC_TASK_CALLBACK(silc_server_timeout_remote);
44 SILC_TASK_CALLBACK(silc_server_channel_key_rekey);
45 SILC_TASK_CALLBACK(silc_server_failure_callback);
46 SILC_TASK_CALLBACK(silc_server_rekey_callback);
47 SILC_TASK_CALLBACK(silc_server_get_stats);
48
49 /* Allocates a new SILC server object. This has to be done before the server
50    can be used. After allocation one must call silc_server_init to initialize
51    the server. The new allocated server object is returned to the new_server
52    argument. */
53
54 int silc_server_alloc(SilcServer *new_server)
55 {
56   SilcServer server;
57
58   SILC_LOG_DEBUG(("Allocating new server object"));
59
60   server = silc_calloc(1, sizeof(*server));
61   server->server_type = SILC_SERVER;
62   server->standalone = TRUE;
63   server->local_list = silc_calloc(1, sizeof(*server->local_list));
64   server->global_list = silc_calloc(1, sizeof(*server->global_list));
65   server->pending_commands = silc_dlist_init();
66 #ifdef SILC_SIM
67   server->sim = silc_dlist_init();
68 #endif
69
70   *new_server = server;
71
72   return TRUE;
73 }
74
75 /* Free's the SILC server object. This is called at the very end before
76    the program ends. */
77
78 void silc_server_free(SilcServer server)
79 {
80   if (server) {
81 #ifdef SILC_SIM
82     SilcSim sim;
83
84     while ((sim = silc_dlist_get(server->sim)) != SILC_LIST_END) {
85       silc_dlist_del(server->sim, sim);
86       silc_sim_free(sim);
87     }
88     silc_dlist_uninit(server->sim);
89 #endif
90
91     silc_server_config_unref(&server->config_ref);
92     if (server->rng)
93       silc_rng_free(server->rng);
94     if (server->pkcs)
95       silc_pkcs_free(server->pkcs);
96     if (server->public_key)
97       silc_pkcs_public_key_free(server->public_key);
98     if (server->private_key)
99       silc_pkcs_private_key_free(server->private_key);
100     if (server->pending_commands)
101       silc_dlist_uninit(server->pending_commands);
102     if (server->id_entry)
103       silc_idlist_del_server(server->local_list, server->id_entry);
104
105     silc_idcache_free(server->local_list->clients);
106     silc_idcache_free(server->local_list->servers);
107     silc_idcache_free(server->local_list->channels);
108     silc_idcache_free(server->global_list->clients);
109     silc_idcache_free(server->global_list->servers);
110     silc_idcache_free(server->global_list->channels);
111     silc_hash_table_free(server->watcher_list);
112
113     silc_free(server->sockets);
114     silc_free(server);
115   }
116 }
117
118 /* Creates a new server listener. */
119
120 static bool silc_server_listen(SilcServer server, const char *server_ip,
121                                SilcUInt16 port, int *sock)
122 {
123   *sock = silc_net_create_server(port, server_ip);
124   if (*sock < 0) {
125     SILC_LOG_ERROR(("Could not create server listener: %s on %hu",
126                         server_ip, port));
127     return FALSE;
128   }
129   return TRUE;
130 }
131
132 /* Adds a secondary listener. */
133 bool silc_server_init_secondary(SilcServer server)
134 {
135   int sock=0, sock_list[server->config->param.connections_max];
136   SilcSocketConnection newsocket = NULL;
137   SilcServerConfigServerInfoInterface *interface;
138
139   for (interface = server->config->server_info->secondary; interface; 
140         interface = interface->next, sock++) {
141           
142     if (!silc_server_listen(server,
143         interface->server_ip, interface->port, &sock_list[sock]))
144       goto err;
145
146     /* Set socket to non-blocking mode */
147     silc_net_set_socket_nonblock(sock_list[sock]);
148
149     /* Add ourselves also to the socket table. The entry allocated above
150        is sent as argument for fast referencing in the future. */
151     silc_socket_alloc(sock_list[sock], 
152                 SILC_SOCKET_TYPE_SERVER, NULL, &newsocket);
153     server->sockets[sock_list[sock]] = newsocket;
154
155     /* Perform name and address lookups to resolve the listenning address
156        and port. */
157     if (!silc_net_check_local_by_sock(sock_list[sock], &newsocket->hostname,
158                             &newsocket->ip)) {
159       if ((server->config->require_reverse_lookup && !newsocket->hostname) ||
160         !newsocket->ip) {
161         SILC_LOG_ERROR(("IP/DNS lookup failed for local host %s",
162                       newsocket->hostname ? newsocket->hostname :
163                       newsocket->ip ? newsocket->ip : ""));
164         server->stat.conn_failures++;
165         goto err;
166       }
167       if (!newsocket->hostname)
168         newsocket->hostname = strdup(newsocket->ip);
169     }
170     newsocket->port = silc_net_get_local_port(sock);
171     
172     newsocket->user_data = (void *)server->id_entry;
173     silc_schedule_task_add(server->schedule, sock_list[sock],
174                          silc_server_accept_new_connection,
175                          (void *)server, 0, 0,
176                          SILC_TASK_FD,
177                          SILC_TASK_PRI_NORMAL);
178
179   }
180
181   return TRUE;
182   
183 err:
184
185   do silc_net_close_server(sock_list[sock--]); while (sock >= 0);
186   return FALSE;
187
188 }
189
190 /* Initializes the entire SILC server. This is called always before running
191    the server. This is called only once at the initialization of the program.
192    This binds the server to its listenning port. After this function returns
193    one should call silc_server_run to start the server. This returns TRUE
194    when everything is ok to run the server. Configuration file must be
195    read and parsed before calling this. */
196
197 bool silc_server_init(SilcServer server)
198 {
199   int sock;
200   SilcServerID *id;
201   SilcServerEntry id_entry;
202   SilcIDListPurge purge;
203   SilcSocketConnection newsocket = NULL;
204
205   SILC_LOG_DEBUG(("Initializing server"));
206
207   server->starttime = time(NULL);
208
209   /* Take config object for us */
210   silc_server_config_ref(&server->config_ref, server->config, 
211                          server->config);
212
213   /* Steal public and private key from the config object */
214   server->public_key = server->config->server_info->public_key;
215   server->private_key = server->config->server_info->private_key;
216   server->config->server_info->public_key = NULL;
217   server->config->server_info->private_key = NULL;
218
219   /* Register all configured ciphers, PKCS and hash functions. */
220   if (!silc_server_config_register_ciphers(server))
221     silc_cipher_register_default();
222   if (!silc_server_config_register_pkcs(server))
223     silc_pkcs_register_default();
224   if (!silc_server_config_register_hashfuncs(server))
225     silc_hash_register_default();
226   if (!silc_server_config_register_hmacs(server))
227     silc_hmac_register_default();
228
229   /* Initialize random number generator for the server. */
230   server->rng = silc_rng_alloc();
231   silc_rng_init(server->rng);
232   silc_rng_global_init(server->rng);
233
234   /* Initialize hash functions for server to use */
235   silc_hash_alloc("md5", &server->md5hash);
236   silc_hash_alloc("sha1", &server->sha1hash);
237
238   /* Allocate PKCS context for local public and private keys */
239   if (!silc_pkcs_alloc(server->public_key->name, &server->pkcs))
240     goto err;
241   silc_pkcs_public_key_set(server->pkcs, server->public_key);
242   silc_pkcs_private_key_set(server->pkcs, server->private_key);
243
244   /* Initialize the scheduler */
245   server->schedule = silc_schedule_init(server->config->param.connections_max);
246   if (!server->schedule)
247     goto err;
248
249   /* First, register log files configuration for error output */
250   silc_server_config_setlogfiles(server);
251
252   /* Initialize ID caches */
253   server->local_list->clients =
254     silc_idcache_alloc(0, SILC_ID_CLIENT, silc_idlist_client_destructor);
255   server->local_list->servers = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
256   server->local_list->channels = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
257
258   /* These are allocated for normal server as well as these hold some
259      global information that the server has fetched from its router. For
260      router these are used as they are supposed to be used on router. */
261   server->global_list->clients =
262     silc_idcache_alloc(0, SILC_ID_CLIENT, silc_idlist_client_destructor);
263   server->global_list->servers = silc_idcache_alloc(0, SILC_ID_SERVER, NULL);
264   server->global_list->channels = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL);
265
266   /* Init watcher list */
267   server->watcher_list = 
268     silc_hash_table_alloc(1, silc_hash_client_id_hash, NULL,
269                           silc_hash_data_compare, (void *)CLIENTID_HASH_LEN,
270                           NULL, NULL, TRUE);
271   if (!server->watcher_list)
272     goto err;
273
274   /* Create a listening server */
275   if (!silc_server_listen(server,
276                 server->config->server_info->primary == NULL ? NULL :
277                         server->config->server_info->primary->server_ip,
278                 server->config->server_info->primary == NULL ? 0 :
279                         server->config->server_info->primary->port,
280                 &sock))
281     goto err;
282
283   /* Set socket to non-blocking mode */
284   silc_net_set_socket_nonblock(sock);
285   server->sock = sock;
286
287   /* Allocate the entire socket list that is used in server. Eventually
288      all connections will have entry in this table (it is a table of
289      pointers to the actual object that is allocated individually
290      later). */
291   server->sockets = silc_calloc(server->config->param.connections_max,
292                                 sizeof(*server->sockets));
293   if (!server->sockets)
294     goto err;
295
296   /* Add ourselves also to the socket table. The entry allocated above
297      is sent as argument for fast referencing in the future. */
298   silc_socket_alloc(sock, SILC_SOCKET_TYPE_SERVER, NULL, &newsocket);
299   server->sockets[sock] = newsocket;
300
301   /* Perform name and address lookups to resolve the listenning address
302      and port. */
303   if (!silc_net_check_local_by_sock(sock, &newsocket->hostname,
304                                     &newsocket->ip)) {
305     if ((server->config->require_reverse_lookup && !newsocket->hostname) ||
306         !newsocket->ip) {
307       SILC_LOG_ERROR(("IP/DNS lookup failed for local host %s",
308                       newsocket->hostname ? newsocket->hostname :
309                       newsocket->ip ? newsocket->ip : ""));
310       server->stat.conn_failures++;
311       goto err;
312     }
313     if (!newsocket->hostname)
314       newsocket->hostname = strdup(newsocket->ip);
315   }
316   newsocket->port = silc_net_get_local_port(sock);
317
318   /* Create a Server ID for the server. */
319   silc_id_create_server_id(newsocket->ip, newsocket->port, server->rng, &id);
320   if (!id)
321     goto err;
322
323   server->id = id;
324   server->id_string = silc_id_id2str(id, SILC_ID_SERVER);
325   server->id_string_len = silc_id_get_len(id, SILC_ID_SERVER);
326   server->id_type = SILC_ID_SERVER;
327   server->server_name = server->config->server_info->server_name;
328   server->config->server_info->server_name = NULL;
329
330   /* Add ourselves to the server list. We don't have a router yet
331      beacuse we haven't established a route yet. It will be done later.
332      For now, NULL is sent as router. This allocates new entry to
333      the ID list. */
334   id_entry =
335     silc_idlist_add_server(server->local_list, strdup(server->server_name),
336                            server->server_type, server->id, NULL, NULL);
337   if (!id_entry) {
338     SILC_LOG_ERROR(("Could not add ourselves to cache"));
339     goto err;
340   }
341   id_entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
342
343   /* Put the allocated socket pointer also to the entry allocated above
344      for fast back-referencing to the socket list. */
345   newsocket->user_data = (void *)id_entry;
346   id_entry->connection = (void *)newsocket;
347   server->id_entry = id_entry;
348
349   /* Register protocols */
350   silc_server_protocols_register();
351
352   /* Add the first task to the scheduler. This is task that is executed by
353      timeout. It expires as soon as the caller calls silc_server_run. This
354      task performs authentication protocol and key exchange with our
355      primary router. */
356   silc_schedule_task_add(server->schedule, 0,
357                          silc_server_connect_to_router,
358                          (void *)server, 0, 1,
359                          SILC_TASK_TIMEOUT,
360                          SILC_TASK_PRI_NORMAL);
361
362   /* Add listener task to the scheduler. This task receives new connections
363      to the server. This task remains on the queue until the end of the
364      program. */
365   silc_schedule_task_add(server->schedule, sock,
366                          silc_server_accept_new_connection,
367                          (void *)server, 0, 0,
368                          SILC_TASK_FD,
369                          SILC_TASK_PRI_NORMAL);
370
371   if (silc_server_init_secondary(server) == FALSE)
372     goto err;
373   
374   server->listenning = TRUE;
375
376   /* If server connections has been configured then we must be router as
377      normal server cannot have server connections, only router connections. */
378   if (server->config->servers) {
379     SilcServerConfigServer *ptr = server->config->servers;
380
381     server->server_type = SILC_ROUTER;
382     while (ptr) {
383       if (ptr->backup_router) {
384         server->server_type = SILC_BACKUP_ROUTER;
385         server->backup_router = TRUE;
386         server->id_entry->server_type = SILC_BACKUP_ROUTER;
387         break;
388       }
389       ptr = ptr->next;
390     }
391   }
392
393   /* Register the ID Cache purge task. This periodically purges the ID cache
394      and removes the expired cache entries. */
395
396   /* Clients local list */
397   purge = silc_calloc(1, sizeof(*purge));
398   purge->cache = server->local_list->clients;
399   purge->schedule = server->schedule;
400   purge->timeout = 600;
401   silc_schedule_task_add(purge->schedule, 0,
402                          silc_idlist_purge,
403                          (void *)purge, purge->timeout, 0,
404                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
405
406   /* Clients global list */
407   purge = silc_calloc(1, sizeof(*purge));
408   purge->cache = server->global_list->clients;
409   purge->schedule = server->schedule;
410   purge->timeout = 300;
411   silc_schedule_task_add(purge->schedule, 0,
412                          silc_idlist_purge,
413                          (void *)purge, purge->timeout, 0,
414                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
415
416   /* If we are normal server we'll retrieve network statisticial information
417      once in a while from the router. */
418   if (server->server_type == SILC_SERVER)
419     silc_schedule_task_add(purge->schedule, 0, silc_server_get_stats,
420                            server, 10, 0, SILC_TASK_TIMEOUT,
421                            SILC_TASK_PRI_LOW);
422
423   if (server->server_type == SILC_ROUTER)
424     server->stat.routers++;
425
426   SILC_LOG_DEBUG(("Server initialized"));
427
428   /* We are done here, return succesfully */
429   return TRUE;
430
431  err:
432   silc_server_config_unref(&server->config_ref);
433   silc_net_close_server(sock);
434   return FALSE;
435 }
436
437 /* This function basically reads the config file again and switches the config
438    object pointed by the server object. After that, we have to fix various
439    things such as the server_name and the listening ports.
440    Keep in mind that we no longer have the root privileges at this point. */
441
442 bool silc_server_rehash(SilcServer server)
443 {
444   SilcServerConfig newconfig;
445
446   SILC_LOG_INFO(("Rehashing server"));
447
448   /* Reset the logging system */
449   silc_log_quick = TRUE;
450   silc_log_flush_all();
451
452   /* Start the main rehash phase (read again the config file) */
453   newconfig = silc_server_config_alloc(server->config_file);
454   if (!newconfig) {
455     SILC_LOG_ERROR(("Rehash FAILED."));
456     return FALSE;
457   }
458
459   /* Reinit scheduler if necessary */
460   if (newconfig->param.connections_max > server->config->param.connections_max)
461     if (!silc_schedule_reinit(server->schedule, 
462                               newconfig->param.connections_max))
463       return FALSE;
464
465   /* Fix the server_name field */
466   if (strcmp(server->server_name, newconfig->server_info->server_name)) {
467     silc_free(server->server_name);
468     server->server_name = newconfig->server_info->server_name;
469     newconfig->server_info->server_name = NULL;
470
471     /* Update the idcache list with a fresh pointer */
472     silc_free(server->id_entry->server_name);
473     server->id_entry->server_name = strdup(server->server_name);
474     if (!silc_idcache_del_by_context(server->local_list->servers, 
475                                      server->id_entry))
476       return FALSE;
477     if (!silc_idcache_add(server->local_list->servers,
478                           server->id_entry->server_name,
479                           server->id_entry->id, server->id_entry, 0, NULL))
480       return FALSE;
481   }
482
483   /* Set logging */
484   silc_server_config_setlogfiles(server);
485
486   /* Change new key pair if necessary */
487   if (newconfig->server_info->public_key &&
488       !silc_pkcs_public_key_compare(server->public_key,
489                                     newconfig->server_info->public_key)) {
490     silc_pkcs_public_key_free(server->public_key);
491     silc_pkcs_private_key_free(server->private_key);
492     server->public_key = newconfig->server_info->public_key;
493     server->private_key = newconfig->server_info->private_key;
494     newconfig->server_info->public_key = NULL;
495     newconfig->server_info->private_key = NULL;
496
497     /* Allocate PKCS context for local public and private keys */
498     silc_pkcs_free(server->pkcs);
499     if (!silc_pkcs_alloc(server->public_key->name, &server->pkcs))
500       return FALSE;
501     silc_pkcs_public_key_set(server->pkcs, server->public_key);
502     silc_pkcs_private_key_set(server->pkcs, server->private_key);
503   }
504
505   /* Go through all configured routers after rehash */
506   silc_schedule_task_add(server->schedule, 0,
507                          silc_server_connect_to_router,
508                          (void *)server, 0, 1,
509                          SILC_TASK_TIMEOUT,
510                          SILC_TASK_PRI_NORMAL);
511
512   /* Check whether our router status has changed */
513   if (newconfig->servers) {
514     SilcServerConfigServer *ptr = newconfig->servers;
515
516     server->server_type = SILC_ROUTER;
517     while (ptr) {
518       if (ptr->backup_router) {
519         server->server_type = SILC_BACKUP_ROUTER;
520         server->backup_router = TRUE;
521         server->id_entry->server_type = SILC_BACKUP_ROUTER;
522         break;
523       }
524       ptr = ptr->next;
525     }
526   }
527
528   /* Our old config is gone now. We'll unreference our reference made in
529      silc_server_init and then destroy it since we are destroying it
530      underneath the application (layer which called silc_server_init). */
531   silc_server_config_unref(&server->config_ref);
532   silc_server_config_destroy(server->config);
533
534   /* Take new config context */
535   server->config = newconfig;
536   silc_server_config_ref(&server->config_ref, server->config, server->config);
537
538   SILC_LOG_DEBUG(("Server rehashed"));
539
540   return TRUE;
541 }
542
543 /* The heart of the server. This runs the scheduler thus runs the server.
544    When this returns the server has been stopped and the program will
545    be terminated. */
546
547 void silc_server_run(SilcServer server)
548 {
549   SILC_LOG_INFO(("SILC Server started"));
550
551   /* Start the scheduler, the heart of the SILC server. When this returns
552      the program will be terminated. */
553   silc_schedule(server->schedule);
554 }
555
556 /* Stops the SILC server. This function is used to shutdown the server.
557    This is usually called after the scheduler has returned. After stopping
558    the server one should call silc_server_free. */
559
560 void silc_server_stop(SilcServer server)
561 {
562   SILC_LOG_DEBUG(("Stopping server"));
563
564   if (server->schedule) {
565     silc_schedule_stop(server->schedule);
566     silc_schedule_uninit(server->schedule);
567     server->schedule = NULL;
568   }
569
570   silc_server_protocols_unregister();
571
572   SILC_LOG_DEBUG(("Server stopped"));
573 }
574
575 /* Function that is called when the network connection to a router has
576    been established.  This will continue with the key exchange protocol
577    with the remote router. */
578
579 void silc_server_start_key_exchange(SilcServer server,
580                                     SilcServerConnection sconn,
581                                     int sock)
582 {
583   SilcSocketConnection newsocket;
584   SilcProtocol protocol;
585   SilcServerKEInternalContext *proto_ctx;
586   SilcServerConfigRouter *conn =
587     (SilcServerConfigRouter *) sconn->conn.ref_ptr;
588   void *context;
589
590   /* Cancel any possible retry timeouts */
591   silc_schedule_task_del_by_callback(server->schedule,
592                                      silc_server_connect_to_router_retry);
593
594   /* Set socket options */
595   silc_net_set_socket_nonblock(sock);
596   silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
597
598   /* Create socket connection for the connection. Even though we
599      know that we are connecting to a router we will mark the socket
600      to be unknown connection until we have executed authentication
601      protocol. */
602   silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
603   server->sockets[sock] = newsocket;
604   newsocket->hostname = strdup(sconn->remote_host);
605   newsocket->ip = strdup(sconn->remote_host);
606   newsocket->port = sconn->remote_port;
607   sconn->sock = newsocket;
608
609   /* Allocate internal protocol context. This is sent as context
610      to the protocol. */
611   proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
612   proto_ctx->server = (void *)server;
613   proto_ctx->context = (void *)sconn;
614   proto_ctx->sock = newsocket;
615   proto_ctx->rng = server->rng;
616   proto_ctx->responder = FALSE;
617
618   /* Set Key Exchange flags from configuration, but fall back to global
619      settings too. */
620   SILC_GET_SKE_FLAGS(conn, proto_ctx);
621   if (server->config->param.key_exchange_pfs)
622     proto_ctx->flags |= SILC_SKE_SP_FLAG_PFS;
623
624   /* Perform key exchange protocol. silc_server_connect_to_router_second
625      will be called after the protocol is finished. */
626   silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
627                       &protocol, proto_ctx,
628                       silc_server_connect_to_router_second);
629   newsocket->protocol = protocol;
630
631   /* Register a timeout task that will be executed if the protocol
632      is not executed within set limit. */
633   proto_ctx->timeout_task =
634     silc_schedule_task_add(server->schedule, sock,
635                            silc_server_timeout_remote,
636                            server, server->config->key_exchange_timeout, 0,
637                            SILC_TASK_TIMEOUT,
638                            SILC_TASK_PRI_LOW);
639
640   /* Register the connection for network input and output. This sets
641      that scheduler will listen for incoming packets for this connection
642      and sets that outgoing packets may be sent to this connection as
643      well. However, this doesn't set the scheduler for outgoing traffic,
644      it will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
645      later when outgoing data is available. */
646   context = (void *)server;
647   SILC_REGISTER_CONNECTION_FOR_IO(sock);
648
649   /* Run the protocol */
650   silc_protocol_execute(protocol, server->schedule, 0, 0);
651 }
652
653 /* Timeout callback that will be called to retry connecting to remote
654    router. This is used by both normal and router server. This will wait
655    before retrying the connecting. The timeout is generated by exponential
656    backoff algorithm. */
657
658 SILC_TASK_CALLBACK(silc_server_connect_to_router_retry)
659 {
660   SilcServerConnection sconn = (SilcServerConnection)context;
661   SilcServer server = sconn->server;
662   SilcServerConfigRouter *conn = sconn->conn.ref_ptr;
663   SilcServerConfigConnParams *param =
664                 (conn->param ? conn->param : &server->config->param);
665
666   SILC_LOG_INFO(("Retrying connecting to a router"));
667
668   /* Calculate next timeout */
669   if (sconn->retry_count >= 1) {
670     sconn->retry_timeout = sconn->retry_timeout * SILC_SERVER_RETRY_MULTIPLIER;
671     if (sconn->retry_timeout > param->reconnect_interval_max)
672       sconn->retry_timeout = param->reconnect_interval_max;
673   } else {
674     sconn->retry_timeout = param->reconnect_interval;
675   }
676   sconn->retry_count++;
677   sconn->retry_timeout = sconn->retry_timeout +
678     silc_rng_get_rn32(server->rng) % SILC_SERVER_RETRY_RANDOMIZER;
679
680   /* If we've reached max retry count, give up. */
681   if ((sconn->retry_count > param->reconnect_count) &&
682       !param->reconnect_keep_trying) {
683     SILC_LOG_ERROR(("Could not connect to router, giving up"));
684     silc_server_config_unref(&sconn->conn);
685     silc_free(sconn->remote_host);
686     silc_free(sconn->backup_replace_ip);
687     silc_free(sconn);
688     return;
689   }
690
691   /* We will lookup a fresh pointer later */
692   silc_server_config_unref(&sconn->conn);
693
694   /* Wait one before retrying */
695   silc_schedule_task_add(server->schedule, 0, silc_server_connect_router,
696                          context, sconn->retry_timeout, 0,
697                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
698 }
699
700 /* Generic routine to use connect to a router. */
701
702 SILC_TASK_CALLBACK(silc_server_connect_router)
703 {
704   SilcServerConnection sconn = (SilcServerConnection)context;
705   SilcServer server = sconn->server;
706   SilcServerConfigRouter *rconn;
707   int sock;
708
709   SILC_LOG_INFO(("Connecting to the %s %s on port %d",
710                  (sconn->backup ? "backup router" : "router"),
711                  sconn->remote_host, sconn->remote_port));
712
713   server->router_connect = time(NULL);
714   rconn = silc_server_config_find_router_conn(server, sconn->remote_host,
715                                               sconn->remote_port);
716   if (!rconn) {
717     SILC_LOG_INFO(("Unconfigured %s connection %s:%d, cannot connect",
718                    (sconn->backup ? "backup router" : "router"),
719                    sconn->remote_host, sconn->remote_port));
720     silc_free(sconn->remote_host);
721     silc_free(sconn->backup_replace_ip);
722     silc_free(sconn);
723     return;
724   }
725   silc_server_config_ref(&sconn->conn, server->config, (void *)rconn);
726
727   /* Connect to remote host */
728   sock = silc_net_create_connection(
729                  (!server->config->server_info->primary ? NULL :
730                   server->config->server_info->primary->server_ip),
731                  sconn->remote_port, sconn->remote_host);
732   if (sock < 0) {
733     SILC_LOG_ERROR(("Could not connect to router %s:%d",
734                     sconn->remote_host, sconn->remote_port));
735     if (!sconn->no_reconnect)
736       silc_schedule_task_add(server->schedule, 0,
737                              silc_server_connect_to_router_retry,
738                              context, 0, 1, SILC_TASK_TIMEOUT,
739                              SILC_TASK_PRI_NORMAL);
740     else
741       silc_server_config_unref(&sconn->conn);
742     return;
743   }
744
745   /* Continue with key exchange protocol */
746   silc_server_start_key_exchange(server, sconn, sock);
747 }
748
749 /* This function connects to our primary router or if we are a router this
750    establishes all our primary routes. This is called at the start of the
751    server to do authentication and key exchange with our router - called
752    from schedule. */
753
754 SILC_TASK_CALLBACK(silc_server_connect_to_router)
755 {
756   SilcServer server = (SilcServer)context;
757   SilcServerConnection sconn;
758   SilcServerConfigRouter *ptr;
759
760   SILC_LOG_DEBUG(("Connecting to router(s)"));
761
762   if (server->server_type == SILC_SERVER) {
763     SILC_LOG_DEBUG(("We are normal server"));
764   } else if (server->server_type == SILC_ROUTER) {
765     SILC_LOG_DEBUG(("We are router"));
766   } else {
767     SILC_LOG_DEBUG(("We are backup router/normal server"));
768   }
769
770   if (!server->config->routers) {
771     /* There wasn't a configured router, we will continue but we don't
772        have a connection to outside world.  We will be standalone server. */
773     SILC_LOG_DEBUG(("No router(s), server will be standalone"));
774     server->standalone = TRUE;
775     return;
776   }
777
778   /* Cancel any possible retry timeouts */
779   silc_schedule_task_del_by_callback(server->schedule,
780                                      silc_server_connect_router);
781   silc_schedule_task_del_by_callback(server->schedule,
782                                      silc_server_connect_to_router_retry);
783
784   /* Create the connections to all our routes */
785   for (ptr = server->config->routers; ptr; ptr = ptr->next) {
786
787     SILC_LOG_DEBUG(("%s connection [%s] %s:%d",
788                     ptr->backup_router ? "Backup router" : "Router",
789                     ptr->initiator ? "Initiator" : "Responder",
790                     ptr->host, ptr->port));
791
792     if (ptr->initiator) {
793       /* Check whether we are connected to this host already */
794       if (silc_server_num_sockets_by_remote(server, 
795                                             silc_net_is_ip(ptr->host) ?
796                                             ptr->host : NULL,
797                                             silc_net_is_ip(ptr->host) ?
798                                             NULL : ptr->host, ptr->port,
799                                             SILC_SOCKET_TYPE_ROUTER)) {
800         SILC_LOG_DEBUG(("We are already connected to this router"));
801         continue;
802       }
803
804       /* Allocate connection object for hold connection specific stuff. */
805       sconn = silc_calloc(1, sizeof(*sconn));
806       sconn->server = server;
807       sconn->remote_host = strdup(ptr->host);
808       sconn->remote_port = ptr->port;
809       sconn->backup = ptr->backup_router;
810       if (sconn->backup) {
811         sconn->backup_replace_ip = strdup(ptr->backup_replace_ip);
812         sconn->backup_replace_port = ptr->backup_replace_port;
813       }
814
815       if (!server->router_conn && !sconn->backup)
816         server->router_conn = sconn;
817
818       silc_schedule_task_add(server->schedule, 0,
819                              silc_server_connect_router,
820                              (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
821                              SILC_TASK_PRI_NORMAL);
822     }
823   }
824 }
825
826 /* Second part of connecting to router(s). Key exchange protocol has been
827    executed and now we will execute authentication protocol. */
828
829 SILC_TASK_CALLBACK(silc_server_connect_to_router_second)
830 {
831   SilcProtocol protocol = (SilcProtocol)context;
832   SilcServerKEInternalContext *ctx =
833     (SilcServerKEInternalContext *)protocol->context;
834   SilcServer server = (SilcServer)ctx->server;
835   SilcServerConnection sconn = (SilcServerConnection)ctx->context;
836   SilcSocketConnection sock = ctx->sock;
837   SilcServerConnAuthInternalContext *proto_ctx;
838   SilcServerConfigRouter *conn = NULL;
839
840   SILC_LOG_DEBUG(("Start"));
841
842   if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
843       protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
844     /* Error occured during protocol */
845     silc_protocol_free(protocol);
846     sock->protocol = NULL;
847     silc_ske_free_key_material(ctx->keymat);
848     if (ctx->packet)
849       silc_packet_context_free(ctx->packet);
850     if (ctx->ske)
851       silc_ske_free(ctx->ske);
852     silc_free(ctx->dest_id);
853     silc_free(ctx);
854     silc_server_config_unref(&sconn->conn);
855     silc_free(sconn->remote_host);
856     silc_free(sconn->backup_replace_ip);
857     silc_free(sconn);
858     silc_schedule_task_del_by_callback(server->schedule,
859                                        silc_server_failure_callback);
860     silc_server_disconnect_remote(server, sock, 
861                                   SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
862     return;
863   }
864
865   /* We now have the key material as the result of the key exchange
866      protocol. Take the key material into use. Free the raw key material
867      as soon as we've set them into use. */
868   if (!silc_server_protocol_ke_set_keys(server, ctx->ske,
869                                         ctx->sock, ctx->keymat,
870                                         ctx->ske->prop->cipher,
871                                         ctx->ske->prop->pkcs,
872                                         ctx->ske->prop->hash,
873                                         ctx->ske->prop->hmac,
874                                         ctx->ske->prop->group,
875                                         ctx->responder)) {
876     silc_protocol_free(protocol);
877     sock->protocol = NULL;
878     silc_ske_free_key_material(ctx->keymat);
879     if (ctx->packet)
880       silc_packet_context_free(ctx->packet);
881     if (ctx->ske)
882       silc_ske_free(ctx->ske);
883     silc_free(ctx->dest_id);
884     silc_free(ctx);
885     silc_server_config_unref(&sconn->conn);
886     silc_free(sconn->remote_host);
887     silc_free(sconn->backup_replace_ip);
888     silc_free(sconn);
889     silc_schedule_task_del_by_callback(server->schedule,
890                                        silc_server_failure_callback);
891     silc_server_disconnect_remote(server, sock, 
892                                   SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
893     return;
894   }
895   silc_ske_free_key_material(ctx->keymat);
896
897   /* Allocate internal context for the authentication protocol. This
898      is sent as context for the protocol. */
899   proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
900   proto_ctx->server = (void *)server;
901   proto_ctx->context = (void *)sconn;
902   proto_ctx->sock = sock;
903   proto_ctx->ske = ctx->ske;       /* Save SKE object from previous protocol */
904   proto_ctx->dest_id_type = ctx->dest_id_type;
905   proto_ctx->dest_id = ctx->dest_id;
906
907   /* Resolve the authentication method used in this connection. Check if
908      we find a match from user configured connections */
909   if (!sconn->conn.ref_ptr)
910     conn = silc_server_config_find_router_conn(server, sock->hostname,
911                                                sock->port);
912   else
913     conn = sconn->conn.ref_ptr;
914
915   if (conn) {
916     /* Match found. Use the configured authentication method. Take only
917        the passphrase, since for public key auth we automatically use
918        our local key pair. */
919     if (conn->passphrase) {
920       if (conn->publickeys && !server->config->prefer_passphrase_auth) {
921         proto_ctx->auth_meth = SILC_AUTH_PUBLIC_KEY;
922       } else {
923         proto_ctx->auth_data = strdup(conn->passphrase);
924         proto_ctx->auth_data_len = strlen(conn->passphrase);
925         proto_ctx->auth_meth = SILC_AUTH_PASSWORD;
926       }
927     } else if (conn->publickeys) {
928       proto_ctx->auth_meth = SILC_AUTH_PUBLIC_KEY;
929     } else {
930       proto_ctx->auth_meth = SILC_AUTH_NONE;
931     }
932   } else {
933     SILC_LOG_ERROR(("Could not find connection data for %s (%s) on port",
934                     sock->hostname, sock->ip, sock->port));
935     silc_protocol_free(protocol);
936     sock->protocol = NULL;
937     if (ctx->packet)
938       silc_packet_context_free(ctx->packet);
939     if (ctx->ske)
940       silc_ske_free(ctx->ske);
941     silc_free(ctx->dest_id);
942     silc_free(ctx);
943     silc_server_config_unref(&sconn->conn);
944     silc_free(sconn->remote_host);
945     silc_free(sconn->backup_replace_ip);
946     silc_free(sconn);
947     silc_schedule_task_del_by_callback(server->schedule,
948                                        silc_server_failure_callback);
949     silc_server_disconnect_remote(server, sock, 
950                                   SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
951     return;
952   }
953
954   /* Free old protocol as it is finished now */
955   silc_protocol_free(protocol);
956   if (ctx->packet)
957     silc_packet_context_free(ctx->packet);
958   silc_free(ctx);
959   sock->protocol = NULL;
960
961   /* Allocate the authentication protocol. This is allocated here
962      but we won't start it yet. We will be receiving party of this
963      protocol thus we will wait that connecting party will make
964      their first move. */
965   silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
966                       &sock->protocol, proto_ctx,
967                       silc_server_connect_to_router_final);
968
969   /* Register timeout task. If the protocol is not executed inside
970      this timelimit the connection will be terminated. */
971   proto_ctx->timeout_task =
972     silc_schedule_task_add(server->schedule, sock->sock,
973                            silc_server_timeout_remote,
974                            (void *)server,
975                            server->config->conn_auth_timeout, 0,
976                            SILC_TASK_TIMEOUT,
977                            SILC_TASK_PRI_LOW);
978
979   /* Run the protocol */
980   silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
981 }
982
983 /* Finalizes the connection to router. Registers a server task to the
984    queue so that we can accept new connections. */
985
986 SILC_TASK_CALLBACK(silc_server_connect_to_router_final)
987 {
988   SilcProtocol protocol = (SilcProtocol)context;
989   SilcServerConnAuthInternalContext *ctx =
990     (SilcServerConnAuthInternalContext *)protocol->context;
991   SilcServer server = (SilcServer)ctx->server;
992   SilcServerConnection sconn = (SilcServerConnection)ctx->context;
993   SilcSocketConnection sock = ctx->sock;
994   SilcServerEntry id_entry;
995   SilcBuffer packet;
996   SilcServerHBContext hb_context;
997   unsigned char *id_string;
998   SilcUInt32 id_len;
999   SilcIDListData idata;
1000   SilcServerConfigRouter *conn = NULL;
1001   SilcServerConfigConnParams *param = NULL;
1002
1003   SILC_LOG_DEBUG(("Start"));
1004
1005   if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1006       protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1007     /* Error occured during protocol */
1008     silc_free(ctx->dest_id);
1009     silc_server_disconnect_remote(server, sock, SILC_STATUS_ERR_AUTH_FAILED,
1010                                   NULL);
1011     goto out;
1012   }
1013
1014   /* Add a task to the queue. This task receives new connections to the
1015      server. This task remains on the queue until the end of the program. */
1016   if (!server->listenning && !sconn->backup) {
1017     silc_schedule_task_add(server->schedule, server->sock,
1018                            silc_server_accept_new_connection,
1019                            (void *)server, 0, 0,
1020                            SILC_TASK_FD,
1021                            SILC_TASK_PRI_NORMAL);
1022     server->listenning = TRUE;
1023   }
1024
1025   /* Send NEW_SERVER packet to the router. We will become registered
1026      to the SILC network after sending this packet. */
1027   id_string = silc_id_id2str(server->id, SILC_ID_SERVER);
1028   id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1029   packet = silc_buffer_alloc(2 + 2 + id_len + strlen(server->server_name));
1030   silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1031   silc_buffer_format(packet,
1032                      SILC_STR_UI_SHORT(id_len),
1033                      SILC_STR_UI_XNSTRING(id_string, id_len),
1034                      SILC_STR_UI_SHORT(strlen(server->server_name)),
1035                      SILC_STR_UI_XNSTRING(server->server_name,
1036                                           strlen(server->server_name)),
1037                      SILC_STR_END);
1038
1039   /* Send the packet */
1040   silc_server_packet_send(server, ctx->sock, SILC_PACKET_NEW_SERVER, 0,
1041                           packet->data, packet->len, TRUE);
1042   silc_buffer_free(packet);
1043   silc_free(id_string);
1044
1045   SILC_LOG_INFO(("Connected to router %s", sock->hostname));
1046
1047   /* Check that we do not have this ID already */
1048   id_entry = silc_idlist_find_server_by_id(server->local_list,
1049                                            ctx->dest_id, TRUE, NULL);
1050   if (id_entry) {
1051     silc_idcache_del_by_context(server->local_list->servers, id_entry);
1052   } else {
1053     id_entry = silc_idlist_find_server_by_id(server->global_list,
1054                                              ctx->dest_id, TRUE, NULL);
1055     if (id_entry)
1056       silc_idcache_del_by_context(server->global_list->servers, id_entry);
1057   }
1058
1059   SILC_LOG_DEBUG(("New server id(%s)",
1060                   silc_id_render(ctx->dest_id, SILC_ID_SERVER)));
1061
1062   /* Add the connected router to global server list */
1063   id_entry = silc_idlist_add_server(server->global_list,
1064                                     strdup(sock->hostname),
1065                                     SILC_ROUTER, ctx->dest_id, NULL, sock);
1066   if (!id_entry) {
1067     silc_free(ctx->dest_id);
1068     SILC_LOG_ERROR(("Cannot add new server entry to cache"));
1069     silc_server_disconnect_remote(server, sock, SILC_STATUS_ERR_AUTH_FAILED,
1070                                   NULL);
1071     goto out;
1072   }
1073
1074   silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
1075   silc_free(sock->user_data);
1076   sock->user_data = (void *)id_entry;
1077   sock->type = SILC_SOCKET_TYPE_ROUTER;
1078   idata = (SilcIDListData)sock->user_data;
1079   idata->status |= SILC_IDLIST_STATUS_REGISTERED;
1080
1081   conn = sconn->conn.ref_ptr;
1082   param = &server->config->param;
1083   if (conn && conn->param)
1084     param = conn->param;
1085
1086   /* Perform keepalive. The `hb_context' will be freed automatically
1087      when finally calling the silc_socket_free function. */
1088   hb_context = silc_calloc(1, sizeof(*hb_context));
1089   hb_context->server = server;
1090   silc_socket_set_heartbeat(sock, param->keepalive_secs, hb_context,
1091                             silc_server_perform_heartbeat,
1092                             server->schedule);
1093
1094   /* Register re-key timeout */
1095   idata->rekey->timeout = param->key_exchange_rekey;
1096   idata->rekey->context = (void *)server;
1097   silc_schedule_task_add(server->schedule, sock->sock,
1098                          silc_server_rekey_callback,
1099                          (void *)sock, idata->rekey->timeout, 0,
1100                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1101
1102   if (!sconn->backup) {
1103     /* Mark this router our primary router if we're still standalone */
1104     if (server->standalone) {
1105       server->id_entry->router = id_entry;
1106       server->router = id_entry;
1107       server->standalone = FALSE;
1108
1109       /* If we are router then announce our possible servers. */
1110       if (server->server_type == SILC_ROUTER)
1111         silc_server_announce_servers(server, FALSE, 0,
1112                                      server->router->connection);
1113
1114       /* Announce our clients and channels to the router */
1115       silc_server_announce_clients(server, 0, server->router->connection);
1116       silc_server_announce_channels(server, 0, server->router->connection);
1117
1118 #ifdef BACKUP_SINGLE_ROUTER
1119       /* If we are backup router then this primary router is whom we are
1120          backing up. */
1121       if (server->server_type == SILC_BACKUP_ROUTER)
1122         silc_server_backup_add(server, server->id_entry, sock->ip, 0, TRUE);
1123 #endif /* BACKUP_SINGLE_ROUTER */
1124     }
1125   } else {
1126     /* Add this server to be our backup router */
1127     silc_server_backup_add(server, id_entry, sconn->backup_replace_ip,
1128                            sconn->backup_replace_port, FALSE);
1129   }
1130
1131   sock->protocol = NULL;
1132
1133   /* Call the completion callback to indicate that we've connected to
1134      the router */
1135   if (sconn->callback)
1136     (*sconn->callback)(server, id_entry, sconn->callback_context);
1137
1138  out:
1139   /* Free the temporary connection data context */
1140   if (sconn) {
1141     silc_server_config_unref(&sconn->conn);
1142     silc_free(sconn->remote_host);
1143     silc_free(sconn->backup_replace_ip);
1144     silc_free(sconn);
1145   }
1146   if (sconn == server->router_conn)
1147     server->router_conn = NULL;
1148
1149   /* Free the protocol object */
1150   if (sock->protocol == protocol)
1151     sock->protocol = NULL;
1152   silc_protocol_free(protocol);
1153   if (ctx->packet)
1154     silc_packet_context_free(ctx->packet);
1155   if (ctx->ske)
1156     silc_ske_free(ctx->ske);
1157   if (ctx->auth_meth == SILC_AUTH_PASSWORD)
1158     silc_free(ctx->auth_data);
1159   silc_free(ctx);
1160 }
1161
1162 /* Host lookup callback that is called after the incoming connection's
1163    IP and FQDN lookup is performed. This will actually check the acceptance
1164    of the incoming connection and will register the key exchange protocol
1165    for this connection. */
1166
1167 static void
1168 silc_server_accept_new_connection_lookup(SilcSocketConnection sock,
1169                                          void *context)
1170 {
1171   SilcServerKEInternalContext *proto_ctx =
1172     (SilcServerKEInternalContext *)context;
1173   SilcServer server = (SilcServer)proto_ctx->server;
1174   SilcServerConfigClient *cconfig = NULL;
1175   SilcServerConfigServer *sconfig = NULL;
1176   SilcServerConfigRouter *rconfig = NULL;
1177   SilcServerConfigDeny *deny;
1178   int port;
1179
1180   context = (void *)server;
1181
1182   SILC_LOG_DEBUG(("Start"));
1183
1184   /* Check whether we could resolve both IP and FQDN. */
1185   if (!sock->ip || (!strcmp(sock->ip, sock->hostname) &&
1186                     server->config->require_reverse_lookup)) {
1187     SILC_LOG_ERROR(("IP/DNS lookup failed %s",
1188                     sock->hostname ? sock->hostname :
1189                     sock->ip ? sock->ip : ""));
1190     server->stat.conn_failures++;
1191     silc_server_disconnect_remote(server, sock,
1192                                   SILC_STATUS_ERR_INCOMPLETE_INFORMATION,
1193                                   "Unknown host or IP");
1194     silc_free(proto_ctx);
1195     return;
1196   }
1197
1198   /* Register the connection for network input and output. This sets
1199      that scheduler will listen for incoming packets for this connection
1200      and sets that outgoing packets may be sent to this connection as well.
1201      However, this doesn't set the scheduler for outgoing traffic, it
1202      will be set separately by calling SILC_SET_CONNECTION_FOR_OUTPUT,
1203      later when outgoing data is available. */
1204   SILC_REGISTER_CONNECTION_FOR_IO(sock->sock);
1205
1206   SILC_LOG_INFO(("Incoming connection %s (%s)", sock->hostname,
1207                  sock->ip));
1208
1209   /* Listenning port */
1210   port = server->sockets[(SilcUInt32)proto_ctx->context]->port;
1211
1212   /* Check whether this connection is denied to connect to us. */
1213   deny = silc_server_config_find_denied(server, sock->ip);
1214   if (!deny)
1215     deny = silc_server_config_find_denied(server, sock->hostname);
1216   if (deny) {
1217     /* The connection is denied */
1218     SILC_LOG_INFO(("Connection %s (%s) is denied",
1219                    sock->hostname, sock->ip));
1220     silc_server_disconnect_remote(server, sock,
1221                                   SILC_STATUS_ERR_BANNED_FROM_SERVER,
1222                                   deny->reason);
1223     server->stat.conn_failures++;
1224     silc_free(proto_ctx);
1225     return;
1226   }
1227
1228   /* Check whether we have configured this sort of connection at all. We
1229      have to check all configurations since we don't know what type of
1230      connection this is. */
1231   if (!(cconfig = silc_server_config_find_client(server, sock->ip)))
1232     cconfig = silc_server_config_find_client(server, sock->hostname);
1233   if (!(sconfig = silc_server_config_find_server_conn(server, sock->ip)))
1234     sconfig = silc_server_config_find_server_conn(server, sock->hostname);
1235   if (server->server_type == SILC_ROUTER) {
1236     if (!(rconfig = silc_server_config_find_router_conn(server,
1237                                                         sock->ip, sock->port)))
1238       rconfig = silc_server_config_find_router_conn(server, sock->hostname,
1239                                                     sock->port);
1240   }
1241   if (!cconfig && !sconfig && !rconfig) {
1242     SILC_LOG_INFO(("Connection %s (%s) is not allowed", sock->hostname,
1243                    sock->ip));
1244     silc_server_disconnect_remote(server, sock,
1245                                   SILC_STATUS_ERR_BANNED_FROM_SERVER, NULL);
1246     server->stat.conn_failures++;
1247     silc_free(proto_ctx);
1248     return;
1249   }
1250
1251   /* The connection is allowed */
1252
1253   /* Set internal context for key exchange protocol. This is
1254      sent as context for the protocol. */
1255   proto_ctx->sock = sock;
1256   proto_ctx->rng = server->rng;
1257   proto_ctx->responder = TRUE;
1258   silc_server_config_ref(&proto_ctx->cconfig, server->config, cconfig);
1259   silc_server_config_ref(&proto_ctx->sconfig, server->config, sconfig);
1260   silc_server_config_ref(&proto_ctx->rconfig, server->config, rconfig);
1261
1262   /* Take flags for key exchange. Since we do not know what type of connection
1263      this is, we go through all found configurations and use the global ones
1264      as well. This will result always into strictest key exchange flags. */
1265   SILC_GET_SKE_FLAGS(cconfig, proto_ctx);
1266   SILC_GET_SKE_FLAGS(sconfig, proto_ctx);
1267   SILC_GET_SKE_FLAGS(rconfig, proto_ctx);
1268   if (server->config->param.key_exchange_pfs)
1269     proto_ctx->flags |= SILC_SKE_SP_FLAG_PFS;
1270
1271   /* Prepare the connection for key exchange protocol. We allocate the
1272      protocol but will not start it yet. The connector will be the
1273      initiator of the protocol thus we will wait for initiation from
1274      there before we start the protocol. */
1275   server->stat.auth_attempts++;
1276   silc_protocol_alloc(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
1277                       &sock->protocol, proto_ctx,
1278                       silc_server_accept_new_connection_second);
1279
1280   /* Register a timeout task that will be executed if the connector
1281      will not start the key exchange protocol within specified timeout
1282      and the connection will be closed. */
1283   proto_ctx->timeout_task =
1284     silc_schedule_task_add(server->schedule, sock->sock,
1285                            silc_server_timeout_remote,
1286                            (void *)server,
1287                            server->config->key_exchange_timeout, 0,
1288                            SILC_TASK_TIMEOUT,
1289                            SILC_TASK_PRI_LOW);
1290 }
1291
1292 /* Accepts new connections to the server. Accepting new connections are
1293    done in three parts to make it async. */
1294
1295 SILC_TASK_CALLBACK(silc_server_accept_new_connection)
1296 {
1297   SilcServer server = (SilcServer)context;
1298   SilcSocketConnection newsocket;
1299   SilcServerKEInternalContext *proto_ctx;
1300   int sock;
1301
1302   SILC_LOG_DEBUG(("Accepting new connection"));
1303
1304   server->stat.conn_attempts++;
1305
1306   sock = silc_net_accept_connection(fd);
1307   if (sock < 0) {
1308     SILC_LOG_ERROR(("Could not accept new connection: %s", strerror(errno)));
1309     server->stat.conn_failures++;
1310     return;
1311   }
1312
1313   /* Check for maximum allowed connections */
1314   if (sock > server->config->param.connections_max) {
1315     SILC_LOG_ERROR(("Refusing connection, server is full"));
1316     server->stat.conn_failures++;
1317     silc_net_close_connection(sock);
1318     return;
1319   }
1320
1321   /* Set socket options */
1322   silc_net_set_socket_nonblock(sock);
1323   silc_net_set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
1324
1325   /* We don't create a ID yet, since we don't know what type of connection
1326      this is yet. But, we do add the connection to the socket table. */
1327   silc_socket_alloc(sock, SILC_SOCKET_TYPE_UNKNOWN, NULL, &newsocket);
1328   server->sockets[sock] = newsocket;
1329
1330   /* Perform asynchronous host lookup. This will lookup the IP and the
1331      FQDN of the remote connection. After the lookup is done the connection
1332      is accepted further. */
1333   proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1334   proto_ctx->server = server;
1335   proto_ctx->context = (void *)fd;
1336   silc_socket_host_lookup(newsocket, TRUE,
1337                           silc_server_accept_new_connection_lookup,
1338                           (void *)proto_ctx, server->schedule);
1339 }
1340
1341 /* Second part of accepting new connection. Key exchange protocol has been
1342    performed and now it is time to do little connection authentication
1343    protocol to figure out whether this connection is client or server
1344    and whether it has right to access this server (especially server
1345    connections needs to be authenticated). */
1346
1347 SILC_TASK_CALLBACK(silc_server_accept_new_connection_second)
1348 {
1349   SilcProtocol protocol = (SilcProtocol)context;
1350   SilcServerKEInternalContext *ctx =
1351     (SilcServerKEInternalContext *)protocol->context;
1352   SilcServer server = (SilcServer)ctx->server;
1353   SilcSocketConnection sock = ctx->sock;
1354   SilcServerConnAuthInternalContext *proto_ctx;
1355
1356   SILC_LOG_DEBUG(("Start"));
1357
1358   if ((protocol->state == SILC_PROTOCOL_STATE_ERROR) ||
1359       (protocol->state == SILC_PROTOCOL_STATE_FAILURE)) {
1360     /* Error occured during protocol */
1361     silc_protocol_free(protocol);
1362     sock->protocol = NULL;
1363     silc_ske_free_key_material(ctx->keymat);
1364     if (ctx->packet)
1365       silc_packet_context_free(ctx->packet);
1366     if (ctx->ske)
1367       silc_ske_free(ctx->ske);
1368     silc_free(ctx->dest_id);
1369     silc_server_config_unref(&ctx->cconfig);
1370     silc_server_config_unref(&ctx->sconfig);
1371     silc_server_config_unref(&ctx->rconfig);
1372     silc_free(ctx);
1373     silc_schedule_task_del_by_callback(server->schedule,
1374                                        silc_server_failure_callback);
1375     silc_server_disconnect_remote(server, sock, 
1376                                   SILC_STATUS_ERR_KEY_EXCHANGE_FAILED,
1377                                   NULL);
1378     server->stat.auth_failures++;
1379     return;
1380   }
1381
1382   /* We now have the key material as the result of the key exchange
1383      protocol. Take the key material into use. Free the raw key material
1384      as soon as we've set them into use. */
1385   if (!silc_server_protocol_ke_set_keys(server, ctx->ske,
1386                                         ctx->sock, ctx->keymat,
1387                                         ctx->ske->prop->cipher,
1388                                         ctx->ske->prop->pkcs,
1389                                         ctx->ske->prop->hash,
1390                                         ctx->ske->prop->hmac,
1391                                         ctx->ske->prop->group,
1392                                         ctx->responder)) {
1393     silc_protocol_free(protocol);
1394     sock->protocol = NULL;
1395     silc_ske_free_key_material(ctx->keymat);
1396     if (ctx->packet)
1397       silc_packet_context_free(ctx->packet);
1398     if (ctx->ske)
1399       silc_ske_free(ctx->ske);
1400     silc_free(ctx->dest_id);
1401     silc_server_config_unref(&ctx->cconfig);
1402     silc_server_config_unref(&ctx->sconfig);
1403     silc_server_config_unref(&ctx->rconfig);
1404     silc_free(ctx);
1405     silc_schedule_task_del_by_callback(server->schedule,
1406                                        silc_server_failure_callback);
1407     silc_server_disconnect_remote(server, sock, 
1408                                   SILC_STATUS_ERR_KEY_EXCHANGE_FAILED, NULL);
1409     server->stat.auth_failures++;
1410     return;
1411   }
1412   silc_ske_free_key_material(ctx->keymat);
1413
1414   /* Allocate internal context for the authentication protocol. This
1415      is sent as context for the protocol. */
1416   proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1417   proto_ctx->server = (void *)server;
1418   proto_ctx->sock = sock;
1419   proto_ctx->ske = ctx->ske;    /* Save SKE object from previous protocol */
1420   proto_ctx->responder = TRUE;
1421   proto_ctx->dest_id_type = ctx->dest_id_type;
1422   proto_ctx->dest_id = ctx->dest_id;
1423   proto_ctx->cconfig = ctx->cconfig;
1424   proto_ctx->sconfig = ctx->sconfig;
1425   proto_ctx->rconfig = ctx->rconfig;
1426
1427   /* Free old protocol as it is finished now */
1428   silc_protocol_free(protocol);
1429   if (ctx->packet)
1430     silc_packet_context_free(ctx->packet);
1431   silc_free(ctx);
1432   sock->protocol = NULL;
1433
1434   /* Allocate the authentication protocol. This is allocated here
1435      but we won't start it yet. We will be receiving party of this
1436      protocol thus we will wait that connecting party will make
1437      their first move. */
1438   silc_protocol_alloc(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
1439                       &sock->protocol, proto_ctx,
1440                       silc_server_accept_new_connection_final);
1441
1442   /* Register timeout task. If the protocol is not executed inside
1443      this timelimit the connection will be terminated. */
1444   proto_ctx->timeout_task =
1445     silc_schedule_task_add(server->schedule, sock->sock,
1446                            silc_server_timeout_remote,
1447                            (void *)server,
1448                            server->config->conn_auth_timeout, 0,
1449                            SILC_TASK_TIMEOUT,
1450                            SILC_TASK_PRI_LOW);
1451 }
1452
1453 /* Final part of accepting new connection. The connection has now
1454    been authenticated and keys has been exchanged. We also know whether
1455    this is client or server connection. */
1456
1457 SILC_TASK_CALLBACK(silc_server_accept_new_connection_final)
1458 {
1459   SilcProtocol protocol = (SilcProtocol)context;
1460   SilcServerConnAuthInternalContext *ctx =
1461     (SilcServerConnAuthInternalContext *)protocol->context;
1462   SilcServer server = (SilcServer)ctx->server;
1463   SilcSocketConnection sock = ctx->sock;
1464   SilcServerHBContext hb_context;
1465   SilcUnknownEntry entry = (SilcUnknownEntry)sock->user_data;
1466   void *id_entry;
1467   SilcUInt32 hearbeat_timeout = server->config->param.keepalive_secs;
1468
1469   SILC_LOG_DEBUG(("Start"));
1470
1471   if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1472       protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
1473     /* Error occured during protocol */
1474     silc_protocol_free(protocol);
1475     sock->protocol = NULL;
1476     if (ctx->packet)
1477       silc_packet_context_free(ctx->packet);
1478     if (ctx->ske)
1479       silc_ske_free(ctx->ske);
1480     silc_free(ctx->dest_id);
1481     silc_server_config_unref(&ctx->cconfig);
1482     silc_server_config_unref(&ctx->sconfig);
1483     silc_server_config_unref(&ctx->rconfig);
1484     silc_free(ctx);
1485     silc_schedule_task_del_by_callback(server->schedule,
1486                                        silc_server_failure_callback);
1487     silc_server_disconnect_remote(server, sock, SILC_STATUS_ERR_AUTH_FAILED,
1488                                   NULL);
1489     server->stat.auth_failures++;
1490     return;
1491   }
1492
1493   entry->data.last_receive = time(NULL);
1494
1495   switch (ctx->conn_type) {
1496   case SILC_SOCKET_TYPE_CLIENT:
1497     {
1498       SilcClientEntry client;
1499       SilcServerConfigClient *conn = ctx->cconfig.ref_ptr;
1500
1501       /* Verify whether this connection is after all allowed to connect */
1502       if (!silc_server_connection_allowed(server, sock, ctx->conn_type,
1503                                           &server->config->param,
1504                                           conn->param, ctx->ske)) {
1505         server->stat.auth_failures++;
1506         goto out;
1507       }
1508
1509       SILC_LOG_DEBUG(("Remote host is client"));
1510       SILC_LOG_INFO(("Connection %s (%s) is client", sock->hostname,
1511                      sock->ip));
1512
1513       /* Add the client to the client ID cache. The nickname and Client ID
1514          and other information is created after we have received NEW_CLIENT
1515          packet from client. */
1516       client = silc_idlist_add_client(server->local_list,
1517                                       NULL, NULL, NULL, NULL, NULL, sock, 0);
1518       if (!client) {
1519         SILC_LOG_ERROR(("Could not add new client to cache"));
1520         silc_free(sock->user_data);
1521         silc_server_disconnect_remote(server, sock, 
1522                                       SILC_STATUS_ERR_AUTH_FAILED, NULL);
1523         server->stat.auth_failures++;
1524         goto out;
1525       }
1526
1527       /* Statistics */
1528       server->stat.my_clients++;
1529       server->stat.clients++;
1530       server->stat.cell_clients++;
1531
1532       /* Get connection parameters */
1533       if (conn->param) {
1534         if (conn->param->keepalive_secs)
1535           hearbeat_timeout = conn->param->keepalive_secs;
1536       }
1537
1538       id_entry = (void *)client;
1539       break;
1540     }
1541   case SILC_SOCKET_TYPE_SERVER:
1542   case SILC_SOCKET_TYPE_ROUTER:
1543     {
1544       SilcServerEntry new_server;
1545       bool initiator = FALSE;
1546       bool backup_local = FALSE;
1547       bool backup_router = FALSE;
1548       char *backup_replace_ip = NULL;
1549       SilcUInt16 backup_replace_port = 0;
1550       SilcServerConfigServer *sconn = ctx->sconfig.ref_ptr;
1551       SilcServerConfigRouter *rconn = ctx->rconfig.ref_ptr;
1552
1553       if (ctx->conn_type == SILC_SOCKET_TYPE_ROUTER) {
1554         /* Verify whether this connection is after all allowed to connect */
1555         if (!silc_server_connection_allowed(server, sock, ctx->conn_type,
1556                                             &server->config->param,
1557                                             rconn ? rconn->param : NULL,
1558                                             ctx->ske)) {
1559           server->stat.auth_failures++;
1560           goto out;
1561         }
1562
1563         if (rconn) {
1564           if (rconn->param) {
1565             if (rconn->param->keepalive_secs)
1566               hearbeat_timeout = rconn->param->keepalive_secs;
1567           }
1568
1569           initiator = rconn->initiator;
1570           backup_local = rconn->backup_local;
1571           backup_router = rconn->backup_router;
1572           backup_replace_ip = rconn->backup_replace_ip;
1573           backup_replace_port = rconn->backup_replace_port;
1574         }
1575       }
1576
1577       if (ctx->conn_type == SILC_SOCKET_TYPE_SERVER) {
1578         /* Verify whether this connection is after all allowed to connect */
1579         if (!silc_server_connection_allowed(server, sock, ctx->conn_type,
1580                                             &server->config->param,
1581                                             sconn ? sconn->param : NULL,
1582                                             ctx->ske)) {
1583           server->stat.auth_failures++;
1584           goto out;
1585         }
1586         if (sconn) {
1587           if (sconn->param) {
1588             if (sconn->param->keepalive_secs)
1589               hearbeat_timeout = sconn->param->keepalive_secs;
1590           }
1591
1592           backup_router = sconn->backup_router;
1593         }
1594       }
1595
1596       SILC_LOG_DEBUG(("Remote host is %s",
1597                       ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1598                       "server" : (backup_router ?
1599                                   "backup router" : "router")));
1600       SILC_LOG_INFO(("Connection %s (%s) is %s", sock->hostname,
1601                      sock->ip, ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1602                      "server" : (backup_router ?
1603                                  "backup router" : "router")));
1604
1605       /* Add the server into server cache. The server name and Server ID
1606          is updated after we have received NEW_SERVER packet from the
1607          server. We mark ourselves as router for this server if we really
1608          are router. */
1609       new_server =
1610         silc_idlist_add_server((ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1611                                 server->local_list : (backup_router ?
1612                                                       server->local_list :
1613                                                       server->global_list)),
1614                                NULL,
1615                                (ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1616                                 SILC_SERVER : SILC_ROUTER),
1617                                NULL,
1618                                (ctx->conn_type == SILC_SOCKET_TYPE_SERVER ?
1619                                 server->id_entry : (backup_router ?
1620                                                     server->id_entry : NULL)),
1621                                sock);
1622       if (!new_server) {
1623         SILC_LOG_ERROR(("Could not add new server to cache"));
1624         silc_free(sock->user_data);
1625         silc_server_disconnect_remote(server, sock, 
1626                                       SILC_STATUS_ERR_AUTH_FAILED, NULL);
1627         server->stat.auth_failures++;
1628         goto out;
1629       }
1630
1631       /* Statistics */
1632       if (ctx->conn_type == SILC_SOCKET_TYPE_SERVER) {
1633         server->stat.my_servers++;
1634       } else {
1635         server->stat.my_routers++;
1636         server->stat.routers++;
1637       }
1638       server->stat.servers++;
1639
1640       id_entry = (void *)new_server;
1641
1642       /* If the incoming connection is router and marked as backup router
1643          then add it to be one of our backups */
1644       if (ctx->conn_type == SILC_SOCKET_TYPE_ROUTER && backup_router) {
1645         silc_server_backup_add(server, new_server, backup_replace_ip,
1646                                backup_replace_port, backup_local);
1647
1648         /* Change it back to SERVER type since that's what it really is. */
1649         if (backup_local)
1650           ctx->conn_type = SILC_SOCKET_TYPE_SERVER;
1651
1652         new_server->server_type = SILC_BACKUP_ROUTER;
1653       }
1654
1655       /* Check whether this connection is to be our primary router connection
1656          if we do not already have the primary route. */
1657       if (!backup_router &&
1658           server->standalone && ctx->conn_type == SILC_SOCKET_TYPE_ROUTER) {
1659         if (silc_server_config_is_primary_route(server) && !initiator)
1660           break;
1661
1662         SILC_LOG_DEBUG(("We are not standalone server anymore"));
1663         server->standalone = FALSE;
1664         if (!server->id_entry->router) {
1665           server->id_entry->router = id_entry;
1666           server->router = id_entry;
1667         }
1668       }
1669
1670       break;
1671     }
1672   default:
1673     goto out;
1674     break;
1675   }
1676
1677   sock->type = ctx->conn_type;
1678
1679   /* Add the common data structure to the ID entry. */
1680   silc_idlist_add_data(id_entry, (SilcIDListData)sock->user_data);
1681
1682   /* Add to sockets internal pointer for fast referencing */
1683   silc_free(sock->user_data);
1684   sock->user_data = id_entry;
1685
1686   /* Connection has been fully established now. Everything is ok. */
1687   SILC_LOG_DEBUG(("New connection authenticated"));
1688
1689   /* Perform keepalive. The `hb_context' will be freed automatically
1690      when finally calling the silc_socket_free function. */
1691   hb_context = silc_calloc(1, sizeof(*hb_context));
1692   hb_context->server = server;
1693   silc_socket_set_heartbeat(sock, hearbeat_timeout, hb_context,
1694                             silc_server_perform_heartbeat,
1695                             server->schedule);
1696
1697  out:
1698   silc_schedule_task_del_by_callback(server->schedule,
1699                                      silc_server_failure_callback);
1700   silc_protocol_free(protocol);
1701   if (ctx->packet)
1702     silc_packet_context_free(ctx->packet);
1703   if (ctx->ske)
1704     silc_ske_free(ctx->ske);
1705   silc_free(ctx->dest_id);
1706   silc_server_config_unref(&ctx->cconfig);
1707   silc_server_config_unref(&ctx->sconfig);
1708   silc_server_config_unref(&ctx->rconfig);
1709   silc_free(ctx);
1710   sock->protocol = NULL;
1711 }
1712
1713 /* This function is used to read packets from network and send packets to
1714    network. This is usually a generic task. */
1715
1716 SILC_TASK_CALLBACK(silc_server_packet_process)
1717 {
1718   SilcServer server = (SilcServer)context;
1719   SilcSocketConnection sock = server->sockets[fd];
1720   SilcIDListData idata;
1721   SilcCipher cipher = NULL;
1722   SilcHmac hmac = NULL;
1723   SilcUInt32 sequence = 0;
1724   int ret;
1725
1726   if (!sock)
1727     return;
1728
1729   SILC_LOG_DEBUG(("Processing packet"));
1730
1731   /* Packet sending */
1732
1733   if (type == SILC_TASK_WRITE) {
1734     /* Do not send data to disconnected connection */
1735     if (SILC_IS_DISCONNECTED(sock))
1736       return;
1737
1738     server->stat.packets_sent++;
1739
1740     /* Send the packet */
1741     ret = silc_packet_send(sock, TRUE);
1742
1743     /* If returned -2 could not write to connection now, will do
1744        it later. */
1745     if (ret == -2)
1746       return;
1747
1748     if (ret == -1) {
1749       SILC_LOG_ERROR(("Error sending packet to connection "
1750                       "%s:%d [%s]", sock->hostname, sock->port,
1751                       (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
1752                        sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
1753                        sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
1754                        "Router")));
1755       return;
1756     }
1757
1758     /* The packet has been sent and now it is time to set the connection
1759        back to only for input. When there is again some outgoing data
1760        available for this connection it will be set for output as well.
1761        This call clears the output setting and sets it only for input. */
1762     SILC_SET_CONNECTION_FOR_INPUT(server->schedule, fd);
1763     SILC_UNSET_OUTBUF_PENDING(sock);
1764
1765     silc_buffer_clear(sock->outbuf);
1766     return;
1767   }
1768
1769   /* Packet receiving */
1770
1771   /* Read some data from connection */
1772   ret = silc_packet_receive(sock);
1773   if (ret < 0) {
1774
1775     if (ret == -1)
1776       SILC_LOG_ERROR(("Error receiving packet from connection "
1777                       "%s:%d [%s] %s", sock->hostname, sock->port,
1778                       (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
1779                        sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
1780                        sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
1781                        "Router"), strerror(errno)));
1782     return;
1783   }
1784
1785   /* EOF */
1786   if (ret == 0) {
1787     SILC_LOG_DEBUG(("Read EOF"));
1788
1789     /* If connection is disconnecting already we will finally
1790        close the connection */
1791     if (SILC_IS_DISCONNECTING(sock)) {
1792       if (sock->user_data)
1793         silc_server_free_sock_user_data(server, sock, NULL);
1794       silc_server_close_connection(server, sock);
1795       return;
1796     }
1797
1798     SILC_LOG_DEBUG(("Premature EOF from connection %d", sock->sock));
1799     SILC_SET_DISCONNECTING(sock);
1800
1801     if (sock->user_data) {
1802       char tmp[128];
1803       if (silc_socket_get_error(sock, tmp, sizeof(tmp) - 1))
1804         silc_server_free_sock_user_data(server, sock, tmp);
1805       else
1806         silc_server_free_sock_user_data(server, sock, NULL);
1807     } else if (server->router_conn && server->router_conn->sock == sock &&
1808              !server->router && server->standalone)
1809       silc_schedule_task_add(server->schedule, 0,
1810                              silc_server_connect_to_router,
1811                              server, 1, 0,
1812                              SILC_TASK_TIMEOUT,
1813                              SILC_TASK_PRI_NORMAL);
1814
1815     silc_server_close_connection(server, sock);
1816     return;
1817   }
1818
1819   /* If connection is disconnecting or disconnected we will ignore
1820      what we read. */
1821   if (SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock)) {
1822     SILC_LOG_DEBUG(("Ignoring read data from disconnected connection"));
1823     return;
1824   }
1825
1826   server->stat.packets_received++;
1827
1828   /* Get keys and stuff from ID entry */
1829   idata = (SilcIDListData)sock->user_data;
1830   if (idata) {
1831     cipher = idata->receive_key;
1832     hmac = idata->hmac_receive;
1833     sequence = idata->psn_receive;
1834   }
1835
1836   /* Process the packet. This will call the parser that will then
1837      decrypt and parse the packet. */
1838   ret = silc_packet_receive_process(sock, server->server_type == SILC_ROUTER ?
1839                                     TRUE : FALSE, cipher, hmac, sequence,
1840                                     silc_server_packet_parse, server);
1841
1842   /* If this socket connection is not authenticated yet and the packet
1843      processing failed we will drop the connection since it can be
1844      a malicious flooder. */
1845   if (sock->type == SILC_SOCKET_TYPE_UNKNOWN && ret == FALSE &&
1846       (!sock->protocol || sock->protocol->protocol->type ==
1847        SILC_PROTOCOL_SERVER_KEY_EXCHANGE)) {
1848     SILC_LOG_DEBUG(("Bad data sent from unknown connection %d", sock->sock));
1849     SILC_SET_DISCONNECTING(sock);
1850
1851     if (sock->user_data)
1852       silc_server_free_sock_user_data(server, sock, NULL);
1853     silc_server_close_connection(server, sock);
1854   }
1855 }
1856
1857 /* Parses whole packet, received earlier. */
1858
1859 SILC_TASK_CALLBACK(silc_server_packet_parse_real)
1860 {
1861   SilcPacketParserContext *parse_ctx = (SilcPacketParserContext *)context;
1862   SilcServer server = (SilcServer)parse_ctx->context;
1863   SilcSocketConnection sock = parse_ctx->sock;
1864   SilcPacketContext *packet = parse_ctx->packet;
1865   SilcIDListData idata = (SilcIDListData)sock->user_data;
1866   int ret;
1867
1868   SILC_LOG_DEBUG(("Start"));
1869
1870   /* Parse the packet */
1871   if (parse_ctx->normal)
1872     ret = silc_packet_parse(packet, idata ? idata->receive_key : NULL);
1873   else
1874     ret = silc_packet_parse_special(packet, idata ? idata->receive_key : NULL);
1875
1876   /* If entry is disabled ignore what we got. */
1877   if (ret != SILC_PACKET_RESUME_ROUTER &&
1878       idata && idata->status & SILC_IDLIST_STATUS_DISABLED) {
1879     SILC_LOG_DEBUG(("Connection is disabled"));
1880     goto out;
1881   }
1882
1883   if (ret == SILC_PACKET_NONE)
1884     goto out;
1885
1886   /* Check that the the current client ID is same as in the client's packet. */
1887   if (sock->type == SILC_SOCKET_TYPE_CLIENT) {
1888     SilcClientEntry client = (SilcClientEntry)sock->user_data;
1889     if (client && client->id) {
1890       void *id = silc_id_str2id(packet->src_id, packet->src_id_len,
1891                                 packet->src_id_type);
1892       if (!id || !SILC_ID_CLIENT_COMPARE(client->id, id)) {
1893         silc_free(id);
1894         goto out;
1895       }
1896       silc_free(id);
1897     }
1898   }
1899
1900   if (server->server_type == SILC_ROUTER) {
1901     /* Route the packet if it is not destined to us. Other ID types but
1902        server are handled separately after processing them. */
1903     if (!(packet->flags & SILC_PACKET_FLAG_BROADCAST) &&
1904         packet->dst_id_type == SILC_ID_SERVER &&
1905         sock->type != SILC_SOCKET_TYPE_CLIENT &&
1906         memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1907
1908       /* Route the packet to fastest route for the destination ID */
1909       void *id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1910                                 packet->dst_id_type);
1911       if (!id)
1912         goto out;
1913       silc_server_packet_route(server,
1914                                silc_server_route_get(server, id,
1915                                                      packet->dst_id_type),
1916                                packet);
1917       silc_free(id);
1918       goto out;
1919     }
1920   }
1921
1922   /* Parse the incoming packet type */
1923   silc_server_packet_parse_type(server, sock, packet);
1924
1925   if (server->server_type == SILC_ROUTER) {
1926     /* Broadcast packet if it is marked as broadcast packet and it is
1927        originated from router and we are router. */
1928     if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1929         packet->flags & SILC_PACKET_FLAG_BROADCAST &&
1930         !server->standalone) {
1931       /* Broadcast to our primary route */
1932       silc_server_packet_broadcast(server, server->router->connection, packet);
1933
1934       /* If we have backup routers then we need to feed all broadcast
1935          data to those servers. */
1936       silc_server_backup_broadcast(server, sock, packet);
1937     }
1938   }
1939
1940  out:
1941   silc_packet_context_free(packet);
1942   silc_free(parse_ctx);
1943 }
1944
1945 /* Parser callback called by silc_packet_receive_process. This merely
1946    registers timeout that will handle the actual parsing when appropriate. */
1947
1948 bool silc_server_packet_parse(SilcPacketParserContext *parser_context,
1949                               void *context)
1950 {
1951   SilcServer server = (SilcServer)context;
1952   SilcSocketConnection sock = parser_context->sock;
1953   SilcIDListData idata = (SilcIDListData)sock->user_data;
1954
1955   if (idata)
1956     idata->psn_receive = parser_context->packet->sequence + 1;
1957
1958   /* If protocol for this connection is key exchange or rekey then we'll
1959      process all packets synchronously, since there might be packets in
1960      queue that we are not able to decrypt without first processing the
1961      packets before them. */
1962   if ((parser_context->packet->type == SILC_PACKET_REKEY ||
1963        parser_context->packet->type == SILC_PACKET_REKEY_DONE) ||
1964       (sock->protocol && sock->protocol->protocol &&
1965        (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
1966         sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY))) {
1967     silc_server_packet_parse_real(server->schedule, 0, sock->sock,
1968                                   parser_context);
1969
1970     /* Reprocess data since we'll return FALSE here.  This is because
1971        the idata->receive_key might have become valid in the last packet
1972        and we want to call this processor with valid cipher. */
1973     if (idata)
1974       silc_packet_receive_process(sock, server->server_type == SILC_ROUTER ?
1975                                   TRUE : FALSE, idata->receive_key,
1976                                   idata->hmac_receive, idata->psn_receive,
1977                                   silc_server_packet_parse, server);
1978     else
1979       silc_packet_receive_process(sock, server->server_type == SILC_ROUTER ?
1980                                   TRUE : FALSE, NULL, NULL, 0,
1981                                   silc_server_packet_parse, server);
1982     return FALSE;
1983   }
1984
1985   switch (sock->type) {
1986   case SILC_SOCKET_TYPE_UNKNOWN:
1987   case SILC_SOCKET_TYPE_CLIENT:
1988     /* Parse the packet with timeout */
1989     silc_schedule_task_add(server->schedule, sock->sock,
1990                            silc_server_packet_parse_real,
1991                            (void *)parser_context, 0, 100000,
1992                            SILC_TASK_TIMEOUT,
1993                            SILC_TASK_PRI_NORMAL);
1994     break;
1995   case SILC_SOCKET_TYPE_SERVER:
1996   case SILC_SOCKET_TYPE_ROUTER:
1997     /* Packets from servers are parsed immediately */
1998     silc_server_packet_parse_real(server->schedule, 0, sock->sock,
1999                                   parser_context);
2000     break;
2001   default:
2002     return TRUE;
2003   }
2004
2005   return TRUE;
2006 }
2007
2008 /* Parses the packet type and calls what ever routines the packet type
2009    requires. This is done for all incoming packets. */
2010
2011 void silc_server_packet_parse_type(SilcServer server,
2012                                    SilcSocketConnection sock,
2013                                    SilcPacketContext *packet)
2014 {
2015   SilcPacketType type = packet->type;
2016   SilcIDListData idata = (SilcIDListData)sock->user_data;
2017
2018   SILC_LOG_DEBUG(("Parsing packet type %d", type));
2019
2020   /* Parse the packet type */
2021   switch (type) {
2022   case SILC_PACKET_DISCONNECT:
2023     {
2024       SilcStatus status;
2025       char *message = NULL;
2026
2027       SILC_LOG_DEBUG(("Disconnect packet"));
2028
2029       if (packet->flags & SILC_PACKET_FLAG_LIST)
2030         break;
2031       if (packet->buffer->len < 1)
2032         break;
2033
2034       status = (SilcStatus)packet->buffer->data[0];
2035       if (packet->buffer->len > 1 &&
2036           silc_utf8_valid(packet->buffer->data + 1, packet->buffer->len - 1))
2037         message = silc_memdup(packet->buffer->data, packet->buffer->len);
2038
2039       SILC_LOG_ERROR(("Disconnected by %s (%s): %s (%d) %s", 
2040                       sock->ip, sock->hostname,
2041                       silc_get_status_message(status), status,
2042                       message ? message : ""));
2043       silc_free(message);
2044     }
2045     break;
2046
2047   case SILC_PACKET_SUCCESS:
2048     /*
2049      * Success received for something. For now we can have only
2050      * one protocol for connection executing at once hence this
2051      * success message is for whatever protocol is executing currently.
2052      */
2053     SILC_LOG_DEBUG(("Success packet"));
2054     if (packet->flags & SILC_PACKET_FLAG_LIST)
2055       break;
2056     if (sock->protocol)
2057       silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2058     break;
2059
2060   case SILC_PACKET_FAILURE:
2061     /*
2062      * Failure received for something. For now we can have only
2063      * one protocol for connection executing at once hence this
2064      * failure message is for whatever protocol is executing currently.
2065      */
2066     SILC_LOG_DEBUG(("Failure packet"));
2067     if (packet->flags & SILC_PACKET_FLAG_LIST)
2068       break;
2069     if (sock->protocol) {
2070       SilcServerFailureContext f;
2071       f = silc_calloc(1, sizeof(*f));
2072       f->server = server;
2073       f->sock = sock;
2074
2075       /* We will wait 5 seconds to process this failure packet */
2076       silc_schedule_task_add(server->schedule, sock->sock,
2077                          silc_server_failure_callback, (void *)f, 5, 0,
2078                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
2079     }
2080     break;
2081
2082   case SILC_PACKET_REJECT:
2083     SILC_LOG_DEBUG(("Reject packet"));
2084     if (packet->flags & SILC_PACKET_FLAG_LIST)
2085       break;
2086     return;
2087     break;
2088
2089   case SILC_PACKET_NOTIFY:
2090     /*
2091      * Received notify packet. Server can receive notify packets from
2092      * router. Server then relays the notify messages to clients if needed.
2093      */
2094     SILC_LOG_DEBUG(("Notify packet"));
2095     if (packet->flags & SILC_PACKET_FLAG_LIST)
2096       silc_server_notify_list(server, sock, packet);
2097     else
2098       silc_server_notify(server, sock, packet);
2099     break;
2100
2101     /*
2102      * Channel packets
2103      */
2104   case SILC_PACKET_CHANNEL_MESSAGE:
2105     /*
2106      * Received channel message. Channel messages are special packets
2107      * (although probably most common ones) thus they are handled
2108      * specially.
2109      */
2110     SILC_LOG_DEBUG(("Channel Message packet"));
2111     if (packet->flags & SILC_PACKET_FLAG_LIST)
2112       break;
2113     idata->last_receive = time(NULL);
2114     silc_server_channel_message(server, sock, packet);
2115     break;
2116
2117   case SILC_PACKET_CHANNEL_KEY:
2118     /*
2119      * Received key for channel. As channels are created by the router
2120      * the keys are as well. We will distribute the key to all of our
2121      * locally connected clients on the particular channel. Router
2122      * never receives this channel and thus is ignored.
2123      */
2124     SILC_LOG_DEBUG(("Channel Key packet"));
2125     if (packet->flags & SILC_PACKET_FLAG_LIST)
2126       break;
2127     silc_server_channel_key(server, sock, packet);
2128     break;
2129
2130     /*
2131      * Command packets
2132      */
2133   case SILC_PACKET_COMMAND:
2134     /*
2135      * Recived command. Processes the command request and allocates the
2136      * command context and calls the command.
2137      */
2138     SILC_LOG_DEBUG(("Command packet"));
2139     if (packet->flags & SILC_PACKET_FLAG_LIST)
2140       break;
2141     silc_server_command_process(server, sock, packet);
2142     break;
2143
2144   case SILC_PACKET_COMMAND_REPLY:
2145     /*
2146      * Received command reply packet. Received command reply to command. It
2147      * may be reply to command sent by us or reply to command sent by client
2148      * that we've routed further.
2149      */
2150     SILC_LOG_DEBUG(("Command Reply packet"));
2151     if (packet->flags & SILC_PACKET_FLAG_LIST)
2152       break;
2153     silc_server_command_reply(server, sock, packet);
2154     break;
2155
2156     /*
2157      * Private Message packets
2158      */
2159   case SILC_PACKET_PRIVATE_MESSAGE:
2160     /*
2161      * Received private message packet. The packet is coming from either
2162      * client or server.
2163      */
2164     SILC_LOG_DEBUG(("Private Message packet"));
2165     if (packet->flags & SILC_PACKET_FLAG_LIST)
2166       break;
2167     idata->last_receive = time(NULL);
2168     silc_server_private_message(server, sock, packet);
2169     break;
2170
2171   case SILC_PACKET_PRIVATE_MESSAGE_KEY:
2172     /*
2173      * Private message key packet.
2174      */
2175     if (packet->flags & SILC_PACKET_FLAG_LIST)
2176       break;
2177     silc_server_private_message_key(server, sock, packet);
2178     break;
2179
2180     /*
2181      * Key Exchange protocol packets
2182      */
2183   case SILC_PACKET_KEY_EXCHANGE:
2184     SILC_LOG_DEBUG(("KE packet"));
2185     if (packet->flags & SILC_PACKET_FLAG_LIST)
2186       break;
2187
2188     if (sock->protocol && sock->protocol->protocol &&
2189         sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE) {
2190       SilcServerKEInternalContext *proto_ctx =
2191         (SilcServerKEInternalContext *)sock->protocol->context;
2192
2193       proto_ctx->packet = silc_packet_context_dup(packet);
2194
2195       /* Let the protocol handle the packet */
2196       silc_protocol_execute(sock->protocol, server->schedule, 0, 100000);
2197     } else {
2198       SILC_LOG_ERROR(("Received Key Exchange packet but no key exchange "
2199                       "protocol active, packet dropped."));
2200     }
2201     break;
2202
2203   case SILC_PACKET_KEY_EXCHANGE_1:
2204     SILC_LOG_DEBUG(("KE 1 packet"));
2205     if (packet->flags & SILC_PACKET_FLAG_LIST)
2206       break;
2207
2208     if (sock->protocol && sock->protocol->protocol &&
2209         (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
2210          sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
2211
2212       if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2213         SilcServerRekeyInternalContext *proto_ctx =
2214           (SilcServerRekeyInternalContext *)sock->protocol->context;
2215
2216         if (proto_ctx->packet)
2217           silc_packet_context_free(proto_ctx->packet);
2218
2219         proto_ctx->packet = silc_packet_context_dup(packet);
2220
2221         /* Let the protocol handle the packet */
2222         silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2223       } else {
2224         SilcServerKEInternalContext *proto_ctx =
2225           (SilcServerKEInternalContext *)sock->protocol->context;
2226
2227         if (proto_ctx->packet)
2228           silc_packet_context_free(proto_ctx->packet);
2229
2230         proto_ctx->packet = silc_packet_context_dup(packet);
2231         proto_ctx->dest_id_type = packet->src_id_type;
2232         proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2233                                             packet->src_id_type);
2234         if (!proto_ctx->dest_id)
2235           break;
2236
2237         /* Let the protocol handle the packet */
2238         silc_protocol_execute(sock->protocol, server->schedule,
2239                               0, 100000);
2240       }
2241     } else {
2242       SILC_LOG_ERROR(("Received Key Exchange 1 packet but no key exchange "
2243                       "protocol active, packet dropped."));
2244     }
2245     break;
2246
2247   case SILC_PACKET_KEY_EXCHANGE_2:
2248     SILC_LOG_DEBUG(("KE 2 packet"));
2249     if (packet->flags & SILC_PACKET_FLAG_LIST)
2250       break;
2251
2252     if (sock->protocol && sock->protocol->protocol &&
2253         (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_KEY_EXCHANGE ||
2254          sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)) {
2255
2256       if (sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2257         SilcServerRekeyInternalContext *proto_ctx =
2258           (SilcServerRekeyInternalContext *)sock->protocol->context;
2259
2260         if (proto_ctx->packet)
2261           silc_packet_context_free(proto_ctx->packet);
2262
2263         proto_ctx->packet = silc_packet_context_dup(packet);
2264
2265         /* Let the protocol handle the packet */
2266         silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2267       } else {
2268         SilcServerKEInternalContext *proto_ctx =
2269           (SilcServerKEInternalContext *)sock->protocol->context;
2270
2271         if (proto_ctx->packet)
2272           silc_packet_context_free(proto_ctx->packet);
2273
2274         proto_ctx->packet = silc_packet_context_dup(packet);
2275         proto_ctx->dest_id_type = packet->src_id_type;
2276         proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_len,
2277                                             packet->src_id_type);
2278         if (!proto_ctx->dest_id)
2279           break;
2280
2281         /* Let the protocol handle the packet */
2282         silc_protocol_execute(sock->protocol, server->schedule,
2283                               0, 100000);
2284       }
2285     } else {
2286       SILC_LOG_ERROR(("Received Key Exchange 2 packet but no key exchange "
2287                       "protocol active, packet dropped."));
2288     }
2289     break;
2290
2291   case SILC_PACKET_CONNECTION_AUTH_REQUEST:
2292     /*
2293      * Connection authentication request packet. When we receive this packet
2294      * we will send to the other end information about our mandatory
2295      * authentication method for the connection. This packet maybe received
2296      * at any time.
2297      */
2298     SILC_LOG_DEBUG(("Connection authentication request packet"));
2299     if (packet->flags & SILC_PACKET_FLAG_LIST)
2300       break;
2301     silc_server_connection_auth_request(server, sock, packet);
2302     break;
2303
2304     /*
2305      * Connection Authentication protocol packets
2306      */
2307   case SILC_PACKET_CONNECTION_AUTH:
2308     /* Start of the authentication protocol. We receive here the
2309        authentication data and will verify it. */
2310     SILC_LOG_DEBUG(("Connection auth packet"));
2311     if (packet->flags & SILC_PACKET_FLAG_LIST)
2312       break;
2313
2314     if (sock->protocol && sock->protocol->protocol->type
2315         == SILC_PROTOCOL_SERVER_CONNECTION_AUTH) {
2316
2317       SilcServerConnAuthInternalContext *proto_ctx =
2318         (SilcServerConnAuthInternalContext *)sock->protocol->context;
2319
2320       proto_ctx->packet = silc_packet_context_dup(packet);
2321
2322       /* Let the protocol handle the packet */
2323       silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2324     } else {
2325       SILC_LOG_ERROR(("Received Connection Auth packet but no authentication "
2326                       "protocol active, packet dropped."));
2327     }
2328     break;
2329
2330   case SILC_PACKET_NEW_ID:
2331     /*
2332      * Received New ID packet. This includes some new ID that has been
2333      * created. It may be for client, server or channel. This is the way
2334      * to distribute information about new registered entities in the
2335      * SILC network.
2336      */
2337     SILC_LOG_DEBUG(("New ID packet"));
2338     if (packet->flags & SILC_PACKET_FLAG_LIST)
2339       silc_server_new_id_list(server, sock, packet);
2340     else
2341       silc_server_new_id(server, sock, packet);
2342     break;
2343
2344   case SILC_PACKET_NEW_CLIENT:
2345     /*
2346      * Received new client packet. This includes client information that
2347      * we will use to create initial client ID. After creating new
2348      * ID we will send it to the client.
2349      */
2350     SILC_LOG_DEBUG(("New Client packet"));
2351     if (packet->flags & SILC_PACKET_FLAG_LIST)
2352       break;
2353     silc_server_new_client(server, sock, packet);
2354     break;
2355
2356   case SILC_PACKET_NEW_SERVER:
2357     /*
2358      * Received new server packet. This includes Server ID and some other
2359      * information that we may save. This is received after server has
2360      * connected to us.
2361      */
2362     SILC_LOG_DEBUG(("New Server packet"));
2363     if (packet->flags & SILC_PACKET_FLAG_LIST)
2364       break;
2365     silc_server_new_server(server, sock, packet);
2366     break;
2367
2368   case SILC_PACKET_NEW_CHANNEL:
2369     /*
2370      * Received new channel packet. Information about new channel in the
2371      * network are distributed using this packet.
2372      */
2373     SILC_LOG_DEBUG(("New Channel packet"));
2374     if (packet->flags & SILC_PACKET_FLAG_LIST)
2375       silc_server_new_channel_list(server, sock, packet);
2376     else
2377       silc_server_new_channel(server, sock, packet);
2378     break;
2379
2380   case SILC_PACKET_HEARTBEAT:
2381     /*
2382      * Received heartbeat.
2383      */
2384     SILC_LOG_DEBUG(("Heartbeat packet"));
2385     if (packet->flags & SILC_PACKET_FLAG_LIST)
2386       break;
2387     break;
2388
2389   case SILC_PACKET_KEY_AGREEMENT:
2390     /*
2391      * Received heartbeat.
2392      */
2393     SILC_LOG_DEBUG(("Key agreement packet"));
2394     if (packet->flags & SILC_PACKET_FLAG_LIST)
2395       break;
2396     silc_server_key_agreement(server, sock, packet);
2397     break;
2398
2399   case SILC_PACKET_REKEY:
2400     /*
2401      * Received re-key packet. The sender wants to regenerate the session
2402      * keys.
2403      */
2404     SILC_LOG_DEBUG(("Re-key packet"));
2405     if (packet->flags & SILC_PACKET_FLAG_LIST)
2406       break;
2407     silc_server_rekey(server, sock, packet);
2408     break;
2409
2410   case SILC_PACKET_REKEY_DONE:
2411     /*
2412      * The re-key is done.
2413      */
2414     SILC_LOG_DEBUG(("Re-key done packet"));
2415     if (packet->flags & SILC_PACKET_FLAG_LIST)
2416       break;
2417
2418     if (sock->protocol && sock->protocol->protocol &&
2419         sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY) {
2420
2421       SilcServerRekeyInternalContext *proto_ctx =
2422         (SilcServerRekeyInternalContext *)sock->protocol->context;
2423
2424       if (proto_ctx->packet)
2425         silc_packet_context_free(proto_ctx->packet);
2426
2427       proto_ctx->packet = silc_packet_context_dup(packet);
2428
2429       /* Let the protocol handle the packet */
2430       silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
2431     } else {
2432       SILC_LOG_ERROR(("Received Re-key done packet but no re-key "
2433                       "protocol active, packet dropped."));
2434     }
2435     break;
2436
2437   case SILC_PACKET_FTP:
2438     /* FTP packet */
2439     SILC_LOG_DEBUG(("FTP packet"));
2440     if (packet->flags & SILC_PACKET_FLAG_LIST)
2441       break;
2442     silc_server_ftp(server, sock, packet);
2443     break;
2444
2445   case SILC_PACKET_RESUME_CLIENT:
2446     /* Resume client */
2447     SILC_LOG_DEBUG(("Resume Client packet"));
2448     if (packet->flags & SILC_PACKET_FLAG_LIST)
2449       break;
2450     silc_server_resume_client(server, sock, packet);
2451     break;
2452
2453   case SILC_PACKET_RESUME_ROUTER:
2454     /* Resume router packet received. This packet is received for backup
2455        router resuming protocol. */
2456     SILC_LOG_DEBUG(("Resume router packet"));
2457     if (packet->flags & SILC_PACKET_FLAG_LIST)
2458       break;
2459     silc_server_backup_resume_router(server, sock, packet);
2460     break;
2461
2462   default:
2463     SILC_LOG_ERROR(("Incorrect packet type %d, packet dropped", type));
2464     break;
2465   }
2466
2467 }
2468
2469 /* Creates connection to a remote router. */
2470
2471 void silc_server_create_connection(SilcServer server,
2472                                    const char *remote_host, SilcUInt32 port)
2473 {
2474   SilcServerConnection sconn;
2475
2476   /* Allocate connection object for hold connection specific stuff. */
2477   sconn = silc_calloc(1, sizeof(*sconn));
2478   sconn->server = server;
2479   sconn->remote_host = strdup(remote_host);
2480   sconn->remote_port = port;
2481   sconn->no_reconnect = TRUE;
2482
2483   silc_schedule_task_add(server->schedule, 0,
2484                          silc_server_connect_router,
2485                          (void *)sconn, 0, 1, SILC_TASK_TIMEOUT,
2486                          SILC_TASK_PRI_NORMAL);
2487 }
2488
2489 SILC_TASK_CALLBACK(silc_server_close_connection_final)
2490 {
2491   silc_socket_free((SilcSocketConnection)context);
2492 }
2493
2494 /* Closes connection to socket connection */
2495
2496 void silc_server_close_connection(SilcServer server,
2497                                   SilcSocketConnection sock)
2498 {
2499   if (!server->sockets[sock->sock])
2500     return;
2501
2502   SILC_LOG_INFO(("Closing connection %s:%d [%s]", sock->hostname,
2503                   sock->port,
2504                   (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
2505                    sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
2506                    sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
2507                    "Router")));
2508
2509   /* We won't listen for this connection anymore */
2510   silc_schedule_unset_listen_fd(server->schedule, sock->sock);
2511
2512   /* Unregister all tasks */
2513   silc_schedule_task_del_by_fd(server->schedule, sock->sock);
2514
2515   /* Close the actual connection */
2516   silc_net_close_connection(sock->sock);
2517   server->sockets[sock->sock] = NULL;
2518
2519   /* If sock->user_data is NULL then we'll check for active protocols
2520      here since the silc_server_free_sock_user_data has not been called
2521      for this connection. */
2522   if (!sock->user_data) {
2523     /* If any protocol is active cancel its execution. It will call
2524        the final callback which will finalize the disconnection. */
2525     if (sock->protocol) {
2526       silc_protocol_cancel(sock->protocol, server->schedule);
2527       sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2528       silc_protocol_execute_final(sock->protocol, server->schedule);
2529       sock->protocol = NULL;
2530       return;
2531     }
2532   }
2533
2534   silc_schedule_task_add(server->schedule, 0,
2535                          silc_server_close_connection_final,
2536                          (void *)sock, 0, 1, SILC_TASK_TIMEOUT,
2537                          SILC_TASK_PRI_NORMAL);
2538 }
2539
2540 /* Sends disconnect message to remote connection and disconnects the
2541    connection. */
2542
2543 void silc_server_disconnect_remote(SilcServer server,
2544                                    SilcSocketConnection sock,
2545                                    SilcStatus status, ...)
2546 {
2547   va_list ap;
2548   unsigned char buf[512];
2549   SilcBuffer buffer;
2550   char *cp;
2551   int len;
2552
2553   if (!sock)
2554     return;
2555
2556   memset(buf, 0, sizeof(buf));
2557   va_start(ap, status);
2558   cp = va_arg(ap, char *);
2559   if (cp) {
2560     vsnprintf(buf, sizeof(buf) - 1, cp, ap);
2561     cp = buf;
2562   }
2563   va_end(ap);
2564
2565   SILC_LOG_DEBUG(("Disconnecting remote host"));
2566
2567   /* Notify remote end that the conversation is over. The notify message
2568      is tried to be sent immediately. */
2569
2570   len = 1;
2571   if (cp)
2572     len += silc_utf8_encoded_len(buf, strlen(buf), SILC_STRING_ASCII);
2573
2574   buffer = silc_buffer_alloc_size(len);
2575   if (!buffer)
2576     goto out;
2577
2578   buffer->data[0] = status;
2579   if (cp)
2580     silc_utf8_encode(buf, strlen(buf), SILC_STRING_ASCII, buffer->data + 1,
2581                      buffer->len - 1);
2582   silc_server_packet_send(server, sock, SILC_PACKET_DISCONNECT, 0,
2583                           buffer->data, buffer->len, TRUE);
2584   silc_buffer_free(buffer);
2585
2586  out:
2587   silc_server_packet_queue_purge(server, sock);
2588
2589   /* Mark the connection to be disconnected */
2590   SILC_SET_DISCONNECTED(sock);
2591   silc_server_close_connection(server, sock);
2592 }
2593
2594 typedef struct {
2595   SilcServer server;
2596   SilcClientEntry client;
2597 } *FreeClientInternal;
2598
2599 SILC_TASK_CALLBACK(silc_server_free_client_data_timeout)
2600 {
2601   FreeClientInternal i = (FreeClientInternal)context;
2602
2603   silc_idlist_del_data(i->client);
2604   silc_idcache_purge_by_context(i->server->local_list->clients, i->client);
2605   silc_free(i);
2606 }
2607
2608 /* Frees client data and notifies about client's signoff. */
2609
2610 void silc_server_free_client_data(SilcServer server,
2611                                   SilcSocketConnection sock,
2612                                   SilcClientEntry client,
2613                                   int notify,
2614                                   const char *signoff)
2615 {
2616   FreeClientInternal i = silc_calloc(1, sizeof(*i));
2617
2618   /* If there is pending outgoing data for the client then purge it
2619      to the network before removing the client entry. */
2620   silc_server_packet_queue_purge(server, sock);
2621
2622   if (client->id) {
2623     /* Check if anyone is watching this nickname */
2624     if (server->server_type == SILC_ROUTER)
2625       silc_server_check_watcher_list(server, client, NULL,
2626                                      SILC_NOTIFY_TYPE_SIGNOFF);
2627
2628     /* Send SIGNOFF notify to routers. */
2629     if (notify && !server->standalone && server->router)
2630       silc_server_send_notify_signoff(server, server->router->connection,
2631                                       server->server_type == SILC_SERVER ?
2632                                       FALSE : TRUE, client->id, signoff);
2633
2634     /* Remove client from all channels */
2635     if (notify)
2636       silc_server_remove_from_channels(server, NULL, client,
2637                                        TRUE, (char *)signoff, TRUE);
2638     else
2639       silc_server_remove_from_channels(server, NULL, client,
2640                                        FALSE, NULL, FALSE);
2641
2642     /* Remove this client from watcher list if it is */
2643     silc_server_del_from_watcher_list(server, client);
2644   }
2645
2646   /* Update statistics */
2647   server->stat.my_clients--;
2648   server->stat.clients--;
2649   if (server->stat.cell_clients)
2650     server->stat.cell_clients--;
2651   SILC_OPER_STATS_UPDATE(client, server, SILC_UMODE_SERVER_OPERATOR);
2652   SILC_OPER_STATS_UPDATE(client, router, SILC_UMODE_ROUTER_OPERATOR);
2653   silc_schedule_task_del_by_context(server->schedule, client);
2654
2655   /* We will not delete the client entry right away. We will take it
2656      into history (for WHOWAS command) for 5 minutes */
2657   i->server = server;
2658   i->client = client;
2659   silc_schedule_task_add(server->schedule, 0,
2660                          silc_server_free_client_data_timeout,
2661                          (void *)i, 300, 0,
2662                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
2663   client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
2664   client->mode = 0;
2665   client->router = NULL;
2666   client->connection = NULL;
2667 }
2668
2669 /* Frees user_data pointer from socket connection object. This also sends
2670    appropriate notify packets to the network to inform about leaving
2671    entities. */
2672
2673 void silc_server_free_sock_user_data(SilcServer server,
2674                                      SilcSocketConnection sock,
2675                                      const char *signoff_message)
2676 {
2677   SILC_LOG_DEBUG(("Start"));
2678
2679   switch (sock->type) {
2680   case SILC_SOCKET_TYPE_CLIENT:
2681     {
2682       SilcClientEntry user_data = (SilcClientEntry)sock->user_data;
2683       silc_server_free_client_data(server, sock, user_data, TRUE,
2684                                    signoff_message);
2685       break;
2686     }
2687   case SILC_SOCKET_TYPE_SERVER:
2688   case SILC_SOCKET_TYPE_ROUTER:
2689     {
2690       SilcServerEntry user_data = (SilcServerEntry)sock->user_data;
2691       SilcServerEntry backup_router = NULL;
2692
2693       if (user_data->id)
2694         backup_router = silc_server_backup_get(server, user_data->id);
2695
2696       /* If this was our primary router connection then we're lost to
2697          the outside world. */
2698       if (server->router == user_data) {
2699         /* Check whether we have a backup router connection */
2700         if (!backup_router || backup_router == user_data) {
2701           silc_schedule_task_add(server->schedule, 0,
2702                                  silc_server_connect_to_router,
2703                                  server, 1, 0,
2704                                  SILC_TASK_TIMEOUT,
2705                                  SILC_TASK_PRI_NORMAL);
2706
2707           server->id_entry->router = NULL;
2708           server->router = NULL;
2709           server->standalone = TRUE;
2710           backup_router = NULL;
2711         } else {
2712           SILC_LOG_INFO(("New primary router is backup router %s",
2713                          backup_router->server_name));
2714           SILC_LOG_DEBUG(("New primary router is backup router %s",
2715                           backup_router->server_name));
2716 #ifdef BACKUP_SINGLE_ROUTER
2717           if (server->id_entry != backup_router) {
2718 #endif /* BACKUP_SINGLE_ROUTER */
2719             server->id_entry->router = backup_router;
2720             server->router = backup_router;
2721             server->router_connect = time(0);
2722             server->backup_primary = TRUE;
2723 #ifdef BACKUP_SINGLE_ROUTER
2724           } else {
2725             server->id_entry->router = NULL;
2726             server->router = NULL;
2727             server->standalone = TRUE;
2728           }
2729 #endif /* BACKUP_SINGLE_ROUTER */
2730
2731           if (server->server_type == SILC_BACKUP_ROUTER) {
2732             server->server_type = SILC_ROUTER;
2733
2734             /* We'll need to constantly try to reconnect to the primary
2735                router so that we'll see when it comes back online. */
2736             silc_server_backup_reconnect(server, sock->ip, sock->port,
2737                                          silc_server_backup_connected,
2738                                          NULL);
2739           }
2740
2741           /* Mark this connection as replaced */
2742           silc_server_backup_replaced_add(server, user_data->id,
2743                                           backup_router);
2744         }
2745       } else if (backup_router) {
2746         SILC_LOG_INFO(("Enabling the use of backup router %s",
2747                        backup_router->server_name));
2748         SILC_LOG_DEBUG(("Enabling the use of backup router %s",
2749                         backup_router->server_name));
2750
2751         /* Mark this connection as replaced */
2752         silc_server_backup_replaced_add(server, user_data->id,
2753                                         backup_router);
2754       }
2755
2756       if (!backup_router) {
2757         /* Free all client entries that this server owns as they will
2758            become invalid now as well. */
2759         if (user_data->id)
2760           silc_server_remove_clients_by_server(server, user_data, TRUE);
2761         if (server->server_type == SILC_SERVER)
2762           silc_server_remove_channels_by_server(server, user_data);
2763       } else {
2764         /* Update the client entries of this server to the new backup
2765            router. This also removes the clients that *really* was owned
2766            by the primary router and went down with the router.  */
2767         silc_server_update_clients_by_server(server, user_data, backup_router,
2768                                              TRUE, TRUE);
2769         silc_server_update_servers_by_server(server, user_data, backup_router);
2770         if (server->server_type == SILC_SERVER)
2771           silc_server_update_channels_by_server(server, user_data,
2772                                                 backup_router);
2773       }
2774
2775       /* Free the server entry */
2776       silc_server_backup_del(server, user_data);
2777       silc_server_backup_replaced_del(server, user_data);
2778       silc_idlist_del_data(user_data);
2779       if (!silc_idlist_del_server(server->local_list, user_data))
2780         silc_idlist_del_server(server->global_list, user_data);
2781       if (sock->type == SILC_SOCKET_TYPE_SERVER) {
2782         server->stat.my_servers--;
2783       } else {
2784         server->stat.my_routers--;
2785         server->stat.routers--;
2786       }
2787       server->stat.servers--;
2788       if (server->server_type == SILC_ROUTER)
2789         server->stat.cell_servers--;
2790
2791       if (backup_router) {
2792         /* Announce all of our stuff that was created about 5 minutes ago.
2793            The backup router knows all the other stuff already. */
2794         if (server->server_type == SILC_ROUTER)
2795           silc_server_announce_servers(server, FALSE, time(0) - 300,
2796                                        backup_router->connection);
2797
2798         /* Announce our clients and channels to the router */
2799         silc_server_announce_clients(server, time(0) - 300,
2800                                      backup_router->connection);
2801         silc_server_announce_channels(server, time(0) - 300,
2802                                       backup_router->connection);
2803       }
2804       break;
2805     }
2806   default:
2807     {
2808       SilcUnknownEntry user_data = (SilcUnknownEntry)sock->user_data;
2809
2810       silc_idlist_del_data(user_data);
2811       silc_free(user_data);
2812       break;
2813     }
2814   }
2815
2816   /* If any protocol is active cancel its execution */
2817   if (sock->protocol) {
2818     silc_protocol_cancel(sock->protocol, server->schedule);
2819     sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
2820     silc_protocol_execute_final(sock->protocol, server->schedule);
2821     sock->protocol = NULL;
2822   }
2823
2824   sock->user_data = NULL;
2825 }
2826
2827 /* Removes client from all channels it has joined. This is used when client
2828    connection is disconnected. If the client on a channel is last, the
2829    channel is removed as well. This sends the SIGNOFF notify types. */
2830
2831 void silc_server_remove_from_channels(SilcServer server,
2832                                       SilcSocketConnection sock,
2833                                       SilcClientEntry client,
2834                                       bool notify,
2835                                       const char *signoff_message,
2836                                       bool keygen)
2837 {
2838   SilcChannelEntry channel;
2839   SilcChannelClientEntry chl;
2840   SilcHashTableList htl;
2841   SilcBuffer clidp;
2842
2843   SILC_LOG_DEBUG(("Start"));
2844
2845   if (!client || !client->id)
2846     return;
2847
2848   clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2849   if (!clidp)
2850     notify = FALSE;
2851
2852   /* Remove the client from all channels. The client is removed from
2853      the channels' user list. */
2854   silc_hash_table_list(client->channels, &htl);
2855   while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
2856     channel = chl->channel;
2857
2858     /* Remove channel if this is last client leaving the channel, unless
2859        the channel is permanent. */
2860     if (server->server_type == SILC_ROUTER &&
2861         silc_hash_table_count(channel->user_list) < 2) {
2862       silc_server_channel_delete(server, channel);
2863       continue;
2864     }
2865
2866     silc_hash_table_del(client->channels, channel);
2867     silc_hash_table_del(channel->user_list, chl->client);
2868     channel->user_count--;
2869
2870     /* If there is no global users on the channel anymore mark the channel
2871        as local channel. Do not check if the removed client is local client. */
2872     if (server->server_type != SILC_ROUTER && channel->global_users &&
2873         chl->client->router && !silc_server_channel_has_global(channel))
2874       channel->global_users = FALSE;
2875
2876     silc_free(chl);
2877
2878     /* Update statistics */
2879     if (client->connection)
2880       server->stat.my_chanclients--;
2881     if (server->server_type == SILC_ROUTER) {
2882       server->stat.cell_chanclients--;
2883       server->stat.chanclients--;
2884     }
2885
2886     /* If there is not at least one local user on the channel then we don't
2887        need the channel entry anymore, we can remove it safely, unless the
2888        channel is permanent channel */
2889     if (server->server_type != SILC_ROUTER &&
2890         !silc_server_channel_has_local(channel)) {
2891       /* Notify about leaving client if this channel has global users. */
2892       if (notify && channel->global_users)
2893         silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2894                                            SILC_NOTIFY_TYPE_SIGNOFF,
2895                                            signoff_message ? 2 : 1,
2896                                            clidp->data, clidp->len,
2897                                            signoff_message, signoff_message ?
2898                                            strlen(signoff_message) : 0);
2899
2900       silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2901       silc_server_channel_delete(server, channel);
2902       continue;
2903     }
2904
2905     /* Send notify to channel about client leaving SILC and channel too */
2906     if (notify)
2907       silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2908                                          SILC_NOTIFY_TYPE_SIGNOFF,
2909                                          signoff_message ? 2 : 1,
2910                                          clidp->data, clidp->len,
2911                                          signoff_message, signoff_message ?
2912                                          strlen(signoff_message) : 0);
2913
2914     /* Re-generate channel key if needed */
2915     if (keygen && !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2916       if (!silc_server_create_channel_key(server, channel, 0))
2917         continue;
2918
2919       /* Send the channel key to the channel. The key of course is not sent
2920          to the client who was removed from the channel. */
2921       silc_server_send_channel_key(server, client->connection, channel,
2922                                    server->server_type == SILC_ROUTER ?
2923                                    FALSE : !server->standalone);
2924     }
2925   }
2926
2927   silc_hash_table_list_reset(&htl);
2928   silc_buffer_free(clidp);
2929 }
2930
2931 /* Removes client from one channel. This is used for example when client
2932    calls LEAVE command to remove itself from the channel. Returns TRUE
2933    if channel still exists and FALSE if the channel is removed when
2934    last client leaves the channel. If `notify' is FALSE notify messages
2935    are not sent. */
2936
2937 bool silc_server_remove_from_one_channel(SilcServer server,
2938                                          SilcSocketConnection sock,
2939                                          SilcChannelEntry channel,
2940                                          SilcClientEntry client,
2941                                          bool notify)
2942 {
2943   SilcChannelClientEntry chl;
2944   SilcBuffer clidp;
2945
2946   SILC_LOG_DEBUG(("Removing %s from channel %s",
2947                   silc_id_render(client->id, SILC_ID_CLIENT), 
2948                   channel->channel_name));
2949
2950   /* Get the entry to the channel, if this client is not on the channel
2951      then return Ok. */
2952   if (!silc_hash_table_find(client->channels, channel, NULL, (void *)&chl))
2953     return TRUE;
2954
2955   /* Remove channel if this is last client leaving the channel, unless
2956      the channel is permanent. */
2957   if (server->server_type == SILC_ROUTER &&
2958       silc_hash_table_count(channel->user_list) < 2) {
2959     silc_server_channel_delete(server, channel);
2960     return FALSE;
2961   }
2962
2963   silc_hash_table_del(client->channels, chl->channel);
2964   silc_hash_table_del(channel->user_list, chl->client);
2965   channel->user_count--;
2966
2967   /* If there is no global users on the channel anymore mark the channel
2968      as local channel. Do not check if the client is local client. */
2969   if (server->server_type != SILC_ROUTER && channel->global_users &&
2970       chl->client->router && !silc_server_channel_has_global(channel))
2971     channel->global_users = FALSE;
2972
2973   silc_free(chl);
2974
2975   /* Update statistics */
2976   if (client->connection)
2977     server->stat.my_chanclients--;
2978   if (server->server_type == SILC_ROUTER) {
2979     server->stat.cell_chanclients--;
2980     server->stat.chanclients--;
2981   }
2982
2983   clidp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
2984   if (!clidp)
2985     notify = FALSE;
2986
2987   /* If there is not at least one local user on the channel then we don't
2988      need the channel entry anymore, we can remove it safely, unless the
2989      channel is permanent channel */
2990   if (server->server_type != SILC_ROUTER &&
2991       !silc_server_channel_has_local(channel)) {
2992     /* Notify about leaving client if this channel has global users. */
2993     if (notify && channel->global_users)
2994       silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
2995                                          SILC_NOTIFY_TYPE_LEAVE, 1,
2996                                          clidp->data, clidp->len);
2997
2998     silc_schedule_task_del_by_context(server->schedule, channel->rekey);
2999     silc_server_channel_delete(server, channel);
3000     silc_buffer_free(clidp);
3001     return FALSE;
3002   }
3003
3004   /* Send notify to channel about client leaving the channel */
3005   if (notify)
3006     silc_server_send_notify_to_channel(server, NULL, channel, FALSE,
3007                                        SILC_NOTIFY_TYPE_LEAVE, 1,
3008                                        clidp->data, clidp->len);
3009
3010   silc_buffer_free(clidp);
3011   return TRUE;
3012 }
3013
3014 /* Timeout callback. This is called if connection is idle or for some
3015    other reason is not responding within some period of time. This
3016    disconnects the remote end. */
3017
3018 SILC_TASK_CALLBACK(silc_server_timeout_remote)
3019 {
3020   SilcServer server = (SilcServer)context;
3021   SilcSocketConnection sock = server->sockets[fd];
3022   SilcProtocolType protocol = 0;
3023
3024   SILC_LOG_DEBUG(("Start"));
3025
3026   if (!sock)
3027     return;
3028
3029   SILC_LOG_ERROR(("No response from %s (%s), Connection timeout",
3030                   sock->hostname, sock->ip));
3031
3032   /* If we have protocol active we must assure that we call the protocol's
3033      final callback so that all the memory is freed. */
3034   if (sock->protocol) {
3035     protocol = sock->protocol->protocol->type;
3036     silc_protocol_cancel(sock->protocol, server->schedule);
3037     sock->protocol->state = SILC_PROTOCOL_STATE_ERROR;
3038     silc_protocol_execute_final(sock->protocol, server->schedule);
3039     sock->protocol = NULL;
3040     return;
3041   }
3042
3043   if (sock->user_data)
3044     silc_server_free_sock_user_data(server, sock, NULL);
3045
3046   silc_server_disconnect_remote(server, sock, 
3047                                 protocol == 
3048                                 SILC_PROTOCOL_SERVER_CONNECTION_AUTH ?
3049                                 SILC_STATUS_ERR_AUTH_FAILED :
3050                                 SILC_STATUS_ERR_KEY_EXCHANGE_FAILED,
3051                                 "Connection timeout");
3052 }
3053
3054 /* Creates new channel. Sends NEW_CHANNEL packet to primary route. This
3055    function may be used only by router. In real SILC network all channels
3056    are created by routers thus this function is never used by normal
3057    server. */
3058
3059 SilcChannelEntry silc_server_create_new_channel(SilcServer server,
3060                                                 SilcServerID *router_id,
3061                                                 char *cipher,
3062                                                 char *hmac,
3063                                                 char *channel_name,
3064                                                 int broadcast)
3065 {
3066   SilcChannelID *channel_id;
3067   SilcChannelEntry entry;
3068   SilcCipher key;
3069   SilcHmac newhmac;
3070
3071   SILC_LOG_DEBUG(("Creating new channel"));
3072
3073   if (!cipher)
3074     cipher = SILC_DEFAULT_CIPHER;
3075   if (!hmac)
3076     hmac = SILC_DEFAULT_HMAC;
3077
3078   /* Allocate cipher */
3079   if (!silc_cipher_alloc(cipher, &key))
3080     return NULL;
3081
3082   /* Allocate hmac */
3083   if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
3084     silc_cipher_free(key);
3085     return NULL;
3086   }
3087
3088   channel_name = strdup(channel_name);
3089
3090   /* Create the channel ID */
3091   if (!silc_id_create_channel_id(server, router_id, server->rng,
3092                                  &channel_id)) {
3093     silc_free(channel_name);
3094     silc_cipher_free(key);
3095     silc_hmac_free(newhmac);
3096     return NULL;
3097   }
3098
3099   /* Create the channel */
3100   entry = silc_idlist_add_channel(server->local_list, channel_name,
3101                                   SILC_CHANNEL_MODE_NONE, channel_id,
3102                                   NULL, key, newhmac, 0);
3103   if (!entry) {
3104     silc_free(channel_name);
3105     silc_cipher_free(key);
3106     silc_hmac_free(newhmac);
3107     silc_free(channel_id);
3108     return NULL;
3109   }
3110
3111   entry->cipher = strdup(cipher);
3112   entry->hmac_name = strdup(hmac);
3113
3114   /* Now create the actual key material */
3115   if (!silc_server_create_channel_key(server, entry,
3116                                       silc_cipher_get_key_len(key) / 8)) {
3117     silc_idlist_del_channel(server->local_list, entry);
3118     return NULL;
3119   }
3120
3121   /* Notify other routers about the new channel. We send the packet
3122      to our primary route. */
3123   if (broadcast && server->standalone == FALSE)
3124     silc_server_send_new_channel(server, server->router->connection, TRUE,
3125                                  channel_name, entry->id,
3126                                  silc_id_get_len(entry->id, SILC_ID_CHANNEL),
3127                                  entry->mode);
3128
3129   /* Distribute to backup routers */
3130   if (broadcast && server->server_type == SILC_ROUTER) {
3131     SilcBuffer packet;
3132     unsigned char *cid;
3133     SilcUInt32 name_len = strlen(channel_name);
3134     SilcUInt32 channel_id_len = silc_id_get_len(entry->id, SILC_ID_CHANNEL);
3135     cid = silc_id_id2str(entry->id, SILC_ID_CHANNEL);
3136
3137     packet = silc_channel_payload_encode(channel_name, name_len,
3138                                          cid, channel_id_len, entry->mode);
3139     silc_server_backup_send(server, NULL, SILC_PACKET_NEW_CHANNEL, 0,
3140                             packet->data, packet->len, FALSE, TRUE);
3141     silc_free(cid);
3142     silc_buffer_free(packet);
3143   }
3144
3145   server->stat.my_channels++;
3146   if (server->server_type == SILC_ROUTER) {
3147     server->stat.channels++;
3148     server->stat.cell_channels++;
3149     entry->users_resolved = TRUE;
3150   }
3151
3152   return entry;
3153 }
3154
3155 /* Same as above but creates the channel with Channel ID `channel_id. */
3156
3157 SilcChannelEntry
3158 silc_server_create_new_channel_with_id(SilcServer server,
3159                                        char *cipher,
3160                                        char *hmac,
3161                                        char *channel_name,
3162                                        SilcChannelID *channel_id,
3163                                        int broadcast)
3164 {
3165   SilcChannelEntry entry;
3166   SilcCipher key;
3167   SilcHmac newhmac;
3168
3169   SILC_LOG_DEBUG(("Creating new channel"));
3170
3171   if (!cipher)
3172     cipher = SILC_DEFAULT_CIPHER;
3173   if (!hmac)
3174     hmac = SILC_DEFAULT_HMAC;
3175
3176   /* Allocate cipher */
3177   if (!silc_cipher_alloc(cipher, &key))
3178     return NULL;
3179
3180   /* Allocate hmac */
3181   if (!silc_hmac_alloc(hmac, NULL, &newhmac)) {
3182     silc_cipher_free(key);
3183     return NULL;
3184   }
3185
3186   channel_name = strdup(channel_name);
3187
3188   /* Create the channel */
3189   entry = silc_idlist_add_channel(server->local_list, channel_name,
3190                                   SILC_CHANNEL_MODE_NONE, channel_id,
3191                                   NULL, key, newhmac, 0);
3192   if (!entry) {
3193     silc_cipher_free(key);
3194     silc_hmac_free(newhmac);
3195     silc_free(channel_name);
3196     return NULL;
3197   }
3198
3199   /* Now create the actual key material */
3200   if (!silc_server_create_channel_key(server, entry,
3201                                       silc_cipher_get_key_len(key) / 8)) {
3202     silc_idlist_del_channel(server->local_list, entry);
3203     return NULL;
3204   }
3205
3206   /* Notify other routers about the new channel. We send the packet
3207      to our primary route. */
3208   if (broadcast && server->standalone == FALSE)
3209     silc_server_send_new_channel(server, server->router->connection, TRUE,
3210                                  channel_name, entry->id,
3211                                  silc_id_get_len(entry->id, SILC_ID_CHANNEL),
3212                                  entry->mode);
3213
3214   /* Distribute to backup routers */
3215   if (broadcast && server->server_type == SILC_ROUTER) {
3216     SilcBuffer packet;
3217     unsigned char *cid;
3218     SilcUInt32 name_len = strlen(channel_name);
3219     SilcUInt32 channel_id_len = silc_id_get_len(entry->id, SILC_ID_CHANNEL);
3220     cid = silc_id_id2str(entry->id, SILC_ID_CHANNEL);
3221
3222     packet = silc_channel_payload_encode(channel_name, name_len,
3223                                          cid, channel_id_len, entry->mode);
3224     silc_server_backup_send(server, NULL, SILC_PACKET_NEW_CHANNEL, 0,
3225                             packet->data, packet->len, FALSE, TRUE);
3226     silc_free(cid);
3227     silc_buffer_free(packet);
3228   }
3229
3230   server->stat.my_channels++;
3231   if (server->server_type == SILC_ROUTER) {
3232     server->stat.channels++;
3233     server->stat.cell_channels++;
3234     entry->users_resolved = TRUE;
3235   }
3236
3237   return entry;
3238 }
3239
3240 /* Channel's key re-key timeout callback. */
3241
3242 SILC_TASK_CALLBACK(silc_server_channel_key_rekey)
3243 {
3244   SilcServerChannelRekey rekey = (SilcServerChannelRekey)context;
3245   SilcServer server = (SilcServer)rekey->context;
3246
3247   rekey->task = NULL;
3248
3249   if (!silc_server_create_channel_key(server, rekey->channel, rekey->key_len))
3250     return;
3251
3252   silc_server_send_channel_key(server, NULL, rekey->channel, FALSE);
3253 }
3254
3255 /* Generates new channel key. This is used to create the initial channel key
3256    but also to re-generate new key for channel. If `key_len' is provided
3257    it is the bytes of the key length. */
3258
3259 bool silc_server_create_channel_key(SilcServer server,
3260                                     SilcChannelEntry channel,
3261                                     SilcUInt32 key_len)
3262 {
3263   int i;
3264   unsigned char channel_key[32], hash[32];
3265   SilcUInt32 len;
3266
3267   SILC_LOG_DEBUG(("Generating channel key"));
3268
3269   if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY) {
3270     SILC_LOG_DEBUG(("Channel has private keys, will not generate new key"));
3271     return TRUE;
3272   }
3273
3274   if (!channel->channel_key)
3275     if (!silc_cipher_alloc(SILC_DEFAULT_CIPHER, &channel->channel_key)) {
3276       channel->channel_key = NULL;
3277       return FALSE;
3278     }
3279
3280   if (key_len)
3281     len = key_len;
3282   else if (channel->key_len)
3283     len = channel->key_len / 8;
3284   else
3285     len = silc_cipher_get_key_len(channel->channel_key) / 8;
3286
3287   /* Create channel key */
3288   for (i = 0; i < len; i++) channel_key[i] = silc_rng_get_byte(server->rng);
3289
3290   /* Set the key */
3291   silc_cipher_set_key(channel->channel_key, channel_key, len * 8);
3292
3293   /* Remove old key if exists */
3294   if (channel->key) {
3295     memset(channel->key, 0, channel->key_len / 8);
3296     silc_free(channel->key);
3297   }
3298
3299   /* Save the key */
3300   channel->key_len = len * 8;
3301   channel->key = silc_memdup(channel_key, len);
3302   memset(channel_key, 0, sizeof(channel_key));
3303
3304   /* Generate HMAC key from the channel key data and set it */
3305   if (!channel->hmac)
3306     silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac);
3307   silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key, len, hash);
3308   silc_hmac_set_key(channel->hmac, hash,
3309                     silc_hash_len(silc_hmac_get_hash(channel->hmac)));
3310   memset(hash, 0, sizeof(hash));
3311
3312   if (server->server_type == SILC_ROUTER) {
3313     if (!channel->rekey)
3314       channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
3315     channel->rekey->context = (void *)server;
3316     channel->rekey->channel = channel;
3317     channel->rekey->key_len = key_len;
3318     if (channel->rekey->task)
3319       silc_schedule_task_del(server->schedule, channel->rekey->task);
3320
3321     channel->rekey->task =
3322       silc_schedule_task_add(server->schedule, 0,
3323                              silc_server_channel_key_rekey,
3324                              (void *)channel->rekey,
3325                              server->config->channel_rekey_secs, 0,
3326                              SILC_TASK_TIMEOUT,
3327                              SILC_TASK_PRI_NORMAL);
3328   }
3329
3330   return TRUE;
3331 }
3332
3333 /* Saves the channel key found in the encoded `key_payload' buffer. This
3334    function is used when we receive Channel Key Payload and also when we're
3335    processing JOIN command reply. Returns entry to the channel. */
3336
3337 SilcChannelEntry silc_server_save_channel_key(SilcServer server,
3338                                               SilcBuffer key_payload,
3339                                               SilcChannelEntry channel)
3340 {
3341   SilcChannelKeyPayload payload = NULL;
3342   SilcChannelID *id = NULL;
3343   unsigned char *tmp, hash[32];
3344   SilcUInt32 tmp_len;
3345   char *cipher;
3346
3347   SILC_LOG_DEBUG(("Start"));
3348
3349   /* Decode channel key payload */
3350   payload = silc_channel_key_payload_parse(key_payload->data,
3351                                            key_payload->len);
3352   if (!payload) {
3353     SILC_LOG_ERROR(("Bad channel key payload received, dropped"));
3354     channel = NULL;
3355     goto out;
3356   }
3357
3358   /* Get the channel entry */
3359   if (!channel) {
3360
3361     /* Get channel ID */
3362     tmp = silc_channel_key_get_id(payload, &tmp_len);
3363     id = silc_id_str2id(tmp, tmp_len, SILC_ID_CHANNEL);
3364     if (!id) {
3365       channel = NULL;
3366       goto out;
3367     }
3368
3369     channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
3370     if (!channel) {
3371       channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
3372       if (!channel) {
3373         SILC_LOG_ERROR(("Received key for non-existent channel %s",
3374                         silc_id_render(id, SILC_ID_CHANNEL)));
3375         goto out;
3376       }
3377     }
3378   }
3379
3380   tmp = silc_channel_key_get_key(payload, &tmp_len);
3381   if (!tmp) {
3382     channel = NULL;
3383     goto out;
3384   }
3385
3386   cipher = silc_channel_key_get_cipher(payload, NULL);
3387   if (!cipher) {
3388     channel = NULL;
3389     goto out;
3390   }
3391
3392   /* Remove old key if exists */
3393   if (channel->key) {
3394     memset(channel->key, 0, channel->key_len / 8);
3395     silc_free(channel->key);
3396     silc_cipher_free(channel->channel_key);
3397   }
3398
3399   /* Create new cipher */
3400   if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
3401     channel->channel_key = NULL;
3402     channel = NULL;
3403     goto out;
3404   }
3405
3406   if (channel->cipher)
3407     silc_free(channel->cipher);
3408   channel->cipher = strdup(cipher);
3409
3410   /* Save the key */
3411   channel->key_len = tmp_len * 8;
3412   channel->key = silc_memdup(tmp, tmp_len);
3413   silc_cipher_set_key(channel->channel_key, tmp, channel->key_len);
3414
3415   /* Generate HMAC key from the channel key data and set it */
3416   if (!channel->hmac)
3417     silc_hmac_alloc(SILC_DEFAULT_HMAC, NULL, &channel->hmac);
3418   silc_hash_make(silc_hmac_get_hash(channel->hmac), tmp, tmp_len, hash);
3419   silc_hmac_set_key(channel->hmac, hash,
3420                     silc_hash_len(silc_hmac_get_hash(channel->hmac)));
3421
3422   memset(hash, 0, sizeof(hash));
3423   memset(tmp, 0, tmp_len);
3424
3425   if (server->server_type == SILC_ROUTER) {
3426     if (!channel->rekey)
3427       channel->rekey = silc_calloc(1, sizeof(*channel->rekey));
3428     channel->rekey->context = (void *)server;
3429     channel->rekey->channel = channel;
3430     if (channel->rekey->task)
3431       silc_schedule_task_del(server->schedule, channel->rekey->task);
3432
3433     channel->rekey->task =
3434       silc_schedule_task_add(server->schedule, 0,
3435                              silc_server_channel_key_rekey,
3436                              (void *)channel->rekey,
3437                              server->config->channel_rekey_secs, 0,
3438                              SILC_TASK_TIMEOUT,
3439                              SILC_TASK_PRI_NORMAL);
3440   }
3441
3442  out:
3443   silc_free(id);
3444   if (payload)
3445     silc_channel_key_payload_free(payload);
3446
3447   return channel;
3448 }
3449
3450 /* Heartbeat callback. This function is set as argument for the
3451    silc_socket_set_heartbeat function. The library will call this function
3452    at the set time interval. */
3453
3454 void silc_server_perform_heartbeat(SilcSocketConnection sock,
3455                                    void *hb_context)
3456 {
3457   SilcServerHBContext hb = (SilcServerHBContext)hb_context;
3458
3459   SILC_LOG_DEBUG(("Sending heartbeat to %s (%s)", sock->hostname, sock->ip));
3460
3461   /* Send the heartbeat */
3462   silc_server_send_heartbeat(hb->server, sock);
3463 }
3464
3465 /* Returns assembled of all servers in the given ID list. The packet's
3466    form is dictated by the New ID payload. */
3467
3468 static void silc_server_announce_get_servers(SilcServer server,
3469                                              SilcServerEntry remote,
3470                                              SilcIDList id_list,
3471                                              SilcBuffer *servers,
3472                                              unsigned long creation_time)
3473 {
3474   SilcIDCacheList list;
3475   SilcIDCacheEntry id_cache;
3476   SilcServerEntry entry;
3477   SilcBuffer idp;
3478
3479   /* Go through all clients in the list */
3480   if (silc_idcache_get_all(id_list->servers, &list)) {
3481     if (silc_idcache_list_first(list, &id_cache)) {
3482       while (id_cache) {
3483         entry = (SilcServerEntry)id_cache->context;
3484
3485         /* Do not announce the one we've sending our announcements and
3486            do not announce ourself. Also check the creation time if it's
3487            provided. */
3488         if ((entry == remote) || (entry == server->id_entry) ||
3489             (creation_time && entry->data.created < creation_time)) {
3490           if (!silc_idcache_list_next(list, &id_cache))
3491             break;
3492           continue;
3493         }
3494
3495         idp = silc_id_payload_encode(entry->id, SILC_ID_SERVER);
3496
3497         *servers = silc_buffer_realloc(*servers,
3498                                        (*servers ?
3499                                         (*servers)->truelen + idp->len :
3500                                         idp->len));
3501         silc_buffer_pull_tail(*servers, ((*servers)->end - (*servers)->data));
3502         silc_buffer_put(*servers, idp->data, idp->len);
3503         silc_buffer_pull(*servers, idp->len);
3504         silc_buffer_free(idp);
3505
3506         if (!silc_idcache_list_next(list, &id_cache))
3507           break;
3508       }
3509     }
3510
3511     silc_idcache_list_free(list);
3512   }
3513 }
3514
3515 static SilcBuffer
3516 silc_server_announce_encode_notify(SilcNotifyType notify, SilcUInt32 argc, ...)
3517 {
3518   va_list ap;
3519   SilcBuffer p;
3520
3521   va_start(ap, argc);
3522   p = silc_notify_payload_encode(notify, argc, ap);
3523   va_end(ap);
3524
3525   return p;
3526 }
3527
3528 /* This function is used by router to announce existing servers to our
3529    primary router when we've connected to it. If `creation_time' is non-zero
3530    then only the servers that has been created after the `creation_time'
3531    will be announced. */
3532
3533 void silc_server_announce_servers(SilcServer server, bool global,
3534                                   unsigned long creation_time,
3535                                   SilcSocketConnection remote)
3536 {
3537   SilcBuffer servers = NULL;
3538
3539   SILC_LOG_DEBUG(("Announcing servers"));
3540
3541   /* Get servers in local list */
3542   silc_server_announce_get_servers(server, remote->user_data,
3543                                    server->local_list, &servers,
3544                                    creation_time);
3545
3546   if (global)
3547     /* Get servers in global list */
3548     silc_server_announce_get_servers(server, remote->user_data,
3549                                      server->global_list, &servers,
3550                                      creation_time);
3551
3552   if (servers) {
3553     silc_buffer_push(servers, servers->data - servers->head);
3554     SILC_LOG_HEXDUMP(("servers"), servers->data, servers->len);
3555
3556     /* Send the packet */
3557     silc_server_packet_send(server, remote,
3558                             SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
3559                             servers->data, servers->len, TRUE);
3560
3561     silc_buffer_free(servers);
3562   }
3563 }
3564
3565 /* Returns assembled packet of all clients in the given ID list. The
3566    packet's form is dictated by the New ID Payload. */
3567
3568 static void silc_server_announce_get_clients(SilcServer server,
3569                                              SilcIDList id_list,
3570                                              SilcBuffer *clients,
3571                                              SilcBuffer *umodes,
3572                                              unsigned long creation_time)
3573 {
3574   SilcIDCacheList list;
3575   SilcIDCacheEntry id_cache;
3576   SilcClientEntry client;
3577   SilcBuffer idp;
3578   SilcBuffer tmp;
3579   unsigned char mode[4];
3580
3581   /* Go through all clients in the list */
3582   if (silc_idcache_get_all(id_list->clients, &list)) {
3583     if (silc_idcache_list_first(list, &id_cache)) {
3584       while (id_cache) {
3585         client = (SilcClientEntry)id_cache->context;
3586
3587         if (creation_time && client->data.created < creation_time) {
3588           if (!silc_idcache_list_next(list, &id_cache))
3589             break;
3590           continue;
3591         }
3592
3593         idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
3594
3595         *clients = silc_buffer_realloc(*clients,
3596                                        (*clients ?
3597                                         (*clients)->truelen + idp->len :
3598                                         idp->len));
3599         silc_buffer_pull_tail(*clients, ((*clients)->end - (*clients)->data));
3600         silc_buffer_put(*clients, idp->data, idp->len);
3601         silc_buffer_pull(*clients, idp->len);
3602
3603         SILC_PUT32_MSB(client->mode, mode);
3604         tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_UMODE_CHANGE,
3605                                                  2, idp->data, idp->len,
3606                                                  mode, 4);
3607         *umodes = silc_buffer_realloc(*umodes,
3608                                       (*umodes ?
3609                                        (*umodes)->truelen + tmp->len :
3610                                        tmp->len));
3611         silc_buffer_pull_tail(*umodes, ((*umodes)->end - (*umodes)->data));
3612         silc_buffer_put(*umodes, tmp->data, tmp->len);
3613         silc_buffer_pull(*umodes, tmp->len);
3614         silc_buffer_free(tmp);
3615
3616         silc_buffer_free(idp);
3617
3618         if (!silc_idcache_list_next(list, &id_cache))
3619           break;
3620       }
3621     }
3622
3623     silc_idcache_list_free(list);
3624   }
3625 }
3626
3627 /* This function is used to announce our existing clients to our router
3628    when we've connected to it. If `creation_time' is non-zero then only
3629    the clients that has been created after the `creation_time' will be
3630    announced. */
3631
3632 void silc_server_announce_clients(SilcServer server,
3633                                   unsigned long creation_time,
3634                                   SilcSocketConnection remote)
3635 {
3636   SilcBuffer clients = NULL;
3637   SilcBuffer umodes = NULL;
3638
3639   SILC_LOG_DEBUG(("Announcing clients"));
3640
3641   /* Get clients in local list */
3642   silc_server_announce_get_clients(server, server->local_list,
3643                                    &clients, &umodes, creation_time);
3644
3645   /* As router we announce our global list as well */
3646   if (server->server_type == SILC_ROUTER)
3647     silc_server_announce_get_clients(server, server->global_list,
3648                                      &clients, &umodes, creation_time);
3649
3650   if (clients) {
3651     silc_buffer_push(clients, clients->data - clients->head);
3652     SILC_LOG_HEXDUMP(("clients"), clients->data, clients->len);
3653
3654     /* Send the packet */
3655     silc_server_packet_send(server, remote,
3656                             SILC_PACKET_NEW_ID, SILC_PACKET_FLAG_LIST,
3657                             clients->data, clients->len, TRUE);
3658
3659     silc_buffer_free(clients);
3660   }
3661
3662   if (umodes) {
3663     silc_buffer_push(umodes, umodes->data - umodes->head);
3664     SILC_LOG_HEXDUMP(("umodes"), umodes->data, umodes->len);
3665
3666     /* Send the packet */
3667     silc_server_packet_send(server, remote,
3668                             SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3669                             umodes->data, umodes->len, TRUE);
3670
3671     silc_buffer_free(umodes);
3672   }
3673 }
3674
3675 /* Returns channel's topic for announcing it */
3676
3677 void silc_server_announce_get_channel_topic(SilcServer server,
3678                                             SilcChannelEntry channel,
3679                                             SilcBuffer *topic)
3680 {
3681   SilcBuffer chidp;
3682
3683   if (channel->topic) {
3684     chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
3685     *topic = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_TOPIC_SET, 2,
3686                                                 chidp->data, chidp->len,
3687                                                 channel->topic,
3688                                                 strlen(channel->topic));
3689     silc_buffer_free(chidp);
3690   }
3691 }
3692
3693 /* Returns assembled packets for channel users of the `channel'. */
3694
3695 void silc_server_announce_get_channel_users(SilcServer server,
3696                                             SilcChannelEntry channel,
3697                                             SilcBuffer *channel_modes,
3698                                             SilcBuffer *channel_users,
3699                                             SilcBuffer *channel_users_modes)
3700 {
3701   SilcChannelClientEntry chl;
3702   SilcHashTableList htl;
3703   SilcBuffer chidp, clidp, csidp;
3704   SilcBuffer tmp;
3705   int len;
3706   unsigned char mode[4], *fkey = NULL;
3707   SilcUInt32 fkey_len = 0;
3708   char *hmac;
3709
3710   SILC_LOG_DEBUG(("Start"));
3711
3712   chidp = silc_id_payload_encode(channel->id, SILC_ID_CHANNEL);
3713   csidp = silc_id_payload_encode(server->id, SILC_ID_SERVER);
3714
3715   /* CMODE notify */
3716   SILC_PUT32_MSB(channel->mode, mode);
3717   hmac = channel->hmac ? (char *)silc_hmac_get_name(channel->hmac) : NULL;
3718   if (channel->founder_key)
3719     fkey = silc_pkcs_public_key_encode(channel->founder_key, &fkey_len);
3720   tmp = 
3721     silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_CMODE_CHANGE,
3722                                        6, csidp->data, csidp->len,
3723                                        mode, sizeof(mode),
3724                                        NULL, 0,
3725                                        hmac, hmac ? strlen(hmac) : 0,
3726                                        channel->passphrase,
3727                                        channel->passphrase ?
3728                                        strlen(channel->passphrase) : 0,
3729                                        fkey, fkey_len);
3730   len = tmp->len;
3731   *channel_modes =
3732     silc_buffer_realloc(*channel_modes,
3733                         (*channel_modes ?
3734                          (*channel_modes)->truelen + len : len));
3735   silc_buffer_pull_tail(*channel_modes,
3736                         ((*channel_modes)->end -
3737                          (*channel_modes)->data));
3738   silc_buffer_put(*channel_modes, tmp->data, tmp->len);
3739   silc_buffer_pull(*channel_modes, len);
3740   silc_buffer_free(tmp);
3741   silc_free(fkey);
3742   fkey = NULL;
3743   fkey_len = 0;
3744
3745   /* Now find all users on the channel */
3746   silc_hash_table_list(channel->user_list, &htl);
3747   while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
3748     clidp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
3749
3750     /* JOIN Notify */
3751     tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_JOIN, 2,
3752                                              clidp->data, clidp->len,
3753                                              chidp->data, chidp->len);
3754     len = tmp->len;
3755     *channel_users =
3756       silc_buffer_realloc(*channel_users,
3757                           (*channel_users ?
3758                            (*channel_users)->truelen + len : len));
3759     silc_buffer_pull_tail(*channel_users,
3760                           ((*channel_users)->end -
3761                            (*channel_users)->data));
3762
3763     silc_buffer_put(*channel_users, tmp->data, tmp->len);
3764     silc_buffer_pull(*channel_users, len);
3765     silc_buffer_free(tmp);
3766
3767     /* CUMODE notify for mode change on the channel */
3768     SILC_PUT32_MSB(chl->mode, mode);
3769     if (chl->mode & SILC_CHANNEL_UMODE_CHANFO && channel->founder_key)
3770       fkey = silc_pkcs_public_key_encode(channel->founder_key, &fkey_len);
3771     tmp = silc_server_announce_encode_notify(SILC_NOTIFY_TYPE_CUMODE_CHANGE,
3772                                              4, csidp->data, csidp->len,
3773                                              mode, sizeof(mode),
3774                                              clidp->data, clidp->len,
3775                                              fkey, fkey_len);
3776     len = tmp->len;
3777     *channel_users_modes =
3778       silc_buffer_realloc(*channel_users_modes,
3779                           (*channel_users_modes ?
3780                            (*channel_users_modes)->truelen + len : len));
3781     silc_buffer_pull_tail(*channel_users_modes,
3782                           ((*channel_users_modes)->end -
3783                            (*channel_users_modes)->data));
3784
3785     silc_buffer_put(*channel_users_modes, tmp->data, tmp->len);
3786     silc_buffer_pull(*channel_users_modes, len);
3787     silc_buffer_free(tmp);
3788     silc_free(fkey);
3789     fkey = NULL;
3790     fkey_len = 0;
3791     silc_buffer_free(clidp);
3792   }
3793   silc_hash_table_list_reset(&htl);
3794   silc_buffer_free(chidp);
3795   silc_buffer_free(csidp);
3796 }
3797
3798 /* Returns assembled packets for all channels and users on those channels
3799    from the given ID List. The packets are in the form dictated by the
3800    New Channel and New Channel User payloads. */
3801
3802 void silc_server_announce_get_channels(SilcServer server,
3803                                        SilcIDList id_list,
3804                                        SilcBuffer *channels,
3805                                        SilcBuffer **channel_modes,
3806                                        SilcBuffer *channel_users,
3807                                        SilcBuffer **channel_users_modes,
3808                                        SilcUInt32 *channel_users_modes_c,
3809                                        SilcBuffer **channel_topics,
3810                                        SilcChannelID ***channel_ids,
3811                                        unsigned long creation_time)
3812 {
3813   SilcIDCacheList list;
3814   SilcIDCacheEntry id_cache;
3815   SilcChannelEntry channel;
3816   unsigned char *cid;
3817   SilcUInt32 id_len;
3818   SilcUInt16 name_len;
3819   int len;
3820   int i = *channel_users_modes_c;
3821   bool announce;
3822
3823   SILC_LOG_DEBUG(("Start"));
3824
3825   /* Go through all channels in the list */
3826   if (silc_idcache_get_all(id_list->channels, &list)) {
3827     if (silc_idcache_list_first(list, &id_cache)) {
3828       while (id_cache) {
3829         channel = (SilcChannelEntry)id_cache->context;
3830
3831         if (creation_time && channel->created < creation_time)
3832           announce = FALSE;
3833         else
3834           announce = TRUE;
3835
3836         cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
3837         id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
3838         name_len = strlen(channel->channel_name);
3839
3840         if (announce) {
3841           len = 4 + name_len + id_len + 4;
3842           *channels =
3843             silc_buffer_realloc(*channels,
3844                                 (*channels ? (*channels)->truelen +
3845                                  len : len));
3846           silc_buffer_pull_tail(*channels,
3847                                 ((*channels)->end - (*channels)->data));
3848           silc_buffer_format(*channels,
3849                              SILC_STR_UI_SHORT(name_len),
3850                              SILC_STR_UI_XNSTRING(channel->channel_name,
3851                                                   name_len),
3852                              SILC_STR_UI_SHORT(id_len),
3853                              SILC_STR_UI_XNSTRING(cid, id_len),
3854                              SILC_STR_UI_INT(channel->mode),
3855                              SILC_STR_END);
3856           silc_buffer_pull(*channels, len);
3857         }
3858
3859         /* Channel user modes */
3860         *channel_users_modes = silc_realloc(*channel_users_modes,
3861                                             sizeof(**channel_users_modes) *
3862                                             (i + 1));
3863         (*channel_users_modes)[i] = NULL;
3864         *channel_modes = silc_realloc(*channel_modes,
3865                                       sizeof(**channel_modes) * (i + 1));
3866         (*channel_modes)[i] = NULL;
3867         *channel_ids = silc_realloc(*channel_ids,
3868                                     sizeof(**channel_ids) * (i + 1));
3869         (*channel_ids)[i] = NULL;
3870         silc_server_announce_get_channel_users(server, channel,
3871                                                &(*channel_modes)[i], 
3872                                                channel_users,
3873                                                &(*channel_users_modes)[i]);
3874         (*channel_ids)[i] = channel->id;
3875
3876         /* Channel's topic */
3877         *channel_topics = silc_realloc(*channel_topics,
3878                                        sizeof(**channel_topics) * (i + 1));
3879         (*channel_topics)[i] = NULL;
3880         silc_server_announce_get_channel_topic(server, channel,
3881                                                &(*channel_topics)[i]);
3882         i++;
3883
3884         if (!silc_idcache_list_next(list, &id_cache))
3885           break;
3886       }
3887
3888       *channel_users_modes_c += i;
3889     }
3890
3891     silc_idcache_list_free(list);
3892   }
3893 }
3894
3895 /* This function is used to announce our existing channels to our router
3896    when we've connected to it. This also announces the users on the
3897    channels to the router. If the `creation_time' is non-zero only the
3898    channels that was created after the `creation_time' are announced.
3899    Note that the channel users are still announced even if the `creation_time'
3900    was provided. */
3901
3902 void silc_server_announce_channels(SilcServer server,
3903                                    unsigned long creation_time,
3904                                    SilcSocketConnection remote)
3905 {
3906   SilcBuffer channels = NULL, *channel_modes = NULL, channel_users = NULL;
3907   SilcBuffer *channel_users_modes = NULL;
3908   SilcBuffer *channel_topics = NULL;
3909   SilcUInt32 channel_users_modes_c = 0;
3910   SilcChannelID **channel_ids = NULL;
3911
3912   SILC_LOG_DEBUG(("Announcing channels and channel users"));
3913
3914   /* Get channels and channel users in local list */
3915   silc_server_announce_get_channels(server, server->local_list,
3916                                     &channels, &channel_modes,
3917                                     &channel_users,
3918                                     &channel_users_modes,
3919                                     &channel_users_modes_c,
3920                                     &channel_topics,
3921                                     &channel_ids, creation_time);
3922
3923   /* Get channels and channel users in global list */
3924   if (server->server_type != SILC_SERVER)
3925     silc_server_announce_get_channels(server, server->global_list,
3926                                       &channels, &channel_modes,
3927                                       &channel_users,
3928                                       &channel_users_modes,
3929                                       &channel_users_modes_c,
3930                                       &channel_topics,
3931                                       &channel_ids, creation_time);
3932
3933   if (channels) {
3934     silc_buffer_push(channels, channels->data - channels->head);
3935     SILC_LOG_HEXDUMP(("channels"), channels->data, channels->len);
3936
3937     /* Send the packet */
3938     silc_server_packet_send(server, remote,
3939                             SILC_PACKET_NEW_CHANNEL, SILC_PACKET_FLAG_LIST,
3940                             channels->data, channels->len,
3941                             FALSE);
3942
3943     silc_buffer_free(channels);
3944   }
3945
3946   if (channel_modes) {
3947     int i;
3948
3949     for (i = 0; i < channel_users_modes_c; i++) {
3950       if (!channel_modes[i])
3951         continue;
3952       silc_buffer_push(channel_modes[i],
3953                        channel_modes[i]->data -
3954                        channel_modes[i]->head);
3955       SILC_LOG_HEXDUMP(("channel modes"), channel_modes[i]->data,
3956                        channel_modes[i]->len);
3957       silc_server_packet_send_dest(server, remote,
3958                                    SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3959                                    channel_ids[i], SILC_ID_CHANNEL,
3960                                    channel_modes[i]->data,
3961                                    channel_modes[i]->len,
3962                                    FALSE);
3963       silc_buffer_free(channel_modes[i]);
3964     }
3965     silc_free(channel_modes);
3966   }
3967
3968   if (channel_users) {
3969     silc_buffer_push(channel_users, channel_users->data - channel_users->head);
3970     SILC_LOG_HEXDUMP(("channel users"), channel_users->data,
3971                      channel_users->len);
3972
3973     /* Send the packet */
3974     silc_server_packet_send(server, remote,
3975                             SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3976                             channel_users->data, channel_users->len,
3977                             FALSE);
3978
3979     silc_buffer_free(channel_users);
3980   }
3981
3982   if (channel_users_modes) {
3983     int i;
3984
3985     for (i = 0; i < channel_users_modes_c; i++) {
3986       if (!channel_users_modes[i])
3987         continue;
3988       silc_buffer_push(channel_users_modes[i],
3989                        channel_users_modes[i]->data -
3990                        channel_users_modes[i]->head);
3991       SILC_LOG_HEXDUMP(("channel users modes"), channel_users_modes[i]->data,
3992                        channel_users_modes[i]->len);
3993       silc_server_packet_send_dest(server, remote,
3994                                    SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
3995                                    channel_ids[i], SILC_ID_CHANNEL,
3996                                    channel_users_modes[i]->data,
3997                                    channel_users_modes[i]->len,
3998                                    FALSE);
3999       silc_buffer_free(channel_users_modes[i]);
4000     }
4001     silc_free(channel_users_modes);
4002   }
4003
4004   if (channel_topics) {
4005     int i;
4006
4007     for (i = 0; i < channel_users_modes_c; i++) {
4008       if (!channel_topics[i])
4009         continue;
4010
4011       silc_buffer_push(channel_topics[i],
4012                        channel_topics[i]->data -
4013                        channel_topics[i]->head);
4014       SILC_LOG_HEXDUMP(("channel topic"), channel_topics[i]->data,
4015                        channel_topics[i]->len);
4016       silc_server_packet_send_dest(server, remote,
4017                                    SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
4018                                    channel_ids[i], SILC_ID_CHANNEL,
4019                                    channel_topics[i]->data,
4020                                    channel_topics[i]->len,
4021                                    FALSE);
4022       silc_buffer_free(channel_topics[i]);
4023     }
4024     silc_free(channel_topics);
4025   }
4026
4027   silc_free(channel_ids);
4028 }
4029
4030 /* Failure timeout callback. If this is called then we will immediately
4031    process the received failure. We always process the failure with timeout
4032    since we do not want to blindly trust to received failure packets.
4033    This won't be called (the timeout is cancelled) if the failure was
4034    bogus (it is bogus if remote does not close the connection after sending
4035    the failure). */
4036
4037 SILC_TASK_CALLBACK(silc_server_failure_callback)
4038 {
4039   SilcServerFailureContext f = (SilcServerFailureContext)context;
4040
4041   if (f->sock->protocol) {
4042     f->sock->protocol->state = SILC_PROTOCOL_STATE_FAILURE;
4043     silc_protocol_execute(f->sock->protocol, f->server->schedule, 0, 0);
4044   }
4045
4046   silc_free(f);
4047 }
4048
4049 /* Assembles user list and users mode list from the `channel'. */
4050
4051 bool silc_server_get_users_on_channel(SilcServer server,
4052                                       SilcChannelEntry channel,
4053                                       SilcBuffer *user_list,
4054                                       SilcBuffer *mode_list,
4055                                       SilcUInt32 *user_count)
4056 {
4057   SilcChannelClientEntry chl;
4058   SilcHashTableList htl;
4059   SilcBuffer client_id_list;
4060   SilcBuffer client_mode_list;
4061   SilcBuffer idp;
4062   SilcUInt32 list_count = 0, len = 0;
4063
4064   if (!silc_hash_table_count(channel->user_list))
4065     return FALSE;
4066
4067   silc_hash_table_list(channel->user_list, &htl);
4068   while (silc_hash_table_get(&htl, NULL, (void *)&chl))
4069     len += (silc_id_get_len(chl->client->id, SILC_ID_CLIENT) + 4);
4070   silc_hash_table_list_reset(&htl);
4071
4072   client_id_list = silc_buffer_alloc(len);
4073   client_mode_list =
4074     silc_buffer_alloc(4 * silc_hash_table_count(channel->user_list));
4075   silc_buffer_pull_tail(client_id_list, SILC_BUFFER_END(client_id_list));
4076   silc_buffer_pull_tail(client_mode_list, SILC_BUFFER_END(client_mode_list));
4077
4078   silc_hash_table_list(channel->user_list, &htl);
4079   while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
4080     /* Client ID */
4081     idp = silc_id_payload_encode(chl->client->id, SILC_ID_CLIENT);
4082     silc_buffer_put(client_id_list, idp->data, idp->len);
4083     silc_buffer_pull(client_id_list, idp->len);
4084     silc_buffer_free(idp);
4085
4086     /* Client's mode on channel */
4087     SILC_PUT32_MSB(chl->mode, client_mode_list->data);
4088     silc_buffer_pull(client_mode_list, 4);
4089
4090     list_count++;
4091   }
4092   silc_hash_table_list_reset(&htl);
4093   silc_buffer_push(client_id_list,
4094                    client_id_list->data - client_id_list->head);
4095   silc_buffer_push(client_mode_list,
4096                    client_mode_list->data - client_mode_list->head);
4097
4098   *user_list = client_id_list;
4099   *mode_list = client_mode_list;
4100   *user_count = list_count;
4101   return TRUE;
4102 }
4103
4104 /* Saves users and their modes to the `channel'. */
4105
4106 void silc_server_save_users_on_channel(SilcServer server,
4107                                        SilcSocketConnection sock,
4108                                        SilcChannelEntry channel,
4109                                        SilcClientID *noadd,
4110                                        SilcBuffer user_list,
4111                                        SilcBuffer mode_list,
4112                                        SilcUInt32 user_count)
4113 {
4114   int i;
4115   SilcUInt16 idp_len;
4116   SilcUInt32 mode;
4117   SilcClientID *client_id;
4118   SilcClientEntry client;
4119   SilcIDCacheEntry cache;
4120   SilcChannelClientEntry chl;
4121   bool global;
4122
4123   SILC_LOG_DEBUG(("Start"));
4124
4125   for (i = 0; i < user_count; i++) {
4126     /* Client ID */
4127     SILC_GET16_MSB(idp_len, user_list->data + 2);
4128     idp_len += 4;
4129     client_id = silc_id_payload_parse_id(user_list->data, idp_len, NULL);
4130     silc_buffer_pull(user_list, idp_len);
4131     if (!client_id)
4132       continue;
4133
4134     /* Mode */
4135     SILC_GET32_MSB(mode, mode_list->data);
4136     silc_buffer_pull(mode_list, 4);
4137
4138     if (noadd && SILC_ID_CLIENT_COMPARE(client_id, noadd)) {
4139       silc_free(client_id);
4140       continue;
4141     }
4142
4143     global = FALSE;
4144
4145     /* Check if we have this client cached already. */
4146     client = silc_idlist_find_client_by_id(server->local_list, client_id,
4147                                            server->server_type, &cache);
4148     if (!client) {
4149       client = silc_idlist_find_client_by_id(server->global_list,
4150                                              client_id, server->server_type,
4151                                              &cache);
4152       global = TRUE;
4153     }
4154     if (!client) {
4155       /* If router did not find such Client ID in its lists then this must
4156          be bogus client or some router in the net is buggy. */
4157       if (server->server_type == SILC_ROUTER) {
4158         silc_free(client_id);
4159         continue;
4160       }
4161
4162       /* We don't have that client anywhere, add it. The client is added
4163          to global list since server didn't have it in the lists so it must be
4164          global. */
4165       client = silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
4166                                       silc_id_dup(client_id, SILC_ID_CLIENT),
4167                                       sock->user_data, NULL, 0);
4168       if (!client) {
4169         SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
4170         silc_free(client_id);
4171         continue;
4172       }
4173
4174       client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
4175     } else {
4176       /* Found, if it is from global list we'll assure that we won't
4177          expire it now that the entry is on channel. */
4178       if (global)
4179         cache->expire = 0;
4180     }
4181
4182     silc_free(client_id);
4183
4184     if (!silc_server_client_on_channel(client, channel, &chl)) {
4185       /* Client was not on the channel, add it. */
4186       chl = silc_calloc(1, sizeof(*chl));
4187       chl->client = client;
4188       chl->mode = mode;
4189       chl->channel = channel;
4190       silc_hash_table_add(channel->user_list, chl->client, chl);
4191       silc_hash_table_add(client->channels, chl->channel, chl);
4192       channel->user_count++;
4193     } else {
4194       /* Update mode */
4195       chl->mode = mode;
4196     }
4197   }
4198 }
4199
4200 /* Saves channels and channels user modes to the `client'.  Removes
4201    the client from those channels that are not sent in the list but
4202    it has joined. */
4203
4204 void silc_server_save_user_channels(SilcServer server,
4205                                     SilcSocketConnection sock,
4206                                     SilcClientEntry client,
4207                                     SilcBuffer channels,
4208                                     SilcBuffer channels_user_modes)
4209 {
4210   SilcDList ch;
4211   SilcUInt32 *chumodes;
4212   SilcChannelPayload entry;
4213   SilcChannelEntry channel;
4214   SilcChannelID *channel_id;
4215   SilcChannelClientEntry chl;
4216   SilcHashTable ht = NULL;
4217   SilcHashTableList htl;
4218   char *name;
4219   int i = 0;
4220
4221   if (!channels ||!channels_user_modes)
4222     goto out;
4223   
4224   ch = silc_channel_payload_parse_list(channels->data, channels->len);
4225   if (ch && silc_get_mode_list(channels_user_modes, silc_dlist_count(ch),
4226                                &chumodes)) {
4227     ht = silc_hash_table_alloc(0, silc_hash_ptr, NULL, NULL, 
4228                                NULL, NULL, NULL, TRUE);
4229     silc_dlist_start(ch);
4230     while ((entry = silc_dlist_get(ch)) != SILC_LIST_END) {
4231       /* Check if we have this channel, and add it if we don't have it.
4232          Also add the client on the channel unless it is there already. */
4233       channel_id = silc_channel_get_id_parse(entry);
4234       channel = silc_idlist_find_channel_by_id(server->local_list, 
4235                                                channel_id, NULL);
4236       if (!channel)
4237         channel = silc_idlist_find_channel_by_id(server->global_list,
4238                                                  channel_id, NULL);
4239       if (!channel) {
4240         if (server->server_type != SILC_SERVER) {
4241           silc_free(channel_id);
4242           i++;
4243           continue;
4244         }
4245         
4246         /* We don't have that channel anywhere, add it. */
4247         name = silc_channel_get_name(entry, NULL);
4248         channel = silc_idlist_add_channel(server->global_list, strdup(name), 0,
4249                                           channel_id, server->router,
4250                                           NULL, NULL, 0);
4251         if (!channel) {
4252           silc_free(channel_id);
4253           i++;
4254           continue;
4255         }
4256         channel_id = NULL;
4257       }
4258
4259       channel->mode = silc_channel_get_mode(entry);
4260
4261       /* Add the client on the channel */
4262       if (!silc_server_client_on_channel(client, channel, &chl)) {
4263         chl = silc_calloc(1, sizeof(*chl));
4264         chl->client = client;
4265         chl->mode = chumodes[i++];
4266         chl->channel = channel;
4267         silc_hash_table_add(channel->user_list, chl->client, chl);
4268         silc_hash_table_add(client->channels, chl->channel, chl);
4269         channel->user_count++;
4270       } else {
4271         /* Update mode */
4272         chl->mode = chumodes[i++];
4273       }
4274
4275       silc_hash_table_add(ht, channel, channel);
4276       silc_free(channel_id);
4277     }
4278     silc_channel_payload_list_free(ch);
4279     silc_free(chumodes);
4280   }
4281
4282  out:
4283   /* Go through the list again and remove client from channels that
4284      are no part of the list. */
4285   if (ht) {
4286     silc_hash_table_list(client->channels, &htl);
4287     while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
4288       if (!silc_hash_table_find(ht, chl->channel, NULL, NULL)) {
4289         silc_hash_table_del(chl->channel->user_list, chl->client);
4290         silc_hash_table_del(chl->client->channels, chl->channel);
4291         silc_free(chl);
4292       }
4293     }
4294     silc_hash_table_list_reset(&htl);
4295     silc_hash_table_free(ht);
4296   } else {
4297     silc_hash_table_list(client->channels, &htl);
4298     while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
4299       silc_hash_table_del(chl->channel->user_list, chl->client);
4300       silc_hash_table_del(chl->client->channels, chl->channel);
4301       silc_free(chl);
4302     }
4303     silc_hash_table_list_reset(&htl);
4304   }
4305 }
4306
4307 /* Lookups route to the client indicated by the `id_data'. The connection
4308    object and internal data object is returned. Returns NULL if route
4309    could not be found to the client. If the `client_id' is specified then
4310    it is used and the `id_data' is ignored. */
4311
4312 SilcSocketConnection
4313 silc_server_get_client_route(SilcServer server,
4314                              unsigned char *id_data,
4315                              SilcUInt32 id_len,
4316                              SilcClientID *client_id,
4317                              SilcIDListData *idata,
4318                              SilcClientEntry *client_entry)
4319 {
4320   SilcClientID *id;
4321   SilcClientEntry client;
4322
4323   SILC_LOG_DEBUG(("Start"));
4324
4325   if (client_entry)
4326     *client_entry = NULL;
4327
4328   /* Decode destination Client ID */
4329   if (!client_id) {
4330     id = silc_id_str2id(id_data, id_len, SILC_ID_CLIENT);
4331     if (!id) {
4332       SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
4333       return NULL;
4334     }
4335   } else {
4336     id = silc_id_dup(client_id, SILC_ID_CLIENT);
4337   }
4338
4339   /* If the destination belongs to our server we don't have to route
4340      the packet anywhere but to send it to the local destination. */
4341   client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
4342   if (client) {
4343     silc_free(id);
4344
4345     /* If we are router and the client has router then the client is in
4346        our cell but not directly connected to us. */
4347     if (server->server_type == SILC_ROUTER && client->router) {
4348       /* We are of course in this case the client's router thus the route
4349          to the client is the server who owns the client. So, we will send
4350          the packet to that server. */
4351       if (idata)
4352         *idata = (SilcIDListData)client->router;
4353       return client->router->connection;
4354     }
4355
4356     /* Seems that client really is directly connected to us */
4357     if (idata)
4358       *idata = (SilcIDListData)client;
4359     if (client_entry)
4360       *client_entry = client;
4361     return client->connection;
4362   }
4363
4364   /* Destination belongs to someone not in this server. If we are normal
4365      server our action is to send the packet to our router. */
4366   if (server->server_type != SILC_ROUTER && !server->standalone) {
4367     silc_free(id);
4368     if (idata)
4369       *idata = (SilcIDListData)server->router;
4370     return server->router->connection;
4371   }
4372
4373   /* We are router and we will perform route lookup for the destination
4374      and send the packet to fastest route. */
4375   if (server->server_type == SILC_ROUTER && !server->standalone) {
4376     /* Check first that the ID is valid */
4377     client = silc_idlist_find_client_by_id(server->global_list, id,
4378                                            TRUE, NULL);
4379     if (client) {
4380       SilcSocketConnection dst_sock;
4381
4382       dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
4383
4384       silc_free(id);
4385       if (idata)
4386         *idata = (SilcIDListData)dst_sock->user_data;
4387       return dst_sock;
4388     }
4389   }
4390
4391   silc_free(id);
4392   return NULL;
4393 }
4394
4395 /* Encodes and returns channel list of channels the `client' has joined.
4396    Secret channels are not put to the list. */
4397
4398 SilcBuffer silc_server_get_client_channel_list(SilcServer server,
4399                                                SilcClientEntry client,
4400                                                bool get_private,
4401                                                bool get_secret,
4402                                                SilcBuffer *user_mode_list)
4403 {
4404   SilcBuffer buffer = NULL;
4405   SilcChannelEntry channel;
4406   SilcChannelClientEntry chl;
4407   SilcHashTableList htl;
4408   unsigned char *cid;
4409   SilcUInt32 id_len;
4410   SilcUInt16 name_len;
4411   int len;
4412
4413   if (user_mode_list)
4414     *user_mode_list = NULL;
4415
4416   silc_hash_table_list(client->channels, &htl);
4417   while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
4418     channel = chl->channel;
4419
4420     if (channel->mode & SILC_CHANNEL_MODE_SECRET && !get_secret)
4421       continue;
4422     if (channel->mode & SILC_CHANNEL_MODE_PRIVATE && !get_private)
4423       continue;
4424
4425     cid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
4426     id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
4427     name_len = strlen(channel->channel_name);
4428
4429     len = 4 + name_len + id_len + 4;
4430     buffer = silc_buffer_realloc(buffer,
4431                                  (buffer ? buffer->truelen + len : len));
4432     silc_buffer_pull_tail(buffer, (buffer->end - buffer->data));
4433     silc_buffer_format(buffer,
4434                        SILC_STR_UI_SHORT(name_len),
4435                        SILC_STR_UI_XNSTRING(channel->channel_name,
4436                                             name_len),
4437                        SILC_STR_UI_SHORT(id_len),
4438                        SILC_STR_UI_XNSTRING(cid, id_len),
4439                        SILC_STR_UI_INT(chl->channel->mode),
4440                        SILC_STR_END);
4441     silc_buffer_pull(buffer, len);
4442     silc_free(cid);
4443
4444     if (user_mode_list) {
4445       *user_mode_list = silc_buffer_realloc(*user_mode_list,
4446                                             (*user_mode_list ?
4447                                              (*user_mode_list)->truelen + 4 :
4448                                              4));
4449       silc_buffer_pull_tail(*user_mode_list, ((*user_mode_list)->end -
4450                                               (*user_mode_list)->data));
4451       SILC_PUT32_MSB(chl->mode, (*user_mode_list)->data);
4452       silc_buffer_pull(*user_mode_list, 4);
4453     }
4454   }
4455   silc_hash_table_list_reset(&htl);
4456
4457   if (buffer)
4458     silc_buffer_push(buffer, buffer->data - buffer->head);
4459   if (user_mode_list && *user_mode_list)
4460     silc_buffer_push(*user_mode_list, ((*user_mode_list)->data -
4461                                        (*user_mode_list)->head));
4462
4463   return buffer;
4464 }
4465
4466 /* Finds client entry by Client ID and if it is not found then resolves
4467    it using WHOIS command. */
4468
4469 SilcClientEntry silc_server_get_client_resolve(SilcServer server,
4470                                                SilcClientID *client_id,
4471                                                bool always_resolve,
4472                                                bool *resolved)
4473 {
4474   SilcClientEntry client;
4475
4476   if (resolved)
4477     *resolved = FALSE;
4478
4479   client = silc_idlist_find_client_by_id(server->local_list, client_id,
4480                                          TRUE, NULL);
4481   if (!client) {
4482     client = silc_idlist_find_client_by_id(server->global_list,
4483                                            client_id, TRUE, NULL);
4484     if (!client && server->server_type == SILC_ROUTER)
4485       return NULL;
4486   }
4487
4488   if (!client && server->standalone)
4489     return NULL;
4490
4491   if (!client || !client->nickname || !client->username ||
4492       always_resolve) {
4493     SilcBuffer buffer, idp;
4494
4495     if (client) {
4496       client->data.status |= SILC_IDLIST_STATUS_RESOLVING;
4497       client->data.status &= ~SILC_IDLIST_STATUS_RESOLVED;
4498       client->resolve_cmd_ident = ++server->cmd_ident;
4499     }
4500
4501     idp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
4502     buffer = silc_command_payload_encode_va(SILC_COMMAND_WHOIS,
4503                                             server->cmd_ident, 1,
4504                                             4, idp->data, idp->len);
4505     silc_server_packet_send(server, client ? client->router->connection :
4506                             server->router->connection,
4507                             SILC_PACKET_COMMAND, 0,
4508                             buffer->data, buffer->len, FALSE);
4509     silc_buffer_free(idp);
4510     silc_buffer_free(buffer);
4511
4512     if (resolved)
4513       *resolved = TRUE;
4514
4515     return NULL;
4516   }
4517
4518   return client;
4519 }
4520
4521 /* A timeout callback for the re-key. We will be the initiator of the
4522    re-key protocol. */
4523
4524 SILC_TASK_CALLBACK(silc_server_rekey_callback)
4525 {
4526   SilcSocketConnection sock = (SilcSocketConnection)context;
4527   SilcIDListData idata = (SilcIDListData)sock->user_data;
4528   SilcServer server = (SilcServer)idata->rekey->context;
4529   SilcProtocol protocol;
4530   SilcServerRekeyInternalContext *proto_ctx;
4531
4532   SILC_LOG_DEBUG(("Start"));
4533
4534   /* Allocate internal protocol context. This is sent as context
4535      to the protocol. */
4536   proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
4537   proto_ctx->server = (void *)server;
4538   proto_ctx->sock = sock;
4539   proto_ctx->responder = FALSE;
4540   proto_ctx->pfs = idata->rekey->pfs;
4541
4542   /* Perform rekey protocol. Will call the final callback after the
4543      protocol is over. */
4544   silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
4545                       &protocol, proto_ctx, silc_server_rekey_final);
4546   sock->protocol = protocol;
4547
4548   /* Run the protocol */
4549   silc_protocol_execute(protocol, server->schedule, 0, 0);
4550
4551   /* Re-register re-key timeout */
4552   silc_schedule_task_add(server->schedule, sock->sock,
4553                          silc_server_rekey_callback,
4554                          context, idata->rekey->timeout, 0,
4555                          SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
4556 }
4557
4558 /* The final callback for the REKEY protocol. This will actually take the
4559    new key material into use. */
4560
4561 SILC_TASK_CALLBACK_GLOBAL(silc_server_rekey_final)
4562 {
4563   SilcProtocol protocol = (SilcProtocol)context;
4564   SilcServerRekeyInternalContext *ctx =
4565     (SilcServerRekeyInternalContext *)protocol->context;
4566   SilcServer server = (SilcServer)ctx->server;
4567   SilcSocketConnection sock = ctx->sock;
4568
4569   SILC_LOG_DEBUG(("Start"));
4570
4571   if (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
4572       protocol->state == SILC_PROTOCOL_STATE_FAILURE) {
4573     /* Error occured during protocol */
4574     SILC_LOG_ERROR(("Error occurred during rekey protocol"));
4575     silc_protocol_cancel(protocol, server->schedule);
4576     silc_protocol_free(protocol);
4577     sock->protocol = NULL;
4578     if (ctx->packet)
4579       silc_packet_context_free(ctx->packet);
4580     if (ctx->ske)
4581       silc_ske_free(ctx->ske);
4582     silc_free(ctx);
4583     return;
4584   }
4585
4586   /* Purge the outgoing data queue to assure that all rekey packets really
4587      go to the network before we quit the protocol. */
4588   silc_server_packet_queue_purge(server, sock);
4589
4590   /* Cleanup */
4591   silc_protocol_free(protocol);
4592   sock->protocol = NULL;
4593   if (ctx->packet)
4594     silc_packet_context_free(ctx->packet);
4595   if (ctx->ske)
4596     silc_ske_free(ctx->ske);
4597   silc_free(ctx);
4598 }
4599
4600 /* Task callback used to retrieve network statistical information from
4601    router server once in a while. */
4602
4603 SILC_TASK_CALLBACK(silc_server_get_stats)
4604 {
4605   SilcServer server = (SilcServer)context;
4606   SilcBuffer idp, packet;
4607
4608   SILC_LOG_DEBUG(("Retrieving stats from router"));
4609
4610   if (!server->standalone) {
4611     idp = silc_id_payload_encode(server->router->id, SILC_ID_SERVER);
4612     packet = silc_command_payload_encode_va(SILC_COMMAND_STATS, 
4613                                             ++server->cmd_ident, 1,
4614                                             1, idp->data, idp->len);
4615     silc_server_packet_send(server, server->router->connection,
4616                             SILC_PACKET_COMMAND, 0, packet->data,
4617                             packet->len, FALSE);
4618     silc_buffer_free(packet);
4619     silc_buffer_free(idp);
4620   }
4621
4622   silc_schedule_task_add(server->schedule, 0, silc_server_get_stats,
4623                          server, 120, 0, SILC_TASK_TIMEOUT,
4624                          SILC_TASK_PRI_LOW);
4625 }