OpenTTD Source  20240919-master-gdf0233f4c2
host.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 
10 #include "../../stdafx.h"
11 #include "../../debug.h"
12 #include "../../core/alloc_func.hpp"
13 #include "address.h"
14 
15 #include "../../safeguards.h"
16 
23 
24 #ifdef _WIN32
25 static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // Win32 implementation
26 {
27  SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
28  if (sock == INVALID_SOCKET) return;
29 
30  DWORD len = 0;
31  int num = 2;
32  INTERFACE_INFO *ifo = CallocT<INTERFACE_INFO>(num);
33 
34  for (;;) {
35  if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, nullptr, 0, ifo, num * sizeof(*ifo), &len, nullptr, nullptr) == 0) break;
36  free(ifo);
37  if (WSAGetLastError() != WSAEFAULT) {
38  closesocket(sock);
39  return;
40  }
41  num *= 2;
42  ifo = CallocT<INTERFACE_INFO>(num);
43  }
44 
45  for (uint j = 0; j < len / sizeof(*ifo); j++) {
46  if (ifo[j].iiFlags & IFF_LOOPBACK) continue;
47  if (!(ifo[j].iiFlags & IFF_BROADCAST)) continue;
48 
49  sockaddr_storage address;
50  memset(&address, 0, sizeof(address));
51  /* iiBroadcast is unusable, because it always seems to be set to 255.255.255.255. */
52  memcpy(&address, &ifo[j].iiAddress.Address, sizeof(sockaddr));
53  ((sockaddr_in*)&address)->sin_addr.s_addr = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
54  NetworkAddress addr(address, sizeof(sockaddr));
55  if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const &elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
56  }
57 
58  free(ifo);
59  closesocket(sock);
60 }
61 
62 #else /* not WIN32 */
64 {
65  struct ifaddrs *ifap, *ifa;
66 
67  if (getifaddrs(&ifap) != 0) return;
68 
69  for (ifa = ifap; ifa != nullptr; ifa = ifa->ifa_next) {
70  if (!(ifa->ifa_flags & IFF_BROADCAST)) continue;
71  if (ifa->ifa_broadaddr == nullptr) continue;
72  if (ifa->ifa_broadaddr->sa_family != AF_INET) continue;
73 
74  NetworkAddress addr(ifa->ifa_broadaddr, sizeof(sockaddr));
75  if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const &elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
76  }
77  freeifaddrs(ifap);
78 }
79 #endif /* all NetworkFindBroadcastIPsInternals */
80 
87 {
89 
90  /* Now display to the debug all the detected ips */
91  Debug(net, 3, "Detected broadcast addresses:");
92  int i = 0;
93  for (NetworkAddress &addr : *broadcast) {
94  addr.SetPort(NETWORK_DEFAULT_PORT);
95  Debug(net, 3, " {}) {}", i++, addr.GetHostname());
96  }
97 }
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
NetworkAddressList
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Definition: address.h:19
address.h
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:334
NetworkFindBroadcastIPsInternal
static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast)
Internal implementation for finding the broadcast IPs.
Definition: host.cpp:63
NETWORK_DEFAULT_PORT
static const uint16_t NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
Definition: config.h:25
NetworkAddress
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
Definition: address.h:28
NetworkFindBroadcastIPs
void NetworkFindBroadcastIPs(NetworkAddressList *broadcast)
Find the IPv4 broadcast addresses; IPv6 uses a completely different strategy for broadcasting.
Definition: host.cpp:86