876eba20217f62d0b8ded91f46b69f3e3a5bba42
[silc.git] / apps / silcd / server_backup.c
1 /*
2
3   server_backup.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 - 2003 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19 /* $Id$ */
20
21 #include "serverincludes.h"
22 #include "server_internal.h"
23
24 SILC_TASK_CALLBACK(silc_server_protocol_backup_done);
25 static void silc_server_backup_connect_primary(SilcServer server,
26                                                SilcServerEntry server_entry,
27                                                void *context);
28
29
30 /************************** Types and Definitions ***************************/
31
32 /* Backup router */
33 typedef struct {
34   SilcServerEntry server;
35   SilcIDIP ip;
36   SilcUInt16 port;
37   bool local;
38 } SilcServerBackupEntry;
39
40 /* Holds IP address and port of the primary router that was replaced
41    by backup router. */
42 typedef struct {
43   SilcIDIP ip;
44   SilcUInt16 port;
45   SilcServerEntry server;       /* Backup router that replaced the primary */
46 } SilcServerBackupReplaced;
47
48 /* Backup context */
49 struct SilcServerBackupStruct {
50   SilcServerBackupEntry *servers;
51   SilcUInt32 servers_count;
52   SilcServerBackupReplaced **replaced;
53   SilcUInt32 replaced_count;
54 };
55
56 typedef struct {
57   SilcUInt8 session;
58   bool connected;
59   SilcServerEntry server_entry;
60 } SilcServerBackupProtocolSession;
61
62 /* Backup resuming protocol context  */
63 typedef struct {
64   SilcServer server;
65   SilcSocketConnection sock;
66   SilcUInt8 type;
67   SilcUInt8 session;
68   SilcServerBackupProtocolSession *sessions;
69   SilcUInt32 sessions_count;
70   long start;
71   unsigned int responder        : 1;
72   unsigned int received_failure : 1;
73   unsigned int timeout          : 1;
74 } *SilcServerBackupProtocolContext;
75
76
77 /********************* Backup Configuration Routines ************************/
78
79 /* Adds the `backup_server' to be one of our backup router. This can be
80    called multiple times to set multiple backup routers. The `ip' and `port'
81    is the IP and port that the `backup_router' will replace if the `ip'
82    will become unresponsive. If `local' is TRUE then the `backup_server' is
83    in the local cell, if FALSE it is in some other cell. */
84
85 void silc_server_backup_add(SilcServer server, SilcServerEntry backup_server,
86                             const char *ip, int port, bool local)
87 {
88   int i;
89
90   if (!ip)
91     return;
92
93   if (!server->backup) {
94     server->backup = silc_calloc(1, sizeof(*server->backup));
95     if (!server->backup)
96       return;
97   }
98
99   /* See if already added */
100   for (i = 0; i < server->backup->servers_count; i++) {
101     if (server->backup->servers[i].server == backup_server)
102       return;
103   }
104
105   SILC_LOG_DEBUG(("Backup router %s will replace %s",
106                   ((SilcSocketConnection)backup_server->connection)->ip,
107                   ip, port));
108
109   for (i = 0; i < server->backup->servers_count; i++) {
110     if (!server->backup->servers[i].server) {
111       server->backup->servers[i].server = backup_server;
112       server->backup->servers[i].local = local;
113       server->backup->servers[i].port = SILC_SWAB_16(port);
114       memset(server->backup->servers[i].ip.data, 0,
115              sizeof(server->backup->servers[i].ip.data));
116       silc_net_addr2bin(ip, server->backup->servers[i].ip.data,
117                         sizeof(server->backup->servers[i].ip.data));
118       return;
119     }
120   }
121
122   i = server->backup->servers_count;
123   server->backup->servers = silc_realloc(server->backup->servers,
124                                          sizeof(*server->backup->servers) *
125                                          (i + 1));
126   server->backup->servers[i].server = backup_server;
127   server->backup->servers[i].local = local;
128   server->backup->servers[i].port = SILC_SWAB_16(port);
129   memset(server->backup->servers[i].ip.data, 0,
130          sizeof(server->backup->servers[i].ip.data));
131   silc_net_addr2bin(ip, server->backup->servers[i].ip.data,
132                     sizeof(server->backup->servers[i].ip.data));
133   server->backup->servers_count++;
134 }
135
136 /* Returns backup router for IP and port in `server_id' or NULL if there
137    does not exist backup router. */
138
139 SilcServerEntry silc_server_backup_get(SilcServer server,
140                                        SilcServerID *server_id)
141 {
142   int i;
143
144   if (!server->backup)
145     return NULL;
146
147   for (i = 0; i < server->backup->servers_count; i++) {
148     if (server->backup->servers[i].server &&
149         server->backup->servers[i].port == server_id->port &&
150         !memcmp(server->backup->servers[i].ip.data, server_id->ip.data,
151                 sizeof(server_id->ip.data))) {
152       SILC_LOG_DEBUG(("Found backup router %s for %s",
153                       server->backup->servers[i].server->server_name,
154                       silc_id_render(server_id, SILC_ID_SERVER)));
155       return server->backup->servers[i].server;
156     }
157   }
158
159   return NULL;
160 }
161
162 /* Deletes the backup server `server_entry'. */
163
164 void silc_server_backup_del(SilcServer server, SilcServerEntry server_entry)
165 {
166   int i;
167
168   if (!server->backup)
169     return;
170
171   for (i = 0; i < server->backup->servers_count; i++) {
172     if (server->backup->servers[i].server == server_entry) {
173       SILC_LOG_DEBUG(("Removing %s as backup router",
174                       silc_id_render(server->backup->servers[i].server->id,
175                                      SILC_ID_SERVER)));
176       server->backup->servers[i].server = NULL;
177       memset(server->backup->servers[i].ip.data, 0,
178              sizeof(server->backup->servers[i].ip.data));
179     }
180   }
181 }
182
183 /* Frees all data allocated for backup routers.  Call this after deleting
184    all backup routers and when new routers are added no more, for example
185    when shutting down the server. */
186
187 void silc_server_backup_free(SilcServer server)
188 {
189   int i;
190
191   if (!server->backup)
192     return;
193
194   /* Delete existing servers if caller didn't do it */
195   for (i = 0; i < server->backup->servers_count; i++) {
196     if (server->backup->servers[i].server)
197       silc_server_backup_del(server, server->backup->servers[i].server);
198   }
199
200   silc_free(server->backup->servers);
201   silc_free(server->backup);
202   server->backup = NULL;
203 }
204
205 /* Marks the IP address and port from the `server_id' as  being replaced
206    by backup router indicated by the `server'. If the router connects at
207    a later time we can check whether it has been replaced by an backup
208    router. */
209
210 void silc_server_backup_replaced_add(SilcServer server,
211                                      SilcServerID *server_id,
212                                      SilcServerEntry server_entry)
213 {
214   int i;
215   SilcServerBackupReplaced *r = silc_calloc(1, sizeof(*r));;
216
217   if (!server->backup)
218     server->backup = silc_calloc(1, sizeof(*server->backup));
219   if (!server->backup->replaced) {
220     server->backup->replaced =
221       silc_calloc(1, sizeof(*server->backup->replaced));
222     server->backup->replaced_count = 1;
223   }
224
225   SILC_LOG_DEBUG(("Replacing router %s with %s",
226                   silc_id_render(server_id, SILC_ID_SERVER),
227                   server_entry->server_name));
228
229   memcpy(&r->ip, &server_id->ip, sizeof(server_id->ip));
230   r->server = server_entry;
231
232   for (i = 0; i < server->backup->replaced_count; i++) {
233     if (!server->backup->replaced[i]) {
234       server->backup->replaced[i] = r;
235       return;
236     }
237   }
238
239   i = server->backup->replaced_count;
240   server->backup->replaced = silc_realloc(server->backup->replaced,
241                                           sizeof(*server->backup->replaced) *
242                                           (i + 1));
243   server->backup->replaced[i] = r;
244   server->backup->replaced_count++;
245 }
246
247 /* Checks whether the IP address and port from the `server_id' has been
248    replaced by an backup router. If it has been then this returns TRUE
249    and the bacup router entry to the `server' pointer if non-NULL. Returns
250    FALSE if the router is not replaced by backup router. */
251
252 bool silc_server_backup_replaced_get(SilcServer server,
253                                      SilcServerID *server_id,
254                                      SilcServerEntry *server_entry)
255 {
256   int i;
257
258   if (!server->backup || !server->backup->replaced)
259     return FALSE;
260
261   for (i = 0; i < server->backup->replaced_count; i++) {
262     if (!server->backup->replaced[i])
263       continue;
264     if (!memcmp(server->backup->replaced[i]->ip.data, server_id->ip.data,
265                 sizeof(server_id->ip.data))) {
266       if (server_entry)
267         *server_entry = server->backup->replaced[i]->server;
268       SILC_LOG_DEBUG(("Router %s is replaced by %s",
269                       silc_id_render(server_id, SILC_ID_SERVER),
270                       server->backup->replaced[i]->server->server_name));
271       return TRUE;
272     }
273   }
274
275   SILC_LOG_DEBUG(("Router %s is not replaced by backup router",
276                   silc_id_render(server_id, SILC_ID_SERVER)));
277   return FALSE;
278 }
279
280 /* Deletes a replaced host by the set `server_entry. */
281
282 void silc_server_backup_replaced_del(SilcServer server,
283                                      SilcServerEntry server_entry)
284 {
285   int i;
286
287   if (!server->backup || !server->backup->replaced)
288     return;
289
290   for (i = 0; i < server->backup->replaced_count; i++) {
291     if (!server->backup->replaced[i])
292       continue;
293     if (server->backup->replaced[i]->server == server_entry) {
294       silc_free(server->backup->replaced[i]);
295       server->backup->replaced[i] = NULL;
296       return;
297     }
298   }
299 }
300
301 /* Broadcast the received packet indicated by `packet' to all of our backup
302    routers. All router wide information is passed using broadcast packets.
303    That is why all backup routers need to get this data too. It is expected
304    that the caller already knows that the `packet' is broadcast packet. */
305
306 void silc_server_backup_broadcast(SilcServer server,
307                                   SilcSocketConnection sender,
308                                   SilcPacketContext *packet)
309 {
310   SilcServerEntry backup;
311   SilcSocketConnection sock;
312   SilcBuffer buffer;
313   const SilcBufferStruct p;
314   SilcIDListData idata;
315   int i;
316
317   if (!server->backup || server->server_type != SILC_ROUTER)
318     return;
319
320   SILC_LOG_DEBUG(("Broadcasting received packet to backup routers"));
321
322   buffer = packet->buffer;
323   silc_buffer_push(buffer, buffer->data - buffer->head);
324
325   for (i = 0; i < server->backup->servers_count; i++) {
326     backup = server->backup->servers[i].server;
327
328     if (!backup || backup->connection == sender ||
329         server->backup->servers[i].local == FALSE)
330       continue;
331     if (server->backup->servers[i].server == server->id_entry)
332       continue;
333
334     idata = (SilcIDListData)backup;
335     sock = backup->connection;
336
337     if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
338                                   (const SilcBuffer)&p)) {
339       SILC_LOG_ERROR(("Cannot send packet"));
340       return;
341     }
342     silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
343     silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
344                         (SilcBuffer)&p, p.len);
345
346     SILC_LOG_HEXDUMP(("Broadcasted packet, len %d", p.len), p.data, p.len);
347
348     /* Now actually send the packet */
349     silc_server_packet_send_real(server, sock, FALSE);
350
351     /* Check for mandatory rekey */
352     if (idata->psn_send == SILC_SERVER_REKEY_THRESHOLD)
353       silc_schedule_task_add(server->schedule, sender->sock,
354                              silc_server_rekey_callback, sender, 0, 1,
355                              SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
356   }
357 }
358
359 /* A generic routine to send data to all backup routers. If the `sender'
360    is provided it will indicate the original sender of the packet and the
361    packet won't be resent to that entity. The `data' is the data that will
362    be assembled to packet context before sending. The packet will be
363    encrypted this function. If the `force_send' is TRUE the data is sent
364    immediately and not put to queue. If `local' is TRUE then the packet
365    will be sent only to local backup routers inside the cell. If false the
366    packet can go from one cell to the other. This function has no effect
367    if there are no any backup routers. */
368
369 void silc_server_backup_send(SilcServer server,
370                              SilcServerEntry sender,
371                              SilcPacketType type,
372                              SilcPacketFlags flags,
373                              unsigned char *data,
374                              SilcUInt32 data_len,
375                              bool force_send,
376                              bool local)
377 {
378   SilcServerEntry backup;
379   SilcSocketConnection sock;
380   int i;
381
382   if (!server->backup || server->server_type != SILC_ROUTER)
383     return;
384
385   for (i = 0; i < server->backup->servers_count; i++) {
386     backup = server->backup->servers[i].server;
387     if (!backup || sender == backup)
388       continue;
389     if (local && server->backup->servers[i].local == FALSE)
390       continue;
391     if (server->backup->servers[i].server == server->id_entry)
392       continue;
393
394     sock = backup->connection;
395
396     SILC_LOG_DEBUG(("Sending %s packet to backup router %s (%s)",
397                     silc_get_packet_name(type), sock->hostname, sock->ip));
398
399     silc_server_packet_send(server, backup->connection, type, flags,
400                             data, data_len, force_send);
401   }
402 }
403
404 /* Same as silc_server_backup_send but sets a specific Destination ID to
405    the packet. The Destination ID is indicated by the `dst_id' and the
406    ID type `dst_id_type'. For example, packets destined to channels must
407    be sent using this function. */
408
409 void silc_server_backup_send_dest(SilcServer server,
410                                   SilcServerEntry sender,
411                                   SilcPacketType type,
412                                   SilcPacketFlags flags,
413                                   void *dst_id,
414                                   SilcIdType dst_id_type,
415                                   unsigned char *data,
416                                   SilcUInt32 data_len,
417                                   bool force_send,
418                                   bool local)
419 {
420   SilcServerEntry backup;
421   SilcSocketConnection sock;
422   int i;
423
424   if (!server->backup || server->server_type != SILC_ROUTER)
425     return;
426
427   for (i = 0; i < server->backup->servers_count; i++) {
428     backup = server->backup->servers[i].server;
429     if (!backup || sender == backup)
430       continue;
431     if (local && server->backup->servers[i].local == FALSE)
432       continue;
433     if (server->backup->servers[i].server == server->id_entry)
434       continue;
435
436     sock = backup->connection;
437
438     SILC_LOG_DEBUG(("Sending %s packet to backup router %s (%s)",
439                     silc_get_packet_name(type), sock->hostname, sock->ip));
440
441     silc_server_packet_send_dest(server, backup->connection, type, flags,
442                                  dst_id, dst_id_type, data, data_len,
443                                  force_send);
444   }
445 }
446
447 /* Send the START_USE indication to remote connection.  If `failure' is
448    TRUE then this sends SILC_PACKET_FAILURE.  Otherwise it sends
449    SILC_PACKET_RESUME_ROUTER. */
450
451 void silc_server_backup_send_start_use(SilcServer server,
452                                        SilcSocketConnection sock,
453                                        bool failure)
454 {
455   unsigned char data[4];
456
457   SILC_LOG_DEBUG(("Sending START_USE (%s) to %s",
458                   failure ? "failure" : "success", sock->ip));
459
460   if (failure) {
461     SILC_PUT32_MSB(SILC_SERVER_BACKUP_START_USE, data);
462     silc_server_packet_send(server, sock, SILC_PACKET_FAILURE, 0,
463                             data, 4, FALSE);
464   } else {
465     data[0] = SILC_SERVER_BACKUP_START_USE;
466     data[1] = 0;
467     silc_server_packet_send(server, sock,
468                             SILC_PACKET_RESUME_ROUTER, 0,
469                             data, 2, FALSE);
470   }
471 }
472
473 /* Send the REPLACED indication to remote router.  This is send by the
474    primary router (remote router) of the primary router that came back
475    online.  This is not sent by backup router or any other server. */
476
477 void silc_server_backup_send_replaced(SilcServer server,
478                                       SilcSocketConnection sock)
479 {
480   unsigned char data[4];
481
482   SILC_LOG_DEBUG(("Sending REPLACED (%s) to %s", sock->ip));
483
484   data[0] = SILC_SERVER_BACKUP_REPLACED;
485   data[1] = 0;
486   silc_server_packet_send(server, sock,
487                           SILC_PACKET_RESUME_ROUTER, 0,
488                           data, 2, FALSE);
489 }
490
491
492 /************************ Backup Resuming Protocol **************************/
493
494 /* Timeout callback for protocol */
495
496 SILC_TASK_CALLBACK(silc_server_backup_timeout)
497 {
498   SilcProtocol protocol = context;
499   SilcServerBackupProtocolContext ctx = protocol->context;
500   SilcServer server = app_context;
501
502   SILC_LOG_INFO(("Timeout occurred during backup resuming protocol"));
503   ctx->timeout = TRUE;
504   silc_protocol_cancel(protocol, server->schedule);
505   protocol->state = SILC_PROTOCOL_STATE_ERROR;
506   silc_protocol_execute_final(protocol, server->schedule);
507 }
508
509 /* Callback to start the protocol as responder */
510
511 SILC_TASK_CALLBACK(silc_server_backup_responder_start)
512 {
513   SilcServerBackupProtocolContext proto_ctx = context;
514   SilcSocketConnection sock = proto_ctx->sock;
515   SilcServer server = app_context;
516
517   /* If other protocol is executing at the same time, start with timeout. */
518   if (sock->protocol) {
519     SILC_LOG_DEBUG(("Other protocol is executing, wait for it to finish"));
520     silc_schedule_task_add(server->schedule, sock->sock,
521                            silc_server_backup_responder_start,
522                            proto_ctx, 2, 0,
523                            SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
524     return;
525   }
526
527   /* Run the backup resuming protocol */
528   silc_protocol_alloc(SILC_PROTOCOL_SERVER_BACKUP,
529                       &sock->protocol, proto_ctx,
530                       silc_server_protocol_backup_done);
531   silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
532   silc_schedule_task_add(server->schedule, sock->sock,
533                          silc_server_backup_timeout,
534                          sock->protocol, 30, 0, SILC_TASK_TIMEOUT,
535                          SILC_TASK_PRI_NORMAL);
536 }
537
538 /* Callback to send START_USE to backup to check whether using backup
539    is ok. */
540
541 SILC_TASK_CALLBACK(silc_server_backup_check_status)
542 {
543   SilcSocketConnection sock = context;
544   SilcServer server = app_context;
545
546   /* Check whether we are still using backup */
547   if (!server->backup_primary)
548     return;
549
550   silc_server_backup_send_start_use(server, sock, FALSE);
551   silc_socket_free(sock);       /* unref */
552 }
553
554 typedef struct {
555   SilcServer server;
556   SilcSocketConnection sock;
557   SilcPacketContext *packet;
558 } *SilcServerBackupPing;
559
560 /* PING command reply callback */
561
562 void silc_server_backup_ping_reply(void *context, void *reply)
563 {
564   SilcServerBackupPing pc = context;
565   SilcServerCommandReplyContext cmdr = reply;
566
567   if (cmdr && !silc_command_get_status(cmdr->payload, NULL, NULL)) {
568     /* Timeout error occurred, the primary is really down. */
569     SilcSocketConnection primary = SILC_PRIMARY_ROUTE(pc->server);
570
571     SILC_LOG_DEBUG(("PING timeout, primary is down"));
572
573     if (primary) {
574       if (primary->user_data)
575         silc_server_free_sock_user_data(pc->server, primary, NULL);
576       SILC_SET_DISCONNECTING(primary);
577       silc_server_close_connection(pc->server, primary);
578     }
579
580     /* Reprocess the RESUME_ROUTER packet */
581     silc_server_backup_resume_router(pc->server, pc->sock, pc->packet);
582   } else {
583     /* The primary is not down, refuse to serve the server as primary */
584     SILC_LOG_DEBUG(("PING received, primary is up"));
585     silc_server_backup_send_start_use(pc->server, pc->sock, TRUE);
586   }
587
588   silc_socket_free(pc->sock);
589   silc_packet_context_free(pc->packet);
590   silc_free(pc);
591 }
592
593 /* Processes incoming RESUME_ROUTER packet. This can give the packet
594    for processing to the protocol handler or allocate new protocol if
595    start command is received. */
596
597 void silc_server_backup_resume_router(SilcServer server,
598                                       SilcSocketConnection sock,
599                                       SilcPacketContext *packet)
600 {
601   SilcUInt8 type, session;
602   SilcServerBackupProtocolContext ctx;
603   SilcIDListData idata;
604   int i, ret;
605
606   SILC_LOG_DEBUG(("Received RESUME_ROUTER packet"));
607
608   if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
609       sock->type == SILC_SOCKET_TYPE_UNKNOWN) {
610     SILC_LOG_DEBUG(("Bad packet received"));
611     return;
612   }
613
614   idata = (SilcIDListData)sock->user_data;
615
616   ret = silc_buffer_unformat(packet->buffer,
617                              SILC_STR_UI_CHAR(&type),
618                              SILC_STR_UI_CHAR(&session),
619                              SILC_STR_END);
620   if (ret < 0) {
621     SILC_LOG_ERROR(("Malformed resume router packet received"));
622     return;
623   }
624
625   /* Check whether this packet is used to tell us that server will start
626      using us as primary router. */
627   if (type == SILC_SERVER_BACKUP_START_USE) {
628     SilcBuffer idp;
629     SilcServerBackupPing pc;
630
631     /* If we are normal server then backup router has sent us back
632        this reply and we use the backup as primary router now. */
633     if (server->server_type == SILC_SERVER) {
634       /* Nothing to do here actually, since we have switched already. */
635       SILC_LOG_DEBUG(("Received successful START_USE from backup router"));
636       return;
637     }
638
639     /* Backup router following. */
640
641     /* If we are marked as router then the primary is down and we send
642        success START_USE back to the server. */
643     if (server->server_type == SILC_ROUTER) {
644       SILC_LOG_DEBUG(("Sending success START_USE back to %s", sock->ip));
645       silc_server_backup_send_start_use(server, sock, FALSE);
646       return;
647     }
648
649     /* We have just lost primary, send success START_USE back */
650     if (server->standalone) {
651       SILC_LOG_DEBUG(("We are stanalone, sending success START_USE back to %s",
652                       sock->ip));
653       silc_server_backup_send_start_use(server, sock, FALSE);
654       return;
655     }
656
657     /* We are backup router. This server claims that our primary is down.
658        We will check this ourselves by sending PING command to the primary. */
659     SILC_LOG_DEBUG(("Sending PING to detect status of primary router"));
660     idp = silc_id_payload_encode(server->router->id, SILC_ID_SERVER);
661     silc_server_send_command(server, SILC_PRIMARY_ROUTE(server),
662                              SILC_COMMAND_PING, ++server->cmd_ident, 1,
663                              1, idp->data, idp->len);
664     silc_buffer_free(idp);
665
666     /* Reprocess this packet after received reply from router */
667     pc = silc_calloc(1, sizeof(*pc));
668     pc->server = server;
669     pc->sock = silc_socket_dup(sock);
670     pc->packet = silc_packet_context_dup(packet);
671     silc_server_command_pending_timed(server, SILC_COMMAND_PING,
672                                       server->cmd_ident,
673                                       silc_server_backup_ping_reply, pc, 15);
674     return;
675   }
676
677
678   /* Start the resuming protocol if requested. */
679   if (type == SILC_SERVER_BACKUP_START) {
680     /* We have received a start for resuming protocol.  We are either
681        primary router that came back online or normal server. */
682     SilcServerBackupProtocolContext proto_ctx;
683
684     /* If backup had closed the connection earlier we won't allow resuming
685        since we (primary router) have never gone away. */
686     if (server->server_type == SILC_ROUTER && !server->backup_router &&
687         server->backup_closed) {
688       unsigned char data[4];
689       SILC_LOG_DEBUG(("Backup resuming not allowed since we are still "
690                       "primary router"));
691       SILC_PUT32_MSB(SILC_SERVER_BACKUP_START, data);
692       silc_server_packet_send(server, sock, SILC_PACKET_FAILURE, 0,
693                               data, 4, FALSE);
694       server->backup_closed = FALSE;
695       return;
696     }
697
698     proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
699     proto_ctx->server = server;
700     proto_ctx->sock = silc_socket_dup(sock);
701     proto_ctx->responder = TRUE;
702     proto_ctx->type = type;
703     proto_ctx->session = session;
704     proto_ctx->start = time(0);
705
706     SILC_LOG_DEBUG(("Starting backup resuming protocol as responder"));
707     SILC_LOG_INFO(("Starting backup resuming protocol"));
708
709     /* Start protocol immediately */
710     silc_schedule_task_add(server->schedule, sock->sock,
711                            silc_server_backup_responder_start,
712                            proto_ctx, 0, 1,
713                            SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
714     return;
715   }
716
717
718   /* If we are router and the packet is coming from our primary router
719      then it means we have been replaced by an backup router in our cell. */
720   if (type == SILC_SERVER_BACKUP_REPLACED &&
721       server->server_type == SILC_ROUTER &&
722       sock->type == SILC_SOCKET_TYPE_ROUTER &&
723       SILC_PRIMARY_ROUTE(server) == sock) {
724     /* We have been replaced by an backup router in our cell. We must
725        mark our primary router connection disabled since we are not allowed
726        to use it at this moment. */
727     SILC_LOG_INFO(("We are replaced by an backup router in this cell, will "
728                    "wait until backup resuming protocol is executed"));
729     idata->status |= SILC_IDLIST_STATUS_DISABLED;
730     return;
731   }
732
733
734   /* Activate the shared protocol context for this socket connection
735      if necessary */
736   if (type == SILC_SERVER_BACKUP_RESUMED &&
737       sock->type == SILC_SOCKET_TYPE_ROUTER && !sock->protocol &&
738       idata->status & SILC_IDLIST_STATUS_DISABLED) {
739     SilcServerEntry backup_router;
740
741     if (silc_server_backup_replaced_get(server, ((SilcServerEntry)idata)->id,
742                                         &backup_router)) {
743       SilcSocketConnection bsock =
744         (SilcSocketConnection)backup_router->connection;
745       if (bsock->protocol && bsock->protocol->protocol &&
746           bsock->protocol->protocol->type == SILC_PROTOCOL_SERVER_BACKUP) {
747         sock->protocol = bsock->protocol;
748         ctx = sock->protocol->context;
749         if (ctx->sock)
750           silc_socket_free(ctx->sock); /* unref */
751         ctx->sock = silc_socket_dup(sock);
752       }
753     }
754   }
755
756
757   /* Call the resuming protocol if the protocol is active. */
758   if (SILC_SERVER_IS_BACKUP(sock)) {
759     ctx = sock->protocol->context;
760     ctx->type = type;
761
762     for (i = 0; i < ctx->sessions_count; i++) {
763       if (session == ctx->sessions[i].session) {
764         ctx->session = session;
765         silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
766         return;
767       }
768     }
769
770     /* If RESUMED received the session ID is zero, execute the protocol. */
771     if (type == SILC_SERVER_BACKUP_RESUMED) {
772       silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
773       return;
774     }
775
776     SILC_LOG_ERROR(("Unknown backup resuming session %d", session));
777     return;
778   }
779 }
780
781 /* Timeout task callback to connect to remote router */
782
783 SILC_TASK_CALLBACK(silc_server_backup_connect_to_router)
784 {
785   SilcServer server = app_context;
786   SilcServerConnection sconn = (SilcServerConnection)context;
787   int sock;
788   const char *server_ip;
789
790   SILC_LOG_DEBUG(("Connecting to router %s:%d", sconn->remote_host,
791                   sconn->remote_port));
792
793   /* Connect to remote host */
794   server_ip = server->config->server_info->primary == NULL ? NULL :
795     server->config->server_info->primary->server_ip;
796   sock = silc_net_create_connection(server_ip, sconn->remote_port,
797                                     sconn->remote_host);
798   if (sock < 0) {
799     if (server->server_type == SILC_SERVER) {
800       sconn->retry_count++;
801       if (sconn->retry_count > 3) {
802         silc_free(sconn->remote_host);
803         silc_free(sconn);
804         return;
805       }
806     }
807     silc_schedule_task_add(server->schedule, 0,
808                            silc_server_backup_connect_to_router,
809                            context, 10, 0, SILC_TASK_TIMEOUT,
810                            SILC_TASK_PRI_NORMAL);
811     return;
812   }
813
814   /* Continue with key exchange protocol */
815   silc_server_start_key_exchange(server, sconn, sock);
816 }
817
818 /* Constantly tries to reconnect to a primary router indicated by the
819    `ip' and `port'. The `connected' callback will be called when the
820    connection is created. */
821
822 void silc_server_backup_reconnect(SilcServer server,
823                                   const char *ip, SilcUInt16 port,
824                                   SilcServerConnectRouterCallback callback,
825                                   void *context)
826 {
827   SilcServerConnection sconn;
828
829   SILC_LOG_INFO(("Attempting to reconnect to primary router"));
830
831   sconn = silc_calloc(1, sizeof(*sconn));
832   sconn->remote_host = strdup(ip);
833   sconn->remote_port = port;
834   sconn->callback = callback;
835   sconn->callback_context = context;
836   sconn->no_reconnect = TRUE;
837   sconn->retry_count = 0;
838   silc_schedule_task_add(server->schedule, 0,
839                          silc_server_backup_connect_to_router,
840                          sconn, 1, 0, SILC_TASK_TIMEOUT,
841                          SILC_TASK_PRI_NORMAL);
842 }
843
844 /* Task that is called after backup router has connected back to
845    primary router and we are starting the resuming protocol */
846
847 SILC_TASK_CALLBACK(silc_server_backup_connected_later)
848 {
849   SilcServerBackupProtocolContext proto_ctx =
850     (SilcServerBackupProtocolContext)context;
851   SilcServer server = proto_ctx->server;
852   SilcSocketConnection sock = proto_ctx->sock;
853
854   /* If running other protocol already run this one a bit later. */
855   if (sock->protocol) {
856     SILC_LOG_DEBUG(("Other protocol is running, wait for it to finish"));
857     silc_schedule_task_add(server->schedule, 0,
858                            silc_server_backup_connected_later,
859                            proto_ctx, 10, 0,
860                            SILC_TASK_TIMEOUT,
861                            SILC_TASK_PRI_NORMAL);
862     return;
863   }
864
865   SILC_LOG_DEBUG(("Starting backup resuming protocol as initiator"));
866   SILC_LOG_INFO(("Starting backup resuming protocol"));
867
868   /* Run the backup resuming protocol */
869   silc_protocol_alloc(SILC_PROTOCOL_SERVER_BACKUP,
870                       &sock->protocol, proto_ctx,
871                       silc_server_protocol_backup_done);
872   silc_protocol_execute(sock->protocol, server->schedule, 0, 0);
873
874   silc_schedule_task_add(server->schedule, sock->sock,
875                          silc_server_backup_timeout,
876                          sock->protocol, 30, 0, SILC_TASK_TIMEOUT,
877                          SILC_TASK_PRI_NORMAL);
878 }
879
880 /* Called when we've established connection back to our primary router
881    when we've acting as backup router and have replaced the primary router
882    in the cell. This function will start the backup resuming protocol. */
883
884 void silc_server_backup_connected(SilcServer server,
885                                   SilcServerEntry server_entry,
886                                   void *context)
887 {
888   SilcServerBackupProtocolContext proto_ctx;
889   SilcSocketConnection sock;
890
891   if (!server_entry) {
892     /* Try again */
893     SilcServerConfigRouter *primary;
894     primary = silc_server_config_get_primary_router(server);
895     if (primary) {
896       if (!silc_server_find_socket_by_host(server, SILC_SOCKET_TYPE_ROUTER,
897                                            primary->host, primary->port))
898         silc_server_backup_reconnect(server,
899                                      primary->host, primary->port,
900                                      silc_server_backup_connected,
901                                      context);
902     }
903     return;
904   }
905
906   sock = (SilcSocketConnection)server_entry->connection;
907   proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
908   proto_ctx->server = server;
909   proto_ctx->sock = silc_socket_dup(sock);
910   proto_ctx->responder = FALSE;
911   proto_ctx->type = SILC_SERVER_BACKUP_START;
912   proto_ctx->start = time(0);
913
914   /* Start through scheduler */
915   silc_schedule_task_add(server->schedule, 0,
916                          silc_server_backup_connected_later,
917                          proto_ctx, 0, 1,
918                          SILC_TASK_TIMEOUT,
919                          SILC_TASK_PRI_NORMAL);
920 }
921
922 /* Called when normal server has connected to its primary router after
923    backup router has sent the START packet in reusming protocol. We will
924    move the protocol context from the backup router connection to the
925    primary router. */
926
927 static void silc_server_backup_connect_primary(SilcServer server,
928                                                SilcServerEntry server_entry,
929                                                void *context)
930 {
931   SilcSocketConnection backup_router = (SilcSocketConnection)context;
932   SilcServerBackupProtocolContext ctx;
933   SilcSocketConnection sock;
934   SilcIDListData idata;
935   unsigned char data[2];
936
937   if (SILC_IS_DISCONNECTING(backup_router) ||
938       SILC_IS_DISCONNECTED(backup_router)) {
939     silc_socket_free(backup_router);
940     return;
941   }
942
943   if (!server_entry) {
944     /* Try again */
945     SilcServerConfigRouter *primary;
946     primary = silc_server_config_get_primary_router(server);
947     if (primary)
948       if (!silc_server_find_socket_by_host(server, SILC_SOCKET_TYPE_ROUTER,
949                                            primary->host, primary->port))
950         silc_server_backup_reconnect(server,
951                                      primary->host, primary->port,
952                                      silc_server_backup_connect_primary,
953                                      context);
954     return;
955   }
956
957   /* Unref */
958   silc_socket_free(backup_router);
959
960   if (!backup_router->protocol)
961     return;
962   if (!server_entry->connection)
963     return;
964
965   ctx = (SilcServerBackupProtocolContext)backup_router->protocol->context;
966   sock = (SilcSocketConnection)server_entry->connection;
967   idata = (SilcIDListData)server_entry;
968
969   SILC_LOG_DEBUG(("Sending CONNECTED packet (session %d)", ctx->session));
970   SILC_LOG_INFO(("Sending CONNECTED (session %d) to backup router",
971                 ctx->session));
972
973   /* Send the CONNECTED packet back to the backup router. */
974   data[0] = SILC_SERVER_BACKUP_CONNECTED;
975   data[1] = ctx->session;
976   silc_server_packet_send(server, backup_router,
977                           SILC_PACKET_RESUME_ROUTER, 0, data, 2, FALSE);
978
979   /* The primary connection is disabled until it sends the RESUMED packet
980      to us. */
981   idata->status |= SILC_IDLIST_STATUS_DISABLED;
982
983   /* Move this protocol context from this backup router connection to
984      the primary router connection since it will send the subsequent
985      packets in this protocol. We don't talk with backup router
986      anymore. */
987   sock->protocol = backup_router->protocol;
988   if (ctx->sock)
989     silc_socket_free(ctx->sock); /* unref */
990   ctx->sock = silc_socket_dup(server_entry->connection);
991   backup_router->protocol = NULL;
992 }
993
994 /* Timeout callback used by the backup router to send the ENDING packet
995    to primary router to indicate that it can now resume as being primary
996    router. All CONNECTED packets has been received when we reach this. */
997
998 SILC_TASK_CALLBACK(silc_server_backup_send_resumed)
999 {
1000   SilcProtocol protocol = (SilcProtocol)context;
1001   SilcServerBackupProtocolContext ctx = protocol->context;
1002   SilcServer server = ctx->server;
1003   unsigned char data[2];
1004   int i;
1005
1006   SILC_LOG_DEBUG(("Start"));
1007
1008   for (i = 0; i < ctx->sessions_count; i++)
1009     if (ctx->sessions[i].server_entry == ctx->sock->user_data)
1010       ctx->session = ctx->sessions[i].session;
1011
1012   /* We've received all the CONNECTED packets and now we'll send the
1013      ENDING packet to the new primary router. */
1014   data[0] = SILC_SERVER_BACKUP_ENDING;
1015   data[1] = ctx->session;
1016   silc_server_packet_send(server, ctx->sock, SILC_PACKET_RESUME_ROUTER, 0,
1017                           data, sizeof(data), FALSE);
1018
1019   /* The protocol will go to END state. */
1020   protocol->state = SILC_PROTOCOL_STATE_END;
1021 }
1022
1023 /* Backup resuming protocol. This protocol is executed when the primary
1024    router wants to resume its position as being primary router. */
1025
1026 SILC_TASK_CALLBACK_GLOBAL(silc_server_protocol_backup)
1027 {
1028   SilcProtocol protocol = (SilcProtocol)context;
1029   SilcServerBackupProtocolContext ctx = protocol->context;
1030   SilcServer server = ctx->server;
1031   SilcServerEntry server_entry;
1032   SilcSocketConnection sock = NULL;
1033   unsigned char data[2];
1034   int i;
1035
1036   if (protocol->state == SILC_PROTOCOL_STATE_UNKNOWN)
1037     protocol->state = SILC_PROTOCOL_STATE_START;
1038
1039   switch(protocol->state) {
1040   case SILC_PROTOCOL_STATE_START:
1041     if (ctx->responder == FALSE) {
1042       /*
1043        * Initiator (backup router)
1044        */
1045
1046       /* Send the START packet to primary router and normal servers. The
1047          packet will indicate to the primary router that it has been replaced
1048          by us.  For normal servers it means that we will be resigning as
1049          being primary router shortly. */
1050       for (i = 0; i < server->config->param.connections_max; i++) {
1051         sock = server->sockets[i];
1052         if (!sock || !sock->user_data ||
1053             sock->user_data == server->id_entry ||
1054             (sock->type != SILC_SOCKET_TYPE_ROUTER &&
1055              sock->type != SILC_SOCKET_TYPE_SERVER))
1056           continue;
1057
1058         server_entry = sock->user_data;
1059         if (server_entry->data.status & SILC_IDLIST_STATUS_DISABLED)
1060           continue;
1061
1062         ctx->sessions = silc_realloc(ctx->sessions,
1063                                      sizeof(*ctx->sessions) *
1064                                      (ctx->sessions_count + 1));
1065         ctx->sessions[ctx->sessions_count].session = ctx->sessions_count;
1066         ctx->sessions[ctx->sessions_count].connected = FALSE;
1067         ctx->sessions[ctx->sessions_count].server_entry = server_entry;
1068
1069         SILC_LOG_DEBUG(("Sending START to %s (session %d)",
1070                         server_entry->server_name, ctx->sessions_count));
1071         SILC_LOG_INFO(("Expecting CONNECTED from %s (session %d)",
1072                        server_entry->server_name, ctx->sessions_count));
1073
1074         /* This connection is performing this protocol too now */
1075         sock->protocol = protocol;
1076
1077         data[0] = SILC_SERVER_BACKUP_START;
1078         data[1] = ctx->sessions_count;
1079         silc_server_packet_send(server, sock, SILC_PACKET_RESUME_ROUTER, 0,
1080                                 data, sizeof(data), FALSE);
1081         ctx->sessions_count++;
1082       }
1083
1084       /* If we are not standalone and our primary is not the one we're
1085          talking to now, then announce our information to it since we
1086          haven't done that yet.  Standalone backup router announces
1087          these during connecting to the primary. */
1088       if (!server->standalone && SILC_PRIMARY_ROUTE(server) != ctx->sock) {
1089         silc_server_announce_servers(server, TRUE, 0, ctx->sock);
1090         silc_server_announce_clients(server, 0, ctx->sock);
1091         silc_server_announce_channels(server, 0, ctx->sock);
1092       }
1093
1094       protocol->state++;
1095
1096     } else {
1097       /*
1098        * Responder (all servers and routers)
1099        */
1100       SilcServerConfigRouter *primary;
1101
1102       /* We should have received START packet */
1103       if (ctx->type != SILC_SERVER_BACKUP_START) {
1104         SILC_LOG_ERROR(("Bad resume router packet START %d", ctx->type));
1105         break;
1106       }
1107
1108       /* Connect to the primary router that was down that is now supposed
1109          to be back online. We send the CONNECTED packet after we've
1110          established the connection to the primary router. */
1111       primary = silc_server_config_get_primary_router(server);
1112       if (primary && server->backup_primary &&
1113           !silc_server_num_sockets_by_remote(server,
1114                                              silc_net_is_ip(primary->host) ?
1115                                              primary->host : NULL,
1116                                              silc_net_is_ip(primary->host) ?
1117                                              NULL : primary->host,
1118                                              primary->port,
1119                                              SILC_SOCKET_TYPE_ROUTER)) {
1120         SILC_LOG_DEBUG(("Received START (session %d), reconnect to router",
1121                         ctx->session));
1122         silc_server_backup_reconnect(server,
1123                                      primary->host, primary->port,
1124                                      silc_server_backup_connect_primary,
1125                                      silc_socket_dup(ctx->sock));
1126       } else {
1127         /* Nowhere to connect just return the CONNECTED packet */
1128         SILC_LOG_DEBUG(("Received START (session %d), send CONNECTED back",
1129                         ctx->session));
1130         SILC_LOG_INFO(("Sending CONNECTED (session %d) to backup router",
1131                       ctx->session));
1132
1133         /* Send the CONNECTED packet back to the backup router. */
1134         data[0] = SILC_SERVER_BACKUP_CONNECTED;
1135         data[1] = ctx->session;
1136         silc_server_packet_send(server, ctx->sock,
1137                                 SILC_PACKET_RESUME_ROUTER, 0,
1138                                 data, sizeof(data), FALSE);
1139       }
1140
1141       /* Add this resuming session */
1142       ctx->sessions = silc_realloc(ctx->sessions,
1143                                    sizeof(*ctx->sessions) *
1144                                    (ctx->sessions_count + 1));
1145       ctx->sessions[ctx->sessions_count].session = ctx->session;
1146       ctx->sessions_count++;
1147
1148       /* Normal server goes directly to the END state. */
1149       if (server->server_type == SILC_ROUTER &&
1150           (!server->router ||
1151            server->router->data.status & SILC_IDLIST_STATUS_DISABLED))
1152         protocol->state++;
1153       else
1154         protocol->state = SILC_PROTOCOL_STATE_END;
1155     }
1156     break;
1157
1158   case 2:
1159     if (ctx->responder == FALSE) {
1160       /*
1161        * Initiator (backup router)
1162        */
1163
1164       /* We should have received CONNECTED packet */
1165       if (ctx->type != SILC_SERVER_BACKUP_CONNECTED) {
1166         SILC_LOG_ERROR(("Bad resume router packet CONNECTED %d", ctx->type));
1167         break;
1168       }
1169
1170       for (i = 0; i < ctx->sessions_count; i++) {
1171         if (ctx->sessions[i].session == ctx->session) {
1172           ctx->sessions[i].connected = TRUE;
1173           SILC_LOG_INFO(("Received CONNECTED from %s (session %d)",
1174                          ctx->sessions[i].server_entry->server_name,
1175                          ctx->session));
1176           SILC_LOG_DEBUG(("Received CONNECTED (session %d)", ctx->session));
1177           break;
1178         }
1179       }
1180
1181       /* See if all returned CONNECTED, if not, then continue waiting. */
1182       for (i = 0; i < ctx->sessions_count; i++) {
1183         if (!ctx->sessions[i].connected)
1184           return;
1185       }
1186
1187       SILC_LOG_INFO(("All sessions have returned CONNECTED packets, "
1188                      "continuing"));
1189       SILC_LOG_DEBUG(("Sending ENDING packet to primary router"));
1190
1191       /* The ENDING is sent with timeout, and then we continue to the
1192          END state in the protocol. */
1193       silc_schedule_task_add(server->schedule, 0,
1194                              silc_server_backup_send_resumed,
1195                              protocol, 1, 0, SILC_TASK_TIMEOUT,
1196                              SILC_TASK_PRI_NORMAL);
1197       return;
1198
1199     } else {
1200       /*
1201        * Responder (primary router)
1202        */
1203
1204       /* We should have been received ENDING packet */
1205       if (ctx->type != SILC_SERVER_BACKUP_ENDING) {
1206         SILC_LOG_ERROR(("Bad resume router packet ENDING %d", ctx->type));
1207         break;
1208       }
1209
1210       SILC_LOG_DEBUG(("Received ENDING packet, we are going to resume now"));
1211
1212       /* Switch announced informations to our primary router of using the
1213          backup router. */
1214       silc_server_local_servers_toggle_enabled(server, TRUE);
1215       silc_server_update_servers_by_server(server, ctx->sock->user_data,
1216                                            server->router);
1217       silc_server_update_clients_by_server(server, ctx->sock->user_data,
1218                                            server->router, TRUE);
1219
1220       /* We as primary router now must send RESUMED packets to all servers
1221          and routers so that they know we are back.   For backup router we
1222          send the packet last so that we give the backup as much time as
1223          possible to deal with message routing at this critical moment. */
1224       for (i = 0; i < server->config->param.connections_max; i++) {
1225         sock = server->sockets[i];
1226         if (!sock || !sock->user_data ||
1227             sock->user_data == server->id_entry ||
1228             (sock->type != SILC_SOCKET_TYPE_ROUTER &&
1229              sock->type != SILC_SOCKET_TYPE_SERVER))
1230           continue;
1231
1232         /* Send to backup last */
1233         if (sock == ctx->sock)
1234           continue;
1235
1236       send_to_backup:
1237         server_entry = sock->user_data;
1238         server_entry->data.status &= ~SILC_IDLIST_STATUS_DISABLED;
1239
1240         SILC_LOG_DEBUG(("Sending RESUMED to %s", server_entry->server_name));
1241         SILC_LOG_INFO(("Sending RESUMED to %s", server_entry->server_name));
1242
1243         /* This connection is performing this protocol too now */
1244         sock->protocol = protocol;
1245
1246         data[0] = SILC_SERVER_BACKUP_RESUMED;
1247         data[1] = 0;
1248         silc_server_packet_send(server, sock, SILC_PACKET_RESUME_ROUTER, 0,
1249                                 data, sizeof(data), FALSE);
1250         silc_server_packet_queue_purge(server,sock);
1251       }
1252
1253       /* Now send the same packet to backup */
1254       if (sock != ctx->sock) {
1255         sleep(1);
1256         sock = ctx->sock;
1257         goto send_to_backup;
1258       }
1259
1260       /* We are now resumed and are back as primary router in the cell. */
1261       SILC_LOG_INFO(("We are now the primary router of our cell again"));
1262       server->wait_backup = FALSE;
1263
1264       /* For us this is the end of this protocol. */
1265       if (protocol->final_callback)
1266         silc_protocol_execute_final(protocol, server->schedule);
1267       else
1268         silc_protocol_free(protocol);
1269     }
1270     break;
1271
1272   case SILC_PROTOCOL_STATE_END:
1273     {
1274       /*
1275        * Responder (backup router, servers, and remote router)
1276        */
1277       SilcServerEntry router, backup_router;
1278
1279       /* We should have been received RESUMED from our primary router. */
1280       if (ctx->type != SILC_SERVER_BACKUP_RESUMED) {
1281         SILC_LOG_ERROR(("Bad resume router packet RESUMED %d", ctx->type));
1282         break;
1283       }
1284
1285       SILC_LOG_INFO(("Received RESUMED from new primary router"));
1286
1287       /* If we are the backup router, mark that we are no longer primary
1288          but are back to backup router status. */
1289       if (server->backup_router)
1290         server->server_type = SILC_BACKUP_ROUTER;
1291
1292       /* We have now new primary router. All traffic goes there from now on. */
1293       router = ctx->sock->user_data;
1294       if (silc_server_backup_replaced_get(server, router->id,
1295                                           &backup_router)) {
1296
1297         if (backup_router == server->router) {
1298           /* We have new primary router now */
1299           server->id_entry->router = router;
1300           server->router = router;
1301           SILC_LOG_INFO(("Switching back to primary router %s",
1302                          server->router->server_name));
1303         } else {
1304           /* We are connected to new primary and now continue using it */
1305           SILC_LOG_INFO(("Resuming the use of primary router %s",
1306                          router->server_name));
1307         }
1308         server->backup_primary = FALSE;
1309         sock = router->connection;
1310
1311         /* Update the client entries of the backup router to the new
1312            router */
1313         silc_server_local_servers_toggle_enabled(server, FALSE);
1314         router->data.status &= ~SILC_IDLIST_STATUS_DISABLED;
1315         silc_server_update_servers_by_server(server, backup_router, router);
1316         silc_server_update_clients_by_server(server, NULL, router, FALSE);
1317         if (server->server_type == SILC_SERVER)
1318           silc_server_update_channels_by_server(server, backup_router, router);
1319         silc_server_backup_replaced_del(server, backup_router);
1320       }
1321
1322       /* Send notify about primary router going down to local operators */
1323       SILC_SERVER_SEND_OPERS(server, FALSE, TRUE,
1324                              SILC_NOTIFY_TYPE_NONE,
1325                              ("%s resumed the use of primary router %s",
1326                               server->server_name,
1327                               server->router->server_name));
1328
1329       /* Protocol has ended, call the final callback */
1330       if (protocol->final_callback)
1331         silc_protocol_execute_final(protocol, server->schedule);
1332       else
1333         silc_protocol_free(protocol);
1334     }
1335     break;
1336
1337   case SILC_PROTOCOL_STATE_ERROR:
1338     /* Protocol has ended, call the final callback */
1339     if (protocol->final_callback)
1340       silc_protocol_execute_final(protocol, server->schedule);
1341     else
1342       silc_protocol_free(protocol);
1343     break;
1344
1345   case SILC_PROTOCOL_STATE_FAILURE:
1346     /* Protocol has ended, call the final callback */
1347     SILC_LOG_ERROR(("Error during backup resume: received Failure"));
1348     ctx->received_failure = TRUE;
1349     if (protocol->final_callback)
1350       silc_protocol_execute_final(protocol, server->schedule);
1351     else
1352       silc_protocol_free(protocol);
1353     break;
1354
1355   case SILC_PROTOCOL_STATE_UNKNOWN:
1356     break;
1357   }
1358 }
1359
1360 /* Final resuming protocol completion callback */
1361
1362 SILC_TASK_CALLBACK(silc_server_protocol_backup_done)
1363 {
1364   SilcProtocol protocol = (SilcProtocol)context;
1365   SilcServerBackupProtocolContext ctx = protocol->context;
1366   SilcServer server = ctx->server;
1367   SilcServerEntry server_entry;
1368   SilcSocketConnection sock;
1369   bool error;
1370   int i;
1371
1372   silc_schedule_task_del_by_context(server->schedule, protocol);
1373
1374   error = (protocol->state == SILC_PROTOCOL_STATE_ERROR ||
1375            protocol->state == SILC_PROTOCOL_STATE_FAILURE);
1376
1377   if (error) {
1378     SILC_LOG_ERROR(("Error occurred during backup router resuming protcool"));
1379     if (server->server_type == SILC_SERVER)
1380       silc_schedule_task_del_by_callback(server->schedule,
1381                                          silc_server_backup_connect_to_router);
1382   }
1383
1384   if (server->server_shutdown)
1385     return;
1386
1387   /* Remove this protocol from all server entries that has it */
1388   for (i = 0; i < server->config->param.connections_max; i++) {
1389     sock = server->sockets[i];
1390     if (!sock || !sock->user_data ||
1391         (sock->type != SILC_SOCKET_TYPE_ROUTER &&
1392          sock->type != SILC_SOCKET_TYPE_SERVER))
1393       continue;
1394
1395     server_entry = sock->user_data;
1396
1397     /* The SilcProtocol context was shared between all connections, clear
1398        it from all connections. */
1399     if (sock->protocol == protocol) {
1400       sock->protocol = NULL;
1401
1402       if (error) {
1403
1404         if (server->server_type == SILC_SERVER &&
1405             server_entry->server_type == SILC_ROUTER)
1406           continue;
1407
1408         /* Backup router */
1409         if (SILC_PRIMARY_ROUTE(server) == sock && server->backup_router) {
1410           if (ctx->sock == sock) {
1411             silc_socket_free(sock); /* unref */
1412             ctx->sock = NULL;
1413           }
1414
1415           if (!ctx->received_failure) {
1416             /* Protocol error, probably timeout. Just restart the protocol. */
1417             SilcServerBackupProtocolContext proto_ctx;
1418
1419             /* Restart the protocol. */
1420             proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
1421             proto_ctx->server = server;
1422             proto_ctx->sock = silc_socket_dup(sock);
1423             proto_ctx->responder = FALSE;
1424             proto_ctx->type = SILC_SERVER_BACKUP_START;
1425             proto_ctx->start = time(0);
1426
1427             /* Start through scheduler */
1428             silc_schedule_task_add(server->schedule, 0,
1429                                    silc_server_backup_connected_later,
1430                                    proto_ctx, 2, 0,
1431                                    SILC_TASK_TIMEOUT,
1432                                    SILC_TASK_PRI_NORMAL);
1433           } else {
1434             /* If failure was received, switch back to normal backup router.
1435                For some reason primary wouldn't accept that we were supposed
1436                to perfom resuming protocol. */
1437             server->server_type = SILC_BACKUP_ROUTER;
1438             silc_server_local_servers_toggle_enabled(server, FALSE);
1439             silc_server_update_servers_by_server(server, server->id_entry,
1440                                                  sock->user_data);
1441             silc_server_update_clients_by_server(server, NULL,
1442                                                  sock->user_data, FALSE);
1443
1444             /* Announce our clients and channels to the router */
1445             silc_server_announce_clients(server, ctx->start, sock);
1446             silc_server_announce_channels(server, ctx->start, sock);
1447           }
1448
1449           continue;
1450         }
1451       }
1452
1453       server_entry->data.status &= ~SILC_IDLIST_STATUS_DISABLED;
1454     }
1455   }
1456
1457   if (!error) {
1458     SILC_LOG_INFO(("Backup resuming protocol ended successfully"));
1459
1460     if (ctx->type == SILC_SERVER_BACKUP_RESUMED && server->router) {
1461       /* Announce all of our information to the router. */
1462       if (server->server_type == SILC_ROUTER)
1463         silc_server_announce_servers(server, FALSE, ctx->start,
1464                                      server->router->connection);
1465
1466       /* Announce our clients and channels to the router */
1467       silc_server_announce_clients(server, ctx->start,
1468                                    server->router->connection);
1469       silc_server_announce_channels(server, ctx->start,
1470                                     server->router->connection);
1471     }
1472   } else {
1473     /* Error */
1474
1475     if (server->server_type == SILC_SERVER) {
1476       /* If we are still using backup router Send confirmation to backup
1477          that using it is still ok and continue sending traffic there.
1478          The backup will reply with error if it's not ok. */
1479       if (server->router && server->backup_primary) {
1480         /* Send START_USE just in case using backup wouldn't be ok. */
1481         silc_server_backup_send_start_use(server, server->router->connection,
1482                                           FALSE);
1483
1484         /* Check couple of times same START_USE just in case. */
1485         silc_schedule_task_add(server->schedule, 0,
1486                                silc_server_backup_check_status,
1487                                silc_socket_dup(server->router->connection),
1488                                5, 1, SILC_TASK_TIMEOUT,
1489                                SILC_TASK_PRI_NORMAL);
1490         silc_schedule_task_add(server->schedule, 0,
1491                                silc_server_backup_check_status,
1492                                silc_socket_dup(server->router->connection),
1493                                20, 1, SILC_TASK_TIMEOUT,
1494                                SILC_TASK_PRI_NORMAL);
1495         silc_schedule_task_add(server->schedule, 0,
1496                                silc_server_backup_check_status,
1497                                silc_socket_dup(server->router->connection),
1498                                60, 1, SILC_TASK_TIMEOUT,
1499                                SILC_TASK_PRI_NORMAL);
1500       }
1501     }
1502   }
1503
1504   if (ctx->sock && ctx->sock->protocol)
1505     ctx->sock->protocol = NULL;
1506   if (ctx->sock)
1507     silc_socket_free(ctx->sock); /* unref */
1508   silc_protocol_free(protocol);
1509   silc_free(ctx->sessions);
1510   silc_free(ctx);
1511 }