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