Merged silc_1_0_branch to trunk.
[silc.git] / apps / silcd / server.h
1 /*
2
3   server.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_H
21 #define SERVER_H
22
23 /* Forward declaration of backup server context */
24 typedef struct SilcServerBackupStruct *SilcServerBackup;
25
26 /* Callback function that is called after the key exchange and connection
27    authentication protocols has been completed with a remote router. The
28    `server_entry' is the remote router entry or NULL on error. */
29 typedef void (*SilcServerConnectRouterCallback)(SilcServer server,
30                                                 SilcServerEntry server_entry,
31                                                 void *context);
32
33 /* Connection structure used when connection to remote */
34 typedef struct {
35   SilcSocketConnection sock;
36
37   /* Remote host name and port */
38   char *remote_host;
39   int remote_port;
40   bool backup;
41   char *backup_replace_ip;
42   int backup_replace_port;
43   bool no_reconnect;
44
45   /* Connection configuration (maybe NULL) */
46   SilcServerConfigRef conn;
47
48   /* Current connection retry info */
49   SilcUInt32 retry_count;
50   SilcUInt32 retry_timeout;
51
52   SilcServerConnectRouterCallback callback;
53   void *callback_context;
54 } *SilcServerConnection;
55
56 /* General definitions */
57
58 /* SILC port */
59 #define SILC_PORT 706
60
61 /* Server and router. Used internally by the code. */
62 #define SILC_SERVER 0
63 #define SILC_ROUTER 1
64 #define SILC_BACKUP_ROUTER 2
65
66 /* Default parameter values */
67
68 /* Connection retry timeout. We implement exponential backoff algorithm
69    in connection retry. The interval of timeout grows when retry count
70    grows. */
71 #define SILC_SERVER_RETRY_COUNT        7         /* Max retry count */
72 #define SILC_SERVER_RETRY_MULTIPLIER   2         /* Interval growth */
73 #define SILC_SERVER_RETRY_RANDOMIZER   2         /* timeout += rnd % 2 */
74 #define SILC_SERVER_RETRY_INTERVAL_MIN 10        /* Min retry timeout */
75 #define SILC_SERVER_RETRY_INTERVAL_MAX 600       /* Max generated timeout */
76
77 #define SILC_SERVER_KEEPALIVE          300       /* Heartbeat interval */
78 #define SILC_SERVER_CHANNEL_REKEY      3600      /* Channel rekey interval */
79 #define SILC_SERVER_REKEY              3600      /* Session rekey interval */
80 #define SILC_SERVER_SKE_TIMEOUT        60        /* SKE timeout */
81 #define SILC_SERVER_CONNAUTH_TIMEOUT   60        /* CONN_AUTH timeout */
82 #define SILC_SERVER_MAX_CONNECTIONS    1000      /* Max connections */
83 #define SILC_SERVER_MAX_CONNECTIONS_SINGLE 1000  /* Max connections per host */
84 #define SILC_SERVER_LOG_FLUSH_DELAY    300       /* Default log flush delay */
85 #define SILC_SERVER_QOS_RATE_LIMIT     10        /* Default QoS rate limit */
86 #define SILC_SERVER_QOS_BYTES_LIMIT    2048      /* Default QoS bytes limit */
87 #define SILC_SERVER_QOS_LIMIT_SEC      0         /* Default QoS limit sec */
88 #define SILC_SERVER_QOS_LIMIT_USEC     500000    /* Default QoS limit usec */
89
90 /* Macros */
91
92 /* This macro is used to send notify messages with formatted string. The
93    string is formatted with arguments and the formatted string is sent as
94    argument. */
95 #define SILC_SERVER_SEND_NOTIFY(server, sock, type, fmt)        \
96 do {                                                            \
97   char *__fmt__ = silc_format fmt;                              \
98   silc_server_send_notify(server, sock, FALSE,                  \
99                           type, 1, __fmt__, strlen(__fmt__));   \
100   silc_free(__fmt__);                                           \
101 } while(0)
102
103 /* Send notify to operators */
104 #define SILC_SERVER_SEND_OPERS(server, route, local, type, fmt)         \
105 do {                                                                    \
106   char *__fmt__ = silc_format fmt;                                      \
107   silc_server_send_opers_notify(server, route, local,                   \
108                                 type, 1, __fmt__, strlen(__fmt__));     \
109   silc_free(__fmt__);                                                   \
110 } while(0)
111
112 /* Check whether rekey protocol is active */
113 #define SILC_SERVER_IS_REKEY(sock)                                      \
114   (sock->protocol && sock->protocol->protocol &&                        \
115    sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)
116
117 /* Check whether backup resuming protocol is active */
118 #define SILC_SERVER_IS_BACKUP(sock)                                     \
119   (sock->protocol && sock->protocol->protocol &&                        \
120    sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_BACKUP)
121
122 /* Output a message to stderr or to the appropriate log facility wether
123    we are in the background or not. */
124 #define SILC_SERVER_LOG_INFO(fmt)                                       \
125   silc_server_stderr(SILC_LOG_INFO, silc_format fmt)
126 #define SILC_SERVER_LOG_WARNING(fmt)                                    \
127   silc_server_stderr(SILC_LOG_WARNING, silc_format fmt)
128 #define SILC_SERVER_LOG_ERROR(fmt)                                      \
129   silc_server_stderr(SILC_LOG_ERROR, silc_format fmt)
130 #define SILC_SERVER_LOG_FATAL(fmt)                                      \
131   silc_server_stderr(SILC_LOG_WARNING, silc_format fmt)
132
133 /* Prototypes */
134 int silc_server_alloc(SilcServer *new_server);
135 void silc_server_free(SilcServer server);
136 bool silc_server_init(SilcServer server);
137 bool silc_server_rehash(SilcServer server);
138 void silc_server_run(SilcServer server);
139 void silc_server_stop(SilcServer server);
140 void silc_server_start_key_exchange(SilcServer server,
141                                     SilcServerConnection sconn,
142                                     int sock);
143 bool silc_server_packet_parse(SilcPacketParserContext *parser_context,
144                               void *context);
145 void silc_server_packet_parse_type(SilcServer server,
146                                    SilcSocketConnection sock,
147                                    SilcPacketContext *packet);
148 void silc_server_create_connection(SilcServer server,
149                                    const char *remote_host, SilcUInt32 port);
150 void silc_server_close_connection(SilcServer server,
151                                   SilcSocketConnection sock);
152 void silc_server_free_client_data(SilcServer server,
153                                   SilcSocketConnection sock,
154                                   SilcClientEntry client,
155                                   int notify,
156                                   const char *signoff);
157 void silc_server_free_sock_user_data(SilcServer server,
158                                      SilcSocketConnection sock,
159                                      const char *signoff_message);
160 void silc_server_remove_from_channels(SilcServer server,
161                                       SilcSocketConnection sock,
162                                       SilcClientEntry client,
163                                       bool notify,
164                                       const char *signoff_message,
165                                       bool keygen, bool killed);
166 bool silc_server_remove_from_one_channel(SilcServer server,
167                                          SilcSocketConnection sock,
168                                          SilcChannelEntry channel,
169                                          SilcClientEntry client,
170                                          bool notify);
171 void silc_server_disconnect_remote(SilcServer server,
172                                    SilcSocketConnection sock,
173                                    SilcStatus status, ...);
174 SilcChannelEntry silc_server_create_new_channel(SilcServer server,
175                                                 SilcServerID *router_id,
176                                                 char *cipher,
177                                                 char *hmac,
178                                                 char *channel_name,
179                                                 int broadcast);
180 SilcChannelEntry
181 silc_server_create_new_channel_with_id(SilcServer server,
182                                        char *cipher,
183                                        char *hmac,
184                                        char *channel_name,
185                                        SilcChannelID *channel_id,
186                                        int broadcast);
187 bool silc_server_create_channel_key(SilcServer server,
188                                     SilcChannelEntry channel,
189                                     SilcUInt32 key_len);
190 SilcChannelEntry silc_server_save_channel_key(SilcServer server,
191                                               SilcBuffer key_payload,
192                                               SilcChannelEntry channel);
193 void silc_server_perform_heartbeat(SilcSocketConnection sock,
194                                    void *hb_context);
195 void silc_server_announce_get_channel_topic(SilcServer server,
196                                             SilcChannelEntry channel,
197                                             SilcBuffer *topic);
198 void silc_server_announce_get_channel_users(SilcServer server,
199                                             SilcChannelEntry channel,
200                                             SilcBuffer *channel_modes,
201                                             SilcBuffer *channel_users,
202                                             SilcBuffer *channel_users_modes);
203 void silc_server_announce_get_channels(SilcServer server,
204                                        SilcIDList id_list,
205                                        SilcBuffer *channels,
206                                        SilcBuffer **channel_modes,
207                                        SilcBuffer *channel_users,
208                                        SilcBuffer **channel_users_modes,
209                                        SilcUInt32 *channel_users_modes_c,
210                                        SilcBuffer **channel_topics,
211                                        SilcBuffer **channel_invites,
212                                        SilcBuffer **channel_bans,
213                                        SilcChannelID ***channel_ids,
214                                        unsigned long creation_time);
215 void silc_server_announce_servers(SilcServer server, bool global,
216                                   unsigned long creation_time,
217                                   SilcSocketConnection remote);
218 void silc_server_announce_clients(SilcServer server,
219                                   unsigned long creation_time,
220                                   SilcSocketConnection remote);
221 void silc_server_announce_channels(SilcServer server,
222                                    unsigned long creation_time,
223                                    SilcSocketConnection remote);
224 void silc_server_announce_watches(SilcServer server,
225                                   SilcSocketConnection remote);
226 bool silc_server_get_users_on_channel(SilcServer server,
227                                       SilcChannelEntry channel,
228                                       SilcBuffer *user_list,
229                                       SilcBuffer *mode_list,
230                                       SilcUInt32 *user_count);
231 void silc_server_save_users_on_channel(SilcServer server,
232                                        SilcSocketConnection sock,
233                                        SilcChannelEntry channel,
234                                        SilcClientID *noadd,
235                                        SilcBuffer user_list,
236                                        SilcBuffer mode_list,
237                                        SilcUInt32 user_count);
238 void silc_server_save_user_channels(SilcServer server,
239                                     SilcSocketConnection sock,
240                                     SilcClientEntry client,
241                                     SilcBuffer channels,
242                                     SilcBuffer channels_user_modes);
243 SilcSocketConnection
244 silc_server_get_client_route(SilcServer server,
245                              unsigned char *id_data,
246                              SilcUInt32 id_len,
247                              SilcClientID *client_id,
248                              SilcIDListData *idata,
249                              SilcClientEntry *client_entry);
250 SilcBuffer silc_server_get_client_channel_list(SilcServer server,
251                                                SilcClientEntry client,
252                                                bool get_private,
253                                                bool get_secret,
254                                                SilcBuffer *user_mode_list);
255 void silc_server_stderr(SilcLogType type, char *message);
256
257 #endif