OpenTTD Source: src/network/core/host.cpp Source File

OpenTTD Source 15.0-RC2

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