Freeciv-3.1
Loading...
Searching...
No Matches
packets.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__PACKETS_H
14#define FC__PACKETS_H
15
16#ifdef __cplusplus
17extern "C" {
18#endif /* __cplusplus */
19
20struct connection;
21struct data_in;
22
23/* utility */
24#include "shared.h" /* MAX_LEN_ADDR */
25
26/* common */
27#include "connection.h" /* struct connection, MAX_LEN_* */
28#include "diptreaty.h"
29#include "effects.h"
30#include "events.h"
31#include "improvement.h" /* bv_imprs */
32#include "player.h"
33#include "requirements.h"
34#include "spaceship.h"
35#include "team.h"
36#include "tile.h"
37#include "traderoutes.h"
38#include "unittype.h"
39#include "worklist.h"
40
41
42/* Used in network protocol. */
43#define MAX_LEN_MSG 1536
44#define MAX_LEN_ROUTE 2000 /* MAX_LEN_PACKET / 2 - header */
45
46#ifdef FREECIV_WEB
47#define web_send_packet(packetname, ...) \
48 send_packet_web_ ##packetname( __VA_ARGS__ )
49#define web_lsend_packet(packetname, ...) \
50 lsend_packet_web_ ##packetname( __VA_ARGS__ )
51#else /* FREECIV_WEB */
52#define web_send_packet(packetname, ...)
53#define web_lsend_packet(packetname, ...)
54#endif /* FREECIV_WEB */
55
56/* The size of opaque (void *) data sent in the network packet. To avoid
57 * fragmentation issues, this SHOULD NOT be larger than the standard
58 * ethernet or PPP 1500 byte frame size (with room for headers).
59 *
60 * Do not spend much time optimizing, you have no idea of the actual dynamic
61 * path characteristics between systems, such as VPNs and tunnels.
62 *
63 * Used in network protocol.
64 */
65#define ATTRIBUTE_CHUNK_SIZE (1400)
66
67/* Indicates that the player initiated a request.
68 *
69 * Used in network protocol. */
70#define REQEST_PLAYER_INITIATED (0)
71
72/* Used in network protocol. */
79
80/* Used in network protocol. */
86
87/* Used in network protocol. */
89 AUTH_LOGIN_FIRST, /* request a password for a returning user */
90 AUTH_NEWUSER_FIRST, /* request a password for a new user */
91 AUTH_LOGIN_RETRY, /* inform the client to try a different password */
92 AUTH_NEWUSER_RETRY /* inform the client to try a different [new] password */
93};
94
95#include "packets_gen.h"
96
98 union {
99 int (*no_packet)(struct connection *pconn);
100 int (*packet)(struct connection *pconn, const void *packet);
101 int (*force_to_send)(struct connection *pconn, const void *packet,
102 bool force_to_send);
104 void *(*receive[PACKET_LAST])(struct connection *pconn);
105};
106
108 enum packet_type *ptype);
109
110#ifdef FREECIV_JSON_CONNECTION
111#define get_packet_from_connection(pc, ptype) get_packet_from_connection_json(pc, ptype)
112#else
113#define get_packet_from_connection(pc, ptype) get_packet_from_connection_raw(pc, ptype)
114#endif
115
117
118void send_attribute_block(const struct player *pplayer,
119 struct connection *pconn);
121 const struct
123 *chunk);
124void packet_handlers_fill_initial(struct packet_handlers *phandlers);
126 const char *capability);
127const char *packet_name(enum packet_type type);
129
132 const struct packet_server_join_reply
133 *packet);
135 const struct
137
140 *packet);
141
142const struct packet_handlers *packet_handlers_initial(void);
143const struct packet_handlers *packet_handlers_get(const char *capability);
144
145void packets_deinit(void);
146
147#ifdef FREECIV_JSON_CONNECTION
148#include "packets_json.h"
149#else
150
151#define SEND_PACKET_START(packet_type) \
152 unsigned char buffer[MAX_LEN_PACKET]; \
153 struct raw_data_out dout; \
154 \
155 dio_output_init(&dout, buffer, sizeof(buffer)); \
156 dio_put_type_raw(&dout, pc->packet_header.length, 0); \
157 dio_put_type_raw(&dout, pc->packet_header.type, packet_type);
158
159#define SEND_PACKET_END(packet_type) \
160 { \
161 size_t size = dio_output_used(&dout); \
162 \
163 dio_output_rewind(&dout); \
164 dio_put_type_raw(&dout, pc->packet_header.length, size); \
165 fc_assert(!dout.too_short); \
166 return send_packet_data(pc, buffer, size, packet_type); \
167 }
168
169#define RECEIVE_PACKET_START(packet_type, result) \
170 struct data_in din; \
171 struct packet_type packet_buf, *result = &packet_buf; \
172 \
173 dio_input_init(&din, pc->buffer->data, \
174 data_type_size(pc->packet_header.length)); \
175 { \
176 int size; \
177 \
178 dio_get_type_raw(&din, pc->packet_header.length, &size); \
179 dio_input_init(&din, pc->buffer->data, MIN(size, pc->buffer->ndata)); \
180 } \
181 dio_input_skip(&din, (data_type_size(pc->packet_header.length) \
182 + data_type_size(pc->packet_header.type)));
183
184#define RECEIVE_PACKET_END(result) \
185 if (!packet_check(&din, pc)) { \
186 return NULL; \
187 } \
188 remove_packet_from_buffer(pc->buffer); \
189 result = fc_malloc(sizeof(*result)); \
190 *result = packet_buf; \
191 return result;
192
193#define RECEIVE_PACKET_FIELD_ERROR(field, ...) \
194 log_packet("Error on field '" #field "'" __VA_ARGS__); \
195 return NULL
196
197#endif /* FREECIV_JSON_PROTOCOL */
198
199int send_packet_data(struct connection *pc, unsigned char *data, int len,
201bool packet_check(struct data_in *din, struct connection *pc);
202
203/* Utilities to exchange strings and string vectors. */
204#define PACKET_STRVEC_SEPARATOR '\3'
205#define PACKET_STRVEC_COMPUTE(str, strvec) \
206 if (NULL != strvec) { \
207 strvec_to_str(strvec, PACKET_STRVEC_SEPARATOR, str, sizeof(str)); \
208 } else { \
209 str[0] = '\0'; \
210 }
211#define PACKET_STRVEC_EXTRACT(strvec, str) \
212 if ('\0' != str[0]) { \
213 strvec = strvec_new(); \
214 strvec_from_str(strvec, PACKET_STRVEC_SEPARATOR, str); \
215 } else { \
216 strvec = NULL; \
217 }
218
219#ifdef __cplusplus
220}
221#endif /* __cplusplus */
222
223#endif /* FC__PACKETS_H */
GType type
Definition repodlgs.c:1312
void post_receive_packet_server_join_reply(struct connection *pconn, const struct packet_server_join_reply *packet)
Definition packets.c:652
void * get_packet_from_connection_raw(struct connection *pc, enum packet_type *ptype)
Definition packets.c:373
bool packet_has_game_info_flag(enum packet_type type)
bool packet_check(struct data_in *din, struct connection *pc)
Definition packets.c:665
void packet_header_init(struct packet_header *packet_header)
Definition packets.c:617
void packet_handlers_fill_capability(struct packet_handlers *phandlers, const char *capability)
const struct packet_handlers * packet_handlers_initial(void)
Definition packets.c:823
void packets_deinit(void)
Definition packets.c:900
report_type
Definition packets.h:73
@ REPORT_WONDERS_OF_THE_WORLD
Definition packets.h:74
@ REPORT_DEMOGRAPHIC
Definition packets.h:76
@ REPORT_TOP_5_CITIES
Definition packets.h:75
@ REPORT_ACHIEVEMENTS
Definition packets.h:77
void pre_send_packet_player_attribute_chunk(struct connection *pc, struct packet_player_attribute_chunk *packet)
Definition packets.c:793
void remove_packet_from_buffer(struct socket_packet_buffer *buffer)
Definition packets.c:598
const char * packet_name(enum packet_type type)
Definition packets_gen.c:49
void post_send_packet_server_join_reply(struct connection *pconn, const struct packet_server_join_reply *packet)
Definition packets.c:640
authentication_type
Definition packets.h:88
@ AUTH_NEWUSER_RETRY
Definition packets.h:92
@ AUTH_NEWUSER_FIRST
Definition packets.h:90
@ AUTH_LOGIN_RETRY
Definition packets.h:91
@ AUTH_LOGIN_FIRST
Definition packets.h:89
int send_packet_data(struct connection *pc, unsigned char *data, int len, enum packet_type packet_type)
Definition packets.c:213
void generic_handle_player_attribute_chunk(struct player *pplayer, const struct packet_player_attribute_chunk *chunk)
Definition packets.c:690
void send_attribute_block(const struct player *pplayer, struct connection *pconn)
Definition packets.c:746
unit_info_use
Definition packets.h:81
@ UNIT_INFO_IDENTITY
Definition packets.h:82
@ UNIT_INFO_CITY_PRESENT
Definition packets.h:84
@ UNIT_INFO_CITY_SUPPORTED
Definition packets.h:83
const struct packet_handlers * packet_handlers_get(const char *capability)
Definition packets.c:840
void packet_handlers_fill_initial(struct packet_handlers *phandlers)
packet_type
@ PACKET_LAST
int len
Definition packhand.c:125
char capability[MAX_LEN_CAPSTR]
Definition connection.h:176
struct socket_packet_buffer * buffer
Definition connection.h:158
int(* force_to_send)(struct connection *pconn, const void *packet, bool force_to_send)
Definition packets.h:101
int(* no_packet)(struct connection *pconn)
Definition packets.h:99
int(* packet)(struct connection *pconn, const void *packet)
Definition packets.h:100
union packet_handlers::@67 send[PACKET_LAST]
void *(* receive[PACKET_LAST])(struct connection *pconn)
Definition packets.h:104