Comment fix: OS is not excluded from permissive.
[freeciv.git] / common / networking / connection.h
blobc00c4f677538c3b1fb8abe4dceed73a79c4c9a96
1 /**********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 ***********************************************************************/
13 #ifndef FC__CONNECTION_H
14 #define FC__CONNECTION_H
16 #ifdef __cplusplus
17 extern "C" {
18 #endif /* __cplusplus */
20 #include <time.h> /* time_t */
22 #ifdef FREECIV_HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25 #ifdef FREECIV_HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
29 #ifdef FREECIV_JSON_CONNECTION
30 #include <jansson.h>
31 #endif /* FREECIV_JSON_CONNECTION */
33 #ifndef FREECIV_JSON_CONNECTION
34 #define USE_COMPRESSION
35 #endif /* FREECIV_JSON_CONNECTION */
37 /**************************************************************************
38 The connection struct and related stuff.
39 Includes cmdlevel stuff, which is connection-based.
40 ***************************************************************************/
42 /* utility */
43 #include "shared.h" /* MAX_LEN_ADDR */
44 #include "support.h" /* bool type */
45 #include "timing.h"
47 /* common */
48 #include "fc_types.h"
50 struct conn_pattern_list;
51 struct genhash;
52 struct packet_handlers;
53 struct timer_list;
55 /* Used in the network protocol. */
56 #define MAX_LEN_PACKET 4096
57 #define MAX_LEN_CAPSTR 512
58 #define MAX_LEN_PASSWORD 512 /* do not change this under any circumstances */
59 #define MAX_LEN_CONTENT (MAX_LEN_PACKET - 20)
61 #define MAX_LEN_BUFFER (MAX_LEN_PACKET * 128)
63 /****************************************************************************
64 Command access levels for client-side use; at present, they are only
65 used to control access to server commands typed at the client chatline.
66 Used in the network protocol.
67 ****************************************************************************/
68 #define SPECENUM_NAME cmdlevel
69 /* User may issue no commands at all. */
70 #define SPECENUM_VALUE0 ALLOW_NONE
71 #define SPECENUM_VALUE0NAME "none"
72 /* Informational or observer commands only. */
73 #define SPECENUM_VALUE1 ALLOW_INFO
74 #define SPECENUM_VALUE1NAME "info"
75 /* User may issue basic player commands. */
76 #define SPECENUM_VALUE2 ALLOW_BASIC
77 #define SPECENUM_VALUE2NAME "basic"
78 /* User may issue commands that affect game & users
79 * (starts a vote if the user's level is 'basic'). */
80 #define SPECENUM_VALUE3 ALLOW_CTRL
81 #define SPECENUM_VALUE3NAME "ctrl"
82 /* User may issue commands that affect the server. */
83 #define SPECENUM_VALUE4 ALLOW_ADMIN
84 #define SPECENUM_VALUE4NAME "admin"
85 /* User may issue *all* commands - dangerous! */
86 #define SPECENUM_VALUE5 ALLOW_HACK
87 #define SPECENUM_VALUE5NAME "hack"
88 #define SPECENUM_COUNT CMDLEVEL_COUNT
89 #include "specenum_gen.h"
91 /***************************************************************************
92 On the distinction between nations(formerly races), players, and users,
93 see doc/HACKING
94 ***************************************************************************/
96 /* where the connection is in the authentication process */
97 enum auth_status {
98 AS_NOT_ESTABLISHED = 0,
99 AS_FAILED,
100 AS_REQUESTING_NEW_PASS,
101 AS_REQUESTING_OLD_PASS,
102 AS_ESTABLISHED
105 /* get 'struct conn_list' and related functions: */
106 /* do this with forward definition of struct connection, so that
107 * connection struct can contain a struct conn_list */
108 struct connection;
109 #define SPECLIST_TAG conn
110 #define SPECLIST_TYPE struct connection
111 #include "speclist.h"
113 #define conn_list_iterate(connlist, pconn) \
114 TYPED_LIST_ITERATE(struct connection, connlist, pconn)
115 #define conn_list_iterate_end LIST_ITERATE_END
117 /***********************************************************
118 This is a buffer where the data is first collected,
119 whenever it arrives to the client/server.
120 ***********************************************************/
121 struct socket_packet_buffer {
122 int ndata;
123 int do_buffer_sends;
124 int nsize;
125 unsigned char *data;
128 struct packet_header {
129 unsigned int length : 4; /* Actually 'enum data_type' */
130 unsigned int type : 4; /* Actually 'enum data_type' */
133 #define SPECVEC_TAG byte
134 #define SPECVEC_TYPE unsigned char
135 #include "specvec.h"
137 /***********************************************************
138 The connection struct represents a single client or server
139 at the other end of a network connection.
140 ***********************************************************/
141 struct connection {
142 int id; /* used for server/client communication */
143 int sock;
144 bool used;
145 bool established; /* have negotiated initial packets */
146 struct packet_header packet_header;
147 char *closing_reason;
149 /* connection is "observer", not controller; may be observing
150 * specific player, or all (implementation incomplete).
152 bool observer;
154 /* NULL for connections not yet associated with a specific player.
156 struct player *playing;
158 struct socket_packet_buffer *buffer;
159 struct socket_packet_buffer *send_buffer;
160 struct timer *last_write;
161 #ifdef FREECIV_JSON_CONNECTION
162 json_t *json_packet;
163 #endif /* FREECIV_JSON_CONNECTION */
165 double ping_time;
167 struct conn_list *self; /* list with this connection as single element */
168 char username[MAX_LEN_NAME];
169 char addr[MAX_LEN_ADDR];
172 * "capability" gives the capability string of the executable (be it
173 * a client or server) at the other end of the connection.
175 char capability[MAX_LEN_CAPSTR];
178 * "access_level" stores the current access level of the client
179 * corresponding to this connection.
181 enum cmdlevel access_level;
183 enum gui_type client_gui;
185 void (*notify_of_writable_data) (struct connection * pc,
186 bool data_available_and_socket_full);
188 union {
189 struct {
190 /* Increases for every packet send to the server. */
191 int last_request_id_used;
193 /* Increases for every received PACKET_PROCESSING_FINISHED packet. */
194 int last_processed_request_id_seen;
196 /* Holds the id of the request which caused this packet. Can be zero. */
197 int request_id_of_currently_handled_packet;
198 } client;
200 struct {
201 /* Holds the id of the request which is processed now. Can be zero. */
202 int currently_processed_request_id;
204 /* Will increase for every received packet. */
205 int last_request_id_seen;
207 /* The start times of the PACKET_CONN_PING which have been sent but
208 * weren't PACKET_CONN_PONGed yet? */
209 struct timer_list *ping_timers;
211 /* Holds number of tries for authentication from client. */
212 int auth_tries;
214 /* the time that the server will respond after receiving an auth reply.
215 * this is used to throttle the connection. Also used to reject a
216 * connection if we've waited too long for a password. */
217 time_t auth_settime;
219 /* used to follow where the connection is in the authentication
220 * process */
221 enum auth_status status;
222 char password[MAX_LEN_PASSWORD];
224 /* for reverse lookup and blacklisting in db */
225 char ipaddr[MAX_LEN_ADDR];
227 /* The access level initially given to the client upon connection. */
228 enum cmdlevel granted_access_level;
230 /* The list of ignored connection patterns. */
231 struct conn_pattern_list *ignore_list;
233 /* Something has occurred that means the connection should be closed,
234 * but the closing has been postponed. */
235 bool is_closing;
237 /* If we use delegation the original player (playing) is replaced. Save
238 * it here to easily restore it. */
239 struct {
240 bool status; /* TRUE if player currently delegated to us */
241 struct player *playing;
242 bool observer;
243 } delegation;
244 } server;
248 * Called before an incoming packet is processed. The packet_type
249 * argument should really be a "enum packet_type". However due
250 * circular dependency this is impossible.
252 void (*incoming_packet_notify) (struct connection * pc,
253 int packet_type, int size);
256 * Called before a packet is sent. The packet_type argument should
257 * really be a "enum packet_type". However due circular dependency
258 * this is impossible.
260 void (*outgoing_packet_notify) (struct connection * pc,
261 int packet_type, int size,
262 int request_id);
263 struct {
264 struct genhash **sent;
265 struct genhash **received;
266 const struct packet_handlers *handlers;
267 } phs;
269 #ifdef USE_COMPRESSION
270 struct {
271 int frozen_level;
273 struct byte_vector queue;
274 } compression;
275 #endif
276 struct {
277 int bytes_send;
278 } statistics;
282 typedef void (*conn_close_fn_t) (struct connection *pconn);
283 void connections_set_close_callback(conn_close_fn_t func);
284 void connection_close(struct connection *pconn, const char *reason);
286 int read_socket_data(int sock, struct socket_packet_buffer *buffer);
287 void flush_connection_send_buffer_all(struct connection *pc);
288 bool connection_send_data(struct connection *pconn,
289 const unsigned char *data, int len);
291 void connection_do_buffer(struct connection *pc);
292 void connection_do_unbuffer(struct connection *pc);
294 void conn_list_do_buffer(struct conn_list *dest);
295 void conn_list_do_unbuffer(struct conn_list *dest);
297 struct connection *conn_by_user(const char *user_name);
298 struct connection *conn_by_user_prefix(const char *user_name,
299 enum m_pre_result *result);
300 struct connection *conn_by_number(int id);
302 struct socket_packet_buffer *new_socket_packet_buffer(void);
303 void connection_common_init(struct connection *pconn);
304 void connection_common_close(struct connection *pconn);
305 void conn_set_capability(struct connection *pconn, const char *capability);
306 void free_compression_queue(struct connection *pconn);
307 void conn_reset_delta_state(struct connection *pconn);
309 void conn_compression_freeze(struct connection *pconn);
310 bool conn_compression_thaw(struct connection *pconn);
311 bool conn_compression_frozen(const struct connection *pconn);
312 void conn_list_compression_freeze(const struct conn_list *pconn_list);
313 void conn_list_compression_thaw(const struct conn_list *pconn_list);
315 const char *conn_description(const struct connection *pconn);
316 bool conn_controls_player(const struct connection *pconn);
317 bool conn_is_global_observer(const struct connection *pconn);
318 enum cmdlevel conn_get_access(const struct connection *pconn);
320 struct player;
321 struct player *conn_get_player(const struct connection *pconn);
323 bool can_conn_edit(const struct connection *pconn);
324 bool can_conn_enable_editing(const struct connection *pconn);
326 int get_next_request_id(int old_request_id);
328 extern const char blank_addr_str[];
331 /* Connection patterns. */
332 struct conn_pattern;
334 #define SPECLIST_TAG conn_pattern
335 #define SPECLIST_TYPE struct conn_pattern
336 #include "speclist.h"
337 #define conn_pattern_list_iterate(plist, ppatern) \
338 TYPED_LIST_ITERATE(struct conn_pattern, plist, ppatern)
339 #define conn_pattern_list_iterate_end LIST_ITERATE_END
341 #define SPECENUM_NAME conn_pattern_type
342 #define SPECENUM_VALUE0 CPT_USER
343 #define SPECENUM_VALUE0NAME "user"
344 #define SPECENUM_VALUE1 CPT_HOST
345 #define SPECENUM_VALUE1NAME "host"
346 #define SPECENUM_VALUE2 CPT_IP
347 #define SPECENUM_VALUE2NAME "ip"
348 #include "specenum_gen.h"
350 struct conn_pattern *conn_pattern_new(enum conn_pattern_type type,
351 const char *wildcard);
352 void conn_pattern_destroy(struct conn_pattern *ppattern);
354 bool conn_pattern_match(const struct conn_pattern *ppattern,
355 const struct connection *pconn);
356 bool conn_pattern_list_match(const struct conn_pattern_list *plist,
357 const struct connection *pconn);
359 size_t conn_pattern_to_string(const struct conn_pattern *ppattern,
360 char *buf, size_t buf_len);
361 struct conn_pattern *conn_pattern_from_string(const char *pattern,
362 enum conn_pattern_type prefer,
363 char *error_buf,
364 size_t error_buf_len);
366 bool conn_is_valid(const struct connection *pconn);
368 #ifdef __cplusplus
370 #endif /* __cplusplus */
372 #endif /* FC__CONNECTION_H */