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 /* Callback function that is called after the key exchange and connection
33    authentication protocols has been completed with a remote router. The
34    `server_entry' is the remote router entry. */
35 typedef void (*SilcServerConnectRouterCallback)(SilcServer server,
36                                                 SilcServerEntry server_entry,
37                                                 void *context);
38
39 /* Connection structure used when connection to remote */
40 typedef struct {
41   SilcSocketConnection sock;
42
43   /* Remote host name and port */
44   char *remote_host;
45   int remote_port;
46   bool backup;
47   char *backup_replace_ip;
48   int backup_replace_port;
49   bool no_reconnect;
50
51   /* Connection configuration (maybe NULL), and connection params */
52   void *conn;
53   void *param;
54
55   /* Current connection retry info */
56   uint32 retry_count;
57   uint32 retry_timeout;
58
59   /* Back pointer to server */
60   SilcServer server;
61
62   SilcServerConnectRouterCallback callback;
63   void *callback_context;
64 } *SilcServerConnection;
65
66 #define SILC_SERVER_MAX_CONNECTIONS 1000
67
68 /* General definitions */
69
70 /* SILC port */
71 #define SILC_PORT 768;
72
73 /* Server and router. Used internally by the code. */
74 #define SILC_SERVER 0
75 #define SILC_ROUTER 1
76 #define SILC_BACKUP_ROUTER 2
77
78 /* Default parameter values */
79
80 /* Connection retry timeout. We implement exponential backoff algorithm
81    in connection retry. The interval of timeout grows when retry count
82    grows. */
83 #define SILC_SERVER_RETRY_COUNT        7         /* Max retry count */
84 #define SILC_SERVER_RETRY_MULTIPLIER   2         /* Interval growth */
85 #define SILC_SERVER_RETRY_RANDOMIZER   2         /* timeout += rnd % 2 */
86 #define SILC_SERVER_RETRY_INTERVAL_MIN 10        /* Min retry timeout */
87 #define SILC_SERVER_RETRY_INTERVAL_MAX 600       /* Max generated timeout */
88
89 #define SILC_SERVER_KEEPALIVE          300       /* Heartbeat interval */
90
91 /* Macros */
92
93 /* This macro is used to send notify messages with formatted string. The
94    string is formatted with arguments and the formatted string is sent as
95    argument. */
96 #define SILC_SERVER_SEND_NOTIFY(server, sock, type, fmt)        \
97 do {                                                            \
98   char *__fmt__ = silc_format fmt;                              \
99   silc_server_send_notify(server, sock, FALSE,                  \
100                           type, 1, __fmt__, strlen(__fmt__));   \
101   silc_free(__fmt__);                                           \
102 } while(0);
103
104 /* Check whether rekey protocol is active */
105 #define SILC_SERVER_IS_REKEY(sock)                                      \
106   (sock->protocol && sock->protocol->protocol &&                        \
107    sock->protocol->protocol->type == SILC_PROTOCOL_SERVER_REKEY)
108
109 /* Prototypes */
110 int silc_server_alloc(SilcServer *new_server);
111 void silc_server_free(SilcServer server);
112 int silc_server_init(SilcServer server);
113 void silc_server_daemonise(SilcServer server);
114 void silc_server_drop(SilcServer server);
115 void silc_server_run(SilcServer server);
116 void silc_server_stop(SilcServer server);
117 void silc_server_start_key_exchange(SilcServer server,
118                                     SilcServerConnection sconn,
119                                     int sock);
120 bool silc_server_packet_parse(SilcPacketParserContext *parser_context,
121                               void *context);
122 void silc_server_packet_parse_type(SilcServer server, 
123                                    SilcSocketConnection sock,
124                                    SilcPacketContext *packet);
125 void silc_server_create_connection(SilcServer server,
126                                    const char *remote_host, uint32 port);
127 void silc_server_close_connection(SilcServer server,
128                                   SilcSocketConnection sock);
129 void silc_server_free_client_data(SilcServer server, 
130                                   SilcSocketConnection sock,
131                                   SilcClientEntry client, 
132                                   int notify,
133                                   const char *signoff);
134 void silc_server_free_sock_user_data(SilcServer server, 
135                                      SilcSocketConnection sock,
136                                      const char *signoff_message);
137 void silc_server_remove_from_channels(SilcServer server, 
138                                       SilcSocketConnection sock,
139                                       SilcClientEntry client,
140                                       int notify,
141                                       char *signoff_message,
142                                       int keygen);
143 int silc_server_remove_from_one_channel(SilcServer server, 
144                                         SilcSocketConnection sock,
145                                         SilcChannelEntry channel,
146                                         SilcClientEntry client,
147                                         int notify);
148 void silc_server_disconnect_remote(SilcServer server,
149                                    SilcSocketConnection sock,
150                                    const char *fmt, ...);
151 SilcChannelEntry silc_server_create_new_channel(SilcServer server, 
152                                                 SilcServerID *router_id,
153                                                 char *cipher, 
154                                                 char *hmac,
155                                                 char *channel_name,
156                                                 int broadcast);
157 SilcChannelEntry 
158 silc_server_create_new_channel_with_id(SilcServer server, 
159                                        char *cipher, 
160                                        char *hmac,
161                                        char *channel_name,
162                                        SilcChannelID *channel_id,
163                                        int broadcast);
164 bool silc_server_create_channel_key(SilcServer server, 
165                                     SilcChannelEntry channel,
166                                     uint32 key_len);
167 SilcChannelEntry silc_server_save_channel_key(SilcServer server,
168                                               SilcBuffer key_payload,
169                                               SilcChannelEntry channel);
170 void silc_server_perform_heartbeat(SilcSocketConnection sock,
171                                    void *hb_context);
172 void silc_server_announce_get_channel_topic(SilcServer server,
173                                             SilcChannelEntry channel,
174                                             SilcBuffer *topic);
175 void silc_server_announce_get_channel_users(SilcServer server,
176                                             SilcChannelEntry channel,
177                                             SilcBuffer *channel_users,
178                                             SilcBuffer *channel_users_modes);
179 void silc_server_announce_get_channels(SilcServer server,
180                                        SilcIDList id_list,
181                                        SilcBuffer *channels,
182                                        SilcBuffer *channel_users,
183                                        SilcBuffer **channel_users_modes,
184                                        uint32 *channel_users_modes_c,
185                                        SilcBuffer **channel_topics,
186                                        SilcChannelID ***channel_ids,
187                                        unsigned long creation_time);
188 void silc_server_announce_servers(SilcServer server, bool global,
189                                   unsigned long creation_time,
190                                   SilcSocketConnection remote);
191 void silc_server_announce_clients(SilcServer server,
192                                   unsigned long creation_time,
193                                   SilcSocketConnection remote);
194 void silc_server_announce_channels(SilcServer server,
195                                    unsigned long creation_time,
196                                    SilcSocketConnection remote);
197 void silc_server_get_users_on_channel(SilcServer server,
198                                       SilcChannelEntry channel,
199                                       SilcBuffer *user_list,
200                                       SilcBuffer *mode_list,
201                                       uint32 *user_count);
202 void silc_server_save_users_on_channel(SilcServer server,
203                                        SilcSocketConnection sock,
204                                        SilcChannelEntry channel,
205                                        SilcClientID *noadd,
206                                        SilcBuffer user_list,
207                                        SilcBuffer mode_list,
208                                        uint32 user_count);
209 SilcSocketConnection silc_server_get_client_route(SilcServer server,
210                                                   unsigned char *id_data,
211                                                   uint32 id_len,
212                                                   SilcClientID *client_id,
213                                                   SilcIDListData *idata);
214 SilcBuffer silc_server_get_client_channel_list(SilcServer server,
215                                                SilcClientEntry client);
216 SilcClientEntry silc_server_get_client_resolve(SilcServer server,
217                                                SilcClientID *client_id,
218                                                bool *resolved);
219
220 #endif