10 #include "../../stdafx.h"
11 #include "../../debug.h"
14 #include "../../safeguards.h"
23 #if defined(HAVE_GETIFADDRS)
26 struct ifaddrs *ifap, *ifa;
28 if (getifaddrs(&ifap) != 0)
return;
30 for (ifa = ifap; ifa !=
nullptr; ifa = ifa->ifa_next) {
31 if (!(ifa->ifa_flags & IFF_BROADCAST))
continue;
32 if (ifa->ifa_broadaddr ==
nullptr)
continue;
33 if (ifa->ifa_broadaddr->sa_family != AF_INET)
continue;
36 if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](
NetworkAddress const& elem) ->
bool { return elem == addr; })) broadcast->push_back(addr);
44 SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
45 if (sock == INVALID_SOCKET)
return;
49 INTERFACE_INFO *ifo = CallocT<INTERFACE_INFO>(num);
52 if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST,
nullptr, 0, ifo, num *
sizeof(*ifo), &len,
nullptr,
nullptr) == 0)
break;
54 if (WSAGetLastError() != WSAEFAULT) {
59 ifo = CallocT<INTERFACE_INFO>(num);
62 for (uint j = 0; j < len /
sizeof(*ifo); j++) {
63 if (ifo[j].iiFlags & IFF_LOOPBACK)
continue;
64 if (!(ifo[j].iiFlags & IFF_BROADCAST))
continue;
66 sockaddr_storage address;
67 memset(&address, 0,
sizeof(address));
69 memcpy(&address, &ifo[j].iiAddress.Address,
sizeof(sockaddr));
70 ((sockaddr_in*)&address)->sin_addr.s_addr = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
72 if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](
NetworkAddress const& elem) ->
bool { return elem == addr; })) broadcast->push_back(addr);
81 #include "../../string_func.h"
85 SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
86 if (sock == INVALID_SOCKET)
return;
91 ifconf.ifc_len =
sizeof(buf);
93 if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) {
98 const char *buf_end = buf + ifconf.ifc_len;
99 for (
const char *p = buf; p < buf_end;) {
100 const struct ifreq *req = (
const struct ifreq*)p;
102 if (req->ifr_addr.sa_family == AF_INET) {
106 if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 &&
107 (r.ifr_flags & IFF_BROADCAST) &&
108 ioctl(sock, SIOCGIFBRDADDR, &r) != -1) {
110 if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](
NetworkAddress const& elem) ->
bool { return elem == addr; })) broadcast->push_back(addr);
114 p +=
sizeof(
struct ifreq);
115 #if defined(AF_LINK) && !defined(SUNOS)
116 p += req->ifr_addr.sa_len -
sizeof(
struct sockaddr);
134 Debug(net, 3,
"Detected broadcast addresses:");
138 Debug(net, 3,
" {}) {}", i++, addr.GetHostname());