network: openbsd.

This commit is contained in:
Alastair Poole 2021-03-02 21:32:29 +00:00
parent 86957f0610
commit 108595dd38
1 changed files with 27 additions and 11 deletions

View File

@ -48,18 +48,19 @@ _freebsd_generic_network_status(uint64_t *in,
#endif #endif
#if defined(__OpenBSD__) #if defined(__OpenBSD__)
static void static net_iface_t **
_openbsd_generic_network_status(uint64_t *in, _openbsd_generic_network_status(int *n)
uint64_t *out)
{ {
struct ifaddrs *interfaces, *ifa; struct ifaddrs *interfaces, *ifa;
if (getifaddrs(&interfaces) < 0) if (getifaddrs(&interfaces) < 0)
return; return NULL;
int sock = socket(AF_INET, SOCK_STREAM, 0); int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) if (sock < 0)
return; return NULL;
net_iface_t **ifaces = NULL;
for (ifa = interfaces; ifa; ifa = ifa->ifa_next) { for (ifa = interfaces; ifa; ifa = ifa->ifa_next) {
struct ifreq ifreq; struct ifreq ifreq;
@ -74,25 +75,38 @@ _openbsd_generic_network_status(uint64_t *in,
ifreq.ifr_data = (void *)&if_data; ifreq.ifr_data = (void *)&if_data;
strncpy(ifreq.ifr_name, ifa->ifa_name, IFNAMSIZ - 1); strncpy(ifreq.ifr_name, ifa->ifa_name, IFNAMSIZ - 1);
if (ioctl(sock, SIOCGIFDATA, &ifreq) < 0) if (ioctl(sock, SIOCGIFDATA, &ifreq) < 0)
return; return NULL;
const struct if_data *ifi = &if_data; const struct if_data *ifi = &if_data;
if (!LINK_STATE_IS_UP(ifi->ifi_link_state)) if (!LINK_STATE_IS_UP(ifi->ifi_link_state))
continue; continue;
net_iface_t *iface = malloc(sizeof(net_iface_t));
if (!iface) return NULL;
if (ifi->ifi_type == IFT_ETHER || if (ifi->ifi_type == IFT_ETHER ||
ifi->ifi_type == IFT_FASTETHER || ifi->ifi_type == IFT_FASTETHER ||
ifi->ifi_type == IFT_GIGABITETHERNET || ifi->ifi_type == IFT_GIGABITETHERNET ||
ifi->ifi_type == IFT_IEEE80211) ifi->ifi_type == IFT_IEEE80211)
{ {
if (ifi->ifi_ibytes) net_iface_t *iface = malloc(sizeof(net_iface_t));
*in += ifi->ifi_ibytes; if (iface)
{
if (ifi->ifi_obytes) snprintf(iface->name, sizeof(iface->name), "%s",
*out += ifi->ifi_obytes; ifa->ifa_name);
iface->xfer.in = ifi->ifi_ibytes;
iface->xfer.out = ifi->ifi_obytes;
void *t = realloc(ifaces, (1 + 1 + *n) * sizeof(net_iface_t *));
ifaces = t;
ifaces[(*n)++] = iface;
}
} }
} }
close(sock); close(sock);
return ifaces;
} }
#endif #endif
@ -145,6 +159,8 @@ system_network_ifaces_get(int *n)
*n = 0; *n = 0;
#if defined(__linux__) #if defined(__linux__)
return _linux_generic_network_status(n); return _linux_generic_network_status(n);
#elif defined(__OpenBSD__)
return _openbsd_generic_network_status(n);
#else #else
return NULL; return NULL;
#endif #endif