2ca105b3a9ce47b4dd1429dadd709148bef2aa13
[silc.git] / apps / silcd / server_backup.h
1 /*
2
3   server_backup.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 - 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; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #ifndef SERVER_BACKUP_H
21 #define SERVER_BACKUP_H
22
23 /* Backup resuming protocol types */
24 #define SILC_SERVER_BACKUP_START            1
25 #define SILC_SERVER_BACKUP_START_GLOBAL     2
26 #define SILC_SERVER_BACKUP_CONNECTED        3
27 #define SILC_SERVER_BACKUP_ENDING           4
28 #define SILC_SERVER_BACKUP_RESUMED          5
29 #define SILC_SERVER_BACKUP_RESUMED_GLOBAL   6
30 #define SILC_SERVER_BACKUP_REPLACED         20
31
32 /* Adds the `backup_server' to be one of our backup router. This can be
33    called multiple times to set multiple backup routers. The `replacing' is
34    the IP and port that the `backup_router' will replace if the `replacing'
35    will become unresponsive. If `local' is TRUE then the `backup_server' is
36    in the local cell, if FALSE it is in some other cell. */
37 void silc_server_backup_add(SilcServer server, SilcServerEntry backup_server,
38                             const char *ip, int port, bool local);
39
40 /* Returns backup router for IP and port in `replacing' or NULL if there
41    does not exist backup router. */
42 SilcServerEntry silc_server_backup_get(SilcServer server, 
43                                        SilcServerID *server_id);
44
45 /* Deletes the backup server `server_entry'. */
46 void silc_server_backup_del(SilcServer server, SilcServerEntry server_entry);
47
48 /* Frees all data allocated for backup routers.  Call this after deleting
49    all backup routers and when new routers are added no more, for example
50    when shutting down the server. */
51 void silc_server_backup_free(SilcServer server);
52
53 /* Marks the IP address and port from the `server_id' as  being replaced
54    by backup router indicated by the `server'. If the router connects at
55    a later time we can check whether it has been replaced by an backup
56    router. */
57 void silc_server_backup_replaced_add(SilcServer server, 
58                                      SilcServerID *server_id,
59                                      SilcServerEntry server_entry);
60
61 /* Checks whether the IP address and port from the `server_id' has been
62    replaced by an backup router. If it has been then this returns TRUE
63    and the bacup router entry to the `server' pointer if non-NULL. Returns
64    FALSE if the router is not replaced by backup router. */
65 bool silc_server_backup_replaced_get(SilcServer server,
66                                      SilcServerID *server_id,
67                                      SilcServerEntry *server_entry);
68
69 /* Deletes a replaced host by the set `server_entry. */
70 void silc_server_backup_replaced_del(SilcServer server,
71                                      SilcServerEntry server_entry);
72
73 /* Broadcast the received packet indicated by `packet' to all of our backup 
74    routers. All router wide information is passed using broadcast packets. 
75    That is why all backup routers need to get this data too. It is expected
76    that the caller already knows that the `packet' is broadcast packet. */
77 void silc_server_backup_broadcast(SilcServer server, 
78                                   SilcSocketConnection sender,
79                                   SilcPacketContext *packet);
80
81 /* A generic routine to send data to all backup routers. If the `sender'
82    is provided it will indicate the original sender of the packet and the
83    packet won't be resent to that entity. The `data' is the data that will
84    be assembled to packet context before sending. The packet will be
85    encrypted this function. If the `force_send' is TRUE the data is sent
86    immediately and not put to queue. If `local' is TRUE then the packet
87    will be sent only to local backup routers inside the cell. If false the
88    packet can go from one cell to the other. This function has no effect
89    if there are no any backup routers. */
90 void silc_server_backup_send(SilcServer server,
91                              SilcServerEntry sender,
92                              SilcPacketType type,
93                              SilcPacketFlags flags,
94                              unsigned char *data,
95                              SilcUInt32 data_len,
96                              bool force_send,
97                              bool local);
98
99 /* Same as silc_server_backup_send but sets a specific Destination ID to
100    the packet. The Destination ID is indicated by the `dst_id' and the
101    ID type `dst_id_type'. For example, packets destined to channels must
102    be sent using this function. */
103 void silc_server_backup_send_dest(SilcServer server,
104                                   SilcServerEntry sender,
105                                   SilcPacketType type,
106                                   SilcPacketFlags flags,
107                                   void *dst_id,
108                                   SilcIdType dst_id_type,
109                                   unsigned char *data,
110                                   SilcUInt32 data_len,
111                                   bool force_send,
112                                   bool local);
113
114 /* Processes incoming RESUME_ROUTER packet. This can give the packet
115    for processing to the protocol handler or allocate new protocol if
116    start command is received. */
117 void silc_server_backup_resume_router(SilcServer server, 
118                                       SilcSocketConnection sock, 
119                                       SilcPacketContext *packet);
120
121 /* Constantly tries to reconnect to a primary router indicated by the
122    `ip' and `port'. The `connected' callback will be called when the
123    connection is created. */
124 void silc_server_backup_reconnect(SilcServer server,
125                                   const char *ip, SilcUInt16 port,
126                                   SilcServerConnectRouterCallback callback,
127                                   void *context);
128
129 /* Called when we've established connection back to our primary router
130    when we've acting as backup router and have replaced the primary router
131    in the cell. This function will start the backup resuming protocol. */
132 void silc_server_backup_connected(SilcServer server,
133                                   SilcServerEntry server_entry,
134                                   void *context);
135
136 /* Backup resuming protocol. This protocol is executed when the primary
137    router wants to resume its position as being primary router. */
138 SILC_TASK_CALLBACK_GLOBAL(silc_server_protocol_backup);
139
140 #endif /* SERVER_BACKUP_H */