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