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 */
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***********************************************************/
122 int ndata;
124 int nsize;
125 unsigned char *data;
126};
127
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***********************************************************/
142 int id; /* used for server/client communication */
143 int sock;
144 bool used;
145 bool established; /* have negotiated initial packets */
148
149 /* connection is "observer", not controller; may be observing
150 * specific player, or all (implementation incomplete).
151 */
153
154 /* NULL for connections not yet associated with a specific player.
155 */
157
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 */
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 */
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
187 bool data_available_and_socket_full);
188
189 union {
190 struct {
191 /* Increases for every packet send to the server. */
193
194 /* Increases for every received PACKET_PROCESSING_FINISHED packet. */
196
197 /* Holds the id of the request which caused this packet. Can be zero. */
200
201 struct {
202 /* Holds the id of the request which is processed now. Can be zero. */
204
205 /* Will increase for every received packet. */
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. */
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. */
219
220 /* used to follow where the connection is in the authentication
221 * process */
224
225 /* for reverse lookup and blacklisting in db */
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. */
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;
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;
269
270#ifdef USE_COMPRESSION
271 struct {
273
274 struct byte_vector queue;
276#endif
277 struct {
280};
281
282
283typedef void (*conn_close_fn_t) (struct connection *pconn);
285void connection_close(struct connection *pconn, const char *reason);
286
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
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 */
char user_name[512]
auth_status
Definition connection.h:97
@ AS_REQUESTING_OLD_PASS
Definition connection.h:101
@ AS_FAILED
Definition connection.h:99
@ AS_NOT_ESTABLISHED
Definition connection.h:98
@ AS_REQUESTING_NEW_PASS
Definition connection.h:100
@ AS_ESTABLISHED
Definition connection.h:102
size_t conn_pattern_to_string(const struct conn_pattern *ppattern, char *buf, size_t buf_len)
Definition connection.c:862
struct player * conn_get_player(const struct connection *pconn)
Definition connection.c:760
void conn_list_do_unbuffer(struct conn_list *dest)
Definition connection.c:365
void free_compression_queue(struct connection *pconn)
Definition connection.c:546
struct connection * conn_by_user_prefix(const char *user_name, enum m_pre_result *result)
Definition connection.c:397
bool can_conn_edit(const struct connection *pconn)
Definition connection.c:510
void(* conn_close_fn_t)(struct connection *pconn)
Definition connection.h:283
struct connection * conn_by_user(const char *user_name)
Definition connection.c:376
void conn_list_compression_thaw(const struct conn_list *pconn_list)
Definition connection.c:729
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:229
int get_next_request_id(int old_request_id)
Definition connection.c:529
struct socket_packet_buffer * new_socket_packet_buffer(void)
Definition connection.c:438
#define MAX_LEN_PASSWORD
Definition connection.h:58
void conn_list_do_buffer(struct conn_list *dest)
Definition connection.c:355
bool can_conn_enable_editing(const struct connection *pconn)
Definition connection.c:520
void connection_do_buffer(struct connection *pc)
Definition connection.c:323
#define MAX_LEN_CAPSTR
Definition connection.h:57
bool conn_pattern_match(const struct conn_pattern *ppattern, const struct connection *pconn)
Definition connection.c:816
void conn_set_capability(struct connection *pconn, const char *capability)
Definition connection.c:658
void conn_pattern_destroy(struct conn_pattern *ppattern)
Definition connection.c:806
void connection_common_init(struct connection *pconn)
Definition connection.c:602
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:717
struct conn_pattern * conn_pattern_new(enum conn_pattern_type type, const char *wildcard)
Definition connection.c:792
bool conn_controls_player(const struct connection *pconn)
Definition connection.c:742
bool conn_is_global_observer(const struct connection *pconn)
Definition connection.c:750
const char * conn_description(const struct connection *pconn)
Definition connection.c:473
void connection_common_close(struct connection *pconn)
Definition connection.c:627
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
void conn_compression_freeze(struct connection *pconn)
Definition connection.c:691
struct connection * conn_by_number(int id)
Definition connection.c:420
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:670
bool connection_send_data(struct connection *pconn, const unsigned char *data, int len)
Definition connection.c:286
enum cmdlevel conn_get_access(const struct connection *pconn)
Definition connection.c:772
bool conn_is_valid(const struct connection *pconn)
Definition connection.c:931
bool conn_compression_frozen(const struct connection *pconn)
Definition connection.c:705
bool conn_pattern_list_match(const struct conn_pattern_list *plist, const struct connection *pconn)
Definition connection.c:847
void connection_do_unbuffer(struct connection *pc)
Definition connection.c:335
#define MAX_LEN_NAME
Definition fc_types.h:66
GType type
Definition repodlgs.c:1312
get_token_fn_t func
Definition inputfile.c:128
packet_type
int len
Definition packhand.c:125
m_pre_result
Definition shared.h:207
#define MAX_LEN_ADDR
Definition shared.h:31
size_t size
Definition specvec.h:72
char * wildcard
Definition connection.c:786
int currently_processed_request_id
Definition connection.h:203
struct genhash ** received
Definition connection.h:266
time_t auth_settime
Definition connection.h:218
bool established
Definition connection.h:145
struct connection::@61 statistics
struct player * playing
Definition connection.h:156
char * closing_reason
Definition connection.h:147
struct connection::@57::@62 client
int request_id_of_currently_handled_packet
Definition connection.h:198
struct connection::@57::@63::@64 delegation
enum cmdlevel access_level
Definition connection.h:182
const struct packet_handlers * handlers
Definition connection.h:267
struct conn_list * self
Definition connection.h:168
int last_request_id_used
Definition connection.h:192
struct timer_list * ping_timers
Definition connection.h:210
bool is_closing
Definition connection.h:236
struct connection::@59 phs
bool observer
Definition connection.h:152
char username[MAX_LEN_NAME]
Definition connection.h:169
enum gui_type client_gui
Definition connection.h:184
struct connection::@57::@63 server
struct timer * last_write
Definition connection.h:160
struct connection::@60 compression
enum auth_status status
Definition connection.h:222
char ipaddr[MAX_LEN_ADDR]
Definition connection.h:226
void(* incoming_packet_notify)(struct connection *pc, int packet_type, int size)
Definition connection.h:253
void(* outgoing_packet_notify)(struct connection *pc, int packet_type, int size, int request_id)
Definition connection.h:261
struct socket_packet_buffer * send_buffer
Definition connection.h:159
char capability[MAX_LEN_CAPSTR]
Definition connection.h:176
int frozen_level
Definition connection.h:272
void(* notify_of_writable_data)(struct connection *pc, bool data_available_and_socket_full)
Definition connection.h:186
int last_processed_request_id_seen
Definition connection.h:195
char addr[MAX_LEN_ADDR]
Definition connection.h:170
double ping_time
Definition connection.h:166
struct genhash ** sent
Definition connection.h:265
struct socket_packet_buffer * buffer
Definition connection.h:158
char password[MAX_LEN_PASSWORD]
Definition connection.h:223
int last_request_id_seen
Definition connection.h:206
struct conn_pattern_list * ignore_list
Definition connection.h:232
enum cmdlevel granted_access_level
Definition connection.h:229
unsigned int length
Definition connection.h:129
unsigned int type
Definition connection.h:130
unsigned char * data
Definition connection.h:125
Definition timing.c:81