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