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