14 #include "../../stdafx.h"
15 #include "../../debug.h"
18 #include "../../safeguards.h"
32 #elif defined(BEOS_NET_SERVER) || defined(__HAIKU__)
34 extern "C" int _netstat(
int fd,
char **output,
int verbose);
36 int seek_past_header(
char **pos,
const char *header)
38 char *new_pos = strstr(*pos, header);
42 *pos += strlen(header) + new_pos - *pos + 1;
48 int sock = socket(AF_INET, SOCK_DGRAM, 0);
51 DEBUG(net, 0,
"[core] error creating socket");
55 char *output_pointer = NULL;
56 int output_length = _netstat(sock, &output_pointer, 1);
57 if (output_length < 0) {
58 DEBUG(net, 0,
"[core] error running _netstat");
62 char **output = &output_pointer;
63 if (seek_past_header(output,
"IP Interfaces:") == B_OK) {
67 uint8 i1, i2, i3, i4, j1, j2, j3, j4;
71 fields = sscanf(*output,
"%u: %hhu.%hhu.%hhu.%hhu, netmask %hhu.%hhu.%hhu.%hhu%n",
72 &n, &i1, &i2, &i3, &i4, &j1, &j2, &j3, &j4, &read);
78 ip = (uint32)i1 << 24 | (uint32)i2 << 16 | (uint32)i3 << 8 | (uint32)i4;
79 netmask = (uint32)j1 << 24 | (uint32)j2 << 16 | (uint32)j3 << 8 | (uint32)j4;
81 if (ip != INADDR_LOOPBACK && ip != INADDR_ANY) {
82 sockaddr_storage address;
83 memset(&address, 0,
sizeof(address));
84 ((sockaddr_in*)&address)->sin_addr.s_addr = htonl(ip | ~netmask);
97 #elif defined(HAVE_GETIFADDRS)
100 struct ifaddrs *ifap, *ifa;
102 if (getifaddrs(&ifap) != 0)
return;
104 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
105 if (!(ifa->ifa_flags & IFF_BROADCAST))
continue;
106 if (ifa->ifa_broadaddr == NULL)
continue;
107 if (ifa->ifa_broadaddr->sa_family != AF_INET)
continue;
118 SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
119 if (sock == INVALID_SOCKET)
return;
123 INTERFACE_INFO *ifo = CallocT<INTERFACE_INFO>(num);
126 if (WSAIoctl(sock, SIO_GET_INTERFACE_LIST, NULL, 0, ifo, num *
sizeof(*ifo), &len, NULL, NULL) == 0)
break;
128 if (WSAGetLastError() != WSAEFAULT) {
133 ifo = CallocT<INTERFACE_INFO>(num);
136 for (uint j = 0; j < len /
sizeof(*ifo); j++) {
137 if (ifo[j].iiFlags & IFF_LOOPBACK)
continue;
138 if (!(ifo[j].iiFlags & IFF_BROADCAST))
continue;
140 sockaddr_storage address;
141 memset(&address, 0,
sizeof(address));
143 memcpy(&address, &ifo[j].iiAddress.Address,
sizeof(sockaddr));
144 ((sockaddr_in*)&address)->sin_addr.s_addr = ifo[j].iiAddress.AddressIn.sin_addr.s_addr | ~ifo[j].iiNetmask.AddressIn.sin_addr.s_addr;
155 #include "../../string_func.h"
159 SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
160 if (sock == INVALID_SOCKET)
return;
163 struct ifconf ifconf;
165 ifconf.ifc_len =
sizeof(buf);
166 ifconf.ifc_buf = buf;
167 if (ioctl(sock, SIOCGIFCONF, &ifconf) == -1) {
172 const char *buf_end = buf + ifconf.ifc_len;
173 for (
const char *p = buf; p < buf_end;) {
174 const struct ifreq *req = (
const struct ifreq*)p;
176 if (req->ifr_addr.sa_family == AF_INET) {
180 if (ioctl(sock, SIOCGIFFLAGS, &r) != -1 &&
181 (r.ifr_flags & IFF_BROADCAST) &&
182 ioctl(sock, SIOCGIFBRDADDR, &r) != -1) {
188 p +=
sizeof(
struct ifreq);
189 #if defined(AF_LINK) && !defined(SUNOS)
190 p += req->ifr_addr.sa_len -
sizeof(
struct sockaddr);
208 DEBUG(net, 3,
"Detected broadcast addresses:");
212 DEBUG(net, 3,
"%d) %s", i++, addr->GetHostname());