Comment fix: OS is not excluded from permissive.
[freeciv.git] / common / networking / packets.h
blob646e0d15f4bd85d6fcee21345b69b034ca5cc992
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.
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
16 #ifdef __cplusplus
17 extern "C" {
18 #endif /* __cplusplus */
20 struct connection;
21 struct data_in;
23 /* utility */
24 #include "shared.h" /* MAX_LEN_ADDR */
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"
42 /* Used in network protocol. */
43 #define MAX_LEN_MSG 1536
44 #define MAX_LEN_ROUTE 2000 /* MAX_LEN_PACKET / 2 - header */
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 */
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).
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.
63 * Used in network protocol.
65 #define ATTRIBUTE_CHUNK_SIZE (1400)
67 /* Used in network protocol. */
68 enum report_type {
69 REPORT_WONDERS_OF_THE_WORLD,
70 REPORT_TOP_5_CITIES,
71 REPORT_DEMOGRAPHIC,
72 REPORT_ACHIEVEMENTS
75 /* Used in network protocol. */
76 enum unit_info_use {
77 UNIT_INFO_IDENTITY,
78 UNIT_INFO_CITY_SUPPORTED,
79 UNIT_INFO_CITY_PRESENT
82 /* Used in network protocol. */
83 enum authentication_type {
84 AUTH_LOGIN_FIRST, /* request a password for a returning user */
85 AUTH_NEWUSER_FIRST, /* request a password for a new user */
86 AUTH_LOGIN_RETRY, /* inform the client to try a different password */
87 AUTH_NEWUSER_RETRY /* inform the client to try a different [new] password */
90 #include "packets_gen.h"
92 struct packet_handlers {
93 union {
94 int (*no_packet)(struct connection *pconn);
95 int (*packet)(struct connection *pconn, const void *packet);
96 int (*force_to_send)(struct connection *pconn, const void *packet,
97 bool force_to_send);
98 } send[PACKET_LAST];
99 void *(*receive[PACKET_LAST])(struct connection *pconn);
102 void *get_packet_from_connection_raw(struct connection *pc,
103 enum packet_type *ptype);
105 #ifdef FREECIV_JSON_CONNECTION
106 #define get_packet_from_connection(pc, ptype) get_packet_from_connection_json(pc, ptype)
107 #else
108 #define get_packet_from_connection(pc, ptype) get_packet_from_connection_raw(pc, ptype)
109 #endif
111 void remove_packet_from_buffer(struct socket_packet_buffer *buffer);
113 void send_attribute_block(const struct player *pplayer,
114 struct connection *pconn);
115 void generic_handle_player_attribute_chunk(struct player *pplayer,
116 const struct
117 packet_player_attribute_chunk
118 *chunk);
119 void packet_handlers_fill_initial(struct packet_handlers *phandlers);
120 void packet_handlers_fill_capability(struct packet_handlers *phandlers,
121 const char *capability);
122 const char *packet_name(enum packet_type type);
123 bool packet_has_game_info_flag(enum packet_type type);
125 void packet_header_init(struct packet_header *packet_header);
126 void post_send_packet_server_join_reply(struct connection *pconn,
127 const struct packet_server_join_reply
128 *packet);
129 void post_receive_packet_server_join_reply(struct connection *pconn,
130 const struct
131 packet_server_join_reply *packet);
133 void pre_send_packet_player_attribute_chunk(struct connection *pc,
134 struct packet_player_attribute_chunk
135 *packet);
137 const struct packet_handlers *packet_handlers_initial(void);
138 const struct packet_handlers *packet_handlers_get(const char *capability);
140 void packets_deinit(void);
142 #ifdef FREECIV_JSON_CONNECTION
143 #include "packets_json.h"
144 #else
146 #define SEND_PACKET_START(packet_type) \
147 unsigned char buffer[MAX_LEN_PACKET]; \
148 struct raw_data_out dout; \
150 dio_output_init(&dout, buffer, sizeof(buffer)); \
151 dio_put_type_raw(&dout, pc->packet_header.length, 0); \
152 dio_put_type_raw(&dout, pc->packet_header.type, packet_type);
154 #define SEND_PACKET_END(packet_type) \
156 size_t size = dio_output_used(&dout); \
158 dio_output_rewind(&dout); \
159 dio_put_type_raw(&dout, pc->packet_header.length, size); \
160 fc_assert(!dout.too_short); \
161 return send_packet_data(pc, buffer, size, packet_type); \
164 #define RECEIVE_PACKET_START(packet_type, result) \
165 struct data_in din; \
166 struct packet_type packet_buf, *result = &packet_buf; \
168 dio_input_init(&din, pc->buffer->data, \
169 data_type_size(pc->packet_header.length)); \
171 int size; \
173 dio_get_type_raw(&din, pc->packet_header.length, &size); \
174 dio_input_init(&din, pc->buffer->data, MIN(size, pc->buffer->ndata)); \
176 dio_input_skip(&din, (data_type_size(pc->packet_header.length) \
177 + data_type_size(pc->packet_header.type)));
179 #define RECEIVE_PACKET_END(result) \
180 if (!packet_check(&din, pc)) { \
181 return NULL; \
183 remove_packet_from_buffer(pc->buffer); \
184 result = fc_malloc(sizeof(*result)); \
185 *result = packet_buf; \
186 return result;
188 #define RECEIVE_PACKET_FIELD_ERROR(field, ...) \
189 log_packet("Error on field '" #field "'" __VA_ARGS__); \
190 return NULL
192 #endif /* FREECIV_JSON_PROTOCOL */
194 int send_packet_data(struct connection *pc, unsigned char *data, int len,
195 enum packet_type packet_type);
196 bool packet_check(struct data_in *din, struct connection *pc);
198 /* Utilities to exchange strings and string vectors. */
199 #define PACKET_STRVEC_SEPARATOR '\3'
200 #define PACKET_STRVEC_COMPUTE(str, strvec) \
201 if (NULL != strvec) { \
202 strvec_to_str(strvec, PACKET_STRVEC_SEPARATOR, str, sizeof(str)); \
203 } else { \
204 str[0] = '\0'; \
206 #define PACKET_STRVEC_EXTRACT(strvec, str) \
207 if ('\0' != str[0]) { \
208 strvec = strvec_new(); \
209 strvec_from_str(strvec, PACKET_STRVEC_SEPARATOR, str); \
210 } else { \
211 strvec = NULL; \
214 #ifdef __cplusplus
216 #endif /* __cplusplus */
218 #endif /* FC__PACKETS_H */