Freeciv-3.3: /fast/freeciv/nightly/main/common/networking/connection.h Source File

Freeciv-3.3

Loading...
Searching...
No Matches
connection.h
Go to the documentation of this file.
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.
7
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
15
16#ifdef __cplusplus
17extern "C" {
18#endif /* __cplusplus */
19
20#include <time.h> /* time_t */
21
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
28
29#ifdef FREECIV_JSON_CONNECTION
30#include <jansson.h>
31#endif /* FREECIV_JSON_CONNECTION */
32
33#ifndef FREECIV_JSON_CONNECTION
34#define USE_COMPRESSION
35#endif /* FREECIV_JSON_CONNECTION */
36
37/**************************************************************************
38 The connection struct and related stuff.
39 Includes cmdlevel stuff, which is connection-based.
40***************************************************************************/
41
42/* utility */
43#include "shared.h" /* MAX_LEN_ADDR */
44#include "support.h" /* bool type */
45#include "timing.h"
46
47/* common */
48#include "fc_types.h"
49
50/* common/networking */
51#include "conn_types.h"
52
53struct conn_pattern_list;
54struct genhash;
55struct packet_handlers;
56struct timer_list;
57
58/****************************************************************************
59 Command access levels for client-side use; at present, they are only
60 used to control access to server commands typed at the client chatline.
61 Used in the network protocol.
62****************************************************************************/
63#define SPECENUM_NAME cmdlevel
64/* User may issue no commands at all. */
65#define SPECENUM_VALUE0 ALLOW_NONE
66#define SPECENUM_VALUE0NAME "none"
67/* Informational or observer commands only. */
68#define SPECENUM_VALUE1 ALLOW_INFO
69#define SPECENUM_VALUE1NAME "info"
70/* User may issue basic player commands. */
71#define SPECENUM_VALUE2 ALLOW_BASIC
72#define SPECENUM_VALUE2NAME "basic"
73/* User may issue commands that affect game & users
74 * (starts a vote if the user's level is 'basic'). */
75#define SPECENUM_VALUE3 ALLOW_CTRL
76#define SPECENUM_VALUE3NAME "ctrl"
77/* User may issue commands that affect the server. */
78#define SPECENUM_VALUE4 ALLOW_ADMIN
79#define SPECENUM_VALUE4NAME "admin"
80/* User may issue *all* commands - dangerous! */
81#define SPECENUM_VALUE5 ALLOW_HACK
82#define SPECENUM_VALUE5NAME "hack"
83#define SPECENUM_COUNT CMDLEVEL_COUNT
84#include "specenum_gen.h"
85
86/***************************************************************************
87 On the distinction between nations(formerly races), players, and users,
88 see doc/HACKING
89***************************************************************************/
90
91/* where the connection is in the authentication process */
92 enum auth_status {
93 AS_NOT_ESTABLISHED = 0,
94 AS_FAILED,
95 AS_REQUESTING_NEW_PASS,
96 AS_REQUESTING_OLD_PASS,
97 AS_ESTABLISHED
98};
99
100/* get 'struct conn_list' and related functions: */
101/* do this with forward definition of struct connection, so that
102 * connection struct can contain a struct conn_list */
103struct connection;
104#define SPECLIST_TAG conn
105#define SPECLIST_TYPE struct connection
106#include "speclist.h"
107
108#define conn_list_iterate(connlist, pconn) \
109 TYPED_LIST_ITERATE(struct connection, connlist, pconn)
110#define conn_list_iterate_end LIST_ITERATE_END
111
112/***********************************************************
113 This is a buffer where the data is first collected,
114 whenever it arrives to the client/server.
115***********************************************************/
116 struct socket_packet_buffer {
117 int ndata;
118 int do_buffer_sends;
119 int nsize;
120 unsigned char *data;
121};
122
123 struct packet_header {
124 unsigned int length : 4; /* Actually 'enum data_type' */
125 unsigned int type : 4; /* Actually 'enum data_type' */
126};
127
128#define SPECVEC_TAG byte
129#define SPECVEC_TYPE unsigned char
130#include "specvec.h"
131
132/***********************************************************
133 The connection struct represents a single client or server
134 at the other end of a network connection.
135***********************************************************/
136 struct connection {
137 int id; /* used for server/client communication */
138 int sock;
139 bool used;
140 bool established; /* have negotiated initial packets */
141 struct packet_header packet_header;
142 char *closing_reason;
143
144 /* connection is "observer", not controller; may be observing
145 * specific player, or all (implementation incomplete).
146 */
147 bool observer;
148
149 /* NULL for connections not yet associated with a specific player.
150 */
151 struct player *playing;
152
153 struct socket_packet_buffer *buffer;
154 struct socket_packet_buffer *send_buffer;
155 struct timer *last_write;
156#ifdef FREECIV_JSON_CONNECTION
157 bool json_mode;
158 json_t *json_packet;
159#endif /* FREECIV_JSON_CONNECTION */
160
161 double ping_time;
162
163 struct conn_list *self; /* list with this connection as single element */
164 char username[MAX_LEN_NAME];
165 char addr[MAX_LEN_ADDR];
166
167 /*
168 * "capability" gives the capability string of the executable (be it
169 * a client or server) at the other end of the connection.
170 */
171 char capability[MAX_LEN_CAPSTR];
172
173 /*
174 * "access_level" stores the current access level of the client
175 * corresponding to this connection.
176 */
177 enum cmdlevel access_level;
178
179 enum gui_type client_gui;
180
181 void (*notify_of_writable_data) (struct connection *pc,
182 bool data_available_and_socket_full);
183
184 union {
185 struct {
186 /* Increases for every packet send to the server. */
187 int last_request_id_used;
188
189 /* Increases for every received PACKET_PROCESSING_FINISHED packet. */
190 int last_processed_request_id_seen;
191
192 /* Holds the id of the request which caused this packet. Can be zero. */
193 int request_id_of_currently_handled_packet;
194 } client;
195
196 struct {
197 /* Holds the id of the request which is processed now. Can be zero. */
198 int currently_processed_request_id;
199
200 /* Will increase for every received packet. */
201 int last_request_id_seen;
202
203 /* The start times of the PACKET_CONN_PING which have been sent but
204 * weren't PACKET_CONN_PONGed yet? */
205 struct timer_list *ping_timers;
206
207 /* Holds number of tries for authentication from client. */
208 int auth_tries;
209
210 /* The time that the server will respond after receiving an auth reply.
211 * This is used to throttle the connection. Also used to reject
212 * a connection if we've waited too long for a password. */
213 time_t auth_settime;
214
215 /* Used to follow where the connection is in the authentication
216 * process */
217 enum auth_status status;
218 char password[MAX_LEN_PASSWORD];
219
220 /* For reverse lookup and blacklisting in db */
221 char ipaddr[MAX_LEN_ADDR];
222
223 /* The access level initially given to the client upon connection. */
224 enum cmdlevel granted_access_level;
225
226 /* Server setting control packet already sent. */
227 bool settings_sent;
228
229 /* The list of ignored connection patterns. */
230 struct conn_pattern_list *ignore_list;
231
232 /* Something has occurred that means the connection should be closed,
233 * but the closing has been postponed. */
234 bool is_closing;
235
236 /* If we use delegation the original player (playing) is replaced. Save
237 * it here to easily restore it. */
238 struct {
239 bool status; /* TRUE if player currently delegated to us */
240 struct player *playing;
241 bool observer;
242 } delegation;
243 } server;
244 };
245
246 /*
247 * Called before an incoming packet is processed. The packet_type
248 * argument should really be a "enum packet_type". However due
249 * circular dependency this is impossible.
250 */
251 void (*incoming_packet_notify) (struct connection * pc,
252 int packet_type, int size);
253
254 /*
255 * Called before a packet is sent. The packet_type argument should
256 * really be a "enum packet_type". However due circular dependency
257 * this is impossible.
258 */
259 void (*outgoing_packet_notify) (struct connection * pc,
260 int packet_type, int size,
261 int request_id);
262 struct {
263 struct genhash **sent;
264 struct genhash **received;
265 const struct packet_handlers *handlers;
266 } phs;
267
268#ifdef USE_COMPRESSION
269 struct {
270 int frozen_level;
271
272 struct byte_vector queue;
273 } compression;
274#endif
275 struct {
276 int bytes_send;
277 } statistics;
278};
279
280
281 typedef void (*conn_close_fn_t) (struct connection *pconn);
282void connections_set_close_callback(conn_close_fn_t func);
283void connection_close(struct connection *pconn, const char *reason);
284
285int read_socket_data(int sock, struct socket_packet_buffer *buffer);
286void flush_connection_send_buffer_all(struct connection *pc);
287bool connection_send_data(struct connection *pconn,
288 const unsigned char *data, int len);
289
290void connection_do_buffer(struct connection *pc);
291void connection_do_unbuffer(struct connection *pc);
292
293void conn_list_do_buffer(struct conn_list *dest);
294void conn_list_do_unbuffer(struct conn_list *dest);
295
296struct connection *conn_by_user(const char *user_name);
297struct connection *conn_by_user_prefix(const char *user_name,
298 enum m_pre_result *result);
299struct connection *conn_by_number(int id);
300
301struct socket_packet_buffer *new_socket_packet_buffer(void);
302void connection_common_init(struct connection *pconn);
303void connection_common_close(struct connection *pconn);
304void conn_set_capability(struct connection *pconn, const char *capability);
305void free_compression_queue(struct connection *pconn);
306void conn_reset_delta_state(struct connection *pconn);
307
308void conn_compression_freeze(struct connection *pconn);
309bool conn_compression_thaw(struct connection *pconn);
310bool conn_compression_frozen(const struct connection *pconn);
311void conn_list_compression_freeze(const struct conn_list *pconn_list);
312void conn_list_compression_thaw(const struct conn_list *pconn_list);
313
314const char *conn_description(const struct connection *pconn);
315bool conn_controls_player(const struct connection *pconn);
316bool conn_is_global_observer(const struct connection *pconn);
317enum cmdlevel conn_get_access(const struct connection *pconn);
318
319struct player;
320struct player *conn_get_player(const struct connection *pconn);
321
322bool can_conn_edit(const struct connection *pconn);
323bool can_conn_enable_editing(const struct connection *pconn);
324
325int get_next_request_id(int old_request_id);
326
327extern const char blank_addr_str[];
328
329
330/* Connection patterns. */
331struct conn_pattern;
332
333#define SPECLIST_TAG conn_pattern
334#define SPECLIST_TYPE struct conn_pattern
335#include "speclist.h"
336#define conn_pattern_list_iterate(plist, ppatern) \
337 TYPED_LIST_ITERATE(struct conn_pattern, plist, ppatern)
338#define conn_pattern_list_iterate_end LIST_ITERATE_END
339
340#define SPECENUM_NAME conn_pattern_type
341#define SPECENUM_VALUE0 CPT_USER
342#define SPECENUM_VALUE0NAME "user"
343#define SPECENUM_VALUE1 CPT_HOST
344#define SPECENUM_VALUE1NAME "host"
345#define SPECENUM_VALUE2 CPT_IP
346#define SPECENUM_VALUE2NAME "ip"
347#include "specenum_gen.h"
348
349struct conn_pattern *conn_pattern_new(enum conn_pattern_type type,
350 const char *wildcard);
351void conn_pattern_destroy(struct conn_pattern *ppattern);
352
353bool conn_pattern_match(const struct conn_pattern *ppattern,
354 const struct connection *pconn);
355bool conn_pattern_list_match(const struct conn_pattern_list *plist,
356 const struct connection *pconn);
357
358size_t conn_pattern_to_string(const struct conn_pattern *ppattern,
359 char *buf, size_t buf_len);
360struct conn_pattern *conn_pattern_from_string(const char *pattern,
361 enum conn_pattern_type prefer,
362 char *error_buf,
363 size_t error_buf_len);
364
365bool conn_is_valid(const struct connection *pconn);
366
367#define conn_is_webclient(__pconn__) ((__pconn__)->client_gui == GUI_WEB)
368
369#ifdef __cplusplus
370}
371#endif /* __cplusplus */
372
373#endif /* FC__CONNECTION_H */
user_name
char user_name[512]
Definition client_main.c:145
incite_cost
char * incite_cost
Definition comments.c:74
conn_types.h
MAX_LEN_PASSWORD
#define MAX_LEN_PASSWORD
Definition conn_types.h:31
MAX_LEN_CAPSTR
#define MAX_LEN_CAPSTR
Definition conn_types.h:30
auth_status
auth_status
Definition connection.h:92
AS_REQUESTING_OLD_PASS
@ AS_REQUESTING_OLD_PASS
Definition connection.h:96
AS_FAILED
@ AS_FAILED
Definition connection.h:94
AS_NOT_ESTABLISHED
@ AS_NOT_ESTABLISHED
Definition connection.h:93
AS_REQUESTING_NEW_PASS
@ AS_REQUESTING_NEW_PASS
Definition connection.h:95
AS_ESTABLISHED
@ AS_ESTABLISHED
Definition connection.h:97
conn_pattern_to_string
size_t conn_pattern_to_string(const struct conn_pattern *ppattern, char *buf, size_t buf_len)
Definition connection.c:865
conn_get_player
struct player * conn_get_player(const struct connection *pconn)
Definition connection.c:763
conn_list_do_unbuffer
void conn_list_do_unbuffer(struct conn_list *dest)
Definition connection.c:366
free_compression_queue
void free_compression_queue(struct connection *pconn)
Definition connection.c:547
conn_by_user_prefix
struct connection * conn_by_user_prefix(const char *user_name, enum m_pre_result *result)
Definition connection.c:398
can_conn_edit
bool can_conn_edit(const struct connection *pconn)
Definition connection.c:511
conn_close_fn_t
void(* conn_close_fn_t)(struct connection *pconn)
Definition connection.h:281
conn_by_user
struct connection * conn_by_user(const char *user_name)
Definition connection.c:377
conn_list_compression_thaw
void conn_list_compression_thaw(const struct conn_list *pconn_list)
Definition connection.c:732
connections_set_close_callback
void connections_set_close_callback(conn_close_fn_t func)
Definition connection.c:80
conn_compression_thaw
bool conn_compression_thaw(struct connection *pconn)
Definition packets.c:194
flush_connection_send_buffer_all
void flush_connection_send_buffer_all(struct connection *pc)
Definition connection.c:230
get_next_request_id
int get_next_request_id(int old_request_id)
Definition connection.c:530
new_socket_packet_buffer
struct socket_packet_buffer * new_socket_packet_buffer(void)
Definition connection.c:439
conn_list_do_buffer
void conn_list_do_buffer(struct conn_list *dest)
Definition connection.c:356
can_conn_enable_editing
bool can_conn_enable_editing(const struct connection *pconn)
Definition connection.c:521
connection_do_buffer
void connection_do_buffer(struct connection *pc)
Definition connection.c:324
conn_pattern_match
bool conn_pattern_match(const struct conn_pattern *ppattern, const struct connection *pconn)
Definition connection.c:819
conn_set_capability
void conn_set_capability(struct connection *pconn, const char *capability)
Definition connection.c:661
conn_pattern_destroy
void conn_pattern_destroy(struct conn_pattern *ppattern)
Definition connection.c:809
connection_common_init
void connection_common_init(struct connection *pconn)
Definition connection.c:603
connection_close
void connection_close(struct connection *pconn, const char *reason)
Definition connection.c:88
conn_list_compression_freeze
void conn_list_compression_freeze(const struct conn_list *pconn_list)
Definition connection.c:720
conn_pattern_new
struct conn_pattern * conn_pattern_new(enum conn_pattern_type type, const char *wildcard)
Definition connection.c:795
conn_controls_player
bool conn_controls_player(const struct connection *pconn)
Definition connection.c:745
conn_is_global_observer
bool conn_is_global_observer(const struct connection *pconn)
Definition connection.c:753
conn_description
const char * conn_description(const struct connection *pconn)
Definition connection.c:474
connection_common_close
void connection_common_close(struct connection *pconn)
Definition connection.c:630
conn_pattern_from_string
struct conn_pattern * conn_pattern_from_string(const char *pattern, enum conn_pattern_type prefer, char *error_buf, size_t error_buf_len)
Definition connection.c:878
conn_compression_freeze
void conn_compression_freeze(struct connection *pconn)
Definition connection.c:694
conn_by_number
struct connection * conn_by_number(int id)
Definition connection.c:421
blank_addr_str
const char blank_addr_str[]
Definition connection.c:55
read_socket_data
int read_socket_data(int sock, struct socket_packet_buffer *buffer)
Definition connection.c:129
conn_reset_delta_state
void conn_reset_delta_state(struct connection *pconn)
Definition connection.c:673
connection_send_data
bool connection_send_data(struct connection *pconn, const unsigned char *data, int len)
Definition connection.c:287
conn_get_access
enum cmdlevel conn_get_access(const struct connection *pconn)
Definition connection.c:775
conn_is_valid
bool conn_is_valid(const struct connection *pconn)
Definition connection.c:934
conn_compression_frozen
bool conn_compression_frozen(const struct connection *pconn)
Definition connection.c:708
conn_pattern_list_match
bool conn_pattern_list_match(const struct conn_pattern_list *plist, const struct connection *pconn)
Definition connection.c:850
connection_do_unbuffer
void connection_do_unbuffer(struct connection *pc)
Definition connection.c:336
fc_types.h
MAX_LEN_NAME
#define MAX_LEN_NAME
Definition fc_types.h:66
type
GType type
Definition repodlgs.c:1313
func
get_token_fn_t func
Definition inputfile.c:128
packet_type
packet_type
Definition packets_gen.h:1884
len
int len
Definition packhand.c:127
shared.h
m_pre_result
m_pre_result
Definition shared.h:213
MAX_LEN_ADDR
#define MAX_LEN_ADDR
Definition shared.h:31
specenum_gen.h
speclist.h
specvec.h
size
size_t size
Definition specvec.h:72
conn_pattern
Definition connection.c:787
conn_pattern::wildcard
char * wildcard
Definition connection.c:789
connection
Definition connection.h:136
connection::currently_processed_request_id
int currently_processed_request_id
Definition connection.h:198
connection::received
struct genhash ** received
Definition connection.h:264
connection::auth_settime
time_t auth_settime
Definition connection.h:213
connection::established
bool established
Definition connection.h:140
connection::playing
struct player * playing
Definition connection.h:151
connection::closing_reason
char * closing_reason
Definition connection.h:142
connection::phs
struct connection::@60 phs
connection::request_id_of_currently_handled_packet
int request_id_of_currently_handled_packet
Definition connection.h:193
connection::client
struct connection::@58::@63 client
connection::access_level
enum cmdlevel access_level
Definition connection.h:177
connection::handlers
const struct packet_handlers * handlers
Definition connection.h:265
connection::self
struct conn_list * self
Definition connection.h:163
connection::last_request_id_used
int last_request_id_used
Definition connection.h:187
connection::ping_timers
struct timer_list * ping_timers
Definition connection.h:205
connection::is_closing
bool is_closing
Definition connection.h:234
connection::settings_sent
bool settings_sent
Definition connection.h:227
connection::observer
bool observer
Definition connection.h:147
connection::username
char username[MAX_LEN_NAME]
Definition connection.h:164
connection::compression
struct connection::@61 compression
connection::client_gui
enum gui_type client_gui
Definition connection.h:179
connection::server
struct connection::@58::@64 server
connection::last_write
struct timer * last_write
Definition connection.h:155
connection::status
enum auth_status status
Definition connection.h:217
connection::status
bool status
Definition connection.h:239
connection::ipaddr
char ipaddr[MAX_LEN_ADDR]
Definition connection.h:221
connection::incoming_packet_notify
void(* incoming_packet_notify)(struct connection *pc, int packet_type, int size)
Definition connection.h:251
connection::bytes_send
int bytes_send
Definition connection.h:276
connection::outgoing_packet_notify
void(* outgoing_packet_notify)(struct connection *pc, int packet_type, int size, int request_id)
Definition connection.h:259
connection::send_buffer
struct socket_packet_buffer * send_buffer
Definition connection.h:154
connection::delegation
struct connection::@58::@64::@65 delegation
connection::capability
char capability[MAX_LEN_CAPSTR]
Definition connection.h:171
connection::frozen_level
int frozen_level
Definition connection.h:270
connection::notify_of_writable_data
void(* notify_of_writable_data)(struct connection *pc, bool data_available_and_socket_full)
Definition connection.h:181
connection::sock
int sock
Definition connection.h:138
connection::last_processed_request_id_seen
int last_processed_request_id_seen
Definition connection.h:190
connection::addr
char addr[MAX_LEN_ADDR]
Definition connection.h:165
connection::auth_tries
int auth_tries
Definition connection.h:208
connection::statistics
struct connection::@62 statistics
connection::ping_time
double ping_time
Definition connection.h:161
connection::sent
struct genhash ** sent
Definition connection.h:263
connection::buffer
struct socket_packet_buffer * buffer
Definition connection.h:153
connection::id
int id
Definition connection.h:137
connection::password
char password[MAX_LEN_PASSWORD]
Definition connection.h:218
connection::last_request_id_seen
int last_request_id_seen
Definition connection.h:201
connection::ignore_list
struct conn_pattern_list * ignore_list
Definition connection.h:230
connection::granted_access_level
enum cmdlevel granted_access_level
Definition connection.h:224
connection::used
bool used
Definition connection.h:139
genhash
Definition genhash.c:83
packet_handlers
Definition packets.h:74
packet_header
Definition connection.h:123
packet_header::length
unsigned int length
Definition connection.h:124
packet_header::type
unsigned int type
Definition connection.h:125
player
Definition player.h:249
socket_packet_buffer
Definition connection.h:116
socket_packet_buffer::ndata
int ndata
Definition connection.h:117
socket_packet_buffer::nsize
int nsize
Definition connection.h:119
socket_packet_buffer::do_buffer_sends
int do_buffer_sends
Definition connection.h:118
socket_packet_buffer::data
unsigned char * data
Definition connection.h:120
timer
Definition timing.c:81
support.h
timing.h