Freeciv21
Develop your civilization from humble roots to a global empire
connection.h
Go to the documentation of this file.
1 /**************************************************************************
2  Copyright (c) 1996-2020 Freeciv21 and Freeciv contributors. This file is
3  __ __ part of Freeciv21. Freeciv21 is free software: you can
4 / \\..// \ redistribute it and/or modify it under the terms of the GNU
5  ( oo ) General Public License as published by the Free Software
6  \__/ Foundation, either version 3 of the License, or (at your
7  option) any later version. You should have received
8  a copy of the GNU General Public License along with Freeciv21. If not,
9  see https://www.gnu.org/licenses/.
10 **************************************************************************/
11 #pragma once
12 
13 #include <ctime> // time_t
14 
15 /**************************************************************************
16  The connection struct and related stuff.
17  Includes cmdlevel stuff, which is connection-based.
18 ***************************************************************************/
19 
20 // Qt
21 #include <QList>
22 #include <QString>
23 
24 // utility
25 #include "shared.h" // MAX_LEN_ADDR
26 #include "support.h" // bool type
27 #include "timing.h"
28 
29 // common
30 #include "fc_types.h"
31 
32 // Forward declarations
33 class QIODevice;
34 class QString;
35 
36 struct conn_pattern_list;
37 struct genhash;
38 struct packet_handlers;
39 
40 // Used in the network protocol.
41 #define MAX_LEN_PACKET 4096
42 #define MAX_LEN_CAPSTR 512
43 #define MAX_LEN_PASSWORD \
44  512 /* do not change this under any circumstances \
45  */
46 #define MAX_LEN_CONTENT (MAX_LEN_PACKET - 20)
47 
48 #define MAX_LEN_BUFFER (MAX_LEN_PACKET * 128)
49 
50 /****************************************************************************
51  Command access levels for client-side use; at present, they are only
52  used to control access to server commands typed at the client chatline.
53  Used in the network protocol.
54 ****************************************************************************/
55 #define SPECENUM_NAME cmdlevel
56 // User may issue no commands at all.
57 #define SPECENUM_VALUE0 ALLOW_NONE
58 #define SPECENUM_VALUE0NAME "none"
59 // Informational or observer commands only.
60 #define SPECENUM_VALUE1 ALLOW_INFO
61 #define SPECENUM_VALUE1NAME "info"
62 // User may issue basic player commands.
63 #define SPECENUM_VALUE2 ALLOW_BASIC
64 #define SPECENUM_VALUE2NAME "basic"
65 /* User may issue commands that affect game & users
66  * (starts a vote if the user's level is 'basic'). */
67 #define SPECENUM_VALUE3 ALLOW_CTRL
68 #define SPECENUM_VALUE3NAME "ctrl"
69 // User may issue commands that affect the server.
70 #define SPECENUM_VALUE4 ALLOW_ADMIN
71 #define SPECENUM_VALUE4NAME "admin"
72 // User may issue *all* commands - dangerous!
73 #define SPECENUM_VALUE5 ALLOW_HACK
74 #define SPECENUM_VALUE5NAME "hack"
75 #define SPECENUM_COUNT CMDLEVEL_COUNT
76 #include "specenum_gen.h"
77 
78 /***************************************************************************
79  On the distinction between nations(formerly races), players, and users,
80  see doc/HACKING
81 ***************************************************************************/
82 
83 // where the connection is in the authentication process
90 };
91 
92 // get 'struct conn_list' and related functions:
93 /* do this with forward definition of struct connection, so that
94  * connection struct can contain a struct conn_list */
95 struct connection;
96 #define SPECLIST_TAG conn
97 #define SPECLIST_TYPE struct connection
98 #include "speclist.h"
99 
100 #define conn_list_iterate(connlist, pconn) \
101  TYPED_LIST_ITERATE(struct connection, connlist, pconn)
102 #define conn_list_iterate_end LIST_ITERATE_END
103 
104 /***********************************************************
105  This is a buffer where the data is first collected,
106  whenever it arrives to the client/server.
107 ***********************************************************/
109  unsigned long ndata;
111  unsigned long nsize;
112  unsigned char *data;
113 };
114 
116  unsigned int length : 4; // Actually 'enum data_type'
117  unsigned int type : 4; // Actually 'enum data_type'
118 };
119 
120 #define SPECVEC_TAG byte
121 #define SPECVEC_TYPE unsigned char
122 #include "specvec.h"
123 
124 /***********************************************************
125  The connection struct represents a single client or server
126  at the other end of a network connection.
127 ***********************************************************/
128 struct connection {
129  int id; /* used for server/client communication */
130  QIODevice *sock = nullptr;
131  bool used;
132  bool established; // have negotiated initial packets
134  QString closing_reason;
135 
136  /* connection is "observer", not controller; may be observing
137  * specific player, or all (implementation incomplete).
138  */
139  bool observer;
140 
141  /* nullptr for connections not yet associated with a specific player.
142  */
143  struct player *playing;
144 
147  class civtimer *last_write;
148 
149  double ping_time;
150 
151  struct conn_list *self; // list with this connection as single element
153  QString addr;
154 
155  /*
156  * "capability" gives the capability string of the executable (be it
157  * a client or server) at the other end of the connection.
158  */
160 
161  /*
162  * "access_level" stores the current access level of the client
163  * corresponding to this connection.
164  */
165  enum cmdlevel access_level;
166 
167  void (*notify_of_writable_data)(struct connection *pc,
168  bool data_available_and_socket_full);
169 
170  union {
171  struct {
172  // Increases for every packet send to the server.
174 
175  // Increases for every received PACKET_PROCESSING_FINISHED packet.
177 
178  /* Holds the id of the request which caused this packet. Can be zero.
179  */
181  } client;
182 
183  struct {
184  // Holds the id of the request which is processed now. Can be zero.
186 
187  // Will increase for every received packet.
189 
190  /* The start times of the PACKET_CONN_PING which have been sent but
191  * weren't PACKET_CONN_PONGed yet? */
192  QList<civtimer *> *ping_timers;
193 
194  // Holds number of tries for authentication from client.
195  int auth_tries;
196 
197  /* the time that the server will respond after receiving an auth reply.
198  * this is used to throttle the connection. Also used to reject a
199  * connection if we've waited too long for a password. */
200  time_t auth_settime;
201 
202  /* used to follow where the connection is in the authentication
203  * process */
206 
207  // for reverse lookup and blacklisting in db
208  char ipaddr[MAX_LEN_ADDR];
209 
210  // The access level initially given to the client upon connection.
211  enum cmdlevel granted_access_level;
212 
213  // The list of ignored connection patterns.
214  struct conn_pattern_list *ignore_list;
215 
216  /* Something has occurred that means the connection should be closed,
217  * but the closing has been postponed. */
218  bool is_closing;
219 
220  /* If we use delegation the original player (playing) is replaced. Save
221  * it here to easily restore it. */
222  struct {
223  bool status; // TRUE if player currently delegated to us
224  struct player *playing;
225  bool observer;
227  } server;
228  };
229 
230  /*
231  * Called before an incoming packet is processed. The packet_type
232  * argument should really be a "enum packet_type". However due
233  * circular dependency this is impossible.
234  */
235  void (*incoming_packet_notify)(struct connection *pc, int packet_type,
236  int size);
237 
238  /*
239  * Called before a packet is sent. The packet_type argument should
240  * really be a "enum packet_type". However due circular dependency
241  * this is impossible.
242  */
243  void (*outgoing_packet_notify)(struct connection *pc, int packet_type,
244  int size, int request_id);
245  struct {
246  struct genhash **sent;
247  struct genhash **received;
248  const struct packet_handlers *handlers;
249  } phs;
250 
251  struct {
252  int frozen_level;
253 
254  struct byte_vector queue;
255  } compression;
256  struct {
258  } statistics;
259 };
260 
261 typedef void (*conn_close_fn_t)(struct connection *pconn);
263 void connection_close(struct connection *pconn, const QString &reason);
264 
265 int read_socket_data(QIODevice *sock, struct socket_packet_buffer *buffer);
267 bool connection_send_data(struct connection *pconn,
268  const unsigned char *data, int len);
269 
270 void connection_do_buffer(struct connection *pc);
271 void connection_do_unbuffer(struct connection *pc);
272 
273 void conn_list_do_buffer(struct conn_list *dest);
274 void conn_list_do_unbuffer(struct conn_list *dest);
275 
276 struct connection *conn_by_user(const char *user_name);
277 struct connection *conn_by_user_prefix(const char *user_name,
278  enum m_pre_result *result);
279 struct connection *conn_by_number(int id);
280 
282 void connection_common_init(struct connection *pconn);
283 void connection_common_close(struct connection *pconn);
284 void conn_set_capability(struct connection *pconn, const char *capability);
285 void free_compression_queue(struct connection *pconn);
286 void conn_reset_delta_state(struct connection *pconn);
287 
288 void conn_compression_freeze(struct connection *pconn);
289 bool conn_compression_thaw(struct connection *pconn);
290 bool conn_compression_frozen(const struct connection *pconn);
291 void conn_list_compression_freeze(const struct conn_list *pconn_list);
292 void conn_list_compression_thaw(const struct conn_list *pconn_list);
293 
294 const char *conn_description(const struct connection *pconn,
295  bool is_private = true);
296 bool conn_controls_player(const struct connection *pconn);
297 bool conn_is_global_observer(const struct connection *pconn);
298 enum cmdlevel conn_get_access(const struct connection *pconn);
299 
300 struct player;
301 struct player *conn_get_player(const struct connection *pconn);
302 
303 bool can_conn_edit(const struct connection *pconn);
304 bool can_conn_enable_editing(const struct connection *pconn);
305 
306 int get_next_request_id(int old_request_id);
307 
308 extern const char blank_addr_str[];
309 
310 // Connection patterns.
311 struct conn_pattern;
312 
313 #define SPECLIST_TAG conn_pattern
314 #define SPECLIST_TYPE struct conn_pattern
315 #include "speclist.h"
316 #define conn_pattern_list_iterate(plist, ppatern) \
317  TYPED_LIST_ITERATE(struct conn_pattern, plist, ppatern)
318 #define conn_pattern_list_iterate_end LIST_ITERATE_END
319 
320 #define SPECENUM_NAME conn_pattern_type
321 #define SPECENUM_VALUE0 CPT_USER
322 #define SPECENUM_VALUE0NAME "user"
323 #define SPECENUM_VALUE1 CPT_HOST
324 #define SPECENUM_VALUE1NAME "host"
325 #define SPECENUM_VALUE2 CPT_IP
326 #define SPECENUM_VALUE2NAME "ip"
327 #include "specenum_gen.h"
328 
329 struct conn_pattern *conn_pattern_new(enum conn_pattern_type type,
330  const char *wildcard);
331 void conn_pattern_destroy(struct conn_pattern *ppattern);
332 
333 bool conn_pattern_match(const struct conn_pattern *ppattern,
334  const struct connection *pconn);
335 bool conn_pattern_list_match(const struct conn_pattern_list *plist,
336  const struct connection *pconn);
337 
338 size_t conn_pattern_to_string(const struct conn_pattern *ppattern, char *buf,
339  size_t buf_len);
340 struct conn_pattern *conn_pattern_from_string(const char *pattern,
341  enum conn_pattern_type prefer,
342  char *error_buf,
343  size_t error_buf_len);
344 
345 bool conn_is_valid(const struct connection *pconn);
auth_status
Definition: connection.h:83
@ AS_REQUESTING_OLD_PASS
Definition: connection.h:87
@ AS_FAILED
Definition: connection.h:85
@ AS_NOT_ESTABLISHED
Definition: connection.h:84
@ AS_REQUESTING_NEW_PASS
Definition: connection.h:86
@ AS_ESTABLISHED
Definition: connection.h:88
size_t conn_pattern_to_string(const struct conn_pattern *ppattern, char *buf, size_t buf_len)
Put a string reprentation of the pattern in 'buf'.
Definition: connection.cpp:796
void conn_list_do_unbuffer(struct conn_list *dest)
Convenience functions to unbuffer a list of connections.
Definition: connection.cpp:319
void free_compression_queue(struct connection *pconn)
Free compression queue for given connection.
Definition: connection.cpp:508
bool can_conn_edit(const struct connection *pconn)
Return TRUE iff the connection is currently allowed to edit.
Definition: connection.cpp:472
void(* conn_close_fn_t)(struct connection *pconn)
Definition: connection.h:260
void conn_list_compression_thaw(const struct conn_list *pconn_list)
Thaw a connection list.
Definition: connection.cpp:665
void connections_set_close_callback(conn_close_fn_t func)
Register the close_callback.
Definition: connection.cpp:61
bool conn_compression_thaw(struct connection *pconn)
Thaw the connection.
Definition: packets.cpp:160
void flush_connection_send_buffer_all(struct connection *pc)
Flush'em.
Definition: connection.cpp:177
int get_next_request_id(int old_request_id)
Get next request id.
Definition: connection.cpp:490
struct player * conn_get_player(const struct connection *pconn)
Returns the player that this connection is attached to, or nullptr.
Definition: connection.cpp:693
int read_socket_data(QIODevice *sock, struct socket_packet_buffer *buffer)
Read data from socket, and check if a packet is ready.
Definition: connection.cpp:110
#define MAX_LEN_PASSWORD
Definition: connection.h:43
void conn_list_do_buffer(struct conn_list *dest)
Convenience functions to buffer a list of connections.
Definition: connection.cpp:310
bool can_conn_enable_editing(const struct connection *pconn)
Return TRUE iff the connection is allowed to start editing.
Definition: connection.cpp:481
void connection_do_buffer(struct connection *pc)
Turn on buffering, using a counter so that calls may be nested.
Definition: connection.cpp:278
#define MAX_LEN_CAPSTR
Definition: connection.h:42
bool conn_pattern_match(const struct conn_pattern *ppattern, const struct connection *pconn)
Returns TRUE whether the connection fits the connection pattern.
Definition: connection.cpp:748
void conn_set_capability(struct connection *pconn, const char *capability)
Set the network capability string for 'pconn'.
Definition: connection.cpp:603
void conn_pattern_destroy(struct conn_pattern *ppattern)
Free a connection pattern.
Definition: connection.cpp:738
void connection_common_init(struct connection *pconn)
Initialize common part of connection structure.
Definition: connection.cpp:555
void conn_list_compression_freeze(const struct conn_list *pconn_list)
Freeze a connection list.
Definition: connection.cpp:656
bool conn_controls_player(const struct connection *pconn)
Returns TRUE if the given connection is attached to a player which it also controls (i....
Definition: connection.cpp:675
bool conn_is_global_observer(const struct connection *pconn)
Returns TRUE if the given connection is a global observer.
Definition: connection.cpp:683
void connection_close(struct connection *pconn, const QString &reason)
Call the conn_close_callback.
Definition: connection.cpp:69
void connection_common_close(struct connection *pconn)
Connection closing part common to server and client.
Definition: connection.cpp:574
void conn_compression_freeze(struct connection *pconn)
Freeze the connection.
Definition: connection.cpp:636
struct connection * conn_by_number(int id)
Find connection by id, from game.all_connections.
Definition: connection.cpp:376
const char blank_addr_str[]
Definition: connection.cpp:35
void conn_reset_delta_state(struct connection *pconn)
Remove all is-game-info cached packets from the connection.
Definition: connection.cpp:615
bool connection_send_data(struct connection *pconn, const unsigned char *data, int len)
Write data to socket.
Definition: connection.cpp:245
struct connection * conn_by_user(const char *user_name)
Find connection by exact user name, from game.all_connections, case-insensitve.
Definition: connection.cpp:329
enum cmdlevel conn_get_access(const struct connection *pconn)
Returns the current access level of the given connection.
Definition: connection.cpp:705
bool conn_is_valid(const struct connection *pconn)
Returns TRUE if the connection is valid, i.e.
Definition: connection.cpp:864
bool conn_compression_frozen(const struct connection *pconn)
Returns TRUE if the connection is frozen.
Definition: connection.cpp:648
struct connection * conn_by_user_prefix(const char *user_name, enum m_pre_result *result)
Definition: connection.cpp:353
const char * conn_description(const struct connection *pconn, bool is_private=true)
° Return pointer to static string containing a description for this ° connection, based on pconn->nam...
Definition: connection.cpp:431
struct conn_pattern * conn_pattern_from_string(const char *pattern, enum conn_pattern_type prefer, char *error_buf, size_t error_buf_len)
Creates a new connection pattern from the string.
Definition: connection.cpp:809
struct socket_packet_buffer * new_socket_packet_buffer()
Return malloced struct, appropriately initialized.
Definition: connection.cpp:395
struct conn_pattern * conn_pattern_new(enum conn_pattern_type type, const char *wildcard)
Creates a new connection pattern.
Definition: connection.cpp:724
bool conn_pattern_list_match(const struct conn_pattern_list *plist, const struct connection *pconn)
Returns TRUE whether the connection fits one of the connection patterns.
Definition: connection.cpp:779
void connection_do_unbuffer(struct connection *pc)
Turn off buffering if internal counter of number of times buffering was turned on falls to zero,...
Definition: connection.cpp:290
#define MAX_LEN_NAME
Definition: fc_types.h:61
get_token_fn_t func
Definition: inputfile.cpp:119
int len
Definition: packhand.cpp:127
m_pre_result
Definition: shared.h:152
#define MAX_LEN_ADDR
Definition: shared.h:27
size_t size
Definition: specvec.h:64
Connection patterns.
Definition: connection.cpp:716
char * wildcard
Definition: connection.cpp:718
enum conn_pattern_type type
Definition: connection.cpp:717
class civtimer * last_write
Definition: connection.h:146
int currently_processed_request_id
Definition: connection.h:184
struct genhash ** received
Definition: connection.h:246
time_t auth_settime
Definition: connection.h:199
bool established
Definition: connection.h:131
struct player * playing
Definition: connection.h:142
int request_id_of_currently_handled_packet
Definition: connection.h:179
enum cmdlevel access_level
Definition: connection.h:164
const struct packet_handlers * handlers
Definition: connection.h:247
QString closing_reason
Definition: connection.h:133
int last_request_id_used
Definition: connection.h:172
struct connection::@55::@61 server
struct connection::@58 compression
struct connection::@55::@61::@62 delegation
struct connection::@59 statistics
struct connection::@57 phs
bool is_closing
Definition: connection.h:217
QIODevice * sock
Definition: connection.h:129
bool observer
Definition: connection.h:138
char username[MAX_LEN_NAME]
Definition: connection.h:151
enum auth_status status
Definition: connection.h:203
char ipaddr[MAX_LEN_ADDR]
Definition: connection.h:207
void(* incoming_packet_notify)(struct connection *pc, int packet_type, int size)
Definition: connection.h:234
int bytes_send
Definition: connection.h:256
void(* outgoing_packet_notify)(struct connection *pc, int packet_type, int size, int request_id)
Definition: connection.h:242
struct connection::@55::@60 client
struct socket_packet_buffer * send_buffer
Definition: connection.h:145
struct byte_vector queue
Definition: connection.h:253
QList< civtimer * > * ping_timers
Definition: connection.h:191
char capability[MAX_LEN_CAPSTR]
Definition: connection.h:158
int frozen_level
Definition: connection.h:251
void(* notify_of_writable_data)(struct connection *pc, bool data_available_and_socket_full)
Definition: connection.h:166
int last_processed_request_id_seen
Definition: connection.h:175
int auth_tries
Definition: connection.h:194
double ping_time
Definition: connection.h:148
struct genhash ** sent
Definition: connection.h:245
struct socket_packet_buffer * buffer
Definition: connection.h:144
char password[MAX_LEN_PASSWORD]
Definition: connection.h:204
QString addr
Definition: connection.h:152
int last_request_id_seen
Definition: connection.h:187
struct conn_pattern_list * ignore_list
Definition: connection.h:213
enum cmdlevel granted_access_level
Definition: connection.h:210
unsigned int length
Definition: connection.h:115
unsigned int type
Definition: connection.h:116
Definition: player.h:231
unsigned long ndata
Definition: connection.h:108
unsigned long nsize
Definition: connection.h:110
unsigned char * data
Definition: connection.h:111