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
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 */
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***********************************************************/
117 int ndata;
119 int nsize;
120 unsigned char *data;
121};
122
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***********************************************************/
137 int id; /* used for server/client communication */
138 int sock;
139 bool used;
140 bool established; /* have negotiated initial packets */
143
144 /* connection is "observer", not controller; may be observing
145 * specific player, or all (implementation incomplete).
146 */
148
149 /* NULL for connections not yet associated with a specific player.
150 */
152
156#ifdef FREECIV_JSON_CONNECTION
157 bool json_mode;
159#endif /* FREECIV_JSON_CONNECTION */
160
161 double ping_time;
162
163 struct conn_list *self; /* list with this connection as single element */
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 */
172
173 /*
174 * "access_level" stores the current access level of the client
175 * corresponding to this connection.
176 */
178
179 enum gui_type client_gui;
180
183
184 union {
185 struct {
186 /* Increases for every packet send to the server. */
188
189 /* Increases for every received PACKET_PROCESSING_FINISHED packet. */
191
192 /* Holds the id of the request which caused this packet. Can be zero. */
195
196 struct {
197 /* Holds the id of the request which is processed now. Can be zero. */
199
200 /* Will increase for every received packet. */
202
203 /* The start times of the PACKET_CONN_PING which have been sent but
204 * weren't PACKET_CONN_PONGed yet? */
206
207 /* Holds number of tries for authentication from client. */
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. */
214
215 /* Used to follow where the connection is in the authentication
216 * process */
219
220 /* For reverse lookup and blacklisting in db */
222
223 /* The access level initially given to the client upon connection. */
225
226 /* Server setting control packet already sent. */
228
229 /* The list of ignored connection patterns. */
231
232 /* Something has occurred that means the connection should be closed,
233 * but the closing has been postponed. */
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;
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 */
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 */
260 int packet_type, int size,
261 int request_id);
262 struct {
263 struct genhash **sent;
267
268#ifdef USE_COMPRESSION
269 struct {
271
272 struct byte_vector queue;
274#endif
275 struct {
278};
279
280
283void connection_close(struct connection *pconn, const char *reason);
284
288 const unsigned char *data, int len);
289
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
304void conn_set_capability(struct connection *pconn, const char *capability);
307
310bool conn_compression_frozen(const struct connection *pconn);
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
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
350 const char *wildcard);
352
353bool conn_pattern_match(const struct conn_pattern *ppattern,
354 const struct connection *pconn);
356 const struct connection *pconn);
357
358size_t conn_pattern_to_string(const struct conn_pattern *ppattern,
359 char *buf, size_t buf_len);
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 */
char user_name[512]
char * incite_cost
Definition comments.c:76
#define MAX_LEN_PASSWORD
Definition conn_types.h:31
#define MAX_LEN_CAPSTR
Definition conn_types.h:30
auth_status
Definition connection.h:92
@ AS_REQUESTING_OLD_PASS
Definition connection.h:96
@ AS_FAILED
Definition connection.h:94
@ AS_NOT_ESTABLISHED
Definition connection.h:93
@ AS_REQUESTING_NEW_PASS
Definition connection.h:95
@ AS_ESTABLISHED
Definition connection.h:97
size_t conn_pattern_to_string(const struct conn_pattern *ppattern, char *buf, size_t buf_len)
Definition connection.c:865
struct player * conn_get_player(const struct connection *pconn)
Definition connection.c:763
void conn_list_do_unbuffer(struct conn_list *dest)
Definition connection.c:366
void free_compression_queue(struct connection *pconn)
Definition connection.c:547
struct connection * conn_by_user_prefix(const char *user_name, enum m_pre_result *result)
Definition connection.c:398
bool can_conn_edit(const struct connection *pconn)
Definition connection.c:511
void(* conn_close_fn_t)(struct connection *pconn)
Definition connection.h:281
struct connection * conn_by_user(const char *user_name)
Definition connection.c:377
void conn_list_compression_thaw(const struct conn_list *pconn_list)
Definition connection.c:732
void connections_set_close_callback(conn_close_fn_t func)
Definition connection.c:80
bool conn_compression_thaw(struct connection *pconn)
Definition packets.c:194
void flush_connection_send_buffer_all(struct connection *pc)
Definition connection.c:230
int get_next_request_id(int old_request_id)
Definition connection.c:530
struct socket_packet_buffer * new_socket_packet_buffer(void)
Definition connection.c:439
void conn_list_do_buffer(struct conn_list *dest)
Definition connection.c:356
bool can_conn_enable_editing(const struct connection *pconn)
Definition connection.c:521
void connection_do_buffer(struct connection *pc)
Definition connection.c:324
bool conn_pattern_match(const struct conn_pattern *ppattern, const struct connection *pconn)
Definition connection.c:819
void conn_set_capability(struct connection *pconn, const char *capability)
Definition connection.c:661
void conn_pattern_destroy(struct conn_pattern *ppattern)
Definition connection.c:809
void connection_common_init(struct connection *pconn)
Definition connection.c:603
void connection_close(struct connection *pconn, const char *reason)
Definition connection.c:88
void conn_list_compression_freeze(const struct conn_list *pconn_list)
Definition connection.c:720
struct conn_pattern * conn_pattern_new(enum conn_pattern_type type, const char *wildcard)
Definition connection.c:795
bool conn_controls_player(const struct connection *pconn)
Definition connection.c:745
bool conn_is_global_observer(const struct connection *pconn)
Definition connection.c:753
const char * conn_description(const struct connection *pconn)
Definition connection.c:474
void connection_common_close(struct connection *pconn)
Definition connection.c:630
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
void conn_compression_freeze(struct connection *pconn)
Definition connection.c:694
struct connection * conn_by_number(int id)
Definition connection.c:421
const char blank_addr_str[]
Definition connection.c:55
int read_socket_data(int sock, struct socket_packet_buffer *buffer)
Definition connection.c:129
void conn_reset_delta_state(struct connection *pconn)
Definition connection.c:673
bool connection_send_data(struct connection *pconn, const unsigned char *data, int len)
Definition connection.c:287
enum cmdlevel conn_get_access(const struct connection *pconn)
Definition connection.c:775
bool conn_is_valid(const struct connection *pconn)
Definition connection.c:934
bool conn_compression_frozen(const struct connection *pconn)
Definition connection.c:708
bool conn_pattern_list_match(const struct conn_pattern_list *plist, const struct connection *pconn)
Definition connection.c:850
void connection_do_unbuffer(struct connection *pc)
Definition connection.c:336
#define MAX_LEN_NAME
Definition fc_types.h:66
GType type
Definition repodlgs.c:1313
get_token_fn_t func
Definition inputfile.c:128
packet_type
int len
Definition packhand.c:127
m_pre_result
Definition shared.h:213
#define MAX_LEN_ADDR
Definition shared.h:31
size_t size
Definition specvec.h:72
char * wildcard
Definition connection.c:789
int currently_processed_request_id
Definition connection.h:198
struct genhash ** received
Definition connection.h:264
time_t auth_settime
Definition connection.h:213
bool established
Definition connection.h:140
struct player * playing
Definition connection.h:151
char * closing_reason
Definition connection.h:142
int request_id_of_currently_handled_packet
Definition connection.h:193
struct connection::@61::@67 server
enum cmdlevel access_level
Definition connection.h:177
const struct packet_handlers * handlers
Definition connection.h:265
struct conn_list * self
Definition connection.h:163
int last_request_id_used
Definition connection.h:187
struct timer_list * ping_timers
Definition connection.h:205
bool is_closing
Definition connection.h:234
bool settings_sent
Definition connection.h:227
bool observer
Definition connection.h:147
char username[MAX_LEN_NAME]
Definition connection.h:164
enum gui_type client_gui
Definition connection.h:179
struct timer * last_write
Definition connection.h:155
struct connection::@63 phs
enum auth_status status
Definition connection.h:217
struct connection::@61::@66 client
char ipaddr[MAX_LEN_ADDR]
Definition connection.h:221
void(* incoming_packet_notify)(struct connection *pc, int packet_type, int size)
Definition connection.h:251
void(* outgoing_packet_notify)(struct connection *pc, int packet_type, int size, int request_id)
Definition connection.h:259
struct socket_packet_buffer * send_buffer
Definition connection.h:154
struct connection::@64 compression
char capability[MAX_LEN_CAPSTR]
Definition connection.h:171
int frozen_level
Definition connection.h:270
struct connection::@61::@67::@68 delegation
void(* notify_of_writable_data)(struct connection *pc, bool data_available_and_socket_full)
Definition connection.h:181
int last_processed_request_id_seen
Definition connection.h:190
char addr[MAX_LEN_ADDR]
Definition connection.h:165
double ping_time
Definition connection.h:161
struct genhash ** sent
Definition connection.h:263
struct socket_packet_buffer * buffer
Definition connection.h:153
char password[MAX_LEN_PASSWORD]
Definition connection.h:218
int last_request_id_seen
Definition connection.h:201
struct conn_pattern_list * ignore_list
Definition connection.h:230
enum cmdlevel granted_access_level
Definition connection.h:224
struct connection::@65 statistics
unsigned int length
Definition connection.h:124
unsigned int type
Definition connection.h:125
unsigned char * data
Definition connection.h:120
Definition timing.c:81