Send also SERVICE and SERVER_DIGITAL_SIGNATURE in server
[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 or NULL on error. */
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   SilcServerConnectRouterCallback callback;
54   void *callback_context;
55 } *SilcServerConnection;
56
57 /* General definitions */
58
59 /* SILC port */
60 #define SILC_PORT 706
61
62 /* Server and router. Used internally by the code. */
63 #define SILC_SERVER 0
64 #define SILC_ROUTER 1
65 #define SILC_BACKUP_ROUTER 2
66
67 /* Default parameter values */
68
69 /* Connection retry timeout. We implement exponential backoff algorithm
70    in connection retry. The interval of timeout grows when retry count
71    grows. */
72 #define SILC_SERVER_RETRY_COUNT        7         /* Max retry count */
73 #define SILC_SERVER_RETRY_MULTIPLIER   2         /* Interval growth */
74 #define SILC_SERVER_RETRY_RANDOMIZER   2         /* timeout += rnd % 2 */
75 #define SILC_SERVER_RETRY_INTERVAL_MIN 10        /* Min retry timeout */
76 #define SILC_SERVER_RETRY_INTERVAL_MAX 600       /* Max generated timeout */
77
78 #define SILC_SERVER_KEEPALIVE          300       /* Heartbeat interval */
79 #define SILC_SERVER_CHANNEL_REKEY      3600      /* Channel rekey interval */
80 #define SILC_SERVER_REKEY              3600      /* Session rekey interval */
81 #define SILC_SERVER_SKE_TIMEOUT        60        /* SKE timeout */
82 #define SILC_SERVER_CONNAUTH_TIMEOUT   60        /* CONN_AUTH timeout */
83 #define SILC_SERVER_MAX_CONNECTIONS    1000      /* Max connections */
84 #define SILC_SERVER_MAX_CONNECTIONS_SINGLE 1000  /* Max connections per host */
85 #define SILC_SERVER_LOG_FLUSH_DELAY    300       /* Default log flush delay */
86
87 /* Macros */
88
89 /* This macro is used to send notify messages with formatted string. The
90    string is formatted with arguments and the formatted string is sent as
91    argument. */
92 #define SILC_SERVER_SEND_NOTIFY(server, sock, type, fmt)        \
93 do {                                                            \
94   char *__fmt__ = silc_format fmt;                              \
95   silc_server_send_notify(server, sock, FALSE,                  \
96                           type, 1, __fmt__, strlen(__fmt__));   \
97   silc_free(__fmt__);                                           \
98 } while(0)
99
100 /* Send notify to operators */
101 #define SILC_SERVER_SEND_OPERS(server, route, local, type, fmt)         \
102 do {                                                                    \
103   char *__fmt__ = silc_format fmt;                                      \
104   silc_server_send_opers_notify(server, route, local,                   \
105                                 type, 1, __fmt__, strlen(__fmt__));     \
106   silc_free(__fmt__);                                                   \
107 } while(0)
108
109 /* Check whether rekey protocol is active */
110 #define SILC_SERVER_IS_REKEY(sock)                                      \
111   (sock->protocol && sock->protocol->protocol &&                        \
112    sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)
113
114 /* Output an error message wether to stderr or LOG_ERROR if we are in the
115    background. */
116 #define SILC_SERVER_LOG_ERROR(fmt) silc_server_stderr(silc_format fmt)
117
118 /* Prototypes */
119 int silc_server_alloc(SilcServer *new_server);
120 void silc_server_free(SilcServer server);
121 bool silc_server_init(SilcServer server);
122 bool silc_server_rehash(SilcServer server);
123 void silc_server_run(SilcServer server);
124 void silc_server_stop(SilcServer server);
125 void silc_server_start_key_exchange(SilcServer server,
126                                     SilcServerConnection sconn,
127                                     int sock);
128 bool silc_server_packet_parse(SilcPacketParserContext *parser_context,
129                               void *context);
130 void silc_server_packet_parse_type(SilcServer server,
131                                    SilcSocketConnection sock,
132                                    SilcPacketContext *packet);
133 void silc_server_create_connection(SilcServer server,
134                                    const char *remote_host, SilcUInt32 port);
135 void silc_server_close_connection(SilcServer server,
136                                   SilcSocketConnection sock);
137 void silc_server_free_client_data(SilcServer server,
138                                   SilcSocketConnection sock,
139                                   SilcClientEntry client,
140                                   int notify,
141                                   const char *signoff);
142 void silc_server_free_sock_user_data(SilcServer server,
143                                      SilcSocketConnection sock,
144                                      const char *signoff_message);
145 void silc_server_remove_from_channels(SilcServer server,
146                                       SilcSocketConnection sock,
147                                       SilcClientEntry client,
148                                       bool notify,
149                                       const char *signoff_message,
150                                       bool keygen);
151 bool silc_server_remove_from_one_channel(SilcServer server,
152                                          SilcSocketConnection sock,
153                                          SilcChannelEntry channel,
154                                          SilcClientEntry client,
155                                          bool notify);
156 void silc_server_disconnect_remote(SilcServer server,
157                                    SilcSocketConnection sock,
158                                    SilcStatus status, ...);
159 SilcChannelEntry silc_server_create_new_channel(SilcServer server,
160                                                 SilcServerID *router_id,
161                                                 char *cipher,
162                                                 char *hmac,
163                                                 char *channel_name,
164                                                 int broadcast);
165 SilcChannelEntry
166 silc_server_create_new_channel_with_id(SilcServer server,
167                                        char *cipher,
168                                        char *hmac,
169                                        char *channel_name,
170                                        SilcChannelID *channel_id,
171                                        int broadcast);
172 bool silc_server_create_channel_key(SilcServer server,
173                                     SilcChannelEntry channel,
174                                     SilcUInt32 key_len);
175 SilcChannelEntry silc_server_save_channel_key(SilcServer server,
176                                               SilcBuffer key_payload,
177                                               SilcChannelEntry channel);
178 void silc_server_perform_heartbeat(SilcSocketConnection sock,
179                                    void *hb_context);
180 void silc_server_announce_get_channel_topic(SilcServer server,
181                                             SilcChannelEntry channel,
182                                             SilcBuffer *topic);
183 void silc_server_announce_get_channel_users(SilcServer server,
184                                             SilcChannelEntry channel,
185                                             SilcBuffer *channel_modes,
186                                             SilcBuffer *channel_users,
187                                             SilcBuffer *channel_users_modes);
188 void silc_server_announce_get_channels(SilcServer server,
189                                        SilcIDList id_list,
190                                        SilcBuffer *channels,
191                                        SilcBuffer **channel_modes,
192                                        SilcBuffer *channel_users,
193                                        SilcBuffer **channel_users_modes,
194                                        SilcUInt32 *channel_users_modes_c,
195                                        SilcBuffer **channel_topics,
196                                        SilcChannelID ***channel_ids,
197                                        unsigned long creation_time);
198 void silc_server_announce_servers(SilcServer server, bool global,
199                                   unsigned long creation_time,
200                                   SilcSocketConnection remote);
201 void silc_server_announce_clients(SilcServer server,
202                                   unsigned long creation_time,
203                                   SilcSocketConnection remote);
204 void silc_server_announce_channels(SilcServer server,
205                                    unsigned long creation_time,
206                                    SilcSocketConnection remote);
207 bool silc_server_get_users_on_channel(SilcServer server,
208                                       SilcChannelEntry channel,
209                                       SilcBuffer *user_list,
210                                       SilcBuffer *mode_list,
211                                       SilcUInt32 *user_count);
212 void silc_server_save_users_on_channel(SilcServer server,
213                                        SilcSocketConnection sock,
214                                        SilcChannelEntry channel,
215                                        SilcClientID *noadd,
216                                        SilcBuffer user_list,
217                                        SilcBuffer mode_list,
218                                        SilcUInt32 user_count);
219 void silc_server_save_user_channels(SilcServer server,
220                                     SilcSocketConnection sock,
221                                     SilcClientEntry client,
222                                     SilcBuffer channels,
223                                     SilcBuffer channels_user_modes);
224 SilcSocketConnection
225 silc_server_get_client_route(SilcServer server,
226                              unsigned char *id_data,
227                              SilcUInt32 id_len,
228                              SilcClientID *client_id,
229                              SilcIDListData *idata,
230                              SilcClientEntry *client_entry);
231 SilcBuffer silc_server_get_client_channel_list(SilcServer server,
232                                                SilcClientEntry client,
233                                                bool get_private,
234                                                bool get_secret,
235                                                SilcBuffer *user_mode_list);
236 void silc_server_stderr(char *message);
237
238 #endif