Freeciv21
Develop your civilization from humble roots to a global empire
packets.h
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 #pragma once
14 
15 struct connection;
16 struct data_in;
17 
18 // utility
19 #include "shared.h" // MAX_LEN_ADDR
20 
21 // common
22 #include "connection.h" // struct connection, MAX_LEN_*
23 #include "diptreaty.h"
24 #include "effects.h"
25 #include "events.h"
26 #include "improvement.h" // bv_imprs
27 #include "player.h"
28 #include "requirements.h"
29 #include "spaceship.h"
30 #include "team.h"
31 #include "tile.h"
32 #include "traderoutes.h"
33 #include "unittype.h"
34 #include "worklist.h"
35 
36 // Used in network protocol.
37 #define MAX_LEN_MSG 1536
38 #define MAX_LEN_ROUTE 2000 /* MAX_LEN_PACKET / 2 - header */
39 
40 /* The size of opaque (void *) data sent in the network packet. To avoid
41  * fragmentation issues, this SHOULD NOT be larger than the standard
42  * ethernet or PPP 1500 byte frame size (with room for headers).
43  *
44  * Do not spend much time optimizing, you have no idea of the actual dynamic
45  * path characteristics between systems, such as VPNs and tunnels.
46  *
47  * Used in network protocol.
48  */
49 #define ATTRIBUTE_CHUNK_SIZE (1400)
50 
51 // Used in network protocol.
57 };
58 
59 // Used in network protocol.
64 };
65 
66 // Used in network protocol.
68  AUTH_LOGIN_FIRST, // request a password for a returning user
69  AUTH_NEWUSER_FIRST, // request a password for a new user
70  AUTH_LOGIN_RETRY, // inform the client to try a different password
71  AUTH_NEWUSER_RETRY /* inform the client to try a different [new] password
72  */
73 };
74 
75 #include "packets_gen.h"
76 
78  union {
79  int (*no_packet)(struct connection *pconn);
80  int (*packet)(struct connection *pconn, const void *packet);
81  int (*force_to_send)(struct connection *pconn, const void *packet,
82  bool force_to_send);
83  } send[PACKET_LAST];
84  void *(*receive[PACKET_LAST])(struct connection *pconn);
85 };
86 
88  enum packet_type *ptype);
89 
90 #define get_packet_from_connection(pc, ptype) \
91  get_packet_from_connection_raw(pc, ptype)
92 
94 
95 void send_attribute_block(const struct player *pplayer,
96  struct connection *pconn);
98  struct player *pplayer,
99  const struct packet_player_attribute_chunk *chunk);
102  const char *capability);
103 const char *packet_name(enum packet_type type);
104 bool packet_has_game_info_flag(enum packet_type type);
105 
108  struct connection *pconn, const struct packet_server_join_reply *packet);
110  struct connection *pconn, const struct packet_server_join_reply *packet);
111 
113  struct connection *pc, struct packet_player_attribute_chunk *packet);
114 
116 const struct packet_handlers *packet_handlers_get(const char *capability);
117 
118 void packets_deinit();
119 
120 #define SEND_PACKET_START(packet_type) \
121  unsigned char buffer[MAX_LEN_PACKET]; \
122  struct raw_data_out dout; \
123  \
124  dio_output_init(&dout, buffer, sizeof(buffer)); \
125  dio_put_type_raw(&dout, (enum data_type) pc->packet_header.length, 0); \
126  dio_put_type_raw(&dout, (enum data_type) pc->packet_header.type, \
127  packet_type);
128 
129 #define SEND_PACKET_END(packet_type) \
130  { \
131  size_t size = dio_output_used(&dout); \
132  \
133  dio_output_rewind(&dout); \
134  dio_put_type_raw(&dout, (enum data_type) pc->packet_header.length, \
135  size); \
136  fc_assert(!dout.too_short); \
137  return send_packet_data(pc, buffer, size, packet_type); \
138  }
139 
140 #define RECEIVE_PACKET_START(packet_type, result) \
141  struct data_in din; \
142  struct packet_type packet_buf, *result = &packet_buf; \
143  \
144  dio_input_init( \
145  &din, pc->buffer->data, \
146  data_type_size((enum data_type) pc->packet_header.length)); \
147  { \
148  int size; \
149  \
150  dio_get_type_raw(&din, (enum data_type) pc->packet_header.length, \
151  &size); \
152  dio_input_init(&din, pc->buffer->data, MIN(size, pc->buffer->ndata)); \
153  } \
154  dio_input_skip( \
155  &din, (data_type_size((enum data_type) pc->packet_header.length) \
156  + data_type_size((enum data_type) pc->packet_header.type)));
157 
158 #define RECEIVE_PACKET_END(result) \
159  if (!packet_check(&din, pc)) { \
160  return nullptr; \
161  } \
162  remove_packet_from_buffer(pc->buffer); \
163  result = new std::remove_reference<decltype(*result)>::type; \
164  *result = packet_buf; \
165  return result;
166 
167 #define RECEIVE_PACKET_FIELD_ERROR(field, ...) \
168  log_packet("Error on field '" #field "'" __VA_ARGS__); \
169  return nullptr
170 
171 int send_packet_data(struct connection *pc, unsigned char *data, int len,
172  enum packet_type packet_type);
173 bool packet_check(struct data_in *din, struct connection *pc);
174 
176  QVector<QString> *qstrvec);
177 QVector<QString> *packet_strvec_extract(const char *str);
178 void qstrvec_from_str(QVector<QString> *, char separator, const char *str);
#define MAX_LEN_PACKET
Definition: connection.h:41
void post_receive_packet_server_join_reply(struct connection *pconn, const struct packet_server_join_reply *packet)
Modify if needed the packet header field lengths.
Definition: packets.cpp:606
bool packet_has_game_info_flag(enum packet_type type)
void packets_deinit()
Call when there is no longer a requirement for protocol processing.
Definition: packets.cpp:804
void packet_strvec_compute(char str[MAX_LEN_PACKET], QVector< QString > *qstrvec)
Definition: packets.cpp:806
const struct packet_handlers * packet_handlers_get(const char *capability)
Returns the packet handlers variant for 'capability'.
Definition: packets.cpp:766
bool packet_check(struct data_in *din, struct connection *pc)
Sanity check packet.
Definition: packets.cpp:617
void packet_header_init(struct packet_header *packet_header)
Set the packet header field lengths used for the login protocol, before the capability of the connect...
Definition: packets.cpp:572
void * get_packet_from_connection_raw(struct connection *pc, enum packet_type *ptype)
Read and return a packet from the connection 'pc'.
Definition: packets.cpp:329
const struct packet_handlers * packet_handlers_initial()
Returns the packet handlers variant with no special capability.
Definition: packets.cpp:749
void packet_handlers_fill_capability(struct packet_handlers *phandlers, const char *capability)
const char * packet_name(enum packet_type type)
report_type
Definition: packets.h:52
@ REPORT_WONDERS_OF_THE_WORLD
Definition: packets.h:53
@ REPORT_DEMOGRAPHIC
Definition: packets.h:55
@ REPORT_TOP_5_CITIES
Definition: packets.h:54
@ REPORT_ACHIEVEMENTS
Definition: packets.h:56
void pre_send_packet_player_attribute_chunk(struct connection *pc, struct packet_player_attribute_chunk *packet)
Test and log for sending player attribute_block.
Definition: packets.cpp:725
void remove_packet_from_buffer(struct socket_packet_buffer *buffer)
Remove the packet from the buffer.
Definition: packets.cpp:553
void post_send_packet_server_join_reply(struct connection *pconn, const struct packet_server_join_reply *packet)
Modify if needed the packet header field lengths.
Definition: packets.cpp:595
authentication_type
Definition: packets.h:67
@ AUTH_NEWUSER_RETRY
Definition: packets.h:71
@ AUTH_NEWUSER_FIRST
Definition: packets.h:69
@ AUTH_LOGIN_RETRY
Definition: packets.h:70
@ AUTH_LOGIN_FIRST
Definition: packets.h:68
int send_packet_data(struct connection *pc, unsigned char *data, int len, enum packet_type packet_type)
It returns the request id of the outgoing packet (or 0 if is_server()).
Definition: packets.cpp:176
void qstrvec_from_str(QVector< QString > *, char separator, const char *str)
Build the string vector from a string until 'str_size' bytes are read.
Definition: packets.cpp:838
QVector< QString > * packet_strvec_extract(const char *str)
Definition: packets.cpp:819
void generic_handle_player_attribute_chunk(struct player *pplayer, const struct packet_player_attribute_chunk *chunk)
Updates pplayer->attribute_block according to the given packet.
Definition: packets.cpp:639
void send_attribute_block(const struct player *pplayer, struct connection *pconn)
Split the attribute block into chunks and send them over pconn.
Definition: packets.cpp:681
unit_info_use
Definition: packets.h:60
@ UNIT_INFO_IDENTITY
Definition: packets.h:61
@ UNIT_INFO_CITY_PRESENT
Definition: packets.h:63
@ UNIT_INFO_CITY_SUPPORTED
Definition: packets.h:62
void packet_handlers_fill_initial(struct packet_handlers *phandlers)
int len
Definition: packhand.cpp:127
char capability[MAX_LEN_CAPSTR]
Definition: connection.h:158
struct socket_packet_buffer * buffer
Definition: connection.h:144
void *(* receive[PACKET_LAST])(struct connection *pconn)
Definition: packets.h:84
int(* force_to_send)(struct connection *pconn, const void *packet, bool force_to_send)
Definition: packets.h:81
int(* no_packet)(struct connection *pconn)
Definition: packets.h:79
int(* packet)(struct connection *pconn, const void *packet)
Definition: packets.h:80
union packet_handlers::@63 send[PACKET_LAST]
Definition: player.h:231