silcd: handle SIGNOFF notify for local clients correctly
[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 - 2009 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_INTERNAL_H
21 #define SERVER_INTERNAL_H
22
23 /* Server statistics structure. This holds various statistics about
24    various things. */
25 typedef struct {
26   /* Local stats (server and router) */
27   SilcUInt32 my_clients;                  /* Locally connected clients */
28   SilcUInt32 my_servers;                  /* Locally connected servers */
29   SilcUInt32 my_routers;                  /* Locally connected routers */
30   SilcUInt32 my_channels;                 /* Locally created channels */
31   SilcUInt32 my_chanclients;              /* Local clients on local channels */
32   SilcUInt32 my_aways;                    /* Local clients away (gone) */
33   SilcUInt32 my_detached;                 /* Local clients detached */
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 aways;                       /* All clients away (gone) */
48   SilcUInt32 detached;                    /* All clients detached */
49   SilcUInt32 server_ops;                  /* All server operators */
50   SilcUInt32 router_ops;                  /* All router operators */
51   /* More to add
52   SilcUInt32 secret_channels;
53   SilcUInt32 private_channels;
54   */
55
56   /* General */
57   SilcUInt32 conn_attempts;               /* Connection attempts */
58   SilcUInt32 conn_failures;               /* Connection failure */
59   SilcUInt32 auth_attempts;               /* Authentication attempts */
60   SilcUInt32 auth_failures;               /* Authentication failures */
61   SilcUInt32 packets_sent;                /* Sent SILC packets */
62   SilcUInt32 packets_received;            /* Received SILC packets */
63   SilcUInt32 conn_num;                    /* Number of connections */
64   SilcUInt32 commands_sent;               /* Commands/replies sent */
65   SilcUInt32 commands_received;           /* Commands/replies received */
66 } SilcServerStatistics;
67
68 /*
69    SILC Server Object.
70
71 */
72 struct SilcServerStruct {
73   SilcSchedule schedule;             /* Server scheduler */
74   SilcDList listeners;               /* TCP listeners */
75   SilcPacketEngine packet_engine;    /* Packet engine */
76   SilcDList conns;                   /* Connections in server */
77   SilcSKR repository;                /* Public key repository */
78   SilcPublicKey public_key;          /* Server public key */
79   SilcPrivateKey private_key;        /* Server private key */
80   SilcDList expired_clients;         /* Expired client entries */
81   SilcHttpServer httpd;              /* HTTP server */
82
83   char *server_name;                 /* Server's name */
84   SilcServerEntry id_entry;          /* Server's local entry */
85   SilcServerID *id;                  /* Server's ID */
86   unsigned char id_string[32];       /* Server's ID as string */
87   SilcUInt32 id_string_len;
88   SilcUInt32 starttime;              /* Server start time */
89
90   SilcServerEntry router;            /* Pointer to the primary router */
91   unsigned long router_connect;      /* Time when router was connected */
92   SilcServerConnection router_conn;  /* non-NULL when connecting to the
93                                         primary router, and NULL otherwise. */
94   SilcServerBackup backup;           /* Backup routers */
95
96   /* Current command identifier, 0 not used */
97   SilcUInt16 cmd_ident;
98
99   /* ID lists. */
100   SilcIDList local_list;
101   SilcIDList global_list;
102   SilcHashTable watcher_list;
103   SilcHashTable watcher_list_pk;
104
105   /* Hash objects for general hashing */
106   SilcHash md5hash;
107   SilcHash sha1hash;
108
109   /* Configuration object */
110   SilcServerConfig config;
111   SilcServerConfigRef config_ref;
112   char *config_file;
113
114   /* Random pool */
115   SilcRng rng;
116
117   /* Server statistics */
118   SilcServerStatistics stat;
119
120   /* Pending command queue */
121   SilcDList pending_commands;
122
123   unsigned int server_type    : 2;   /* Server type (server.h) */
124   unsigned int standalone     : 1;   /* Set if server is standalone, and
125                                         does not have connection to network. */
126   unsigned int listenning     : 1;   /* Set if server is listenning for
127                                         incoming connections. */
128   unsigned int background     : 1;   /* Set when server is on background */
129   unsigned int backup_router  : 1;   /* Set if this is backup router */
130   unsigned int backup_primary : 1;   /* Set if we've switched our primary
131                                         router to a backup router. */
132   unsigned int backup_noswitch: 1;   /* Set if we've won't switch to
133                                         become primary (we are backup) */
134   unsigned int backup_closed  : 1;   /* Set if backup closed connection.
135                                         Do not allow resuming in this case. */
136   unsigned int wait_backup    : 1;   /* Set if we are waiting for backup
137                                         router to connect to us. */
138   unsigned int server_shutdown: 1;   /* Set when shutting down */
139   unsigned int no_reconnect   : 1;   /* If set, server won't reconnect to
140                                         router after disconnection. */
141   unsigned int no_conf        : 1;   /* Set when connecting without
142                                         configuration. */
143 };
144
145 /* Failure context. This is allocated when failure packet is received.
146    Failure packets are processed with timeout and data is saved in this
147    structure. */
148 typedef struct {
149   SilcPacketStream sock;
150   SilcUInt32 failure;
151 } *SilcServerFailureContext;
152
153 /* Rekey must be performed at the lastest when this many packets is sent */
154 #define SILC_SERVER_REKEY_THRESHOLD 0xfffffe00
155
156 /* Macros */
157
158 /* Return pointer to the primary router connection */
159 #define SILC_PRIMARY_ROUTE(server) \
160   (!server->standalone && server->router ? server->router->connection : NULL)
161
162 /* Return TRUE if a packet must be broadcasted (router broadcasts) */
163 #define SILC_BROADCAST(server) (server->server_type == SILC_ROUTER)
164
165 /* Return TRUE if entry is locally connected or local to us */
166 #define SILC_IS_LOCAL(entry) \
167   (((SilcIDListData)entry)->status & SILC_IDLIST_STATUS_LOCAL)
168
169 #define SILC_OPER_STATS_UPDATE(c, type, mod)    \
170 do {                                            \
171   if ((c)->mode & (mod)) {                      \
172     if (SILC_IS_LOCAL((c)))                     \
173       server->stat.my_ ## type ## _ops--;       \
174     if (server->server_type == SILC_ROUTER)     \
175       server->stat. type ## _ops--;             \
176     (c)->mode &= ~(mod);                        \
177   }                                             \
178 } while(0)
179
180 #define SILC_UMODE_STATS_UPDATE(oper, mod)      \
181 do {                                            \
182     if (client->mode & (mod)) {                 \
183       if (!(mode & (mod))) {                    \
184         if (SILC_IS_LOCAL(client))              \
185           server->stat.my_ ## oper ## _ops--;   \
186         if (server->server_type == SILC_ROUTER) \
187           server->stat. oper ## _ops--;         \
188       }                                         \
189     } else {                                    \
190       if (mode & (mod)) {                       \
191         if (SILC_IS_LOCAL(client))              \
192           server->stat.my_ ## oper ## _ops++;   \
193         if (server->server_type == SILC_ROUTER) \
194           server->stat. oper ## _ops++;         \
195       }                                         \
196     }                                           \
197 } while(0)
198
199 #define SILC_GET_SKE_FLAGS(x, p)                        \
200   if ((x)) {                                            \
201     if ((x)->param && (x)->param->key_exchange_pfs)     \
202       (p) |= SILC_SKE_SP_FLAG_PFS;                      \
203     if (!(x)->publickeys)                               \
204       (p) |= SILC_SKE_SP_FLAG_MUTUAL;                   \
205   }
206
207 #define SILC_CONNTYPE_STRING(ctype)                     \
208   (ctype == SILC_CONN_CLIENT ? "Client" :               \
209    ctype == SILC_CONN_SERVER ? "Server" :               \
210    ctype == SILC_CONN_ROUTER ? "Router" : "Unknown")
211
212 /* Prototypes */
213 SILC_TASK_CALLBACK(silc_server_rekey_final);
214 SILC_TASK_CALLBACK(silc_server_rekey_callback);
215 SILC_TASK_CALLBACK(silc_server_connect_to_router);
216 SILC_TASK_CALLBACK(silc_server_connect_to_router_retry);
217 void silc_server_watcher_list_destroy(void *key, void *context,
218                                       void *user_context);
219 void silc_server_connection_free(SilcServerConnection sconn);
220
221 #endif