10 #include "../../stdafx.h"
11 #include "../../debug.h"
14 #include "../../safeguards.h"
23 #if defined(__HAIKU__)
25 extern "C" int _netstat(
int fd,
char **output,
int verbose);
27 int seek_past_header(
char **pos,
const char *header)
29 char *new_pos = strstr(*pos, header);
33 *pos += strlen(header) + new_pos - *pos + 1;
39 int sock = socket(AF_INET, SOCK_DGRAM, 0);
42 DEBUG(net, 0,
"[core] error creating socket");
46 char *output_pointer =
nullptr;
47 int output_length = _netstat(sock, &output_pointer, 1);
48 if (output_length < 0) {
49 DEBUG(net, 0,
"[core] error running _netstat");
53 char **output = &output_pointer;
54 if (seek_past_header(output,
"IP Interfaces:") == B_OK) {
58 uint8 i1, i2, i3, i4, j1, j2, j3, j4;
62 fields = sscanf(*output,
"%u: %hhu.%hhu.%hhu.%hhu, netmask %hhu.%hhu.%hhu.%hhu%n",
63 &n, &i1, &i2, &i3, &i4, &j1, &j2, &j3, &j4, &read);
69 ip = (uint32)i1 << 24 | (uint32)i2 << 16 | (uint32)i3 << 8 | (uint32)i4;
70 netmask = (uint32)j1 << 24 | (uint32)j2 << 16 | (uint32)j3 << 8 | (uint32)j4;
72 if (ip != INADDR_LOOPBACK && ip != INADDR_ANY) {
73 sockaddr_storage address;
74 memset(&address, 0,
sizeof(address));
75 ((sockaddr_in*)&address)->sin_addr.s_addr = htonl(ip | ~netmask);
77 if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](
NetworkAddress const& elem) ->
bool { return elem == addr; })) broadcast->push_back(addr);
88 #elif defined(HAVE_GETIFADDRS)
91 struct ifaddrs *ifap, *ifa;
93 if (getifaddrs(&ifap) != 0)
return;
95 for (ifa = ifap; ifa !=
nullptr; ifa = ifa->ifa_next) {
96 if (!(ifa->ifa_flags & IFF_BROADCAST))
continue;
97 if (ifa->ifa_broadaddr ==
nullptr)
continue;
98 if (ifa->ifa_broadaddr->sa_family != AF_INET)
continue;
101 if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](
NetworkAddress const& elem) ->
bool { return elem == addr; })) broadcast->push_back(addr);
106 #elif defined(_WIN32)
109 SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
110 if (sock == INVALID_SOCKET)
return;
114 INTERFACE_INFO *ifo = CallocT<INTERFACE_INFO>(num);
117 if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST,
nullptr, 0, ifo, num *
sizeof(*ifo), &len,
nullptr,
nullptr) == 0)
break;
119 if (WSAGetLastError() != WSAEFAULT) {
124 ifo = CallocT<INTERFACE_INFO>(num);
127 for (uint j = 0; j < len /
sizeof(*ifo); j++) {
128 if (ifo[j].iiFlags & IFF_LOOPBACK)
continue;
129 if (!(ifo[j].iiFlags & IFF_BROADCAST))
continue;
131 sockaddr_storage address;
132 memset(&address, 0,
sizeof(address));
134 memcpy(&address, &ifo[j].iiAddress.Address,
sizeof(sockaddr));
135 ((sockaddr_in*)&address)->sin_addr.s_addr = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
137 if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](
NetworkAddress const& elem) ->
bool { return elem == addr; })) broadcast->push_back(addr);
146 #include "../../string_func.h"
150 SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
151 if (sock == INVALID_SOCKET)
return;
154 struct ifconf ifconf;
156 ifconf.ifc_len =
sizeof(buf);
157 ifconf.ifc_buf = buf;
158 if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) {
163 const char *buf_end = buf + ifconf.ifc_len;
164 for (
const char *p = buf; p < buf_end;) {
165 const struct ifreq *req = (
const struct ifreq*)p;
167 if (req->ifr_addr.sa_family == AF_INET) {
171 if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 &&
172 (r.ifr_flags & IFF_BROADCAST) &&
173 ioctl(sock, SIOCGIFBRDADDR, &r) != -1) {
175 if (std::none_of(broadcast->begin(), broadcast->end(), [&addr](
NetworkAddress const& elem) ->
bool { return elem == addr; })) broadcast->push_back(addr);
179 p +=
sizeof(
struct ifreq);
180 #if defined(AF_LINK) && !defined(SUNOS)
181 p += req->ifr_addr.sa_len -
sizeof(
struct sockaddr);
199 DEBUG(net, 3,
"Detected broadcast addresses:");
203 DEBUG(net, 3,
"%d) %s", i++, addr.GetHostname());