author | celestar |
Mon, 15 Jan 2007 20:14:06 +0000 | |
branch | custombridgeheads |
changeset 5650 | aefc131bf5ce |
parent 5649 | 55c8267c933f |
permissions | -rw-r--r-- |
5642 | 1 |
/* $Id$ */ |
2 |
||
3 |
#ifdef ENABLE_NETWORK |
|
4 |
||
5 |
#include "../../stdafx.h" |
|
6 |
#include "../../debug.h" |
|
7 |
#include "../../macros.h" |
|
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
8 |
#include "../../helpers.hpp" |
5642 | 9 |
#include "packet.h" |
10 |
#include "udp.h" |
|
11 |
||
12 |
/** |
|
13 |
* @file udp.c Basic functions to receive and send UDP packets. |
|
14 |
*/ |
|
15 |
||
16 |
/** |
|
17 |
* Start listening on the given host and port. |
|
18 |
* @param udp the place where the (references to the) UDP are stored |
|
19 |
* @param host the host (ip) to listen on |
|
20 |
* @param port the port to listen on |
|
21 |
* @param broadcast whether to allow broadcast sending/receiving |
|
22 |
* @return true if the listening succeeded |
|
23 |
*/ |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
24 |
bool NetworkUDPListen(SOCKET *udp, const uint32 host, const uint16 port, const bool broadcast) |
5642 | 25 |
{ |
26 |
struct sockaddr_in sin; |
|
27 |
||
28 |
/* Make sure socket is closed */ |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
29 |
NetworkUDPClose(udp); |
5642 | 30 |
|
31 |
*udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
|
32 |
if (*udp == INVALID_SOCKET) { |
|
33 |
DEBUG(net, 0, "[udp] failed to start UDP listener"); |
|
34 |
return false; |
|
35 |
} |
|
36 |
||
37 |
/* set nonblocking mode for socket */ |
|
38 |
{ |
|
39 |
unsigned long blocking = 1; |
|
40 |
#ifndef BEOS_NET_SERVER |
|
41 |
ioctlsocket(*udp, FIONBIO, &blocking); |
|
42 |
#else |
|
43 |
setsockopt(*udp, SOL_SOCKET, SO_NONBLOCK, &blocking, NULL); |
|
44 |
#endif |
|
45 |
} |
|
46 |
||
47 |
sin.sin_family = AF_INET; |
|
48 |
/* Listen on all IPs */ |
|
49 |
sin.sin_addr.s_addr = host; |
|
50 |
sin.sin_port = htons(port); |
|
51 |
||
52 |
if (bind(*udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) { |
|
53 |
DEBUG(net, 0, "[udp] bind failed on %s:%i", inet_ntoa(*(struct in_addr *)&host), port); |
|
54 |
return false; |
|
55 |
} |
|
56 |
||
57 |
if (broadcast) { |
|
58 |
/* Enable broadcast */ |
|
59 |
unsigned long val = 1; |
|
60 |
#ifndef BEOS_NET_SERVER // will work around this, some day; maybe. |
|
61 |
setsockopt(*udp, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val)); |
|
62 |
#endif |
|
63 |
} |
|
64 |
||
65 |
DEBUG(net, 1, "[udp] listening on port %s:%d", inet_ntoa(*(struct in_addr *)&host), port); |
|
66 |
||
67 |
return true; |
|
68 |
} |
|
69 |
||
70 |
/** |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
71 |
* Close the given UDP socket |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
72 |
* @param udp the socket to close |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
73 |
*/ |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
74 |
void NetworkUDPClose(SOCKET *udp) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
75 |
{ |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
76 |
if (*udp == INVALID_SOCKET) return; |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
77 |
|
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
78 |
closesocket(*udp); |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
79 |
*udp = INVALID_SOCKET; |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
80 |
} |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
81 |
|
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
82 |
|
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
83 |
/** |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
84 |
* Send a packet over UDP |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
85 |
* @param udp the socket to send over |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
86 |
* @param p the packet to send |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
87 |
* @param recv the receiver (target) of the packet |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
88 |
*/ |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
89 |
void NetworkSendUDP_Packet(const SOCKET udp, Packet *p, const struct sockaddr_in *recv) |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
90 |
{ |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
91 |
int res; |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
92 |
|
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
93 |
NetworkSend_FillPacketSize(p); |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
94 |
|
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
95 |
/* Send the buffer */ |
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
96 |
res = sendto(udp, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv)); |
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
97 |
|
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
98 |
/* Check for any errors, but ignore it otherwise */ |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
99 |
if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR()); |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
100 |
} |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
101 |
|
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
102 |
/** |
5642 | 103 |
* Receive a packet at UDP level |
104 |
* @param udp the socket to receive the packet on |
|
105 |
*/ |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
106 |
void NetworkUDPReceive(const SOCKET udp) |
5642 | 107 |
{ |
108 |
struct sockaddr_in client_addr; |
|
109 |
socklen_t client_len; |
|
110 |
int nbytes; |
|
111 |
Packet p; |
|
112 |
int packet_len; |
|
113 |
||
114 |
packet_len = sizeof(p.buffer); |
|
115 |
client_len = sizeof(client_addr); |
|
116 |
||
117 |
/* Try to receive anything */ |
|
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
118 |
nbytes = recvfrom(udp, (char*)p.buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len); |
5642 | 119 |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
120 |
/* We got some bytes for the base header of the packet. */ |
5642 | 121 |
if (nbytes > 2) { |
122 |
NetworkRecv_ReadPacketSize(&p); |
|
123 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
124 |
/* If the size does not match the packet must be corrupted. |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
125 |
* Otherwise it will be marked as corrupted later on. */ |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
126 |
if (nbytes != p.size) { |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
127 |
DEBUG(net, 1, "received a packet with mismatching size from %s:%d", |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
128 |
inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
129 |
|
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
130 |
return; |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
131 |
} |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
132 |
|
5642 | 133 |
/* Put the position on the right place */ |
134 |
p.pos = 2; |
|
135 |
p.next = NULL; |
|
136 |
||
137 |
/* Handle the packet */ |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
138 |
NetworkHandleUDPPacket(udp, &p, &client_addr); |
5642 | 139 |
} |
140 |
} |
|
141 |
||
142 |
||
143 |
/** |
|
144 |
* Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet |
|
145 |
* @param p the packet to write the data to |
|
146 |
* @param c the configuration to write the GRF ID and MD5 checksum from |
|
147 |
*/ |
|
148 |
void NetworkSend_GRFIdentifier(Packet *p, const GRFConfig *c) |
|
149 |
{ |
|
150 |
uint j; |
|
151 |
NetworkSend_uint32(p, c->grfid); |
|
152 |
for (j = 0; j < sizeof(c->md5sum); j++) { |
|
153 |
NetworkSend_uint8 (p, c->md5sum[j]); |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
/** |
|
158 |
* Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet |
|
159 |
* @param cs the client state (for closing connect on out-of-bounds reading etc) |
|
160 |
* @param p the packet to read the data from |
|
161 |
* @param c the configuration to write the GRF ID and MD5 checksum to |
|
162 |
*/ |
|
163 |
void NetworkRecv_GRFIdentifier(NetworkClientState *cs, Packet *p, GRFConfig *c) |
|
164 |
{ |
|
165 |
uint j; |
|
166 |
c->grfid = NetworkRecv_uint32(cs, p); |
|
167 |
for (j = 0; j < sizeof(c->md5sum); j++) { |
|
168 |
c->md5sum[j] = NetworkRecv_uint8(cs, p); |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
||
173 |
/** |
|
174 |
* Serializes the NetworkGameInfo struct to the packet |
|
175 |
* @param p the packet to write the data to |
|
176 |
* @param info the NetworkGameInfo struct to serialize |
|
177 |
*/ |
|
178 |
void NetworkSend_NetworkGameInfo(Packet *p, const NetworkGameInfo *info) |
|
179 |
{ |
|
180 |
NetworkSend_uint8 (p, NETWORK_GAME_INFO_VERSION); |
|
181 |
||
182 |
/* |
|
183 |
* Please observe the order. |
|
184 |
* The parts must be read in the same order as they are sent! |
|
185 |
*/ |
|
186 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
187 |
/* Update the documentation in udp.h on changes |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
188 |
* to the NetworkGameInfo wire-protocol! */ |
5642 | 189 |
|
190 |
/* NETWORK_GAME_INFO_VERSION = 4 */ |
|
191 |
{ |
|
192 |
/* Only send the GRF Identification (GRF_ID and MD5 checksum) of |
|
193 |
* the GRFs that are needed, i.e. the ones that the server has |
|
194 |
* selected in the NewGRF GUI and not the ones that are used due |
|
195 |
* to the fact that they are in [newgrf-static] in openttd.cfg */ |
|
196 |
const GRFConfig *c; |
|
197 |
uint count = 0; |
|
198 |
||
199 |
/* Count number of GRFs to send information about */ |
|
200 |
for (c = info->grfconfig; c != NULL; c = c->next) { |
|
201 |
if (!HASBIT(c->flags, GCF_STATIC)) count++; |
|
202 |
} |
|
203 |
NetworkSend_uint8 (p, count); // Send number of GRFs |
|
204 |
||
205 |
/* Send actual GRF Identifications */ |
|
206 |
for (c = info->grfconfig; c != NULL; c = c->next) { |
|
207 |
if (!HASBIT(c->flags, GCF_STATIC)) NetworkSend_GRFIdentifier(p, c); |
|
208 |
} |
|
209 |
} |
|
210 |
||
211 |
/* NETWORK_GAME_INFO_VERSION = 3 */ |
|
212 |
NetworkSend_uint32(p, info->game_date); |
|
213 |
NetworkSend_uint32(p, info->start_date); |
|
214 |
||
215 |
/* NETWORK_GAME_INFO_VERSION = 2 */ |
|
216 |
NetworkSend_uint8 (p, info->companies_max); |
|
217 |
NetworkSend_uint8 (p, info->companies_on); |
|
218 |
NetworkSend_uint8 (p, info->spectators_max); |
|
219 |
||
220 |
/* NETWORK_GAME_INFO_VERSION = 1 */ |
|
221 |
NetworkSend_string(p, info->server_name); |
|
222 |
NetworkSend_string(p, info->server_revision); |
|
223 |
NetworkSend_uint8 (p, info->server_lang); |
|
224 |
NetworkSend_uint8 (p, info->use_password); |
|
225 |
NetworkSend_uint8 (p, info->clients_max); |
|
226 |
NetworkSend_uint8 (p, info->clients_on); |
|
227 |
NetworkSend_uint8 (p, info->spectators_on); |
|
228 |
NetworkSend_string(p, info->map_name); |
|
229 |
NetworkSend_uint16(p, info->map_width); |
|
230 |
NetworkSend_uint16(p, info->map_height); |
|
231 |
NetworkSend_uint8 (p, info->map_set); |
|
232 |
NetworkSend_uint8 (p, info->dedicated); |
|
233 |
} |
|
234 |
||
235 |
/** |
|
236 |
* Deserializes the NetworkGameInfo struct from the packet |
|
237 |
* @param cs the client state (for closing connect on out-of-bounds reading etc) |
|
238 |
* @param p the packet to read the data from |
|
239 |
* @param info the NetworkGameInfo to deserialize into |
|
240 |
*/ |
|
241 |
void NetworkRecv_NetworkGameInfo(NetworkClientState *cs, Packet *p, NetworkGameInfo *info) |
|
242 |
{ |
|
243 |
info->game_info_version = NetworkRecv_uint8(cs, p); |
|
244 |
||
245 |
/* |
|
246 |
* Please observe the order. |
|
247 |
* The parts must be read in the same order as they are sent! |
|
248 |
*/ |
|
249 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
250 |
/* Update the documentation in udp.h on changes |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
251 |
* to the NetworkGameInfo wire-protocol! */ |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
252 |
|
5642 | 253 |
switch (info->game_info_version) { |
254 |
case 4: { |
|
255 |
GRFConfig *c, **dst = &info->grfconfig; |
|
256 |
uint i; |
|
257 |
uint num_grfs = NetworkRecv_uint8(cs, p); |
|
258 |
||
259 |
for (i = 0; i < num_grfs; i++) { |
|
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
260 |
CallocT(&c, 1); |
5642 | 261 |
NetworkRecv_GRFIdentifier(cs, p, c); |
262 |
HandleIncomingNetworkGameInfoGRFConfig(c); |
|
263 |
||
264 |
/* Append GRFConfig to the list */ |
|
265 |
*dst = c; |
|
266 |
dst = &c->next; |
|
267 |
} |
|
268 |
} /* Fallthrough */ |
|
269 |
case 3: |
|
270 |
info->game_date = NetworkRecv_uint32(cs, p); |
|
271 |
info->start_date = NetworkRecv_uint32(cs, p); |
|
272 |
/* Fallthrough */ |
|
273 |
case 2: |
|
274 |
info->companies_max = NetworkRecv_uint8 (cs, p); |
|
275 |
info->companies_on = NetworkRecv_uint8 (cs, p); |
|
276 |
info->spectators_max = NetworkRecv_uint8 (cs, p); |
|
277 |
/* Fallthrough */ |
|
278 |
case 1: |
|
279 |
NetworkRecv_string(cs, p, info->server_name, sizeof(info->server_name)); |
|
280 |
NetworkRecv_string(cs, p, info->server_revision, sizeof(info->server_revision)); |
|
281 |
info->server_lang = NetworkRecv_uint8 (cs, p); |
|
282 |
info->use_password = NetworkRecv_uint8 (cs, p); |
|
283 |
info->clients_max = NetworkRecv_uint8 (cs, p); |
|
284 |
info->clients_on = NetworkRecv_uint8 (cs, p); |
|
285 |
info->spectators_on = NetworkRecv_uint8 (cs, p); |
|
286 |
if (info->game_info_version < 3) { // 16 bits dates got scrapped and are read earlier |
|
287 |
info->game_date = NetworkRecv_uint16(cs, p) + DAYS_TILL_ORIGINAL_BASE_YEAR; |
|
288 |
info->start_date = NetworkRecv_uint16(cs, p) + DAYS_TILL_ORIGINAL_BASE_YEAR; |
|
289 |
} |
|
290 |
NetworkRecv_string(cs, p, info->map_name, sizeof(info->map_name)); |
|
291 |
info->map_width = NetworkRecv_uint16(cs, p); |
|
292 |
info->map_height = NetworkRecv_uint16(cs, p); |
|
293 |
info->map_set = NetworkRecv_uint8 (cs, p); |
|
5650
aefc131bf5ce
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents:
5649
diff
changeset
|
294 |
info->dedicated = (NetworkRecv_uint8 (cs, p) != 0); |
5642 | 295 |
} |
296 |
} |
|
297 |
||
298 |
#endif /* ENABLE_NETWORK */ |