updates
[silc.git] / apps / silcd / server.h
1 /*
2
3   server.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 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 for SILC Server object. The actual object is
25    defined in internal header file for server routines. I want to keep
26    the object private hence this declaration. */
27 typedef struct SilcServerStruct *SilcServer;
28
29 /* Forward declaration of backup server context */
30 typedef struct SilcServerBackupStruct *SilcServerBackup;
31
32 #define SILC_SERVER_MAX_CONNECTIONS 1000
33
34 /* General definitions */
35
36 /* SILC port */
37 #define SILC_PORT 768;
38
39 /* Server and router. Used internally by the code. */
40 #define SILC_SERVER 0
41 #define SILC_ROUTER 1
42 #define SILC_BACKUP_ROUTER 2
43
44 /* Connection retry timeout. We implement exponential backoff algorithm
45    in connection retry. The interval of timeout grows when retry count
46    grows. */
47 #define SILC_SERVER_RETRY_COUNT        7         /* Max retry count */
48 #define SILC_SERVER_RETRY_MULTIPLIER   2         /* Interval growth */
49 #define SILC_SERVER_RETRY_RANDOMIZER   2         /* timeout += rnd % 2 */
50 #define SILC_SERVER_RETRY_INTERVAL_MIN 10        /* Min retry timeout */
51 #define SILC_SERVER_RETRY_INTERVAL_MAX 600       /* Max generated timeout */
52
53 /* 
54    Silc Server Params.
55
56    Structure to hold various default parameters for server that can be
57    given before running the server. 
58
59 */
60 typedef struct {
61   uint32 retry_count;
62   uint32 retry_interval_min;
63   uint32 retry_interval_min_usec;
64   uint32 retry_interval_max;
65   char retry_keep_trying;
66
67   uint32 protocol_timeout;
68   uint32 protocol_timeout_usec;
69
70   char require_reverse_mapping;
71 } *SilcServerParams;
72
73 /* Callback function that is called after the key exchange and connection
74    authentication protocols has been completed with a remote router. The
75    `server_entry' is the remote router entry. */
76 typedef void (*SilcServerConnectRouterCallback)(SilcServer server,
77                                                 SilcServerEntry server_entry,
78                                                 void *context);
79
80 typedef struct {
81   SilcSocketConnection sock;
82
83   /* Remote host name and port */
84   char *remote_host;
85   int remote_port;
86   bool backup;
87   char *backup_replace_ip;
88   int backup_replace_port;
89   bool no_reconnect;
90   
91   /* Current connection retry info */
92   uint32 retry_count;
93   uint32 retry_timeout;
94
95   /* Back pointer to server */
96   SilcServer server;
97
98   SilcServerConnectRouterCallback callback;
99   void *callback_context;
100 } *SilcServerConnection;
101
102 /* Macros */
103
104 /* This macro is used to send notify messages with formatted string. The
105    string is formatted with arguments and the formatted string is sent as
106    argument. */
107 #define SILC_SERVER_SEND_NOTIFY(server, sock, type, fmt)        \
108 do {                                                            \
109   char *__fmt__ = silc_format fmt;                              \
110   silc_server_send_notify(server, sock, FALSE,                  \
111                           type, 1, __fmt__, strlen(__fmt__));   \
112   silc_free(__fmt__);                                           \
113 } while(0);
114
115 /* Check whether rekey protocol is active */
116 #define SILC_SERVER_IS_REKEY(sock)                                      \
117   (sock->protocol && sock->protocol->protocol &&                        \
118    sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)
119
120 /* Prototypes */
121 int silc_server_alloc(SilcServer *new_server);
122 void silc_server_free(SilcServer server);
123 int silc_server_init(SilcServer server);
124 void silc_server_daemonise(SilcServer server);
125 void silc_server_drop(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, uint32 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                                       int notify,
152                                       char *signoff_message,
153                                       int keygen);
154 int silc_server_remove_from_one_channel(SilcServer server, 
155                                         SilcSocketConnection sock,
156                                         SilcChannelEntry channel,
157                                         SilcClientEntry client,
158                                         int notify);
159 void silc_server_disconnect_remote(SilcServer server,
160                                    SilcSocketConnection sock,
161                                    const char *fmt, ...);
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                                     uint32 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_users,
189                                             SilcBuffer *channel_users_modes);
190 void silc_server_announce_get_channels(SilcServer server,
191                                        SilcIDList id_list,
192                                        SilcBuffer *channels,
193                                        SilcBuffer *channel_users,
194                                        SilcBuffer **channel_users_modes,
195                                        uint32 *channel_users_modes_c,
196                                        SilcBuffer **channel_topics,
197                                        SilcChannelID ***channel_ids,
198                                        unsigned long creation_time);
199 void silc_server_announce_servers(SilcServer server, bool global,
200                                   unsigned long creation_time,
201                                   SilcSocketConnection remote);
202 void silc_server_announce_clients(SilcServer server,
203                                   unsigned long creation_time,
204                                   SilcSocketConnection remote);
205 void silc_server_announce_channels(SilcServer server,
206                                    unsigned long creation_time,
207                                    SilcSocketConnection remote);
208 void silc_server_get_users_on_channel(SilcServer server,
209                                       SilcChannelEntry channel,
210                                       SilcBuffer *user_list,
211                                       SilcBuffer *mode_list,
212                                       uint32 *user_count);
213 void silc_server_save_users_on_channel(SilcServer server,
214                                        SilcSocketConnection sock,
215                                        SilcChannelEntry channel,
216                                        SilcClientID *noadd,
217                                        SilcBuffer user_list,
218                                        SilcBuffer mode_list,
219                                        uint32 user_count);
220 SilcSocketConnection silc_server_get_client_route(SilcServer server,
221                                                   unsigned char *id_data,
222                                                   uint32 id_len,
223                                                   SilcClientID *client_id,
224                                                   SilcIDListData *idata);
225 SilcBuffer silc_server_get_client_channel_list(SilcServer server,
226                                                SilcClientEntry client);
227 SilcClientEntry silc_server_get_client_resolve(SilcServer server,
228                                                SilcClientID *client_id,
229                                                bool *resolved);
230
231 #endif