OpenTTD Source  1.11.0-beta2
network_udp.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
15 #include "../stdafx.h"
16 #include "../date_func.h"
17 #include "../map_func.h"
18 #include "../debug.h"
19 #include "network_gamelist.h"
20 #include "network_internal.h"
21 #include "network_udp.h"
22 #include "network.h"
23 #include "../core/endian_func.hpp"
24 #include "../company_base.h"
25 #include "../thread.h"
26 #include "../rev.h"
27 #include "../newgrf_text.h"
28 #include "../strings_func.h"
29 #include "table/strings.h"
30 #include <mutex>
31 
32 #include "core/udp.h"
33 
34 #include "../safeguards.h"
35 
37 static std::mutex _network_udp_mutex;
38 
40 static uint64 _session_key = 0;
41 
42 static const std::chrono::minutes ADVERTISE_NORMAL_INTERVAL(15);
43 static const std::chrono::seconds ADVERTISE_RETRY_INTERVAL(10);
44 static const uint32 ADVERTISE_RETRY_TIMES = 3;
45 
49 
56 static void DoNetworkUDPQueryServer(NetworkAddress &address, bool needs_mutex, bool manually)
57 {
58  /* Clear item in gamelist */
59  NetworkGameList *item = CallocT<NetworkGameList>(1);
61  strecpy(item->info.hostname, address.GetHostname(), lastof(item->info.hostname));
62  item->address = address;
63  item->manually = manually;
65 
66  std::unique_lock<std::mutex> lock(_network_udp_mutex, std::defer_lock);
67  if (needs_mutex) lock.lock();
68  /* Init the packet */
70  if (_udp_client_socket != nullptr) _udp_client_socket->SendPacket(&p, &address);
71 }
72 
78 void NetworkUDPQueryServer(NetworkAddress address, bool manually)
79 {
80  if (address.IsResolved() || !StartNewThread(nullptr, "ottd:udp-query", &DoNetworkUDPQueryServer, std::move(address), true, std::move(manually))) {
81  DoNetworkUDPQueryServer(address, true, manually);
82  }
83 }
84 
86 
89 protected:
90  void Receive_MASTER_ACK_REGISTER(Packet *p, NetworkAddress *client_addr) override;
91  void Receive_MASTER_SESSION_KEY(Packet *p, NetworkAddress *client_addr) override;
92 public:
98  virtual ~MasterNetworkUDPSocketHandler() {}
99 };
100 
102 {
104  DEBUG(net, 2, "[udp] advertising on master server successful (%s)", NetworkAddress::AddressFamilyAsString(client_addr->GetAddress()->ss_family));
105 
106  /* We are advertised, but we don't want to! */
108 }
109 
111 {
112  _session_key = p->Recv_uint64();
113  DEBUG(net, 2, "[udp] received new session key from master server (%s)", NetworkAddress::AddressFamilyAsString(client_addr->GetAddress()->ss_family));
114 }
115 
117 
120 protected:
121  void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr) override;
122  void Receive_CLIENT_DETAIL_INFO(Packet *p, NetworkAddress *client_addr) override;
123  void Receive_CLIENT_GET_NEWGRFS(Packet *p, NetworkAddress *client_addr) override;
124 public:
130  virtual ~ServerNetworkUDPSocketHandler() {}
131 };
132 
134 {
135  /* Just a fail-safe.. should never happen */
136  if (!_network_udp_server) {
137  return;
138  }
139 
140  NetworkGameInfo ngi;
141 
142  /* Update some game_info */
145 
149  ngi.companies_on = (byte)Company::GetNumItems();
151  ngi.spectators_on = NetworkSpectatorCount();
153  ngi.game_date = _date;
154  ngi.map_width = MapSizeX();
155  ngi.map_height = MapSizeY();
158  ngi.grfconfig = _grfconfig;
159 
163 
165  this->SendNetworkGameInfo(&packet, &ngi);
166 
167  /* Let the client know that we are here */
168  this->SendPacket(&packet, client_addr);
169 
170  DEBUG(net, 2, "[udp] queried from %s", client_addr->GetHostname());
171 }
172 
174 {
175  /* Just a fail-safe.. should never happen */
176  if (!_network_udp_server) return;
177 
179 
180  /* Send the amount of active companies */
182  packet.Send_uint8 ((uint8)Company::GetNumItems());
183 
184  /* Fetch the latest version of the stats */
185  NetworkCompanyStats company_stats[MAX_COMPANIES];
186  NetworkPopulateCompanyStats(company_stats);
187 
188  /* The minimum company information "blob" size. */
189  static const uint MIN_CI_SIZE = 54;
190  uint max_cname_length = NETWORK_COMPANY_NAME_LENGTH;
191 
192  if (Company::GetNumItems() * (MIN_CI_SIZE + NETWORK_COMPANY_NAME_LENGTH) >= (uint)SEND_MTU - packet.size) {
193  /* Assume we can at least put the company information in the packets. */
194  assert(Company::GetNumItems() * MIN_CI_SIZE < (uint)SEND_MTU - packet.size);
195 
196  /* At this moment the company names might not fit in the
197  * packet. Check whether that is really the case. */
198 
199  for (;;) {
200  int free = SEND_MTU - packet.size;
201  for (const Company *company : Company::Iterate()) {
202  char company_name[NETWORK_COMPANY_NAME_LENGTH];
203  SetDParam(0, company->index);
204  GetString(company_name, STR_COMPANY_NAME, company_name + max_cname_length - 1);
205  free -= MIN_CI_SIZE;
206  free -= (int)strlen(company_name);
207  }
208  if (free >= 0) break;
209 
210  /* Try again, with slightly shorter strings. */
211  assert(max_cname_length > 0);
212  max_cname_length--;
213  }
214  }
215 
216  /* Go through all the companies */
217  for (const Company *company : Company::Iterate()) {
218  /* Send the information */
219  this->SendCompanyInformation(&packet, company, &company_stats[company->index], max_cname_length);
220  }
221 
222  this->SendPacket(&packet, client_addr);
223 }
224 
239 {
240  uint8 num_grfs;
241  uint i;
242 
243  const GRFConfig *in_reply[NETWORK_MAX_GRF_COUNT];
244  uint8 in_reply_count = 0;
245  size_t packet_len = 0;
246 
247  DEBUG(net, 6, "[udp] newgrf data request from %s", client_addr->GetAddressAsString().c_str());
248 
249  num_grfs = p->Recv_uint8 ();
250  if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
251 
252  for (i = 0; i < num_grfs; i++) {
253  GRFIdentifier c;
254  const GRFConfig *f;
255 
256  this->ReceiveGRFIdentifier(p, &c);
257 
258  /* Find the matching GRF file */
260  if (f == nullptr) continue; // The GRF is unknown to this server
261 
262  /* If the reply might exceed the size of the packet, only reply
263  * the current list and do not send the other data.
264  * The name could be an empty string, if so take the filename. */
265  packet_len += sizeof(c.grfid) + sizeof(c.md5sum) +
266  std::min(strlen(f->GetName()) + 1, (size_t)NETWORK_GRF_NAME_LENGTH);
267  if (packet_len > SEND_MTU - 4) { // 4 is 3 byte header + grf count in reply
268  break;
269  }
270  in_reply[in_reply_count] = f;
271  in_reply_count++;
272  }
273 
274  if (in_reply_count == 0) return;
275 
277  packet.Send_uint8(in_reply_count);
278  for (i = 0; i < in_reply_count; i++) {
279  char name[NETWORK_GRF_NAME_LENGTH];
280 
281  /* The name could be an empty string, if so take the filename */
282  strecpy(name, in_reply[i]->GetName(), lastof(name));
283  this->SendGRFIdentifier(&packet, &in_reply[i]->ident);
284  packet.Send_string(name);
285  }
286 
287  this->SendPacket(&packet, client_addr);
288 }
289 
291 
294 protected:
295  void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr) override;
296  void Receive_MASTER_RESPONSE_LIST(Packet *p, NetworkAddress *client_addr) override;
297  void Receive_SERVER_NEWGRFS(Packet *p, NetworkAddress *client_addr) override;
298  void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) override;
299 public:
300  virtual ~ClientNetworkUDPSocketHandler() {}
301 };
302 
304 {
305  NetworkGameList *item;
306 
307  /* Just a fail-safe.. should never happen */
308  if (_network_udp_server) return;
309 
310  DEBUG(net, 4, "[udp] server response from %s", client_addr->GetAddressAsString().c_str());
311 
312  /* Find next item */
313  item = NetworkGameListAddItem(*client_addr);
314 
316  this->ReceiveNetworkGameInfo(p, &item->info);
317 
318  item->info.compatible = true;
319  {
320  /* Checks whether there needs to be a request for names of GRFs and makes
321  * the request if necessary. GRFs that need to be requested are the GRFs
322  * that do not exist on the clients system and we do not have the name
323  * resolved of, i.e. the name is still UNKNOWN_GRF_NAME_PLACEHOLDER.
324  * The in_request array and in_request_count are used so there is no need
325  * to do a second loop over the GRF list, which can be relatively expensive
326  * due to the string comparisons. */
327  const GRFConfig *in_request[NETWORK_MAX_GRF_COUNT];
328  const GRFConfig *c;
329  uint in_request_count = 0;
330 
331  for (c = item->info.grfconfig; c != nullptr; c = c->next) {
332  if (c->status == GCS_NOT_FOUND) item->info.compatible = false;
333  if (c->status != GCS_NOT_FOUND || strcmp(c->GetName(), UNKNOWN_GRF_NAME_PLACEHOLDER) != 0) continue;
334  in_request[in_request_count] = c;
335  in_request_count++;
336  }
337 
338  if (in_request_count > 0) {
339  /* There are 'unknown' GRFs, now send a request for them */
340  uint i;
342 
343  packet.Send_uint8(in_request_count);
344  for (i = 0; i < in_request_count; i++) {
345  this->SendGRFIdentifier(&packet, &in_request[i]->ident);
346  }
347 
348  this->SendPacket(&packet, &item->address);
349  }
350  }
351 
352  if (item->info.hostname[0] == '\0') {
353  seprintf(item->info.hostname, lastof(item->info.hostname), "%s", client_addr->GetHostname());
354  }
355 
356  if (client_addr->GetAddress()->ss_family == AF_INET6) {
357  strecat(item->info.server_name, " (IPv6)", lastof(item->info.server_name));
358  }
359 
360  /* Check if we are allowed on this server based on the revision-match */
362  item->info.compatible &= item->info.version_compatible; // Already contains match for GRFs
363 
364  item->online = true;
365 
367 }
368 
370 {
371  /* packet begins with the protocol version (uint8)
372  * then an uint16 which indicates how many
373  * ip:port pairs are in this packet, after that
374  * an uint32 (ip) and an uint16 (port) for each pair.
375  */
376 
377  ServerListType type = (ServerListType)(p->Recv_uint8() - 1);
378 
379  if (type < SLT_END) {
380  for (int i = p->Recv_uint16(); i != 0 ; i--) {
381  sockaddr_storage addr_storage;
382  memset(&addr_storage, 0, sizeof(addr_storage));
383 
384  if (type == SLT_IPv4) {
385  addr_storage.ss_family = AF_INET;
386  ((sockaddr_in*)&addr_storage)->sin_addr.s_addr = TO_LE32(p->Recv_uint32());
387  } else {
388  assert(type == SLT_IPv6);
389  addr_storage.ss_family = AF_INET6;
390  byte *addr = (byte*)&((sockaddr_in6*)&addr_storage)->sin6_addr;
391  for (uint i = 0; i < sizeof(in6_addr); i++) *addr++ = p->Recv_uint8();
392  }
393  NetworkAddress addr(addr_storage, type == SLT_IPv4 ? sizeof(sockaddr_in) : sizeof(sockaddr_in6));
394  addr.SetPort(p->Recv_uint16());
395 
396  /* Somehow we reached the end of the packet */
397  if (this->HasClientQuit()) return;
398 
399  DoNetworkUDPQueryServer(addr, false, false);
400  }
401  }
402 }
403 
406 {
407  uint8 num_grfs;
408  uint i;
409 
410  DEBUG(net, 6, "[udp] newgrf data reply from %s", client_addr->GetAddressAsString().c_str());
411 
412  num_grfs = p->Recv_uint8 ();
413  if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
414 
415  for (i = 0; i < num_grfs; i++) {
416  char name[NETWORK_GRF_NAME_LENGTH];
417  GRFIdentifier c;
418 
419  this->ReceiveGRFIdentifier(p, &c);
420  p->Recv_string(name, sizeof(name));
421 
422  /* An empty name is not possible under normal circumstances
423  * and causes problems when showing the NewGRF list. */
424  if (StrEmpty(name)) continue;
425 
426  /* Try to find the GRFTextWrapper for the name of this GRF ID and MD5sum tuple.
427  * If it exists and not resolved yet, then name of the fake GRF is
428  * overwritten with the name from the reply. */
429  GRFTextWrapper unknown_name = FindUnknownGRFName(c.grfid, c.md5sum, false);
430  if (unknown_name && strcmp(GetGRFStringFromGRFText(unknown_name), UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) {
431  AddGRFTextToList(unknown_name, name);
432  }
433  }
434 }
435 
437 {
438  /* Find the matching GRF file */
439  const GRFConfig *f = FindGRFConfig(config->ident.grfid, FGCM_EXACT, config->ident.md5sum);
440  if (f == nullptr) {
441  /* Don't know the GRF, so mark game incompatible and the (possibly)
442  * already resolved name for this GRF (another server has sent the
443  * name of the GRF already */
444  config->name = FindUnknownGRFName(config->ident.grfid, config->ident.md5sum, true);
445  config->status = GCS_NOT_FOUND;
446  } else {
447  config->filename = f->filename;
448  config->name = f->name;
449  config->info = f->info;
450  config->url = f->url;
451  }
452  SetBit(config->flags, GCF_COPY);
453 }
454 
457 {
458  for (NetworkAddress &addr : _broadcast_list) {
460 
461  DEBUG(net, 4, "[udp] broadcasting to %s", addr.GetHostname());
462 
463  socket->SendPacket(&p, &addr, true, true);
464  }
465 }
466 
467 
470 {
473 
474  /* packet only contains protocol version */
477 
478  _udp_client_socket->SendPacket(&p, &out_addr, true);
479 
480  DEBUG(net, 2, "[udp] master server queried at %s", out_addr.GetAddressAsString().c_str());
481 }
482 
485 {
486  /* We are still searching.. */
487  if (_network_udp_broadcast > 0) return;
488 
489  DEBUG(net, 0, "[udp] searching server");
490 
492  _network_udp_broadcast = 300; // Stay searching for 300 ticks
493 }
494 
499 {
500  DEBUG(net, 1, "[udp] removing advertise from master server");
501 
502  /* Find somewhere to send */
504 
505  /* Send the packet */
507  /* Packet is: Version, server_port */
510 
511  std::lock_guard<std::mutex> lock(_network_udp_mutex);
512  if (_udp_master_socket != nullptr) _udp_master_socket->SendPacket(&p, &out_addr, true);
513 }
514 
519 void NetworkUDPRemoveAdvertise(bool blocking)
520 {
521  /* Check if we are advertising */
522  if (!_networking || !_network_server || !_network_udp_server) return;
523 
524  if (blocking || !StartNewThread(nullptr, "ottd:udp-advert", &NetworkUDPRemoveAdvertiseThread)) {
526  }
527 }
528 
533 {
534  /* Find somewhere to send */
536 
537  DEBUG(net, 1, "[udp] advertising to master server");
538 
539  /* Add a bit more messaging when we cannot get a session key */
540  static byte session_key_retries = 0;
541  if (_session_key == 0 && session_key_retries++ == 2) {
542  DEBUG(net, 0, "[udp] advertising to the master server is failing");
543  DEBUG(net, 0, "[udp] we are not receiving the session key from the server");
544  DEBUG(net, 0, "[udp] please allow udp packets from %s to you to be delivered", out_addr.GetAddressAsString(false).c_str());
545  DEBUG(net, 0, "[udp] please allow udp packets from you to %s to be delivered", out_addr.GetAddressAsString(false).c_str());
546  }
547  if (_session_key != 0 && _network_advertise_retries == 0) {
548  DEBUG(net, 0, "[udp] advertising to the master server is failing");
549  DEBUG(net, 0, "[udp] we are not receiving the acknowledgement from the server");
550  DEBUG(net, 0, "[udp] this usually means that the master server cannot reach us");
551  DEBUG(net, 0, "[udp] please allow udp and tcp packets to port %u to be delivered", _settings_client.network.server_port);
552  DEBUG(net, 0, "[udp] please allow udp and tcp packets from port %u to be delivered", _settings_client.network.server_port);
553  }
554 
555  /* Send the packet */
557  /* Packet is: WELCOME_MESSAGE, Version, server_port */
562 
563  std::lock_guard<std::mutex> lock(_network_udp_mutex);
564  if (_udp_master_socket != nullptr) _udp_master_socket->SendPacket(&p, &out_addr, true);
565 }
566 
572 {
573  static std::chrono::steady_clock::time_point _last_advertisement = {};
574 
575  /* Check if we should send an advertise */
577 
579  /* Forced advertisement. */
580  _network_need_advertise = false;
582  } else {
583  /* Only send once every ADVERTISE_NORMAL_INTERVAL ticks */
584  if (_network_advertise_retries == 0) {
585  if (std::chrono::steady_clock::now() <= _last_advertisement + ADVERTISE_NORMAL_INTERVAL) return;
586 
588  } else {
589  /* An actual retry. */
590  if (std::chrono::steady_clock::now() <= _last_advertisement + ADVERTISE_RETRY_INTERVAL) return;
591  }
592  }
593 
595  _last_advertisement = std::chrono::steady_clock::now();
596 
597  if (!StartNewThread(nullptr, "ottd:udp-advert", &NetworkUDPAdvertiseThread)) {
599  }
600 }
601 
604 {
605  /* If not closed, then do it. */
606  if (_udp_server_socket != nullptr) NetworkUDPClose();
607 
608  DEBUG(net, 1, "[udp] initializing listeners");
609  assert(_udp_client_socket == nullptr && _udp_server_socket == nullptr && _udp_master_socket == nullptr);
610 
611  std::lock_guard<std::mutex> lock(_network_udp_mutex);
612 
614 
615  NetworkAddressList server;
618 
619  server.clear();
620  GetBindAddresses(&server, 0);
622 
623  _network_udp_server = false;
625 }
626 
629 {
630  std::lock_guard<std::mutex> lock(_network_udp_mutex);
634  delete _udp_client_socket;
635  delete _udp_server_socket;
636  delete _udp_master_socket;
637  _udp_client_socket = nullptr;
638  _udp_server_socket = nullptr;
639  _udp_master_socket = nullptr;
640 
641  _network_udp_server = false;
643  DEBUG(net, 1, "[udp] closed listeners");
644 }
645 
648 {
649  std::lock_guard<std::mutex> lock(_network_udp_mutex);
650 
651  if (_network_udp_server) {
654  } else {
657  }
658 }
NetworkGameInfo
The game information that is sent from the server to the clients.
Definition: game.h:32
NetworkCompanyStats
Simple calculated statistics of a company.
Definition: network_type.h:57
PACKET_UDP_CLIENT_GET_LIST
@ PACKET_UDP_CLIENT_GET_LIST
Request for serverlist from master server.
Definition: udp.h:27
GRFConfig::info
GRFTextWrapper info
NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
Definition: newgrf_config.h:161
NetworkUDPSocketHandler
Base socket handler for all UDP sockets.
Definition: udp.h:46
Packet::Recv_string
void Recv_string(char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads a string till it finds a '\0' in the stream.
Definition: packet.cpp:286
NetworkAddress::GetAddress
const sockaddr_storage * GetAddress()
Get the address in its internal representation.
Definition: address.cpp:123
NetworkSettings::max_spectators
uint8 max_spectators
maximum amount of spectators
Definition: settings_type.h:270
_network_game_info
NetworkServerGameInfo _network_game_info
Information about our game.
Definition: network.cpp:57
FindGRFConfig
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
Definition: newgrf_config.cpp:755
PACKET_UDP_CLIENT_GET_NEWGRFS
@ PACKET_UDP_CLIENT_GET_NEWGRFS
Requests the name for a list of GRFs (GRF_ID and MD5)
Definition: udp.h:30
NetworkGameInfo::companies_max
byte companies_max
Max companies allowed on server.
Definition: game.h:49
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:401
GetBindAddresses
void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
Get the addresses to bind to.
Definition: network.cpp:623
GameCreationSettings::landscape
byte landscape
the landscape we're currently in
Definition: settings_type.h:295
NetworkSettings::max_clients
uint8 max_clients
maximum amount of clients
Definition: settings_type.h:269
ServerListType
ServerListType
The types of server lists we can get.
Definition: udp.h:37
NetworkSettings::server_port
uint16 server_port
port the server listens on
Definition: settings_type.h:252
NetworkSettings::server_lang
uint8 server_lang
language of the server
Definition: settings_type.h:273
lock
std::mutex lock
synchronization for playback status fields
Definition: win32_m.cpp:34
_network_server
bool _network_server
network-server is active
Definition: network.cpp:53
NetworkGameListAddItem
NetworkGameList * NetworkGameListAddItem(NetworkAddress address)
Add a new item to the linked gamelist.
Definition: network_gamelist.cpp:71
_network_udp_broadcast
uint16 _network_udp_broadcast
Timeout for the UDP broadcasts.
Definition: network.cpp:78
NetworkAddress::SetPort
void SetPort(uint16 port)
Set the port.
Definition: address.cpp:54
PACKET_UDP_CLIENT_FIND_SERVER
@ PACKET_UDP_CLIENT_FIND_SERVER
Queries a game server for game information.
Definition: udp.h:21
NetworkPopulateCompanyStats
void NetworkPopulateCompanyStats(NetworkCompanyStats *stats)
Populate the company stats.
Definition: network_server.cpp:1565
_udp_master_socket
NetworkUDPSocketHandler * _udp_master_socket
udp master socket
Definition: network_udp.cpp:48
_network_udp_mutex
static std::mutex _network_udp_mutex
Mutex for all out threaded udp resolution and such.
Definition: network_udp.cpp:37
NetworkUDPAdvertiseThread
static void NetworkUDPAdvertiseThread()
Thread entry point for advertising.
Definition: network_udp.cpp:532
PACKET_UDP_SERVER_UNREGISTER
@ PACKET_UDP_SERVER_UNREGISTER
Request to be removed from the server-list.
Definition: udp.h:29
NetworkServerGameInfo::map_name
char map_name[NETWORK_NAME_LENGTH]
Map which is played ["random" for a randomized map].
Definition: game.h:25
PACKET_UDP_SERVER_REGISTER
@ PACKET_UDP_SERVER_REGISTER
Packet to register itself to the master server.
Definition: udp.h:25
NetworkGameInfo::use_password
bool use_password
Is this server passworded?
Definition: game.h:44
NetworkUDPBroadCast
static void NetworkUDPBroadCast(NetworkUDPSocketHandler *socket)
Broadcast to all ips.
Definition: network_udp.cpp:456
NetworkUDPSocketHandler::Close
void Close() override
Close the given UDP socket.
Definition: udp.cpp:58
MasterNetworkUDPSocketHandler::Receive_MASTER_SESSION_KEY
void Receive_MASTER_SESSION_KEY(Packet *p, NetworkAddress *client_addr) override
The master server sends us a session key.
Definition: network_udp.cpp:110
NetworkGameInfo::hostname
char hostname[NETWORK_HOSTNAME_LENGTH]
Hostname of the server (if any)
Definition: game.h:39
GCS_NOT_FOUND
@ GCS_NOT_FOUND
GRF file was not found in the local cache.
Definition: newgrf_config.h:37
GRFConfig::ident
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
Definition: newgrf_config.h:157
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
NetworkUDPAdvertise
void NetworkUDPAdvertise()
Register us to the master server This function checks if it needs to send an advertise.
Definition: network_udp.cpp:571
GCF_COPY
@ GCF_COPY
The data is copied from a grf in _all_grfs.
Definition: newgrf_config.h:27
UpdateNetworkGameWindow
void UpdateNetworkGameWindow()
Update the network new window because a new server is found on the network.
Definition: network_gui.cpp:77
NetworkUDPSearchGame
void NetworkUDPSearchGame()
Find all servers.
Definition: network_udp.cpp:484
GRFConfig::status
GRFStatus status
NOSAVE: GRFStatus, enum.
Definition: newgrf_config.h:168
_udp_server_socket
NetworkUDPSocketHandler * _udp_server_socket
udp server socket
Definition: network_udp.cpp:47
ADVERTISE_RETRY_INTERVAL
static const std::chrono::seconds ADVERTISE_RETRY_INTERVAL(10)
re-advertise when no response after this amount of time.
NetworkGameInfo::map_width
uint16 map_width
Map width.
Definition: game.h:36
SLT_IPv6
@ SLT_IPv6
Get the IPv6 addresses.
Definition: udp.h:39
NetworkGameList::address
NetworkAddress address
The connection info of the game server.
Definition: network_gamelist.h:19
Packet::Send_uint8
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:96
GRFIdentifier::md5sum
uint8 md5sum[16]
MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF)
Definition: newgrf_config.h:85
NetworkGameInfo::version_compatible
bool version_compatible
Can we connect to this server or not? (based on server_revision)
Definition: game.h:42
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:199
GRFIdentifier::grfid
uint32 grfid
GRF ID (defined by Action 0x08)
Definition: newgrf_config.h:84
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:550
ADVERTISE_RETRY_TIMES
static const uint32 ADVERTISE_RETRY_TIMES
give up re-advertising after this much failed retries
Definition: network_udp.cpp:44
MapSizeX
static uint MapSizeX()
Get the size of the map along the X.
Definition: map_func.h:72
NetworkUDPQueryServer
void NetworkUDPQueryServer(NetworkAddress address, bool manually)
Query a specific server.
Definition: network_udp.cpp:78
NETWORK_MAX_GRF_COUNT
static const uint NETWORK_MAX_GRF_COUNT
Maximum number of GRFs that can be sent.
Definition: config.h:58
GRFIdentifier
Basic data to distinguish a GRF.
Definition: newgrf_config.h:83
NetworkGameInfo::start_date
Date start_date
When the game started.
Definition: game.h:34
ClientNetworkUDPSocketHandler::Receive_SERVER_NEWGRFS
void Receive_SERVER_NEWGRFS(Packet *p, NetworkAddress *client_addr) override
The return of the client's request of the names of some NewGRFs.
Definition: network_udp.cpp:405
NetworkGameInfo::server_lang
byte server_lang
Language of the server (we should make a nice table for this)
Definition: game.h:46
Packet::Recv_uint32
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:246
NetworkSettings::server_password
char server_password[NETWORK_PASSWORD_LENGTH]
password for joining this server
Definition: settings_type.h:256
NetworkUDPQueryMasterServer
void NetworkUDPQueryMasterServer()
Request the the server-list from the master server.
Definition: network_udp.cpp:469
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:152
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:20
MasterNetworkUDPSocketHandler::Receive_MASTER_ACK_REGISTER
void Receive_MASTER_ACK_REGISTER(Packet *p, NetworkAddress *client_addr) override
The master server acknowledges the registration.
Definition: network_udp.cpp:101
NetworkGameInfo::companies_on
byte companies_on
How many started companies do we have.
Definition: game.h:48
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
ClientNetworkUDPSocketHandler::HandleIncomingNetworkGameInfoGRFConfig
void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) override
Function that is called for every GRFConfig that is read when receiving a NetworkGameInfo.
Definition: network_udp.cpp:436
Packet::Send_string
void Send_string(const char *data)
Sends a string over the network.
Definition: packet.cpp:148
NetworkAddress::GetHostname
const char * GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
Definition: address.cpp:22
GRFConfig::flags
uint8 flags
NOSAVE: GCF_Flags, bitset.
Definition: newgrf_config.h:167
ConvertYMDToDate
Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
Definition: date.cpp:149
NetworkSocketHandler::SendGRFIdentifier
void SendGRFIdentifier(Packet *p, const GRFIdentifier *grf)
Serializes the GRFIdentifier (GRF ID and MD5 checksum) to the packet.
Definition: core.cpp:57
Packet::size
PacketSize size
The size of the whole packet for received packets.
Definition: packet.h:48
GetGRFStringFromGRFText
const char * GetGRFStringFromGRFText(const GRFTextList &text_list)
Get a C-string from a GRFText-list.
Definition: newgrf_text.cpp:621
UNKNOWN_GRF_NAME_PLACEHOLDER
#define UNKNOWN_GRF_NAME_PLACEHOLDER
For communication about GRFs over the network.
Definition: newgrf_config.h:232
SEND_MTU
static const uint16 SEND_MTU
Number of bytes we can pack in a single packet.
Definition: config.h:33
NetworkGameList::manually
bool manually
True if the server was added manually.
Definition: network_gamelist.h:21
StartNewThread
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:48
IsNetworkCompatibleVersion
bool IsNetworkCompatibleVersion(const char *other)
Checks whether the given version string is compatible with our version.
Definition: network.cpp:1147
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:80
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
Packet::Send_uint16
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:106
_udp_client_socket
NetworkUDPSocketHandler * _udp_client_socket
udp client socket
Definition: network_udp.cpp:46
NetworkUDPRemoveAdvertise
void NetworkUDPRemoveAdvertise(bool blocking)
Remove our advertise from the master-server.
Definition: network_udp.cpp:519
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
PACKET_UDP_SERVER_DETAIL_INFO
@ PACKET_UDP_SERVER_DETAIL_INFO
Reply of the game server about details of the game, such as companies.
Definition: udp.h:24
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:52
NetworkUDPSocketHandler::SendNetworkGameInfo
void SendNetworkGameInfo(Packet *p, const NetworkGameInfo *info)
Serializes the NetworkGameInfo struct to the packet.
Definition: udp.cpp:158
MapSizeY
static uint MapSizeY()
Get the size of the map along the Y.
Definition: map_func.h:82
NetworkSettings::server_name
char server_name[NETWORK_NAME_LENGTH]
name of the server
Definition: settings_type.h:255
NetworkUDPSocketHandler::ReceiveNetworkGameInfo
void ReceiveNetworkGameInfo(Packet *p, NetworkGameInfo *info)
Deserializes the NetworkGameInfo struct from the packet.
Definition: udp.cpp:220
_network_dedicated
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:55
SLT_END
@ SLT_END
End of 'arrays' marker.
Definition: udp.h:42
SLT_IPv4
@ SLT_IPv4
Get the IPv4 addresses.
Definition: udp.h:38
Packet
Internal entity of a packet.
Definition: packet.h:40
NETWORK_COMPANY_INFO_VERSION
static const byte NETWORK_COMPANY_INFO_VERSION
What version of company info is this?
Definition: config.h:37
NetworkGameInfo::server_name
char server_name[NETWORK_NAME_LENGTH]
Server name.
Definition: game.h:38
ClientNetworkUDPSocketHandler
‍*** Communication with servers (we are client) ***‍/
Definition: network_udp.cpp:293
PACKET_UDP_SERVER_NEWGRFS
@ PACKET_UDP_SERVER_NEWGRFS
Sends the list of NewGRF's requested.
Definition: udp.h:31
PACKET_UDP_SERVER_RESPONSE
@ PACKET_UDP_SERVER_RESPONSE
Reply of the game server with game information.
Definition: udp.h:22
ServerNetworkUDPSocketHandler::Receive_CLIENT_FIND_SERVER
void Receive_CLIENT_FIND_SERVER(Packet *p, NetworkAddress *client_addr) override
Queries to the server for information about the game.
Definition: network_udp.cpp:133
NetworkSettings::server_advertise
bool server_advertise
advertise the server to the masterserver
Definition: settings_type.h:259
NetworkBackgroundUDPLoop
void NetworkBackgroundUDPLoop()
Receive the UDP packets.
Definition: network_udp.cpp:647
NETWORK_MASTER_SERVER_PORT
static const uint16 NETWORK_MASTER_SERVER_PORT
The default port of the master server (UDP)
Definition: config.h:26
ServerNetworkUDPSocketHandler
‍*** Communication with clients (we are server) ***‍/
Definition: network_udp.cpp:119
NETWORK_MASTER_SERVER_VERSION
static const byte NETWORK_MASTER_SERVER_VERSION
What version of master-server-protocol do we use?
Definition: config.h:38
NetworkSocketHandler::HasClientQuit
bool HasClientQuit() const
Whether the current client connected to the socket has quit.
Definition: core.h:67
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:29
Pool::PoolItem<&_company_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:378
DoNetworkUDPQueryServer
static void DoNetworkUDPQueryServer(NetworkAddress &address, bool needs_mutex, bool manually)
Helper function doing the actual work for querying the server.
Definition: network_udp.cpp:56
ClientNetworkUDPSocketHandler::Receive_SERVER_RESPONSE
void Receive_SERVER_RESPONSE(Packet *p, NetworkAddress *client_addr) override
Return of server information to the client.
Definition: network_udp.cpp:303
GRFConfig::name
GRFTextWrapper name
NOSAVE: GRF name (Action 0x08)
Definition: newgrf_config.h:160
NetworkGameInfo::clients_max
byte clients_max
Max clients allowed on server.
Definition: game.h:47
Pool::PoolItem<&_company_pool >::GetNumItems
static size_t GetNumItems()
Returns number of valid items in the pool.
Definition: pool_type.hpp:359
ServerNetworkUDPSocketHandler::Receive_CLIENT_GET_NEWGRFS
void Receive_CLIENT_GET_NEWGRFS(Packet *p, NetworkAddress *client_addr) override
A client has requested the names of some NewGRFs.
Definition: network_udp.cpp:238
network_udp.h
Packet::Send_uint64
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
Definition: packet.cpp:130
MasterNetworkUDPSocketHandler
‍*** Communication with the masterserver ***‍/
Definition: network_udp.cpp:88
NETWORK_MASTER_SERVER_HOST
static const char *const NETWORK_MASTER_SERVER_HOST
DNS hostname of the masterserver.
Definition: config.h:16
NetworkAddress::IsResolved
bool IsResolved() const
Check whether the IP address has been resolved already.
Definition: address.h:117
NETWORK_GRF_NAME_LENGTH
static const uint NETWORK_GRF_NAME_LENGTH
Maximum length of the name of a GRF.
Definition: config.h:52
GRFConfig::next
struct GRFConfig * next
NOSAVE: Next item in the linked list.
Definition: newgrf_config.h:177
NetworkUDPInitialize
void NetworkUDPInitialize()
Initialize the whole UDP bit.
Definition: network_udp.cpp:603
ServerNetworkUDPSocketHandler::Receive_CLIENT_DETAIL_INFO
void Receive_CLIENT_DETAIL_INFO(Packet *p, NetworkAddress *client_addr) override
Query for detailed information about companies.
Definition: network_udp.cpp:173
network_internal.h
SLT_AUTODETECT
@ SLT_AUTODETECT
Autodetect the type based on the connection.
Definition: udp.h:40
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:442
ClientNetworkUDPSocketHandler::Receive_MASTER_RESPONSE_LIST
void Receive_MASTER_RESPONSE_LIST(Packet *p, NetworkAddress *client_addr) override
The server sends a list of servers.
Definition: network_udp.cpp:369
udp.h
_network_need_advertise
bool _network_need_advertise
Whether we need to advertise.
Definition: network.cpp:61
GameCreationSettings::starting_year
Year starting_year
starting date
Definition: settings_type.h:283
GRFConfig::url
GRFTextWrapper url
NOSAVE: URL belonging to this GRF.
Definition: newgrf_config.h:162
Packet::Recv_uint8
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:217
_network_udp_server
bool _network_udp_server
Is the UDP server started?
Definition: network.cpp:77
NetworkGameInfo::game_date
Date game_date
Current date.
Definition: game.h:35
NetworkGameList
Structure with information shown in the game list (GUI)
Definition: network_gamelist.h:17
network.h
NetworkSocketHandler::ReceiveGRFIdentifier
void ReceiveGRFIdentifier(Packet *p, GRFIdentifier *grf)
Deserializes the GRFIdentifier (GRF ID and MD5 checksum) from the packet.
Definition: core.cpp:71
_grfconfig
GRFConfig * _grfconfig
First item in list of current GRF set up.
Definition: newgrf_config.cpp:170
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
NetworkGameInfo::server_revision
char server_revision[NETWORK_REVISION_LENGTH]
The version number the server is using (e.g.: 'r304' or 0.5.0)
Definition: game.h:40
NetworkUDPClose
void NetworkUDPClose()
Close all UDP related stuff.
Definition: network_udp.cpp:628
ClientSettings::network
NetworkSettings network
settings related to the network
Definition: settings_type.h:568
Packet::Recv_uint64
uint64 Recv_uint64()
Read a 64 bits integer from the packet.
Definition: packet.cpp:263
NetworkSocketHandler::SendCompanyInformation
void SendCompanyInformation(Packet *p, const struct Company *c, const struct NetworkCompanyStats *stats, uint max_len=NETWORK_COMPANY_NAME_LENGTH)
Package some generic company information into a packet.
Definition: network_server.cpp:1516
NetworkGameInfo::spectators_on
byte spectators_on
How many spectators do we have?
Definition: game.h:50
NetworkUDPSocketHandler::ReceivePackets
void ReceivePackets()
Receive a packet at UDP level.
Definition: udp.cpp:115
network_gamelist.h
MasterNetworkUDPSocketHandler::MasterNetworkUDPSocketHandler
MasterNetworkUDPSocketHandler(NetworkAddressList *addresses)
Create the socket.
Definition: network_udp.cpp:97
NetworkUDPRemoveAdvertiseThread
static void NetworkUDPRemoveAdvertiseThread()
Thread entry point for de-advertising.
Definition: network_udp.cpp:498
FGCM_EXACT
@ FGCM_EXACT
Only find Grfs matching md5sum.
Definition: newgrf_config.h:193
ADVERTISE_NORMAL_INTERVAL
static const std::chrono::minutes ADVERTISE_NORMAL_INTERVAL(15)
interval between advertising.
ServerNetworkUDPSocketHandler::ServerNetworkUDPSocketHandler
ServerNetworkUDPSocketHandler(NetworkAddressList *addresses)
Create the socket.
Definition: network_udp.cpp:129
GRFTextWrapper
std::shared_ptr< GRFTextList > GRFTextWrapper
Reference counted wrapper around a GRFText pointer.
Definition: newgrf_text.h:33
GRFConfig::filename
char * filename
Filename - either with or without full path.
Definition: newgrf_config.h:159
NetworkGameInfo::map_height
uint16 map_height
Map height.
Definition: game.h:37
NetworkGameList::info
NetworkGameInfo info
The game information of this server.
Definition: network_gamelist.h:18
NetworkServerGameInfo::clients_on
byte clients_on
Current count of clients on server.
Definition: game.h:26
FindUnknownGRFName
GRFTextWrapper FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
Finds the name of a NewGRF in the list of names for unknown GRFs.
Definition: newgrf_config.cpp:802
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
strecat
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:84
GetNetworkRevisionString
const char * GetNetworkRevisionString()
Get the network version string used by this build.
Definition: network.cpp:1098
NetworkGameInfo::grfconfig
GRFConfig * grfconfig
List of NewGRF files used.
Definition: game.h:33
NetworkAddress::AddressFamilyAsString
static const char * AddressFamilyAsString(int family)
Convert the address family into a string.
Definition: address.cpp:443
NETWORK_MASTER_SERVER_WELCOME_MESSAGE
static const char *const NETWORK_MASTER_SERVER_WELCOME_MESSAGE
Message sent to the masterserver to 'identify' this client as OpenTTD.
Definition: config.h:24
Company
Definition: company_base.h:110
_network_advertise_retries
uint8 _network_advertise_retries
The number of advertisement retries we did.
Definition: network.cpp:79
NetworkGameList::online
bool online
False if the server did not respond (default status)
Definition: network_gamelist.h:20
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
_broadcast_list
NetworkAddressList _broadcast_list
List of broadcast addresses.
Definition: network.cpp:70
NetworkAddress::GetAddressAsString
void GetAddressAsString(char *buffer, const char *last, bool with_family=true)
Get the address as a string, e.g.
Definition: address.cpp:77
NetworkGameInfo::compatible
bool compatible
Can we connect to this server or not? (based on server_revision and grf_match.
Definition: game.h:43
AddGRFTextToList
static void AddGRFTextToList(GRFTextList &list, byte langid, const std::string &text_to_add)
Add a new text to a GRFText list.
Definition: newgrf_text.cpp:494
NetworkUDPSocketHandler::SendPacket
void SendPacket(Packet *p, NetworkAddress *recv, bool all=false, bool broadcast=false)
Send a packet over UDP.
Definition: udp.cpp:79
NetworkGameListAddItemDelayed
void NetworkGameListAddItemDelayed(NetworkGameList *item)
Add a new item to the linked gamelist, but do it delayed in the next tick or so to prevent race condi...
Definition: network_gamelist.cpp:33
NetworkGameInfo::dedicated
bool dedicated
Is this a dedicated server?
Definition: game.h:41
Packet::Recv_uint16
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
Definition: packet.cpp:231
GRFConfig::GetName
const char * GetName() const
Get the name of this grf.
Definition: newgrf_config.cpp:104
NetworkGameInfo::spectators_max
byte spectators_max
Max spectators allowed on server.
Definition: game.h:51
_session_key
static uint64 _session_key
Session key to register ourselves to the master server.
Definition: network_udp.cpp:40
NetworkSettings::max_companies
uint8 max_companies
maximum amount of companies
Definition: settings_type.h:268
NETWORK_COMPANY_NAME_LENGTH
static const uint NETWORK_COMPANY_NAME_LENGTH
The maximum length of the company name, in bytes including '\0'.
Definition: config.h:41
NetworkGameInfo::map_set
byte map_set
Graphical set.
Definition: game.h:52