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