Backup router related fixes.
[silc.git] / apps / silcd / server_internal.h
1 /*
2
3   server_internal.h
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 #ifndef SERVER_INTERNAL_H
22 #define SERVER_INTERNAL_H
23
24 /* Server statistics structure. This holds various statistics about
25    various things. */
26 typedef struct {
27   /* Local stats (server and router) */
28   SilcUInt32 my_clients;                  /* Locally connected clients */
29   SilcUInt32 my_servers;                  /* Locally connected servers */
30   SilcUInt32 my_routers;                  /* Locally connected routers */
31   SilcUInt32 my_channels;                 /* Locally created channels */
32   SilcUInt32 my_chanclients;      /* Local clients on local channels */
33   SilcUInt32 my_aways;            /* Local clients away (XXX) */
34   SilcUInt32 my_server_ops;               /* Local server operators */
35   SilcUInt32 my_router_ops;               /* Local router operators */
36
37   /* Global stats (mainly for router) */
38   SilcUInt32 cell_clients;                /* All clients in cell */
39   SilcUInt32 cell_servers;                /* All servers in cell */
40   SilcUInt32 cell_channels;               /* All channels in cell */
41   SilcUInt32 cell_chanclients;    /* All clients on cell's channels */
42   SilcUInt32 clients;             /* All clients */
43   SilcUInt32 servers;             /* All servers */
44   SilcUInt32 routers;             /* All routers */
45   SilcUInt32 channels;            /* All channels */
46   SilcUInt32 chanclients;                 /* All clients on channels */
47   SilcUInt32 server_ops;                  /* All server operators */
48   SilcUInt32 router_ops;                  /* All router operators */
49
50   /* General */
51   SilcUInt32 conn_attempts;               /* Connection attempts */
52   SilcUInt32 conn_failures;               /* Connection failure */
53   SilcUInt32 auth_attempts;               /* Authentication attempts */
54   SilcUInt32 auth_failures;               /* Authentication failures */
55   SilcUInt32 packets_sent;                /* Sent packets */
56   SilcUInt32 packets_received;    /* Received packets */
57 } SilcServerStatistics;
58
59 /*
60    SILC Server Object.
61
62 */
63 struct SilcServerStruct {
64   char *server_name;
65   int server_type;
66   int sock;
67   SilcServerID *id;
68   unsigned char *id_string;
69   SilcUInt32 id_string_len;
70   SilcIdType id_type;
71
72   SilcUInt32 starttime;
73   bool standalone;                   /* TRUE if server is standalone, and
74                                         does not have connection to network. */
75   bool listenning;                   /* TRUE if server is listenning for
76                                         incoming connections. */
77   bool background;
78   SilcServerEntry id_entry;          /* Server's own ID entry */
79   SilcServerEntry router;            /* Pointer to the primary router */
80   unsigned long router_connect;      /* Time when router was connected */
81   SilcServerBackup backup;           /* Backup routers */
82   bool backup_router;                /* TRUE if this is backup router */
83   bool backup_primary;               /* TRUE if we've switched our primary
84                                         router to a backup router. */
85   SilcServerConnection router_conn;  /* non-NULL when connecting to the
86                                         primary router, and NULL otherwise. */
87
88   /* Current command identifier, 0 not used */
89   SilcUInt16 cmd_ident;
90
91   /* SILC server scheduler */
92   SilcSchedule schedule;
93
94   /* ID lists. */
95   SilcIDList local_list;
96   SilcIDList global_list;
97   SilcHashTable watcher_list;
98
99   /* Table of connected sockets */
100   SilcSocketConnection *sockets;
101
102   /* Server public key */
103   SilcPKCS pkcs;
104   SilcPublicKey public_key;
105   SilcPrivateKey private_key;
106
107   /* Hash objects for general hashing */
108   SilcHash md5hash;
109   SilcHash sha1hash;
110
111   /* HMAC objects for MAC's. */
112   SilcHmac md5hmac;
113   SilcHmac sha1hmac;
114
115   /* Configuration object */
116   SilcServerConfig config;
117   SilcServerConfigRef config_ref;
118   char *config_file;
119
120   /* Random pool */
121   SilcRng rng;
122
123   /* Server statistics */
124   SilcServerStatistics stat;
125
126   /* Pending command queue */
127   SilcDList pending_commands;
128
129 #ifdef SILC_SIM
130   /* SIM (SILC Module) list */
131   SilcDList sim;
132 #endif
133 };
134
135 /* Server's heartbeat context */
136 typedef struct {
137   SilcServer server;
138 } *SilcServerHBContext;
139
140 /* Failure context. This is allocated when failure packet is received.
141    Failure packets are processed with timeout and data is saved in this
142    structure. */
143 typedef struct {
144   SilcServer server;
145   SilcSocketConnection sock;
146   SilcUInt32 failure;
147 } *SilcServerFailureContext;
148
149 /* Macros */
150
151 /* Registers generic task for file descriptor for reading from network and
152    writing to network. As being generic task the actual task is allocated 
153    only once and after that the same task applies to all registered fd's. */
154 #define SILC_REGISTER_CONNECTION_FOR_IO(fd)             \
155 do {                                                    \
156   silc_schedule_task_add(server->schedule, (fd),        \
157                          silc_server_packet_process,    \
158                          context, 0, 0,                 \
159                          SILC_TASK_GENERIC,             \
160                          SILC_TASK_PRI_NORMAL);         \
161 } while(0)
162
163 #define SILC_SET_CONNECTION_FOR_INPUT(s, fd)                    \
164 do {                                                            \
165   silc_schedule_set_listen_fd((s), (fd), SILC_TASK_READ);       \
166 } while(0)
167      
168 #define SILC_SET_CONNECTION_FOR_OUTPUT(s, fd)                                 \
169 do {                                                                          \
170   silc_schedule_set_listen_fd((s), (fd), (SILC_TASK_READ | SILC_TASK_WRITE)); \
171 } while(0)
172
173 #define SILC_OPER_STATS_UPDATE(c, type, mod)    \
174 do {                                            \
175   if ((c)->mode & (mod)) {                      \
176     if ((c)->connection)                        \
177       server->stat.my_ ## type ## _ops--;       \
178     if (server->server_type == SILC_ROUTER)     \
179       server->stat. type ## _ops--;             \
180   }                                             \
181 } while(0)
182
183 #define SILC_UMODE_STATS_UPDATE(oper, mod)      \
184 do {                                            \
185     if (client->mode & (mod)) {                 \
186       if (!(mode & (mod))) {                    \
187         if (client->connection)                 \
188           server->stat.my_ ## oper ## _ops--;   \
189         if (server->server_type == SILC_ROUTER) \
190           server->stat. oper ## _ops--;         \
191       }                                         \
192     } else {                                    \
193       if (mode & (mod)) {                       \
194         if (client->connection)                 \
195           server->stat.my_ ## oper ## _ops++;   \
196         if (server->server_type == SILC_ROUTER) \
197           server->stat. oper ## _ops++;         \
198       }                                         \
199     }                                           \
200 } while(0)
201
202 #define SILC_GET_SKE_FLAGS(x, p)                        \
203   if ((x)) {                                            \
204     if ((x)->param && (x)->param->key_exchange_pfs)     \
205       (p)->flags |= SILC_SKE_SP_FLAG_PFS;               \
206     if (!(x)->publickeys)                               \
207       (p)->flags |= SILC_SKE_SP_FLAG_MUTUAL;            \
208   }
209
210 /* Prototypes */
211 SILC_TASK_CALLBACK_GLOBAL(silc_server_rekey_final);
212 void silc_server_watcher_list_destroy(void *key, void *context,
213                                       void *user_context);
214
215 #endif