author | celestar |
Thu, 11 Jan 2007 13:16:26 +0000 | |
branch | custombridgeheads |
changeset 5648 | 1608018c5ff2 |
parent 5643 | 3778051e8095 |
permissions | -rw-r--r-- |
5642 | 1 |
/* $Id$ */ |
2 |
||
3 |
#include "../stdafx.h" |
|
4 |
#include "network_data.h" |
|
5 |
||
6 |
#if defined(WITH_REV) |
|
7 |
extern const char _openttd_revision[]; |
|
8 |
#elif defined(WITH_REV_HACK) |
|
9 |
#define WITH_REV |
|
10 |
const char _openttd_revision[] = WITH_REV_HACK; |
|
11 |
#else |
|
12 |
const char _openttd_revision[] = NOREV_STRING; |
|
13 |
#endif |
|
14 |
||
15 |
||
16 |
#ifdef ENABLE_NETWORK |
|
17 |
||
18 |
#include "../openttd.h" |
|
19 |
#include "../debug.h" |
|
20 |
#include "../functions.h" |
|
21 |
#include "../string.h" |
|
22 |
#include "../strings.h" |
|
23 |
#include "../map.h" |
|
24 |
#include "../command.h" |
|
25 |
#include "../variables.h" |
|
26 |
#include "../date.h" |
|
27 |
#include "../newgrf_config.h" |
|
5643
3778051e8095
(svn r7790) [cbh] -Sync: execute the same script for /branches/custombridgeheads as was done for /trunk in r7759.
rubidium
parents:
5642
diff
changeset
|
28 |
#include "table/strings.h" |
5642 | 29 |
#include "network_client.h" |
30 |
#include "network_server.h" |
|
31 |
#include "network_udp.h" |
|
32 |
#include "network_gamelist.h" |
|
33 |
#include "core/udp.h" |
|
34 |
#include "core/tcp.h" |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
35 |
#include "core/core.h" |
5642 | 36 |
#include "network_gui.h" |
37 |
#include "../console.h" /* IConsoleCmdExec */ |
|
38 |
#include <stdarg.h> /* va_list */ |
|
39 |
#include "../md5.h" |
|
40 |
||
41 |
// The listen socket for the server |
|
42 |
static SOCKET _listensocket; |
|
43 |
||
44 |
// The amount of clients connected |
|
45 |
static byte _network_clients_connected = 0; |
|
46 |
// The index counter for new clients (is never decreased) |
|
47 |
static uint16 _network_client_index = NETWORK_SERVER_INDEX + 1; |
|
48 |
||
49 |
/* Some externs / forwards */ |
|
50 |
extern void StateGameLoop(void); |
|
51 |
||
52 |
// Function that looks up the CI for a given client-index |
|
53 |
NetworkClientInfo *NetworkFindClientInfoFromIndex(uint16 client_index) |
|
54 |
{ |
|
55 |
NetworkClientInfo *ci; |
|
56 |
||
57 |
for (ci = _network_client_info; ci != endof(_network_client_info); ci++) { |
|
58 |
if (ci->client_index == client_index) return ci; |
|
59 |
} |
|
60 |
||
61 |
return NULL; |
|
62 |
} |
|
63 |
||
64 |
/** Return the CI for a given IP |
|
65 |
* @param ip IP of the client we are looking for. This must be in string-format |
|
66 |
* @return return a pointer to the corresponding NetworkClientInfo struct or NULL on failure */ |
|
67 |
NetworkClientInfo *NetworkFindClientInfoFromIP(const char *ip) |
|
68 |
{ |
|
69 |
NetworkClientInfo *ci; |
|
70 |
uint32 ip_number = inet_addr(ip); |
|
71 |
||
72 |
for (ci = _network_client_info; ci != endof(_network_client_info); ci++) { |
|
73 |
if (ci->client_ip == ip_number) return ci; |
|
74 |
} |
|
75 |
||
76 |
return NULL; |
|
77 |
} |
|
78 |
||
79 |
// Function that looks up the CS for a given client-index |
|
80 |
NetworkClientState *NetworkFindClientStateFromIndex(uint16 client_index) |
|
81 |
{ |
|
82 |
NetworkClientState *cs; |
|
83 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
84 |
for (cs = _clients; cs != endof(_clients); cs++) { |
5642 | 85 |
if (cs->index == client_index) return cs; |
86 |
} |
|
87 |
||
88 |
return NULL; |
|
89 |
} |
|
90 |
||
91 |
// NetworkGetClientName is a server-safe function to get the name of the client |
|
92 |
// if the user did not send it yet, Client #<no> is used. |
|
93 |
void NetworkGetClientName(char *client_name, size_t size, const NetworkClientState *cs) |
|
94 |
{ |
|
95 |
const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs); |
|
96 |
||
97 |
if (ci->client_name[0] == '\0') { |
|
98 |
snprintf(client_name, size, "Client #%4d", cs->index); |
|
99 |
} else { |
|
100 |
ttd_strlcpy(client_name, ci->client_name, size); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
byte NetworkSpectatorCount(void) |
|
105 |
{ |
|
106 |
const NetworkClientState *cs; |
|
107 |
byte count = 0; |
|
108 |
||
109 |
FOR_ALL_CLIENTS(cs) { |
|
110 |
if (DEREF_CLIENT_INFO(cs)->client_playas == PLAYER_SPECTATOR) count++; |
|
111 |
} |
|
112 |
||
113 |
return count; |
|
114 |
} |
|
115 |
||
116 |
// This puts a text-message to the console, or in the future, the chat-box, |
|
117 |
// (to keep it all a bit more general) |
|
118 |
// If 'self_send' is true, this is the client who is sending the message |
|
119 |
void CDECL NetworkTextMessage(NetworkAction action, uint16 color, bool self_send, const char *name, const char *str, ...) |
|
120 |
{ |
|
121 |
char buf[1024]; |
|
122 |
va_list va; |
|
123 |
const int duration = 10; // Game days the messages stay visible |
|
124 |
char message[1024]; |
|
125 |
char temp[1024]; |
|
126 |
||
127 |
va_start(va, str); |
|
128 |
vsnprintf(buf, lengthof(buf), str, va); |
|
129 |
va_end(va); |
|
130 |
||
131 |
switch (action) { |
|
132 |
case NETWORK_ACTION_SERVER_MESSAGE: |
|
133 |
color = 1; |
|
134 |
snprintf(message, sizeof(message), "*** %s", buf); |
|
135 |
break; |
|
136 |
case NETWORK_ACTION_JOIN: |
|
137 |
color = 1; |
|
138 |
GetString(temp, STR_NETWORK_CLIENT_JOINED, lastof(temp)); |
|
139 |
snprintf(message, sizeof(message), "*** %s %s", name, temp); |
|
140 |
break; |
|
141 |
case NETWORK_ACTION_LEAVE: |
|
142 |
color = 1; |
|
143 |
GetString(temp, STR_NETWORK_ERR_LEFT, lastof(temp)); |
|
144 |
snprintf(message, sizeof(message), "*** %s %s (%s)", name, temp, buf); |
|
145 |
break; |
|
146 |
case NETWORK_ACTION_GIVE_MONEY: |
|
147 |
if (self_send) { |
|
148 |
SetDParamStr(0, name); |
|
149 |
SetDParam(1, atoi(buf)); |
|
150 |
GetString(temp, STR_NETWORK_GAVE_MONEY_AWAY, lastof(temp)); |
|
151 |
snprintf(message, sizeof(message), "*** %s", temp); |
|
152 |
} else { |
|
153 |
SetDParam(0, atoi(buf)); |
|
154 |
GetString(temp, STR_NETWORK_GIVE_MONEY, lastof(temp)); |
|
155 |
snprintf(message, sizeof(message), "*** %s %s", name, temp); |
|
156 |
} |
|
157 |
break; |
|
158 |
case NETWORK_ACTION_NAME_CHANGE: |
|
159 |
GetString(temp, STR_NETWORK_NAME_CHANGE, lastof(temp)); |
|
160 |
snprintf(message, sizeof(message), "*** %s %s %s", name, temp, buf); |
|
161 |
break; |
|
162 |
case NETWORK_ACTION_CHAT_COMPANY: |
|
163 |
SetDParamStr(0, name); |
|
164 |
SetDParamStr(1, buf); |
|
165 |
GetString(temp, self_send ? STR_NETWORK_CHAT_TO_COMPANY : STR_NETWORK_CHAT_COMPANY, lastof(temp)); |
|
166 |
ttd_strlcpy(message, temp, sizeof(message)); |
|
167 |
break; |
|
168 |
case NETWORK_ACTION_CHAT_CLIENT: |
|
169 |
SetDParamStr(0, name); |
|
170 |
SetDParamStr(1, buf); |
|
171 |
GetString(temp, self_send ? STR_NETWORK_CHAT_TO_CLIENT : STR_NETWORK_CHAT_CLIENT, lastof(temp)); |
|
172 |
ttd_strlcpy(message, temp, sizeof(message)); |
|
173 |
break; |
|
174 |
default: |
|
175 |
SetDParamStr(0, name); |
|
176 |
SetDParamStr(1, buf); |
|
177 |
GetString(temp, STR_NETWORK_CHAT_ALL, lastof(temp)); |
|
178 |
ttd_strlcpy(message, temp, sizeof(message)); |
|
179 |
break; |
|
180 |
} |
|
181 |
||
182 |
IConsolePrintF(color, "%s", message); |
|
183 |
AddTextMessage(color, duration, "%s", message); |
|
184 |
} |
|
185 |
||
186 |
// Calculate the frame-lag of a client |
|
187 |
uint NetworkCalculateLag(const NetworkClientState *cs) |
|
188 |
{ |
|
189 |
int lag = cs->last_frame_server - cs->last_frame; |
|
190 |
// This client has missed his ACK packet after 1 DAY_TICKS.. |
|
191 |
// so we increase his lag for every frame that passes! |
|
192 |
// The packet can be out by a max of _net_frame_freq |
|
193 |
if (cs->last_frame_server + DAY_TICKS + _network_frame_freq < _frame_counter) |
|
194 |
lag += _frame_counter - (cs->last_frame_server + DAY_TICKS + _network_frame_freq); |
|
195 |
||
196 |
return lag; |
|
197 |
} |
|
198 |
||
199 |
||
200 |
// There was a non-recoverable error, drop back to the main menu with a nice |
|
201 |
// error |
|
202 |
static void NetworkError(StringID error_string) |
|
203 |
{ |
|
204 |
_switch_mode = SM_MENU; |
|
205 |
_switch_mode_errorstr = error_string; |
|
206 |
} |
|
207 |
||
208 |
static void ClientStartError(const char *error) |
|
209 |
{ |
|
210 |
DEBUG(net, 0, "[client] could not start network: %s",error); |
|
211 |
NetworkError(STR_NETWORK_ERR_CLIENT_START); |
|
212 |
} |
|
213 |
||
214 |
static void ServerStartError(const char *error) |
|
215 |
{ |
|
216 |
DEBUG(net, 0, "[server] could not start network: %s",error); |
|
217 |
NetworkError(STR_NETWORK_ERR_SERVER_START); |
|
218 |
} |
|
219 |
||
220 |
static void NetworkClientError(NetworkRecvStatus res, NetworkClientState* cs) |
|
221 |
{ |
|
222 |
// First, send a CLIENT_ERROR to the server, so he knows we are |
|
223 |
// disconnection (and why!) |
|
224 |
NetworkErrorCode errorno; |
|
225 |
||
226 |
// We just want to close the connection.. |
|
227 |
if (res == NETWORK_RECV_STATUS_CLOSE_QUERY) { |
|
228 |
cs->has_quit = true; |
|
229 |
NetworkCloseClient(cs); |
|
230 |
_networking = false; |
|
231 |
||
232 |
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0); |
|
233 |
return; |
|
234 |
} |
|
235 |
||
236 |
switch (res) { |
|
237 |
case NETWORK_RECV_STATUS_DESYNC: errorno = NETWORK_ERROR_DESYNC; break; |
|
238 |
case NETWORK_RECV_STATUS_SAVEGAME: errorno = NETWORK_ERROR_SAVEGAME_FAILED; break; |
|
239 |
default: errorno = NETWORK_ERROR_GENERAL; break; |
|
240 |
} |
|
241 |
// This means we fucked up and the server closed the connection |
|
242 |
if (res != NETWORK_RECV_STATUS_SERVER_ERROR && res != NETWORK_RECV_STATUS_SERVER_FULL && |
|
243 |
res != NETWORK_RECV_STATUS_SERVER_BANNED) { |
|
244 |
SEND_COMMAND(PACKET_CLIENT_ERROR)(errorno); |
|
245 |
||
246 |
// Dequeue all commands before closing the socket |
|
247 |
NetworkSend_Packets(DEREF_CLIENT(0)); |
|
248 |
} |
|
249 |
||
250 |
_switch_mode = SM_MENU; |
|
251 |
NetworkCloseClient(cs); |
|
252 |
_networking = false; |
|
253 |
} |
|
254 |
||
255 |
/** Retrieve a string representation of an internal error number |
|
256 |
* @param buf buffer where the error message will be stored |
|
257 |
* @param err NetworkErrorCode |
|
258 |
* @return returns a pointer to the error message (buf) */ |
|
259 |
char* GetNetworkErrorMsg(char* buf, NetworkErrorCode err, const char* last) |
|
260 |
{ |
|
261 |
/* List of possible network errors, used by |
|
262 |
* PACKET_SERVER_ERROR and PACKET_CLIENT_ERROR */ |
|
263 |
static const StringID network_error_strings[] = { |
|
264 |
STR_NETWORK_ERR_CLIENT_GENERAL, |
|
265 |
STR_NETWORK_ERR_CLIENT_DESYNC, |
|
266 |
STR_NETWORK_ERR_CLIENT_SAVEGAME, |
|
267 |
STR_NETWORK_ERR_CLIENT_CONNECTION_LOST, |
|
268 |
STR_NETWORK_ERR_CLIENT_PROTOCOL_ERROR, |
|
269 |
STR_NETWORK_ERR_CLIENT_NOT_AUTHORIZED, |
|
270 |
STR_NETWORK_ERR_CLIENT_NOT_EXPECTED, |
|
271 |
STR_NETWORK_ERR_CLIENT_WRONG_REVISION, |
|
272 |
STR_NETWORK_ERR_CLIENT_NAME_IN_USE, |
|
273 |
STR_NETWORK_ERR_CLIENT_WRONG_PASSWORD, |
|
274 |
STR_NETWORK_ERR_CLIENT_PLAYER_MISMATCH, |
|
275 |
STR_NETWORK_ERR_CLIENT_KICKED, |
|
276 |
STR_NETWORK_ERR_CLIENT_CHEATER, |
|
277 |
STR_NETWORK_ERR_CLIENT_SERVER_FULL |
|
278 |
}; |
|
279 |
||
280 |
if (err >= lengthof(network_error_strings)) err = 0; |
|
281 |
||
282 |
return GetString(buf, network_error_strings[err], last); |
|
283 |
} |
|
284 |
||
285 |
/* Count the number of active clients connected */ |
|
286 |
static uint NetworkCountPlayers(void) |
|
287 |
{ |
|
288 |
const NetworkClientState *cs; |
|
289 |
uint count = 0; |
|
290 |
||
291 |
FOR_ALL_CLIENTS(cs) { |
|
292 |
const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs); |
|
293 |
if (IsValidPlayer(ci->client_playas)) count++; |
|
294 |
} |
|
295 |
||
296 |
return count; |
|
297 |
} |
|
298 |
||
299 |
static bool _min_players_paused = false; |
|
300 |
||
301 |
/* Check if the minimum number of players has been reached and pause or unpause the game as appropriate */ |
|
302 |
void CheckMinPlayers(void) |
|
303 |
{ |
|
304 |
if (!_network_dedicated) return; |
|
305 |
||
306 |
if (NetworkCountPlayers() < _network_min_players) { |
|
307 |
if (_min_players_paused) return; |
|
308 |
||
309 |
_min_players_paused = true; |
|
310 |
DoCommandP(0, 1, 0, NULL, CMD_PAUSE); |
|
311 |
NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game paused (not enough players)", NETWORK_SERVER_INDEX); |
|
312 |
} else { |
|
313 |
if (!_min_players_paused) return; |
|
314 |
||
315 |
_min_players_paused = false; |
|
316 |
DoCommandP(0, 0, 0, NULL, CMD_PAUSE); |
|
317 |
NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused (enough players)", NETWORK_SERVER_INDEX); |
|
318 |
} |
|
319 |
} |
|
320 |
||
321 |
// Find all IP-aliases for this host |
|
322 |
static void NetworkFindIPs(void) |
|
323 |
{ |
|
324 |
int i; |
|
325 |
||
326 |
#if defined(BEOS_NET_SERVER) /* doesn't have neither getifaddrs or net/if.h */ |
|
327 |
/* Based on Andrew Bachmann's netstat+.c. Big thanks to him! */ |
|
328 |
int _netstat(int fd, char **output, int verbose); |
|
329 |
||
330 |
int seek_past_header(char **pos, const char *header) { |
|
331 |
char *new_pos = strstr(*pos, header); |
|
332 |
if (new_pos == 0) { |
|
333 |
return B_ERROR; |
|
334 |
} |
|
335 |
*pos += strlen(header) + new_pos - *pos + 1; |
|
336 |
return B_OK; |
|
337 |
} |
|
338 |
||
339 |
int output_length; |
|
340 |
char *output_pointer = NULL; |
|
341 |
char **output; |
|
342 |
int sock = socket(AF_INET, SOCK_DGRAM, 0); |
|
343 |
i = 0; |
|
344 |
||
345 |
// If something fails, make sure the list is empty |
|
346 |
_broadcast_list[0] = 0; |
|
347 |
||
348 |
if (sock < 0) { |
|
349 |
DEBUG(net, 0, "[core] error creating socket"); |
|
350 |
return; |
|
351 |
} |
|
352 |
||
353 |
output_length = _netstat(sock, &output_pointer, 1); |
|
354 |
if (output_length < 0) { |
|
355 |
DEBUG(net, 0, "[core] error running _netstat"); |
|
356 |
return; |
|
357 |
} |
|
358 |
||
359 |
output = &output_pointer; |
|
360 |
if (seek_past_header(output, "IP Interfaces:") == B_OK) { |
|
361 |
for (;;) { |
|
362 |
uint32 n, fields, read; |
|
363 |
uint8 i1, i2, i3, i4, j1, j2, j3, j4; |
|
364 |
struct in_addr inaddr; |
|
365 |
uint32 ip; |
|
366 |
uint32 netmask; |
|
367 |
||
368 |
fields = sscanf(*output, "%u: %hhu.%hhu.%hhu.%hhu, netmask %hhu.%hhu.%hhu.%hhu%n", |
|
369 |
&n, &i1,&i2,&i3,&i4, &j1,&j2,&j3,&j4, &read); |
|
370 |
read += 1; |
|
371 |
if (fields != 9) { |
|
372 |
break; |
|
373 |
} |
|
374 |
||
375 |
ip = (uint32)i1 << 24 | (uint32)i2 << 16 | (uint32)i3 << 8 | (uint32)i4; |
|
376 |
netmask = (uint32)j1 << 24 | (uint32)j2 << 16 | (uint32)j3 << 8 | (uint32)j4; |
|
377 |
||
378 |
if (ip != INADDR_LOOPBACK && ip != INADDR_ANY) { |
|
379 |
inaddr.s_addr = htonl(ip | ~netmask); |
|
380 |
_broadcast_list[i] = inaddr.s_addr; |
|
381 |
i++; |
|
382 |
} |
|
383 |
if (read < 0) { |
|
384 |
break; |
|
385 |
} |
|
386 |
*output += read; |
|
387 |
} |
|
388 |
/* XXX - Using either one of these crashes openttd heavily? - wber */ |
|
389 |
/*free(output_pointer);*/ |
|
390 |
/*free(output);*/ |
|
391 |
closesocket(sock); |
|
392 |
} |
|
393 |
#elif defined(HAVE_GETIFADDRS) |
|
394 |
struct ifaddrs *ifap, *ifa; |
|
395 |
||
396 |
// If something fails, make sure the list is empty |
|
397 |
_broadcast_list[0] = 0; |
|
398 |
||
399 |
if (getifaddrs(&ifap) != 0) |
|
400 |
return; |
|
401 |
||
402 |
i = 0; |
|
403 |
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { |
|
404 |
if (!(ifa->ifa_flags & IFF_BROADCAST)) continue; |
|
405 |
if (ifa->ifa_broadaddr == NULL) continue; |
|
406 |
if (ifa->ifa_broadaddr->sa_family != AF_INET) continue; |
|
407 |
_broadcast_list[i] = ((struct sockaddr_in*)ifa->ifa_broadaddr)->sin_addr.s_addr; |
|
408 |
i++; |
|
409 |
} |
|
410 |
freeifaddrs(ifap); |
|
411 |
||
412 |
#else /* not HAVE_GETIFADDRS */ |
|
413 |
SOCKET sock; |
|
414 |
#ifdef WIN32 |
|
415 |
DWORD len = 0; |
|
416 |
INTERFACE_INFO ifo[MAX_INTERFACES]; |
|
417 |
uint j; |
|
418 |
#else |
|
419 |
char buf[4 * 1024]; // Arbitrary buffer size |
|
420 |
struct ifconf ifconf; |
|
421 |
const char* buf_end; |
|
422 |
const char* p; |
|
423 |
#endif |
|
424 |
||
425 |
// If something fails, make sure the list is empty |
|
426 |
_broadcast_list[0] = 0; |
|
427 |
||
428 |
sock = socket(AF_INET, SOCK_DGRAM, 0); |
|
429 |
if (sock == INVALID_SOCKET) return; |
|
430 |
||
431 |
#ifdef WIN32 |
|
432 |
memset(&ifo[0], 0, sizeof(ifo)); |
|
433 |
if ((WSAIoctl(sock, SIO_GET_INTERFACE_LIST, NULL, 0, &ifo[0], sizeof(ifo), &len, NULL, NULL)) != 0) { |
|
434 |
closesocket(sock); |
|
435 |
return; |
|
436 |
} |
|
437 |
||
438 |
i = 0; |
|
439 |
for (j = 0; j < len / sizeof(*ifo); j++) { |
|
440 |
if (ifo[j].iiFlags & IFF_LOOPBACK) continue; |
|
441 |
if (!(ifo[j].iiFlags & IFF_BROADCAST)) continue; |
|
442 |
/* iiBroadcast is unusable, because it always seems to be set to |
|
443 |
* 255.255.255.255. |
|
444 |
*/ |
|
445 |
_broadcast_list[i++] = |
|
446 |
ifo[j].iiAddress.AddressIn.sin_addr.s_addr | |
|
447 |
~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr; |
|
448 |
} |
|
449 |
#else |
|
450 |
ifconf.ifc_len = sizeof(buf); |
|
451 |
ifconf.ifc_buf = buf; |
|
452 |
if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) { |
|
453 |
closesocket(sock); |
|
454 |
return; |
|
455 |
} |
|
456 |
||
457 |
i = 0; |
|
458 |
buf_end = buf + ifconf.ifc_len; |
|
459 |
for (p = buf; p < buf_end;) { |
|
460 |
const struct ifreq* req = (const struct ifreq*)p; |
|
461 |
||
462 |
if (req->ifr_addr.sa_family == AF_INET) { |
|
463 |
struct ifreq r; |
|
464 |
||
465 |
strncpy(r.ifr_name, req->ifr_name, lengthof(r.ifr_name)); |
|
466 |
if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 && |
|
467 |
r.ifr_flags & IFF_BROADCAST && |
|
468 |
ioctl(sock, SIOCGIFBRDADDR, &r) != -1) { |
|
469 |
_broadcast_list[i++] = |
|
470 |
((struct sockaddr_in*)&r.ifr_broadaddr)->sin_addr.s_addr; |
|
471 |
} |
|
472 |
} |
|
473 |
||
474 |
p += sizeof(struct ifreq); |
|
475 |
#ifdef AF_LINK |
|
476 |
p += req->ifr_addr.sa_len - sizeof(struct sockaddr); |
|
477 |
#endif |
|
478 |
} |
|
479 |
#endif |
|
480 |
||
481 |
closesocket(sock); |
|
482 |
#endif /* not HAVE_GETIFADDRS */ |
|
483 |
||
484 |
_broadcast_list[i] = 0; |
|
485 |
||
486 |
DEBUG(net, 3, "Detected broadcast addresses:"); |
|
487 |
// Now display to the debug all the detected ips |
|
488 |
for (i = 0; _broadcast_list[i] != 0; i++) { |
|
489 |
DEBUG(net, 3, "%d) %s", i, inet_ntoa(*(struct in_addr *)&_broadcast_list[i]));//inet_ntoa(inaddr)); |
|
490 |
} |
|
491 |
} |
|
492 |
||
493 |
// Resolve a hostname to a inet_addr |
|
494 |
unsigned long NetworkResolveHost(const char *hostname) |
|
495 |
{ |
|
496 |
in_addr_t ip; |
|
497 |
||
498 |
// First try: is it an ip address? |
|
499 |
ip = inet_addr(hostname); |
|
500 |
||
501 |
// If not try to resolve the name |
|
502 |
if (ip == INADDR_NONE) { |
|
503 |
struct hostent *he = gethostbyname(hostname); |
|
504 |
if (he == NULL) { |
|
505 |
DEBUG(net, 0, "Cannot resolve '%s'", hostname); |
|
506 |
} else { |
|
507 |
struct in_addr addr = *(struct in_addr *)he->h_addr_list[0]; |
|
508 |
DEBUG(net, 1, "Resolved '%s' to %s", hostname, inet_ntoa(addr)); |
|
509 |
ip = addr.s_addr; |
|
510 |
} |
|
511 |
} |
|
512 |
return ip; |
|
513 |
} |
|
514 |
||
515 |
// Converts a string to ip/port/player |
|
516 |
// Format: IP#player:port |
|
517 |
// |
|
518 |
// connection_string will be re-terminated to seperate out the hostname, and player and port will |
|
519 |
// be set to the player and port strings given by the user, inside the memory area originally |
|
520 |
// occupied by connection_string. |
|
521 |
void ParseConnectionString(const char **player, const char **port, char *connection_string) |
|
522 |
{ |
|
523 |
char *p; |
|
524 |
for (p = connection_string; *p != '\0'; p++) { |
|
525 |
if (*p == '#') { |
|
526 |
*p = '\0'; |
|
527 |
*player = ++p; |
|
528 |
while (IsValidChar(*p, CS_NUMERAL)) p++; |
|
529 |
if (*p == '\0') break; |
|
530 |
} else if (*p == ':') { |
|
531 |
*port = p + 1; |
|
532 |
*p = '\0'; |
|
533 |
} |
|
534 |
} |
|
535 |
} |
|
536 |
||
537 |
// Creates a new client from a socket |
|
538 |
// Used both by the server and the client |
|
539 |
static NetworkClientState *NetworkAllocClient(SOCKET s) |
|
540 |
{ |
|
541 |
NetworkClientState *cs; |
|
542 |
byte client_no = 0; |
|
543 |
||
544 |
if (_network_server) { |
|
545 |
// Can we handle a new client? |
|
546 |
if (_network_clients_connected >= MAX_CLIENTS) return NULL; |
|
547 |
if (_network_game_info.clients_on >= _network_game_info.clients_max) return NULL; |
|
548 |
||
549 |
// Register the login |
|
550 |
client_no = _network_clients_connected++; |
|
551 |
} |
|
552 |
||
553 |
cs = DEREF_CLIENT(client_no); |
|
554 |
memset(cs, 0, sizeof(*cs)); |
|
555 |
cs->socket = s; |
|
556 |
cs->last_frame = 0; |
|
557 |
cs->has_quit = false; |
|
558 |
||
559 |
cs->last_frame = _frame_counter; |
|
560 |
cs->last_frame_server = _frame_counter; |
|
561 |
||
562 |
if (_network_server) { |
|
563 |
NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs); |
|
564 |
memset(ci, 0, sizeof(*ci)); |
|
565 |
||
566 |
cs->index = _network_client_index++; |
|
567 |
ci->client_index = cs->index; |
|
568 |
ci->client_playas = PLAYER_INACTIVE_CLIENT; |
|
569 |
ci->join_date = _date; |
|
570 |
||
571 |
InvalidateWindow(WC_CLIENT_LIST, 0); |
|
572 |
} |
|
573 |
||
574 |
return cs; |
|
575 |
} |
|
576 |
||
577 |
// Close a connection |
|
578 |
void NetworkCloseClient(NetworkClientState *cs) |
|
579 |
{ |
|
580 |
NetworkClientInfo *ci; |
|
581 |
// Socket is already dead |
|
582 |
if (cs->socket == INVALID_SOCKET) { |
|
583 |
cs->has_quit = true; |
|
584 |
return; |
|
585 |
} |
|
586 |
||
587 |
DEBUG(net, 1, "Closed client connection %d", cs->index); |
|
588 |
||
589 |
if (!cs->has_quit && _network_server && cs->status > STATUS_INACTIVE) { |
|
590 |
// We did not receive a leave message from this client... |
|
591 |
NetworkErrorCode errorno = NETWORK_ERROR_CONNECTION_LOST; |
|
592 |
char str[100]; |
|
593 |
char client_name[NETWORK_CLIENT_NAME_LENGTH]; |
|
594 |
NetworkClientState *new_cs; |
|
595 |
||
596 |
NetworkGetClientName(client_name, sizeof(client_name), cs); |
|
597 |
||
598 |
GetNetworkErrorMsg(str, errorno, lastof(str)); |
|
599 |
||
600 |
NetworkTextMessage(NETWORK_ACTION_LEAVE, 1, false, client_name, "%s", str); |
|
601 |
||
602 |
// Inform other clients of this... strange leaving ;) |
|
603 |
FOR_ALL_CLIENTS(new_cs) { |
|
604 |
if (new_cs->status > STATUS_AUTH && cs != new_cs) { |
|
605 |
SEND_COMMAND(PACKET_SERVER_ERROR_QUIT)(new_cs, cs->index, errorno); |
|
606 |
} |
|
607 |
} |
|
608 |
} |
|
609 |
||
610 |
/* When the client was PRE_ACTIVE, the server was in pause mode, so unpause */ |
|
611 |
if (cs->status == STATUS_PRE_ACTIVE && _network_pause_on_join) { |
|
612 |
DoCommandP(0, 0, 0, NULL, CMD_PAUSE); |
|
613 |
NetworkServer_HandleChat(NETWORK_ACTION_SERVER_MESSAGE, DESTTYPE_BROADCAST, 0, "Game unpaused", NETWORK_SERVER_INDEX); |
|
614 |
} |
|
615 |
||
616 |
closesocket(cs->socket); |
|
617 |
cs->writable = false; |
|
618 |
cs->has_quit = true; |
|
619 |
||
620 |
// Free all pending and partially received packets |
|
621 |
while (cs->packet_queue != NULL) { |
|
622 |
Packet *p = cs->packet_queue->next; |
|
623 |
free(cs->packet_queue); |
|
624 |
cs->packet_queue = p; |
|
625 |
} |
|
626 |
free(cs->packet_recv); |
|
627 |
cs->packet_recv = NULL; |
|
628 |
||
629 |
while (cs->command_queue != NULL) { |
|
630 |
CommandPacket *p = cs->command_queue->next; |
|
631 |
free(cs->command_queue); |
|
632 |
cs->command_queue = p; |
|
633 |
} |
|
634 |
||
635 |
// Close the gap in the client-list |
|
636 |
ci = DEREF_CLIENT_INFO(cs); |
|
637 |
||
638 |
if (_network_server) { |
|
639 |
// We just lost one client :( |
|
640 |
if (cs->status > STATUS_INACTIVE) _network_game_info.clients_on--; |
|
641 |
_network_clients_connected--; |
|
642 |
||
643 |
while ((cs + 1) != DEREF_CLIENT(MAX_CLIENTS) && (cs + 1)->socket != INVALID_SOCKET) { |
|
644 |
*cs = *(cs + 1); |
|
645 |
*ci = *(ci + 1); |
|
646 |
cs++; |
|
647 |
ci++; |
|
648 |
} |
|
649 |
||
650 |
InvalidateWindow(WC_CLIENT_LIST, 0); |
|
651 |
} |
|
652 |
||
653 |
// Reset the status of the last socket |
|
654 |
cs->socket = INVALID_SOCKET; |
|
655 |
cs->status = STATUS_INACTIVE; |
|
656 |
cs->index = NETWORK_EMPTY_INDEX; |
|
657 |
ci->client_index = NETWORK_EMPTY_INDEX; |
|
658 |
||
659 |
CheckMinPlayers(); |
|
660 |
} |
|
661 |
||
662 |
// A client wants to connect to a server |
|
663 |
static bool NetworkConnect(const char *hostname, int port) |
|
664 |
{ |
|
665 |
SOCKET s; |
|
666 |
struct sockaddr_in sin; |
|
667 |
||
668 |
DEBUG(net, 1, "Connecting to %s %d", hostname, port); |
|
669 |
||
670 |
s = socket(AF_INET, SOCK_STREAM, 0); |
|
671 |
if (s == INVALID_SOCKET) { |
|
672 |
ClientStartError("socket() failed"); |
|
673 |
return false; |
|
674 |
} |
|
675 |
||
676 |
if (!SetNoDelay(s)) DEBUG(net, 1, "Setting TCP_NODELAY failed"); |
|
677 |
||
678 |
sin.sin_family = AF_INET; |
|
679 |
sin.sin_addr.s_addr = NetworkResolveHost(hostname); |
|
680 |
sin.sin_port = htons(port); |
|
681 |
_network_last_host_ip = sin.sin_addr.s_addr; |
|
682 |
||
683 |
/* We failed to connect for which reason what so ever */ |
|
684 |
if (connect(s, (struct sockaddr*) &sin, sizeof(sin)) != 0) return false; |
|
685 |
||
686 |
if (!SetNonBlocking(s)) DEBUG(net, 0, "Setting non-blocking mode failed"); // XXX should this be an error? |
|
687 |
||
688 |
// in client mode, only the first client field is used. it's pointing to the server. |
|
689 |
NetworkAllocClient(s); |
|
690 |
||
691 |
_network_join_status = NETWORK_JOIN_STATUS_CONNECTING; |
|
692 |
ShowJoinStatusWindow(); |
|
693 |
||
694 |
return true; |
|
695 |
} |
|
696 |
||
697 |
// For the server, to accept new clients |
|
698 |
static void NetworkAcceptClients(void) |
|
699 |
{ |
|
700 |
struct sockaddr_in sin; |
|
701 |
NetworkClientState *cs; |
|
702 |
uint i; |
|
703 |
bool banned; |
|
704 |
||
705 |
// Should never ever happen.. is it possible?? |
|
706 |
assert(_listensocket != INVALID_SOCKET); |
|
707 |
||
708 |
for (;;) { |
|
709 |
socklen_t sin_len = sizeof(sin); |
|
710 |
SOCKET s = accept(_listensocket, (struct sockaddr*)&sin, &sin_len); |
|
711 |
if (s == INVALID_SOCKET) return; |
|
712 |
||
713 |
SetNonBlocking(s); // XXX error handling? |
|
714 |
||
715 |
DEBUG(net, 1, "Client connected from %s on frame %d", inet_ntoa(sin.sin_addr), _frame_counter); |
|
716 |
||
717 |
SetNoDelay(s); // XXX error handling? |
|
718 |
||
719 |
/* Check if the client is banned */ |
|
720 |
banned = false; |
|
721 |
for (i = 0; i < lengthof(_network_ban_list); i++) { |
|
722 |
if (_network_ban_list[i] == NULL) continue; |
|
723 |
||
724 |
if (sin.sin_addr.s_addr == inet_addr(_network_ban_list[i])) { |
|
725 |
Packet *p = NetworkSend_Init(PACKET_SERVER_BANNED); |
|
726 |
||
727 |
DEBUG(net, 1, "Banned ip tried to join (%s), refused", _network_ban_list[i]); |
|
728 |
||
729 |
p->buffer[0] = p->size & 0xFF; |
|
730 |
p->buffer[1] = p->size >> 8; |
|
731 |
||
732 |
send(s, p->buffer, p->size, 0); |
|
733 |
closesocket(s); |
|
734 |
||
735 |
free(p); |
|
736 |
||
737 |
banned = true; |
|
738 |
break; |
|
739 |
} |
|
740 |
} |
|
741 |
/* If this client is banned, continue with next client */ |
|
742 |
if (banned) continue; |
|
743 |
||
744 |
cs = NetworkAllocClient(s); |
|
745 |
if (cs == NULL) { |
|
746 |
// no more clients allowed? |
|
747 |
// Send to the client that we are full! |
|
748 |
Packet *p = NetworkSend_Init(PACKET_SERVER_FULL); |
|
749 |
||
750 |
p->buffer[0] = p->size & 0xFF; |
|
751 |
p->buffer[1] = p->size >> 8; |
|
752 |
||
753 |
send(s, p->buffer, p->size, 0); |
|
754 |
closesocket(s); |
|
755 |
||
756 |
free(p); |
|
757 |
||
758 |
continue; |
|
759 |
} |
|
760 |
||
761 |
// a new client has connected. We set him at inactive for now |
|
762 |
// maybe he is only requesting server-info. Till he has sent a PACKET_CLIENT_MAP_OK |
|
763 |
// the client stays inactive |
|
764 |
cs->status = STATUS_INACTIVE; |
|
765 |
||
766 |
DEREF_CLIENT_INFO(cs)->client_ip = sin.sin_addr.s_addr; // Save the IP of the client |
|
767 |
} |
|
768 |
} |
|
769 |
||
770 |
// Set up the listen socket for the server |
|
771 |
static bool NetworkListen(void) |
|
772 |
{ |
|
773 |
SOCKET ls; |
|
774 |
struct sockaddr_in sin; |
|
775 |
||
776 |
DEBUG(net, 1, "Listening on %s:%d", _network_server_bind_ip_host, _network_server_port); |
|
777 |
||
778 |
ls = socket(AF_INET, SOCK_STREAM, 0); |
|
779 |
if (ls == INVALID_SOCKET) { |
|
780 |
ServerStartError("socket() on listen socket failed"); |
|
781 |
return false; |
|
782 |
} |
|
783 |
||
784 |
{ // reuse the socket |
|
785 |
int reuse = 1; |
|
786 |
// The (const char*) cast is needed for windows!! |
|
787 |
if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) == -1) { |
|
788 |
ServerStartError("setsockopt() on listen socket failed"); |
|
789 |
return false; |
|
790 |
} |
|
791 |
} |
|
792 |
||
793 |
if (!SetNonBlocking(ls)) DEBUG(net, 0, "Setting non-blocking mode failed"); // XXX should this be an error? |
|
794 |
||
795 |
sin.sin_family = AF_INET; |
|
796 |
sin.sin_addr.s_addr = _network_server_bind_ip; |
|
797 |
sin.sin_port = htons(_network_server_port); |
|
798 |
||
799 |
if (bind(ls, (struct sockaddr*)&sin, sizeof(sin)) != 0) { |
|
800 |
ServerStartError("bind() failed"); |
|
801 |
return false; |
|
802 |
} |
|
803 |
||
804 |
if (listen(ls, 1) != 0) { |
|
805 |
ServerStartError("listen() failed"); |
|
806 |
return false; |
|
807 |
} |
|
808 |
||
809 |
_listensocket = ls; |
|
810 |
||
811 |
return true; |
|
812 |
} |
|
813 |
||
814 |
// Close all current connections |
|
815 |
static void NetworkClose(void) |
|
816 |
{ |
|
817 |
NetworkClientState *cs; |
|
818 |
||
819 |
FOR_ALL_CLIENTS(cs) { |
|
820 |
if (!_network_server) { |
|
821 |
SEND_COMMAND(PACKET_CLIENT_QUIT)("leaving"); |
|
822 |
NetworkSend_Packets(cs); |
|
823 |
} |
|
824 |
NetworkCloseClient(cs); |
|
825 |
} |
|
826 |
||
827 |
if (_network_server) { |
|
828 |
// We are a server, also close the listensocket |
|
829 |
closesocket(_listensocket); |
|
830 |
_listensocket = INVALID_SOCKET; |
|
831 |
DEBUG(net, 1, "Closed listener"); |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
832 |
NetworkUDPStop(); |
5642 | 833 |
} |
834 |
} |
|
835 |
||
836 |
// Inits the network (cleans sockets and stuff) |
|
837 |
static void NetworkInitialize(void) |
|
838 |
{ |
|
839 |
NetworkClientState *cs; |
|
840 |
||
841 |
_local_command_queue = NULL; |
|
842 |
||
843 |
// Clean all client-sockets |
|
844 |
memset(_clients, 0, sizeof(_clients)); |
|
845 |
for (cs = _clients; cs != &_clients[MAX_CLIENTS]; cs++) { |
|
846 |
cs->socket = INVALID_SOCKET; |
|
847 |
cs->status = STATUS_INACTIVE; |
|
848 |
cs->command_queue = NULL; |
|
849 |
} |
|
850 |
||
851 |
// Clean the client_info memory |
|
852 |
memset(&_network_client_info, 0, sizeof(_network_client_info)); |
|
853 |
memset(&_network_player_info, 0, sizeof(_network_player_info)); |
|
854 |
||
855 |
_sync_frame = 0; |
|
856 |
_network_first_time = true; |
|
857 |
||
858 |
_network_reconnect = 0; |
|
859 |
||
860 |
NetworkUDPInitialize(); |
|
861 |
} |
|
862 |
||
863 |
// Query a server to fetch his game-info |
|
864 |
// If game_info is true, only the gameinfo is fetched, |
|
865 |
// else only the client_info is fetched |
|
866 |
NetworkGameList *NetworkQueryServer(const char* host, unsigned short port, bool game_info) |
|
867 |
{ |
|
868 |
if (!_network_available) return NULL; |
|
869 |
||
870 |
NetworkDisconnect(); |
|
871 |
||
872 |
if (game_info) return NetworkUDPQueryServer(host, port); |
|
873 |
||
874 |
NetworkInitialize(); |
|
875 |
||
876 |
_network_server = false; |
|
877 |
||
878 |
// Try to connect |
|
879 |
_networking = NetworkConnect(host, port); |
|
880 |
||
881 |
// We are connected |
|
882 |
if (_networking) { |
|
883 |
SEND_COMMAND(PACKET_CLIENT_COMPANY_INFO)(); |
|
884 |
} else { // No networking, close everything down again |
|
885 |
NetworkDisconnect(); |
|
886 |
} |
|
887 |
||
888 |
return NULL; |
|
889 |
} |
|
890 |
||
891 |
/* Validates an address entered as a string and adds the server to |
|
892 |
* the list. If you use this function, the games will be marked |
|
893 |
* as manually added. */ |
|
894 |
void NetworkAddServer(const char *b) |
|
895 |
{ |
|
896 |
if (*b != '\0') { |
|
897 |
NetworkGameList *item; |
|
898 |
const char *port = NULL; |
|
899 |
const char *player = NULL; |
|
900 |
char host[NETWORK_HOSTNAME_LENGTH]; |
|
901 |
uint16 rport; |
|
902 |
||
903 |
ttd_strlcpy(host, b, lengthof(host)); |
|
904 |
||
905 |
ttd_strlcpy(_network_default_ip, b, lengthof(_network_default_ip)); |
|
906 |
rport = NETWORK_DEFAULT_PORT; |
|
907 |
||
908 |
ParseConnectionString(&player, &port, host); |
|
909 |
if (port != NULL) rport = atoi(port); |
|
910 |
||
911 |
item = NetworkQueryServer(host, rport, true); |
|
912 |
item->manually = true; |
|
913 |
} |
|
914 |
} |
|
915 |
||
916 |
/* Generates the list of manually added hosts from NetworkGameList and |
|
917 |
* dumps them into the array _network_host_list. This array is needed |
|
918 |
* by the function that generates the config file. */ |
|
919 |
void NetworkRebuildHostList(void) |
|
920 |
{ |
|
921 |
uint i = 0; |
|
922 |
const NetworkGameList *item = _network_game_list; |
|
923 |
while (item != NULL && i != lengthof(_network_host_list)) { |
|
924 |
if (item->manually) { |
|
925 |
free(_network_host_list[i]); |
|
926 |
_network_host_list[i++] = str_fmt("%s:%i", item->info.hostname, item->port); |
|
927 |
} |
|
928 |
item = item->next; |
|
929 |
} |
|
930 |
||
931 |
for (; i < lengthof(_network_host_list); i++) { |
|
932 |
free(_network_host_list[i]); |
|
933 |
_network_host_list[i] = NULL; |
|
934 |
} |
|
935 |
} |
|
936 |
||
937 |
// Used by clients, to connect to a server |
|
938 |
bool NetworkClientConnectGame(const char *host, uint16 port) |
|
939 |
{ |
|
940 |
if (!_network_available) return false; |
|
941 |
||
942 |
if (port == 0) return false; |
|
943 |
||
944 |
ttd_strlcpy(_network_last_host, host, sizeof(_network_last_host)); |
|
945 |
_network_last_port = port; |
|
946 |
||
947 |
NetworkDisconnect(); |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
948 |
NetworkUDPStop(); |
5642 | 949 |
NetworkInitialize(); |
950 |
||
951 |
// Try to connect |
|
952 |
_networking = NetworkConnect(host, port); |
|
953 |
||
954 |
// We are connected |
|
955 |
if (_networking) { |
|
956 |
IConsoleCmdExec("exec scripts/on_client.scr 0"); |
|
957 |
NetworkClient_Connected(); |
|
958 |
} else { |
|
959 |
// Connecting failed |
|
960 |
NetworkError(STR_NETWORK_ERR_NOCONNECTION); |
|
961 |
} |
|
962 |
||
963 |
return _networking; |
|
964 |
} |
|
965 |
||
966 |
static void NetworkInitGameInfo(void) |
|
967 |
{ |
|
968 |
NetworkClientInfo *ci; |
|
969 |
||
970 |
ttd_strlcpy(_network_game_info.server_name, _network_server_name, sizeof(_network_game_info.server_name)); |
|
971 |
ttd_strlcpy(_network_game_info.server_password, _network_server_password, sizeof(_network_server_password)); |
|
972 |
ttd_strlcpy(_network_game_info.rcon_password, _network_rcon_password, sizeof(_network_rcon_password)); |
|
973 |
if (_network_game_info.server_name[0] == '\0') |
|
974 |
snprintf(_network_game_info.server_name, sizeof(_network_game_info.server_name), "Unnamed Server"); |
|
975 |
||
976 |
ttd_strlcpy(_network_game_info.server_revision, _openttd_revision, sizeof(_network_game_info.server_revision)); |
|
977 |
||
978 |
// The server is a client too ;) |
|
979 |
if (_network_dedicated) { |
|
980 |
_network_game_info.clients_on = 0; |
|
981 |
_network_game_info.companies_on = 0; |
|
982 |
_network_game_info.dedicated = true; |
|
983 |
} else { |
|
984 |
_network_game_info.clients_on = 1; |
|
985 |
_network_game_info.companies_on = 1; |
|
986 |
_network_game_info.dedicated = false; |
|
987 |
} |
|
988 |
||
989 |
_network_game_info.spectators_on = 0; |
|
990 |
||
991 |
_network_game_info.game_date = _date; |
|
992 |
_network_game_info.start_date = ConvertYMDToDate(_patches.starting_year, 0, 1); |
|
993 |
_network_game_info.map_width = MapSizeX(); |
|
994 |
_network_game_info.map_height = MapSizeY(); |
|
995 |
_network_game_info.map_set = _opt.landscape; |
|
996 |
||
997 |
_network_game_info.use_password = (_network_server_password[0] != '\0'); |
|
998 |
||
999 |
// We use _network_client_info[MAX_CLIENT_INFO - 1] to store the server-data in it |
|
1000 |
// The index is NETWORK_SERVER_INDEX ( = 1) |
|
1001 |
ci = &_network_client_info[MAX_CLIENT_INFO - 1]; |
|
1002 |
memset(ci, 0, sizeof(*ci)); |
|
1003 |
||
1004 |
ci->client_index = NETWORK_SERVER_INDEX; |
|
1005 |
ci->client_playas = _network_dedicated ? PLAYER_SPECTATOR : _local_player; |
|
1006 |
||
1007 |
ttd_strlcpy(ci->client_name, _network_player_name, sizeof(ci->client_name)); |
|
1008 |
ttd_strlcpy(ci->unique_id, _network_unique_id, sizeof(ci->unique_id)); |
|
1009 |
} |
|
1010 |
||
1011 |
bool NetworkServerStart(void) |
|
1012 |
{ |
|
1013 |
if (!_network_available) return false; |
|
1014 |
||
1015 |
/* Call the pre-scripts */ |
|
1016 |
IConsoleCmdExec("exec scripts/pre_server.scr 0"); |
|
1017 |
if (_network_dedicated) IConsoleCmdExec("exec scripts/pre_dedicated.scr 0"); |
|
1018 |
||
1019 |
NetworkInitialize(); |
|
1020 |
if (!NetworkListen()) return false; |
|
1021 |
||
1022 |
// Try to start UDP-server |
|
1023 |
_network_udp_server = true; |
|
1024 |
_network_udp_server = NetworkUDPListen(&_udp_server_socket, _network_server_bind_ip, _network_server_port, false); |
|
1025 |
||
1026 |
_network_server = true; |
|
1027 |
_networking = true; |
|
1028 |
_frame_counter = 0; |
|
1029 |
_frame_counter_server = 0; |
|
1030 |
_frame_counter_max = 0; |
|
1031 |
_last_sync_frame = 0; |
|
1032 |
_network_own_client_index = NETWORK_SERVER_INDEX; |
|
1033 |
||
1034 |
/* Non-dedicated server will always be player #1 */ |
|
1035 |
if (!_network_dedicated) _network_playas = 0; |
|
1036 |
||
1037 |
_network_clients_connected = 0; |
|
1038 |
||
1039 |
NetworkInitGameInfo(); |
|
1040 |
||
1041 |
// execute server initialization script |
|
1042 |
IConsoleCmdExec("exec scripts/on_server.scr 0"); |
|
1043 |
// if the server is dedicated ... add some other script |
|
1044 |
if (_network_dedicated) IConsoleCmdExec("exec scripts/on_dedicated.scr 0"); |
|
1045 |
||
1046 |
_min_players_paused = false; |
|
1047 |
CheckMinPlayers(); |
|
1048 |
||
1049 |
/* Try to register us to the master server */ |
|
1050 |
_network_last_advertise_frame = 0; |
|
1051 |
_network_need_advertise = true; |
|
1052 |
NetworkUDPAdvertise(); |
|
1053 |
return true; |
|
1054 |
} |
|
1055 |
||
1056 |
// The server is rebooting... |
|
1057 |
// The only difference with NetworkDisconnect, is the packets that is sent |
|
1058 |
void NetworkReboot(void) |
|
1059 |
{ |
|
1060 |
if (_network_server) { |
|
1061 |
NetworkClientState *cs; |
|
1062 |
FOR_ALL_CLIENTS(cs) { |
|
1063 |
SEND_COMMAND(PACKET_SERVER_NEWGAME)(cs); |
|
1064 |
NetworkSend_Packets(cs); |
|
1065 |
} |
|
1066 |
} |
|
1067 |
||
1068 |
NetworkClose(); |
|
1069 |
||
1070 |
// Free all queued commands |
|
1071 |
while (_local_command_queue != NULL) { |
|
1072 |
CommandPacket *p = _local_command_queue; |
|
1073 |
_local_command_queue = _local_command_queue->next; |
|
1074 |
free(p); |
|
1075 |
} |
|
1076 |
||
1077 |
_networking = false; |
|
1078 |
_network_server = false; |
|
1079 |
} |
|
1080 |
||
1081 |
// We want to disconnect from the host/clients |
|
1082 |
void NetworkDisconnect(void) |
|
1083 |
{ |
|
1084 |
if (_network_server) { |
|
1085 |
NetworkClientState *cs; |
|
1086 |
FOR_ALL_CLIENTS(cs) { |
|
1087 |
SEND_COMMAND(PACKET_SERVER_SHUTDOWN)(cs); |
|
1088 |
NetworkSend_Packets(cs); |
|
1089 |
} |
|
1090 |
} |
|
1091 |
||
1092 |
if (_network_advertise) NetworkUDPRemoveAdvertise(); |
|
1093 |
||
1094 |
DeleteWindowById(WC_NETWORK_STATUS_WINDOW, 0); |
|
1095 |
||
1096 |
NetworkClose(); |
|
1097 |
||
1098 |
// Free all queued commands |
|
1099 |
while (_local_command_queue != NULL) { |
|
1100 |
CommandPacket *p = _local_command_queue; |
|
1101 |
_local_command_queue = _local_command_queue->next; |
|
1102 |
free(p); |
|
1103 |
} |
|
1104 |
||
1105 |
_networking = false; |
|
1106 |
_network_server = false; |
|
1107 |
} |
|
1108 |
||
1109 |
// Receives something from the network |
|
1110 |
static bool NetworkReceive(void) |
|
1111 |
{ |
|
1112 |
NetworkClientState *cs; |
|
1113 |
int n; |
|
1114 |
fd_set read_fd, write_fd; |
|
1115 |
struct timeval tv; |
|
1116 |
||
1117 |
FD_ZERO(&read_fd); |
|
1118 |
FD_ZERO(&write_fd); |
|
1119 |
||
1120 |
FOR_ALL_CLIENTS(cs) { |
|
1121 |
FD_SET(cs->socket, &read_fd); |
|
1122 |
FD_SET(cs->socket, &write_fd); |
|
1123 |
} |
|
1124 |
||
1125 |
// take care of listener port |
|
1126 |
if (_network_server) FD_SET(_listensocket, &read_fd); |
|
1127 |
||
1128 |
tv.tv_sec = tv.tv_usec = 0; // don't block at all. |
|
1129 |
#if !defined(__MORPHOS__) && !defined(__AMIGA__) |
|
1130 |
n = select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv); |
|
1131 |
#else |
|
1132 |
n = WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL); |
|
1133 |
#endif |
|
1134 |
if (n == -1 && !_network_server) NetworkError(STR_NETWORK_ERR_LOSTCONNECTION); |
|
1135 |
||
1136 |
// accept clients.. |
|
1137 |
if (_network_server && FD_ISSET(_listensocket, &read_fd)) NetworkAcceptClients(); |
|
1138 |
||
1139 |
// read stuff from clients |
|
1140 |
FOR_ALL_CLIENTS(cs) { |
|
1141 |
cs->writable = !!FD_ISSET(cs->socket, &write_fd); |
|
1142 |
if (FD_ISSET(cs->socket, &read_fd)) { |
|
1143 |
if (_network_server) { |
|
1144 |
NetworkServer_ReadPackets(cs); |
|
1145 |
} else { |
|
1146 |
NetworkRecvStatus res; |
|
1147 |
||
1148 |
// The client already was quiting! |
|
1149 |
if (cs->has_quit) return false; |
|
1150 |
||
1151 |
res = NetworkClient_ReadPackets(cs); |
|
1152 |
if (res != NETWORK_RECV_STATUS_OKAY) { |
|
1153 |
// The client made an error of which we can not recover |
|
1154 |
// close the client and drop back to main menu |
|
1155 |
NetworkClientError(res, cs); |
|
1156 |
return false; |
|
1157 |
} |
|
1158 |
} |
|
1159 |
} |
|
1160 |
} |
|
1161 |
return true; |
|
1162 |
} |
|
1163 |
||
1164 |
// This sends all buffered commands (if possible) |
|
1165 |
static void NetworkSend(void) |
|
1166 |
{ |
|
1167 |
NetworkClientState *cs; |
|
1168 |
FOR_ALL_CLIENTS(cs) { |
|
1169 |
if (cs->writable) { |
|
1170 |
NetworkSend_Packets(cs); |
|
1171 |
||
1172 |
if (cs->status == STATUS_MAP) { |
|
1173 |
// This client is in the middle of a map-send, call the function for that |
|
1174 |
SEND_COMMAND(PACKET_SERVER_MAP)(cs); |
|
1175 |
} |
|
1176 |
} |
|
1177 |
} |
|
1178 |
} |
|
1179 |
||
1180 |
// Handle the local-command-queue |
|
1181 |
static void NetworkHandleLocalQueue(void) |
|
1182 |
{ |
|
1183 |
CommandPacket *cp, **cp_prev; |
|
1184 |
||
1185 |
cp_prev = &_local_command_queue; |
|
1186 |
||
1187 |
while ( (cp = *cp_prev) != NULL) { |
|
1188 |
||
1189 |
// The queue is always in order, which means |
|
1190 |
// that the first element will be executed first. |
|
1191 |
if (_frame_counter < cp->frame) break; |
|
1192 |
||
1193 |
if (_frame_counter > cp->frame) { |
|
1194 |
// If we reach here, it means for whatever reason, we've already executed |
|
1195 |
// past the command we need to execute. |
|
1196 |
DEBUG(net, 0, "Trying to execute a packet in the past!"); |
|
1197 |
assert(0); |
|
1198 |
} |
|
1199 |
||
1200 |
// We can execute this command |
|
1201 |
NetworkExecuteCommand(cp); |
|
1202 |
||
1203 |
*cp_prev = cp->next; |
|
1204 |
free(cp); |
|
1205 |
} |
|
1206 |
||
1207 |
// Just a safety check, to be removed in the future. |
|
1208 |
// Make sure that no older command appears towards the end of the queue |
|
1209 |
// In that case we missed executing it. This will never happen. |
|
1210 |
for (cp = _local_command_queue; cp; cp = cp->next) { |
|
1211 |
assert(_frame_counter < cp->frame); |
|
1212 |
} |
|
1213 |
||
1214 |
} |
|
1215 |
||
1216 |
static bool NetworkDoClientLoop(void) |
|
1217 |
{ |
|
1218 |
_frame_counter++; |
|
1219 |
||
1220 |
NetworkHandleLocalQueue(); |
|
1221 |
||
1222 |
StateGameLoop(); |
|
1223 |
||
1224 |
// Check if we are in sync! |
|
1225 |
if (_sync_frame != 0) { |
|
1226 |
if (_sync_frame == _frame_counter) { |
|
1227 |
#ifdef NETWORK_SEND_DOUBLE_SEED |
|
1228 |
if (_sync_seed_1 != _random_seeds[0][0] || _sync_seed_2 != _random_seeds[0][1]) { |
|
1229 |
#else |
|
1230 |
if (_sync_seed_1 != _random_seeds[0][0]) { |
|
1231 |
#endif |
|
1232 |
NetworkError(STR_NETWORK_ERR_DESYNC); |
|
1233 |
DEBUG(net, 0, "Sync error detected!"); |
|
1234 |
NetworkClientError(NETWORK_RECV_STATUS_DESYNC, DEREF_CLIENT(0)); |
|
1235 |
return false; |
|
1236 |
} |
|
1237 |
||
1238 |
// If this is the first time we have a sync-frame, we |
|
1239 |
// need to let the server know that we are ready and at the same |
|
1240 |
// frame as he is.. so we can start playing! |
|
1241 |
if (_network_first_time) { |
|
1242 |
_network_first_time = false; |
|
1243 |
SEND_COMMAND(PACKET_CLIENT_ACK)(); |
|
1244 |
} |
|
1245 |
||
1246 |
_sync_frame = 0; |
|
1247 |
} else if (_sync_frame < _frame_counter) { |
|
1248 |
DEBUG(net, 1, "Missed frame for sync-test (%d / %d)", _sync_frame, _frame_counter); |
|
1249 |
_sync_frame = 0; |
|
1250 |
} |
|
1251 |
} |
|
1252 |
||
1253 |
return true; |
|
1254 |
} |
|
1255 |
||
1256 |
// We have to do some UDP checking |
|
1257 |
void NetworkUDPGameLoop(void) |
|
1258 |
{ |
|
1259 |
if (_network_udp_server) { |
|
1260 |
NetworkUDPReceive(_udp_server_socket); |
|
1261 |
if (_udp_master_socket != INVALID_SOCKET) { |
|
1262 |
NetworkUDPReceive(_udp_master_socket); |
|
1263 |
} |
|
1264 |
} else if (_udp_client_socket != INVALID_SOCKET) { |
|
1265 |
NetworkUDPReceive(_udp_client_socket); |
|
1266 |
if (_network_udp_broadcast > 0) _network_udp_broadcast--; |
|
1267 |
} |
|
1268 |
} |
|
1269 |
||
1270 |
// The main loop called from ttd.c |
|
1271 |
// Here we also have to do StateGameLoop if needed! |
|
1272 |
void NetworkGameLoop(void) |
|
1273 |
{ |
|
1274 |
if (!_networking) return; |
|
1275 |
||
1276 |
if (!NetworkReceive()) return; |
|
1277 |
||
1278 |
if (_network_server) { |
|
1279 |
bool send_frame = false; |
|
1280 |
||
1281 |
// We first increase the _frame_counter |
|
1282 |
_frame_counter++; |
|
1283 |
// Update max-frame-counter |
|
1284 |
if (_frame_counter > _frame_counter_max) { |
|
1285 |
_frame_counter_max = _frame_counter + _network_frame_freq; |
|
1286 |
send_frame = true; |
|
1287 |
} |
|
1288 |
||
1289 |
NetworkHandleLocalQueue(); |
|
1290 |
||
1291 |
// Then we make the frame |
|
1292 |
StateGameLoop(); |
|
1293 |
||
1294 |
_sync_seed_1 = _random_seeds[0][0]; |
|
1295 |
#ifdef NETWORK_SEND_DOUBLE_SEED |
|
1296 |
_sync_seed_2 = _random_seeds[0][1]; |
|
1297 |
#endif |
|
1298 |
||
1299 |
NetworkServer_Tick(send_frame); |
|
1300 |
} else { |
|
1301 |
// Client |
|
1302 |
||
1303 |
// Make sure we are at the frame were the server is (quick-frames) |
|
1304 |
if (_frame_counter_server > _frame_counter) { |
|
1305 |
while (_frame_counter_server > _frame_counter) { |
|
1306 |
if (!NetworkDoClientLoop()) break; |
|
1307 |
} |
|
1308 |
} else { |
|
1309 |
// Else, keep on going till _frame_counter_max |
|
1310 |
if (_frame_counter_max > _frame_counter) NetworkDoClientLoop(); |
|
1311 |
} |
|
1312 |
} |
|
1313 |
||
1314 |
NetworkSend(); |
|
1315 |
} |
|
1316 |
||
1317 |
static void NetworkGenerateUniqueId(void) |
|
1318 |
{ |
|
1319 |
md5_state_t state; |
|
1320 |
md5_byte_t digest[16]; |
|
1321 |
char hex_output[16*2 + 1]; |
|
1322 |
char coding_string[NETWORK_NAME_LENGTH]; |
|
1323 |
int di; |
|
1324 |
||
1325 |
snprintf(coding_string, sizeof(coding_string), "%d%s", (uint)Random(), "OpenTTD Unique ID"); |
|
1326 |
||
1327 |
/* Generate the MD5 hash */ |
|
1328 |
md5_init(&state); |
|
1329 |
md5_append(&state, (const md5_byte_t*)coding_string, strlen(coding_string)); |
|
1330 |
md5_finish(&state, digest); |
|
1331 |
||
1332 |
for (di = 0; di < 16; ++di) |
|
1333 |
sprintf(hex_output + di * 2, "%02x", digest[di]); |
|
1334 |
||
1335 |
/* _network_unique_id is our id */ |
|
1336 |
snprintf(_network_unique_id, sizeof(_network_unique_id), "%s", hex_output); |
|
1337 |
} |
|
1338 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
1339 |
/** This tries to launch the network for a given OS */ |
5642 | 1340 |
void NetworkStartUp(void) |
1341 |
{ |
|
1342 |
DEBUG(net, 3, "[core] starting network..."); |
|
1343 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
1344 |
/* Network is available */ |
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
1345 |
_network_available = NetworkCoreInitialize();; |
5642 | 1346 |
_network_dedicated = false; |
1347 |
_network_last_advertise_frame = 0; |
|
1348 |
_network_need_advertise = true; |
|
1349 |
_network_advertise_retries = 0; |
|
1350 |
||
1351 |
/* Load the ip from the openttd.cfg */ |
|
1352 |
_network_server_bind_ip = inet_addr(_network_server_bind_ip_host); |
|
1353 |
/* And put the data back in it in case it was an invalid ip */ |
|
1354 |
snprintf(_network_server_bind_ip_host, sizeof(_network_server_bind_ip_host), "%s", inet_ntoa(*(struct in_addr *)&_network_server_bind_ip)); |
|
1355 |
||
1356 |
/* Generate an unique id when there is none yet */ |
|
1357 |
if (_network_unique_id[0] == '\0') NetworkGenerateUniqueId(); |
|
1358 |
||
1359 |
{ |
|
1360 |
byte cl_max = _network_game_info.clients_max; |
|
1361 |
byte cp_max = _network_game_info.companies_max; |
|
1362 |
byte sp_max = _network_game_info.spectators_max; |
|
1363 |
||
1364 |
memset(&_network_game_info, 0, sizeof(_network_game_info)); |
|
1365 |
_network_game_info.clients_max = cl_max; |
|
1366 |
_network_game_info.companies_max = cp_max; |
|
1367 |
_network_game_info.spectators_max = sp_max; |
|
1368 |
} |
|
1369 |
||
1370 |
||
1371 |
NetworkInitialize(); |
|
1372 |
DEBUG(net, 3, "[core] network online, multiplayer available"); |
|
1373 |
NetworkFindIPs(); |
|
1374 |
} |
|
1375 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
1376 |
/** This shuts the network down */ |
5642 | 1377 |
void NetworkShutDown(void) |
1378 |
{ |
|
1379 |
NetworkDisconnect(); |
|
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
1380 |
NetworkUDPStop(); |
5642 | 1381 |
|
1382 |
DEBUG(net, 3, "[core] shutting down network"); |
|
1383 |
||
1384 |
_network_available = false; |
|
1385 |
||
5648
1608018c5ff2
(svn r8059) [cbh] - Sync with r7788:8032 from trunk
celestar
parents:
5643
diff
changeset
|
1386 |
NetworkCoreShutdown(); |
5642 | 1387 |
} |
1388 |
||
1389 |
#endif /* ENABLE_NETWORK */ |