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