10#include "../../stdafx.h"
11#include "../../debug.h"
12#include "../../core/alloc_func.hpp"
15#include "../../safeguards.h"
27 SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
28 if (sock == INVALID_SOCKET)
return;
32 INTERFACE_INFO *ifo = CallocT<INTERFACE_INFO>(num);
35 if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST,
nullptr, 0, ifo, num *
sizeof(*ifo), &len,
nullptr,
nullptr) == 0)
break;
37 if (WSAGetLastError() != WSAEFAULT) {
42 ifo = CallocT<INTERFACE_INFO>(num);
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;
49 sockaddr_storage address;
50 memset(&address, 0,
sizeof(address));
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;
55 if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](
NetworkAddress const &elem) ->
bool { return elem == addr; })) broadcast->push_back(addr);
65 struct ifaddrs *ifap, *ifa;
67 if (getifaddrs(&ifap) != 0)
return;
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;
75 if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](
NetworkAddress const &elem) ->
bool { return elem == addr; })) broadcast->push_back(addr);
91 Debug(net, 3,
"Detected broadcast addresses:");
95 Debug(net, 3,
" {}) {}", i++, addr.GetHostname());
Wrapper for network addresses.
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
static const uint16_t NETWORK_DEFAULT_PORT
The default port of the game server (TCP & UDP)
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
void NetworkFindBroadcastIPs(NetworkAddressList *broadcast)
Find the IPv4 broadcast addresses; IPv6 uses a completely different strategy for broadcasting.
static void NetworkFindBroadcastIPsInternal(NetworkAddressList *broadcast)
Internal implementation for finding the broadcast IPs.
void free(const void *ptr)
Version of the standard free that accepts const pointers.