Added silc_server_send_opers[_notify] to send packets to operators.
[silc.git] / apps / silcd / server.h
1 /*
2
3   server.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_H
22 #define SERVER_H
23
24 /* Forward declaration of backup server context */
25 typedef struct SilcServerBackupStruct *SilcServerBackup;
26
27 /* Callback function that is called after the key exchange and connection
28    authentication protocols has been completed with a remote router. The
29    `server_entry' is the remote router entry. */
30 typedef void (*SilcServerConnectRouterCallback)(SilcServer server,
31                                                 SilcServerEntry server_entry,
32                                                 void *context);
33
34 /* Connection structure used when connection to remote */
35 typedef struct {
36   SilcSocketConnection sock;
37
38   /* Remote host name and port */
39   char *remote_host;
40   int remote_port;
41   bool backup;
42   char *backup_replace_ip;
43   int backup_replace_port;
44   bool no_reconnect;
45
46   /* Connection configuration (maybe NULL) */
47   SilcServerConfigRef conn;
48
49   /* Current connection retry info */
50   SilcUInt32 retry_count;
51   SilcUInt32 retry_timeout;
52
53   /* Back pointer to server */
54   SilcServer server;
55
56   SilcServerConnectRouterCallback callback;
57   void *callback_context;
58 } *SilcServerConnection;
59
60 /* General definitions */
61
62 /* SILC port */
63 #define SILC_PORT 768;
64
65 /* Server and router. Used internally by the code. */
66 #define SILC_SERVER 0
67 #define SILC_ROUTER 1
68 #define SILC_BACKUP_ROUTER 2
69
70 /* Default parameter values */
71
72 /* Connection retry timeout. We implement exponential backoff algorithm
73    in connection retry. The interval of timeout grows when retry count
74    grows. */
75 #define SILC_SERVER_RETRY_COUNT        7         /* Max retry count */
76 #define SILC_SERVER_RETRY_MULTIPLIER   2         /* Interval growth */
77 #define SILC_SERVER_RETRY_RANDOMIZER   2         /* timeout += rnd % 2 */
78 #define SILC_SERVER_RETRY_INTERVAL_MIN 10        /* Min retry timeout */
79 #define SILC_SERVER_RETRY_INTERVAL_MAX 600       /* Max generated timeout */
80
81 #define SILC_SERVER_KEEPALIVE          300       /* Heartbeat interval */
82 #define SILC_SERVER_CHANNEL_REKEY      3600      /* Channel rekey interval */
83 #define SILC_SERVER_REKEY              3600      /* Session rekey interval */
84 #define SILC_SERVER_SKE_TIMEOUT        60        /* SKE timeout */
85 #define SILC_SERVER_CONNAUTH_TIMEOUT   60        /* CONN_AUTH timeout */
86 #define SILC_SERVER_MAX_CONNECTIONS    1000      /* Max connections */
87 #define SILC_SERVER_MAX_CONNECTIONS_SINGLE 1000  /* Max connections per host */
88 #define SILC_SERVER_LOG_FLUSH_DELAY    300       /* Default log flush delay */
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 /* Output an error message wether to stderr or LOG_ERROR if we are in the
118    background. */
119 #define SILC_SERVER_LOG_ERROR(fmt) silc_server_stderr(silc_format fmt)
120
121 /* Prototypes */
122 int silc_server_alloc(SilcServer *new_server);
123 void silc_server_free(SilcServer server);
124 bool silc_server_init(SilcServer server);
125 bool silc_server_rehash(SilcServer server);
126 void silc_server_run(SilcServer server);
127 void silc_server_stop(SilcServer server);
128 void silc_server_start_key_exchange(SilcServer server,
129                                     SilcServerConnection sconn,
130                                     int sock);
131 bool silc_server_packet_parse(SilcPacketParserContext *parser_context,
132                               void *context);
133 void silc_server_packet_parse_type(SilcServer server,
134                                    SilcSocketConnection sock,
135                                    SilcPacketContext *packet);
136 void silc_server_create_connection(SilcServer server,
137                                    const char *remote_host, SilcUInt32 port);
138 void silc_server_close_connection(SilcServer server,
139                                   SilcSocketConnection sock);
140 void silc_server_free_client_data(SilcServer server,
141                                   SilcSocketConnection sock,
142                                   SilcClientEntry client,
143                                   int notify,
144                                   const char *signoff);
145 void silc_server_free_sock_user_data(SilcServer server,
146                                      SilcSocketConnection sock,
147                                      const char *signoff_message);
148 void silc_server_remove_from_channels(SilcServer server,
149                                       SilcSocketConnection sock,
150                                       SilcClientEntry client,
151                                       bool notify,
152                                       const char *signoff_message,
153                                       bool keygen);
154 bool silc_server_remove_from_one_channel(SilcServer server,
155                                          SilcSocketConnection sock,
156                                          SilcChannelEntry channel,
157                                          SilcClientEntry client,
158                                          bool notify);
159 void silc_server_disconnect_remote(SilcServer server,
160                                    SilcSocketConnection sock,
161                                    SilcStatus status, ...);
162 SilcChannelEntry silc_server_create_new_channel(SilcServer server,
163                                                 SilcServerID *router_id,
164                                                 char *cipher,
165                                                 char *hmac,
166                                                 char *channel_name,
167                                                 int broadcast);
168 SilcChannelEntry
169 silc_server_create_new_channel_with_id(SilcServer server,
170                                        char *cipher,
171                                        char *hmac,
172                                        char *channel_name,
173                                        SilcChannelID *channel_id,
174                                        int broadcast);
175 bool silc_server_create_channel_key(SilcServer server,
176                                     SilcChannelEntry channel,
177                                     SilcUInt32 key_len);
178 SilcChannelEntry silc_server_save_channel_key(SilcServer server,
179                                               SilcBuffer key_payload,
180                                               SilcChannelEntry channel);
181 void silc_server_perform_heartbeat(SilcSocketConnection sock,
182                                    void *hb_context);
183 void silc_server_announce_get_channel_topic(SilcServer server,
184                                             SilcChannelEntry channel,
185                                             SilcBuffer *topic);
186 void silc_server_announce_get_channel_users(SilcServer server,
187                                             SilcChannelEntry channel,
188                                             SilcBuffer *channel_modes,
189                                             SilcBuffer *channel_users,
190                                             SilcBuffer *channel_users_modes);
191 void silc_server_announce_get_channels(SilcServer server,
192                                        SilcIDList id_list,
193                                        SilcBuffer *channels,
194                                        SilcBuffer **channel_modes,
195                                        SilcBuffer *channel_users,
196                                        SilcBuffer **channel_users_modes,
197                                        SilcUInt32 *channel_users_modes_c,
198                                        SilcBuffer **channel_topics,
199                                        SilcChannelID ***channel_ids,
200                                        unsigned long creation_time);
201 void silc_server_announce_servers(SilcServer server, bool global,
202                                   unsigned long creation_time,
203                                   SilcSocketConnection remote);
204 void silc_server_announce_clients(SilcServer server,
205                                   unsigned long creation_time,
206                                   SilcSocketConnection remote);
207 void silc_server_announce_channels(SilcServer server,
208                                    unsigned long creation_time,
209                                    SilcSocketConnection remote);
210 bool silc_server_get_users_on_channel(SilcServer server,
211                                       SilcChannelEntry channel,
212                                       SilcBuffer *user_list,
213                                       SilcBuffer *mode_list,
214                                       SilcUInt32 *user_count);
215 void silc_server_save_users_on_channel(SilcServer server,
216                                        SilcSocketConnection sock,
217                                        SilcChannelEntry channel,
218                                        SilcClientID *noadd,
219                                        SilcBuffer user_list,
220                                        SilcBuffer mode_list,
221                                        SilcUInt32 user_count);
222 void silc_server_save_user_channels(SilcServer server,
223                                     SilcSocketConnection sock,
224                                     SilcClientEntry client,
225                                     SilcBuffer channels,
226                                     SilcBuffer channels_user_modes);
227 SilcSocketConnection
228 silc_server_get_client_route(SilcServer server,
229                              unsigned char *id_data,
230                              SilcUInt32 id_len,
231                              SilcClientID *client_id,
232                              SilcIDListData *idata,
233                              SilcClientEntry *client_entry);
234 SilcBuffer silc_server_get_client_channel_list(SilcServer server,
235                                                SilcClientEntry client,
236                                                bool get_private,
237                                                bool get_secret,
238                                                SilcBuffer *user_mode_list);
239 SilcClientEntry silc_server_get_client_resolve(SilcServer server,
240                                                SilcClientID *client_id,
241                                                bool always_resolve,
242                                                bool *resolved);
243 void silc_server_stderr(char *message);
244
245 #endif