Merged silc_1_0_branch to trunk.
[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 - 2005 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   char *server_name;
74   int sock;
75   SilcServerEntry id_entry;
76   SilcServerID *id;
77   unsigned char *id_string;
78   SilcUInt32 id_string_len;
79   SilcUInt32 starttime;
80
81   unsigned int server_type    : 2;   /* Server type (server.h) */
82   unsigned int standalone     : 1;   /* Set if server is standalone, and
83                                         does not have connection to network. */
84   unsigned int listenning     : 1;   /* Set if server is listenning for
85                                         incoming connections. */
86   unsigned int background     : 1;   /* Set when server is on background */
87   unsigned int backup_router  : 1;   /* Set if this is backup router */
88   unsigned int backup_primary : 1;   /* Set if we've switched our primary
89                                         router to a backup router. */
90   unsigned int backup_noswitch: 1;   /* Set if we've won't switch to
91                                         become primary (we are backup) */
92   unsigned int backup_closed  : 1;   /* Set if backup closed connection.
93                                         Do not allow resuming in this case. */
94   unsigned int wait_backup    : 1;   /* Set if we are waiting for backup
95                                         router to connect to us. */
96   unsigned int server_shutdown: 1;   /* Set when shutting down */
97   unsigned int no_reconnect   : 1;   /* If set, server won't reconnect to
98                                         router after disconnection. */
99
100   SilcServerEntry router;            /* Pointer to the primary router */
101   unsigned long router_connect;      /* Time when router was connected */
102   SilcServerConnection router_conn;  /* non-NULL when connecting to the
103                                         primary router, and NULL otherwise. */
104   SilcServerBackup backup;           /* Backup routers */
105
106   /* Current command identifier, 0 not used */
107   SilcUInt16 cmd_ident;
108
109   /* SILC server scheduler */
110   SilcSchedule schedule;
111
112   /* ID lists. */
113   SilcIDList local_list;
114   SilcIDList global_list;
115   SilcHashTable watcher_list;
116   SilcHashTable watcher_list_pk;
117
118   /* Table of connected sockets */
119   SilcSocketConnection *sockets;
120
121   /* Server public key */
122   SilcPKCS pkcs;
123   SilcPublicKey public_key;
124   SilcPrivateKey private_key;
125
126   /* Hash objects for general hashing */
127   SilcHash md5hash;
128   SilcHash sha1hash;
129
130   /* Configuration object */
131   SilcServerConfig config;
132   SilcServerConfigRef config_ref;
133   char *config_file;
134
135   /* Random pool */
136   SilcRng rng;
137
138   /* Server statistics */
139   SilcServerStatistics stat;
140
141   /* Pending command queue */
142   SilcDList pending_commands;
143
144   /* Purge context for disconnected clients */
145   SilcIDListPurge purge_i;
146   SilcIDListPurge purge_g;
147
148 #ifdef SILC_SIM
149   /* SIM (SILC Module) list */
150   SilcDList sim;
151 #endif
152
153   /* Hash table for public keys of all clients */
154   SilcHashTable pk_hash;
155 };
156
157 /* Failure context. This is allocated when failure packet is received.
158    Failure packets are processed with timeout and data is saved in this
159    structure. */
160 typedef struct {
161   SilcSocketConnection sock;
162   SilcUInt32 failure;
163 } *SilcServerFailureContext;
164
165 /* Rekey must be performed at the lastest when this many packets is sent */
166 #define SILC_SERVER_REKEY_THRESHOLD 0xfffffe00
167
168 /* Macros */
169
170 /* Return pointer to the primary router connection */
171 #define SILC_PRIMARY_ROUTE(server) \
172   (!server->standalone && server->router ? server->router->connection : NULL)
173
174 /* Return TRUE if a packet must be broadcasted (router broadcasts) */
175 #define SILC_BROADCAST(server) (server->server_type == SILC_ROUTER)
176
177 /* Return TRUE if entry is locally connected or local to us */
178 #define SILC_IS_LOCAL(entry) \
179   (((SilcIDListData)entry)->status & SILC_IDLIST_STATUS_LOCAL)
180
181 /* Registers generic task for file descriptor for reading from network and
182    writing to network. As being generic task the actual task is allocated
183    only once and after that the same task applies to all registered fd's. */
184 #define SILC_REGISTER_CONNECTION_FOR_IO(fd)             \
185 do {                                                    \
186   silc_schedule_task_add(server->schedule, (fd),        \
187                          silc_server_packet_process,    \
188                          context, 0, 0,                 \
189                          SILC_TASK_GENERIC,             \
190                          SILC_TASK_PRI_NORMAL);         \
191 } while(0)
192
193 #define SILC_SET_CONNECTION_FOR_INPUT(s, fd)                            \
194 do {                                                                    \
195   silc_schedule_set_listen_fd((s), (fd), SILC_TASK_READ, FALSE);        \
196 } while(0)
197
198 #define SILC_SET_CONNECTION_FOR_OUTPUT(s, fd)                                \
199 do {                                                                         \
200   silc_schedule_set_listen_fd((s), (fd), (SILC_TASK_READ | SILC_TASK_WRITE), \
201                               FALSE);                                        \
202 } while(0)
203
204 #define SILC_OPER_STATS_UPDATE(c, type, mod)    \
205 do {                                            \
206   if ((c)->mode & (mod)) {                      \
207     if (SILC_IS_LOCAL((c)))                     \
208       server->stat.my_ ## type ## _ops--;       \
209     if (server->server_type == SILC_ROUTER)     \
210       server->stat. type ## _ops--;             \
211     (c)->mode &= ~(mod);                        \
212   }                                             \
213 } while(0)
214
215 #define SILC_UMODE_STATS_UPDATE(oper, mod)      \
216 do {                                            \
217     if (client->mode & (mod)) {                 \
218       if (!(mode & (mod))) {                    \
219         if (SILC_IS_LOCAL(client))              \
220           server->stat.my_ ## oper ## _ops--;   \
221         if (server->server_type == SILC_ROUTER) \
222           server->stat. oper ## _ops--;         \
223       }                                         \
224     } else {                                    \
225       if (mode & (mod)) {                       \
226         if (SILC_IS_LOCAL(client))              \
227           server->stat.my_ ## oper ## _ops++;   \
228         if (server->server_type == SILC_ROUTER) \
229           server->stat. oper ## _ops++;         \
230       }                                         \
231     }                                           \
232 } while(0)
233
234 #define SILC_GET_SKE_FLAGS(x, p)                        \
235   if ((x)) {                                            \
236     if ((x)->param && (x)->param->key_exchange_pfs)     \
237       (p)->flags |= SILC_SKE_SP_FLAG_PFS;               \
238     if (!(x)->publickeys)                               \
239       (p)->flags |= SILC_SKE_SP_FLAG_MUTUAL;            \
240   }
241
242 /* Prototypes */
243 SILC_TASK_CALLBACK_GLOBAL(silc_server_rekey_final);
244 SILC_TASK_CALLBACK_GLOBAL(silc_server_rekey_callback);
245 SILC_TASK_CALLBACK_GLOBAL(silc_server_connect_to_router);
246 void silc_server_watcher_list_destroy(void *key, void *context,
247                                       void *user_context);
248
249 #endif