2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-08-04 14:56:38 +02:00
|
|
|
/** @file host.cpp Functions related to getting host specific data (IPs). */
|
|
|
|
|
|
|
|
#include "../../stdafx.h"
|
|
|
|
#include "../../debug.h"
|
2021-05-01 22:06:17 +02:00
|
|
|
#include "../../core/alloc_func.hpp"
|
2009-04-03 13:02:41 +02:00
|
|
|
#include "address.h"
|
2008-08-04 14:56:38 +02:00
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2008-08-04 14:56:38 +02:00
|
|
|
/**
|
|
|
|
* Internal implementation for finding the broadcast IPs.
|
|
|
|
* This function is implemented multiple times for multiple targets.
|
|
|
|
* @param broadcast the list of broadcasts to write into.
|
|
|
|
*/
|
2009-04-07 21:06:36 +02:00
|
|
|
static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast);
|
2008-08-04 14:56:38 +02:00
|
|
|
|
2023-06-22 21:22:45 +02:00
|
|
|
#ifdef _WIN32
|
2009-04-07 21:06:36 +02:00
|
|
|
static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast) // Win32 implementation
|
2008-08-04 14:56:38 +02:00
|
|
|
{
|
|
|
|
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
|
2009-04-07 21:06:36 +02:00
|
|
|
if (sock == INVALID_SOCKET) return;
|
2008-08-04 14:56:38 +02:00
|
|
|
|
|
|
|
DWORD len = 0;
|
2009-04-08 02:10:20 +02:00
|
|
|
int num = 2;
|
|
|
|
INTERFACE_INFO *ifo = CallocT<INTERFACE_INFO>(num);
|
|
|
|
|
2009-08-14 19:14:04 +02:00
|
|
|
for (;;) {
|
2019-04-10 23:07:06 +02:00
|
|
|
if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, nullptr, 0, ifo, num * sizeof(*ifo), &len, nullptr, nullptr) == 0) break;
|
2009-04-08 02:10:20 +02:00
|
|
|
free(ifo);
|
|
|
|
if (WSAGetLastError() != WSAEFAULT) {
|
|
|
|
closesocket(sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
num *= 2;
|
|
|
|
ifo = CallocT<INTERFACE_INFO>(num);
|
2008-08-04 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
2009-04-07 21:06:36 +02:00
|
|
|
for (uint j = 0; j < len / sizeof(*ifo); j++) {
|
2008-08-04 14:56:38 +02:00
|
|
|
if (ifo[j].iiFlags & IFF_LOOPBACK) continue;
|
|
|
|
if (!(ifo[j].iiFlags & IFF_BROADCAST)) continue;
|
|
|
|
|
2009-04-03 13:02:41 +02:00
|
|
|
sockaddr_storage address;
|
|
|
|
memset(&address, 0, sizeof(address));
|
2008-08-04 14:56:38 +02:00
|
|
|
/* iiBroadcast is unusable, because it always seems to be set to 255.255.255.255. */
|
2009-04-03 18:22:34 +02:00
|
|
|
memcpy(&address, &ifo[j].iiAddress.Address, sizeof(sockaddr));
|
2009-04-03 14:48:11 +02:00
|
|
|
((sockaddr_in*)&address)->sin_addr.s_addr = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
|
2009-04-24 12:33:16 +02:00
|
|
|
NetworkAddress addr(address, sizeof(sockaddr));
|
2024-01-03 22:33:38 +01:00
|
|
|
if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const &elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
|
2008-08-04 14:56:38 +02:00
|
|
|
}
|
|
|
|
|
2009-04-08 02:10:20 +02:00
|
|
|
free(ifo);
|
2008-08-04 14:56:38 +02:00
|
|
|
closesocket(sock);
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:22:45 +02:00
|
|
|
#else /* not WIN32 */
|
|
|
|
static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast)
|
2008-08-04 14:56:38 +02:00
|
|
|
{
|
2023-06-22 21:22:45 +02:00
|
|
|
struct ifaddrs *ifap, *ifa;
|
2008-08-04 14:56:38 +02:00
|
|
|
|
2023-06-22 21:22:45 +02:00
|
|
|
if (getifaddrs(&ifap) != 0) return;
|
2008-08-04 14:56:38 +02:00
|
|
|
|
2023-06-22 21:22:45 +02:00
|
|
|
for (ifa = ifap; ifa != nullptr; ifa = ifa->ifa_next) {
|
|
|
|
if (!(ifa->ifa_flags & IFF_BROADCAST)) continue;
|
|
|
|
if (ifa->ifa_broadaddr == nullptr) continue;
|
|
|
|
if (ifa->ifa_broadaddr->sa_family != AF_INET) continue;
|
2008-08-04 14:56:38 +02:00
|
|
|
|
2023-06-22 21:22:45 +02:00
|
|
|
NetworkAddress addr(ifa->ifa_broadaddr, sizeof(sockaddr));
|
2024-01-03 22:33:38 +01:00
|
|
|
if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](NetworkAddress const &elem) -> bool { return elem == addr; })) broadcast->push_back(addr);
|
2008-08-04 14:56:38 +02:00
|
|
|
}
|
2023-06-22 21:22:45 +02:00
|
|
|
freeifaddrs(ifap);
|
2008-08-04 14:56:38 +02:00
|
|
|
}
|
|
|
|
#endif /* all NetworkFindBroadcastIPsInternals */
|
|
|
|
|
|
|
|
/**
|
2009-04-03 13:02:41 +02:00
|
|
|
* Find the IPv4 broadcast addresses; IPv6 uses a completely different
|
|
|
|
* strategy for broadcasting.
|
2008-08-04 14:56:38 +02:00
|
|
|
* @param broadcast the list of broadcasts to write into.
|
|
|
|
*/
|
2009-04-07 21:06:36 +02:00
|
|
|
void NetworkFindBroadcastIPs(NetworkAddressList *broadcast)
|
2008-08-04 14:56:38 +02:00
|
|
|
{
|
2009-04-07 21:06:36 +02:00
|
|
|
NetworkFindBroadcastIPsInternal(broadcast);
|
2008-08-04 14:56:38 +02:00
|
|
|
|
|
|
|
/* Now display to the debug all the detected ips */
|
2021-06-12 09:10:17 +02:00
|
|
|
Debug(net, 3, "Detected broadcast addresses:");
|
2009-04-07 21:06:36 +02:00
|
|
|
int i = 0;
|
2019-02-17 12:20:52 +01:00
|
|
|
for (NetworkAddress &addr : *broadcast) {
|
|
|
|
addr.SetPort(NETWORK_DEFAULT_PORT);
|
2021-06-12 09:10:17 +02:00
|
|
|
Debug(net, 3, " {}) {}", i++, addr.GetHostname());
|
2008-08-04 14:56:38 +02:00
|
|
|
}
|
|
|
|
}
|