Freeciv-3.1: /fast/freeciv/nightly/S3_1/common/networking/connection.h Source File

Freeciv-3.1

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
50struct conn_pattern_list;
51struct genhash;
52struct packet_handlers;
53struct timer_list;
54
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)
60
61#define MAX_LEN_BUFFER (MAX_LEN_PACKET * 128)
62
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"
90
91/***************************************************************************
92 On the distinction between nations(formerly races), players, and users,
93 see doc/HACKING
94***************************************************************************/
95
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
103};
104
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 */
108struct connection;
109#define SPECLIST_TAG conn
110#define SPECLIST_TYPE struct connection
111#include "speclist.h"
112
113#define conn_list_iterate(connlist, pconn) \
114 TYPED_LIST_ITERATE(struct connection, connlist, pconn)
115#define conn_list_iterate_end LIST_ITERATE_END
116
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;
126};
127
128 struct packet_header {
129 unsigned int length : 4; /* Actually 'enum data_type' */
130 unsigned int type : 4; /* Actually 'enum data_type' */
131};
132
133#define SPECVEC_TAG byte
134#define SPECVEC_TYPE unsigned char
135#include "specvec.h"
136
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;
148
149 /* connection is "observer", not controller; may be observing
150 * specific player, or all (implementation incomplete).
151 */
152 bool observer;
153
154 /* NULL for connections not yet associated with a specific player.
155 */
156 struct player *playing;
157
158 struct socket_packet_buffer *buffer;
159 struct socket_packet_buffer *send_buffer;
160 struct timer *last_write;
161#ifdef FREECIV_JSON_CONNECTION
162 bool json_mode;
163 json_t *json_packet;
164#endif /* FREECIV_JSON_CONNECTION */
165
166 double ping_time;
167
168 struct conn_list *self; /* list with this connection as single element */
169 char username[MAX_LEN_NAME];
170 char addr[MAX_LEN_ADDR];
171
172 /*
173 * "capability" gives the capability string of the executable (be it
174 * a client or server) at the other end of the connection.
175 */
176 char capability[MAX_LEN_CAPSTR];
177
178 /*
179 * "access_level" stores the current access level of the client
180 * corresponding to this connection.
181 */
182 enum cmdlevel access_level;
183
184 enum gui_type client_gui;
185
186 void (*notify_of_writable_data) (struct connection * pc,
187 bool data_available_and_socket_full);
188
189 union {
190 struct {
191 /* Increases for every packet send to the server. */
192 int last_request_id_used;
193
194 /* Increases for every received PACKET_PROCESSING_FINISHED packet. */
195 int last_processed_request_id_seen;
196
197 /* Holds the id of the request which caused this packet. Can be zero. */
198 int request_id_of_currently_handled_packet;
199 } client;
200
201 struct {
202 /* Holds the id of the request which is processed now. Can be zero. */
203 int currently_processed_request_id;
204
205 /* Will increase for every received packet. */
206 int last_request_id_seen;
207
208 /* The start times of the PACKET_CONN_PING which have been sent but
209 * weren't PACKET_CONN_PONGed yet? */
210 struct timer_list *ping_timers;
211
212 /* Holds number of tries for authentication from client. */
213 int auth_tries;
214
215 /* the time that the server will respond after receiving an auth reply.
216 * this is used to throttle the connection. Also used to reject a
217 * connection if we've waited too long for a password. */
218 time_t auth_settime;
219
220 /* used to follow where the connection is in the authentication
221 * process */
222 enum auth_status status;
223 char password[MAX_LEN_PASSWORD];
224
225 /* for reverse lookup and blacklisting in db */
226 char ipaddr[MAX_LEN_ADDR];
227
228 /* The access level initially given to the client upon connection. */
229 enum cmdlevel granted_access_level;
230
231 /* The list of ignored connection patterns. */
232 struct conn_pattern_list *ignore_list;
233
234 /* Something has occurred that means the connection should be closed,
235 * but the closing has been postponed. */
236 bool is_closing;
237
238 /* If we use delegation the original player (playing) is replaced. Save
239 * it here to easily restore it. */
240 struct {
241 bool status; /* TRUE if player currently delegated to us */
242 struct player *playing;
243 bool observer;
244 } delegation;
245 } server;
246 };
247
248 /*
249 * Called before an incoming packet is processed. The packet_type
250 * argument should really be a "enum packet_type". However due
251 * circular dependency this is impossible.
252 */
253 void (*incoming_packet_notify) (struct connection * pc,
254 int packet_type, int size);
255
256 /*
257 * Called before a packet is sent. The packet_type argument should
258 * really be a "enum packet_type". However due circular dependency
259 * this is impossible.
260 */
261 void (*outgoing_packet_notify) (struct connection * pc,
262 int packet_type, int size,
263 int request_id);
264 struct {
265 struct genhash **sent;
266 struct genhash **received;
267 const struct packet_handlers *handlers;
268 } phs;
269
270#ifdef USE_COMPRESSION
271 struct {
272 int frozen_level;
273
274 struct byte_vector queue;
275 } compression;
276#endif
277 struct {
278 int bytes_send;
279 } statistics;
280};
281
282
283 typedef void (*conn_close_fn_t) (struct connection *pconn);
284void connections_set_close_callback(conn_close_fn_t func);
285void connection_close(struct connection *pconn, const char *reason);
286
287int read_socket_data(int sock, struct socket_packet_buffer *buffer);
288void flush_connection_send_buffer_all(struct connection *pc);
289bool connection_send_data(struct connection *pconn,
290 const unsigned char *data, int len);
291
292void connection_do_buffer(struct connection *pc);
293void connection_do_unbuffer(struct connection *pc);
294
295void conn_list_do_buffer(struct conn_list *dest);
296void conn_list_do_unbuffer(struct conn_list *dest);
297
298struct connection *conn_by_user(const char *user_name);
299struct connection *conn_by_user_prefix(const char *user_name,
300 enum m_pre_result *result);
301struct connection *conn_by_number(int id);
302
303struct socket_packet_buffer *new_socket_packet_buffer(void);
304void connection_common_init(struct connection *pconn);
305void connection_common_close(struct connection *pconn);
306void conn_set_capability(struct connection *pconn, const char *capability);
307void free_compression_queue(struct connection *pconn);
308void conn_reset_delta_state(struct connection *pconn);
309
310void conn_compression_freeze(struct connection *pconn);
311bool conn_compression_thaw(struct connection *pconn);
312bool conn_compression_frozen(const struct connection *pconn);
313void conn_list_compression_freeze(const struct conn_list *pconn_list);
314void conn_list_compression_thaw(const struct conn_list *pconn_list);
315
316const char *conn_description(const struct connection *pconn);
317bool conn_controls_player(const struct connection *pconn);
318bool conn_is_global_observer(const struct connection *pconn);
319enum cmdlevel conn_get_access(const struct connection *pconn);
320
321struct player;
322struct player *conn_get_player(const struct connection *pconn);
323
324bool can_conn_edit(const struct connection *pconn);
325bool can_conn_enable_editing(const struct connection *pconn);
326
327int get_next_request_id(int old_request_id);
328
329extern const char blank_addr_str[];
330
331
332/* Connection patterns. */
333struct conn_pattern;
334
335#define SPECLIST_TAG conn_pattern
336#define SPECLIST_TYPE struct conn_pattern
337#include "speclist.h"
338#define conn_pattern_list_iterate(plist, ppatern) \
339 TYPED_LIST_ITERATE(struct conn_pattern, plist, ppatern)
340#define conn_pattern_list_iterate_end LIST_ITERATE_END
341
342#define SPECENUM_NAME conn_pattern_type
343#define SPECENUM_VALUE0 CPT_USER
344#define SPECENUM_VALUE0NAME "user"
345#define SPECENUM_VALUE1 CPT_HOST
346#define SPECENUM_VALUE1NAME "host"
347#define SPECENUM_VALUE2 CPT_IP
348#define SPECENUM_VALUE2NAME "ip"
349#include "specenum_gen.h"
350
351struct conn_pattern *conn_pattern_new(enum conn_pattern_type type,
352 const char *wildcard);
353void conn_pattern_destroy(struct conn_pattern *ppattern);
354
355bool conn_pattern_match(const struct conn_pattern *ppattern,
356 const struct connection *pconn);
357bool conn_pattern_list_match(const struct conn_pattern_list *plist,
358 const struct connection *pconn);
359
360size_t conn_pattern_to_string(const struct conn_pattern *ppattern,
361 char *buf, size_t buf_len);
362struct conn_pattern *conn_pattern_from_string(const char *pattern,
363 enum conn_pattern_type prefer,
364 char *error_buf,
365 size_t error_buf_len);
366
367bool conn_is_valid(const struct connection *pconn);
368
369#ifdef __cplusplus
370}
371#endif /* __cplusplus */
372
373#endif /* FC__CONNECTION_H */
user_name
char user_name[512]
Definition client_main.c:141
auth_status
auth_status
Definition connection.h:97
AS_REQUESTING_OLD_PASS
@ AS_REQUESTING_OLD_PASS
Definition connection.h:101
AS_FAILED
@ AS_FAILED
Definition connection.h:99
AS_NOT_ESTABLISHED
@ AS_NOT_ESTABLISHED
Definition connection.h:98
AS_REQUESTING_NEW_PASS
@ AS_REQUESTING_NEW_PASS
Definition connection.h:100
AS_ESTABLISHED
@ AS_ESTABLISHED
Definition connection.h:102
conn_pattern_to_string
size_t conn_pattern_to_string(const struct conn_pattern *ppattern, char *buf, size_t buf_len)
Definition connection.c:862
conn_get_player
struct player * conn_get_player(const struct connection *pconn)
Definition connection.c:760
conn_list_do_unbuffer
void conn_list_do_unbuffer(struct conn_list *dest)
Definition connection.c:365
free_compression_queue
void free_compression_queue(struct connection *pconn)
Definition connection.c:546
conn_by_user_prefix
struct connection * conn_by_user_prefix(const char *user_name, enum m_pre_result *result)
Definition connection.c:397
can_conn_edit
bool can_conn_edit(const struct connection *pconn)
Definition connection.c:510
conn_close_fn_t
void(* conn_close_fn_t)(struct connection *pconn)
Definition connection.h:283
conn_by_user
struct connection * conn_by_user(const char *user_name)
Definition connection.c:376
conn_list_compression_thaw
void conn_list_compression_thaw(const struct conn_list *pconn_list)
Definition connection.c:729
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:229
get_next_request_id
int get_next_request_id(int old_request_id)
Definition connection.c:529
new_socket_packet_buffer
struct socket_packet_buffer * new_socket_packet_buffer(void)
Definition connection.c:438
MAX_LEN_PASSWORD
#define MAX_LEN_PASSWORD
Definition connection.h:58
conn_list_do_buffer
void conn_list_do_buffer(struct conn_list *dest)
Definition connection.c:355
can_conn_enable_editing
bool can_conn_enable_editing(const struct connection *pconn)
Definition connection.c:520
connection_do_buffer
void connection_do_buffer(struct connection *pc)
Definition connection.c:323
MAX_LEN_CAPSTR
#define MAX_LEN_CAPSTR
Definition connection.h:57
conn_pattern_match
bool conn_pattern_match(const struct conn_pattern *ppattern, const struct connection *pconn)
Definition connection.c:816
conn_set_capability
void conn_set_capability(struct connection *pconn, const char *capability)
Definition connection.c:658
conn_pattern_destroy
void conn_pattern_destroy(struct conn_pattern *ppattern)
Definition connection.c:806
connection_common_init
void connection_common_init(struct connection *pconn)
Definition connection.c:602
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:717
conn_pattern_new
struct conn_pattern * conn_pattern_new(enum conn_pattern_type type, const char *wildcard)
Definition connection.c:792
conn_controls_player
bool conn_controls_player(const struct connection *pconn)
Definition connection.c:742
conn_is_global_observer
bool conn_is_global_observer(const struct connection *pconn)
Definition connection.c:750
conn_description
const char * conn_description(const struct connection *pconn)
Definition connection.c:473
connection_common_close
void connection_common_close(struct connection *pconn)
Definition connection.c:627
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:875
conn_compression_freeze
void conn_compression_freeze(struct connection *pconn)
Definition connection.c:691
conn_by_number
struct connection * conn_by_number(int id)
Definition connection.c:420
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:670
connection_send_data
bool connection_send_data(struct connection *pconn, const unsigned char *data, int len)
Definition connection.c:286
conn_get_access
enum cmdlevel conn_get_access(const struct connection *pconn)
Definition connection.c:772
conn_is_valid
bool conn_is_valid(const struct connection *pconn)
Definition connection.c:931
conn_compression_frozen
bool conn_compression_frozen(const struct connection *pconn)
Definition connection.c:705
conn_pattern_list_match
bool conn_pattern_list_match(const struct conn_pattern_list *plist, const struct connection *pconn)
Definition connection.c:847
connection_do_unbuffer
void connection_do_unbuffer(struct connection *pc)
Definition connection.c:335
fc_types.h
MAX_LEN_NAME
#define MAX_LEN_NAME
Definition fc_types.h:66
type
GType type
Definition repodlgs.c:1312
func
get_token_fn_t func
Definition inputfile.c:128
packet_type
packet_type
Definition packets_gen.h:1883
len
int len
Definition packhand.c:125
shared.h
m_pre_result
m_pre_result
Definition shared.h:207
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:784
conn_pattern::wildcard
char * wildcard
Definition connection.c:786
connection
Definition connection.h:141
connection::currently_processed_request_id
int currently_processed_request_id
Definition connection.h:203
connection::received
struct genhash ** received
Definition connection.h:266
connection::auth_settime
time_t auth_settime
Definition connection.h:218
connection::established
bool established
Definition connection.h:145
connection::statistics
struct connection::@61 statistics
connection::playing
struct player * playing
Definition connection.h:156
connection::closing_reason
char * closing_reason
Definition connection.h:147
connection::client
struct connection::@57::@62 client
connection::request_id_of_currently_handled_packet
int request_id_of_currently_handled_packet
Definition connection.h:198
connection::delegation
struct connection::@57::@63::@64 delegation
connection::access_level
enum cmdlevel access_level
Definition connection.h:182
connection::handlers
const struct packet_handlers * handlers
Definition connection.h:267
connection::self
struct conn_list * self
Definition connection.h:168
connection::last_request_id_used
int last_request_id_used
Definition connection.h:192
connection::ping_timers
struct timer_list * ping_timers
Definition connection.h:210
connection::is_closing
bool is_closing
Definition connection.h:236
connection::phs
struct connection::@59 phs
connection::observer
bool observer
Definition connection.h:152
connection::username
char username[MAX_LEN_NAME]
Definition connection.h:169
connection::client_gui
enum gui_type client_gui
Definition connection.h:184
connection::server
struct connection::@57::@63 server
connection::last_write
struct timer * last_write
Definition connection.h:160
connection::compression
struct connection::@60 compression
connection::status
enum auth_status status
Definition connection.h:222
connection::status
bool status
Definition connection.h:241
connection::ipaddr
char ipaddr[MAX_LEN_ADDR]
Definition connection.h:226
connection::incoming_packet_notify
void(* incoming_packet_notify)(struct connection *pc, int packet_type, int size)
Definition connection.h:253
connection::bytes_send
int bytes_send
Definition connection.h:278
connection::outgoing_packet_notify
void(* outgoing_packet_notify)(struct connection *pc, int packet_type, int size, int request_id)
Definition connection.h:261
connection::send_buffer
struct socket_packet_buffer * send_buffer
Definition connection.h:159
connection::capability
char capability[MAX_LEN_CAPSTR]
Definition connection.h:176
connection::frozen_level
int frozen_level
Definition connection.h:272
connection::notify_of_writable_data
void(* notify_of_writable_data)(struct connection *pc, bool data_available_and_socket_full)
Definition connection.h:186
connection::sock
int sock
Definition connection.h:143
connection::last_processed_request_id_seen
int last_processed_request_id_seen
Definition connection.h:195
connection::addr
char addr[MAX_LEN_ADDR]
Definition connection.h:170
connection::auth_tries
int auth_tries
Definition connection.h:213
connection::ping_time
double ping_time
Definition connection.h:166
connection::sent
struct genhash ** sent
Definition connection.h:265
connection::buffer
struct socket_packet_buffer * buffer
Definition connection.h:158
connection::id
int id
Definition connection.h:142
connection::password
char password[MAX_LEN_PASSWORD]
Definition connection.h:223
connection::last_request_id_seen
int last_request_id_seen
Definition connection.h:206
connection::ignore_list
struct conn_pattern_list * ignore_list
Definition connection.h:232
connection::granted_access_level
enum cmdlevel granted_access_level
Definition connection.h:229
connection::used
bool used
Definition connection.h:144
genhash
Definition genhash.c:83
packet_handlers
Definition packets.h:97
packet_header
Definition connection.h:128
packet_header::length
unsigned int length
Definition connection.h:129
packet_header::type
unsigned int type
Definition connection.h:130
player
Definition player.h:249
socket_packet_buffer
Definition connection.h:121
socket_packet_buffer::ndata
int ndata
Definition connection.h:122
socket_packet_buffer::nsize
int nsize
Definition connection.h:124
socket_packet_buffer::do_buffer_sends
int do_buffer_sends
Definition connection.h:123
socket_packet_buffer::data
unsigned char * data
Definition connection.h:125
timer
Definition timing.c:81
support.h
timing.h