From 024be3b009d500970168e41f375ff8f0c79c80b0 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 2 Nov 2023 08:22:30 -0700 Subject: [PATCH] Introduce getifaddrs() This function was invented by the BSDs (it's not in POSIX.1). It provides a high-level interface into ioctl(SIOCGIFCONF) which is comparatively clumsy to use. We already made the ioctls portable across our entire support vector back in 2021, so this interface is portable too. See o//tool/viz/getifaddrs.com for our demo app --- libc/isystem/ifaddrs.h | 4 ++ libc/sock/ifaddrs.c | 114 ++++++++++++++++++++++++++++++++++++++++ libc/sock/ifaddrs.h | 25 +++++++++ libc/sysv/consts.sh | 2 +- tool/viz/getifaddrs.c | 116 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 260 insertions(+), 1 deletion(-) create mode 100644 libc/isystem/ifaddrs.h create mode 100644 libc/sock/ifaddrs.c create mode 100644 libc/sock/ifaddrs.h create mode 100644 tool/viz/getifaddrs.c diff --git a/libc/isystem/ifaddrs.h b/libc/isystem/ifaddrs.h new file mode 100644 index 00000000000..3d59abbdec3 --- /dev/null +++ b/libc/isystem/ifaddrs.h @@ -0,0 +1,4 @@ +#ifndef _IFADDRS_H +#define _IFADDRS_H +#include "libc/sock/ifaddrs.h" +#endif /* _IFADDRS_H */ diff --git a/libc/sock/ifaddrs.c b/libc/sock/ifaddrs.c new file mode 100644 index 00000000000..fbbd07f0319 --- /dev/null +++ b/libc/sock/ifaddrs.c @@ -0,0 +1,114 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2023 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/sock/ifaddrs.h" +#include "libc/calls/calls.h" +#include "libc/mem/mem.h" +#include "libc/sock/sock.h" +#include "libc/sock/struct/ifconf.h" +#include "libc/sock/struct/ifreq.h" +#include "libc/str/str.h" +#include "libc/sysv/consts/af.h" +#include "libc/sysv/consts/iff.h" +#include "libc/sysv/consts/sio.h" +#include "libc/sysv/consts/sock.h" + +struct IfAddr { + struct ifaddrs ifaddrs; + char name[IFNAMSIZ]; + struct sockaddr_in addr; + struct sockaddr_in netmask; + struct sockaddr_in bstaddr; +}; + +/** + * Frees network interface address list. + */ +void freeifaddrs(struct ifaddrs *ifp) { + struct ifaddrs *next; + while (ifp) { + next = ifp->ifa_next; + free(ifp); + ifp = next; + } +} + +/** + * Gets network interface address list. + * + * @return 0 on success, or -1 w/ errno + * @see tool/viz/getifaddrs.c for example code + */ +int getifaddrs(struct ifaddrs **out_ifpp) { + int rc = -1; + int fd; + if ((fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)) != -1) { + char *data; + size_t size; + if ((data = malloc((size = 16384)))) { + struct ifconf conf; + conf.ifc_buf = data; + conf.ifc_len = size; + if (!ioctl(fd, SIOCGIFCONF, &conf)) { + struct ifaddrs *res = 0; + for (struct ifreq *ifr = (struct ifreq *)data; + (char *)ifr < data + conf.ifc_len; ++ifr) { + if (ifr->ifr_addr.sa_family != AF_INET) { + continue; // TODO(jart): IPv6 support + } + struct IfAddr *addr; + if ((addr = calloc(1, sizeof(struct IfAddr)))) { + memcpy(addr->name, ifr->ifr_name, IFNAMSIZ); + addr->ifaddrs.ifa_name = addr->name; + memcpy(&addr->addr, &ifr->ifr_addr, sizeof(struct sockaddr_in)); + addr->ifaddrs.ifa_addr = (struct sockaddr *)&addr->addr; + addr->ifaddrs.ifa_netmask = (struct sockaddr *)&addr->netmask; + if (!ioctl(fd, SIOCGIFFLAGS, ifr)) { + addr->ifaddrs.ifa_flags = ifr->ifr_flags; + } + if (!ioctl(fd, SIOCGIFNETMASK, ifr)) { + memcpy(&addr->netmask, &ifr->ifr_addr, + sizeof(struct sockaddr_in)); + } + unsigned long op; + if (addr->ifaddrs.ifa_flags & IFF_BROADCAST) { + op = SIOCGIFBRDADDR; + } else if (addr->ifaddrs.ifa_flags & IFF_POINTOPOINT) { + op = SIOCGIFDSTADDR; + } else { + op = 0; + } + if (op && !ioctl(fd, op, ifr)) { + memcpy(&addr->bstaddr, &ifr->ifr_addr, + sizeof(struct sockaddr_in)); + addr->ifaddrs.ifa_broadaddr = // is union'd w/ ifu_dstaddr + (struct sockaddr *)&addr->bstaddr; + } + addr->ifaddrs.ifa_next = res; + res = (struct ifaddrs *)addr; + } + } + *out_ifpp = res; + rc = 0; + } + free(data); + } + close(fd); + } + return rc; +} diff --git a/libc/sock/ifaddrs.h b/libc/sock/ifaddrs.h new file mode 100644 index 00000000000..a333a2df707 --- /dev/null +++ b/libc/sock/ifaddrs.h @@ -0,0 +1,25 @@ +#ifndef COSMOPOLITAN_LIBC_SOCK_IFADDRS_H_ +#define COSMOPOLITAN_LIBC_SOCK_IFADDRS_H_ +#include "libc/sock/struct/sockaddr.h" +#if !(__ASSEMBLER__ + __LINKER__ + 0) +COSMOPOLITAN_C_START_ + +struct ifaddrs { + struct ifaddrs *ifa_next; + char *ifa_name; + unsigned ifa_flags; + struct sockaddr *ifa_addr; + struct sockaddr *ifa_netmask; + union { + struct sockaddr *ifa_broadaddr; + struct sockaddr *ifa_dstaddr; + }; + void *ifa_data; +}; + +void freeifaddrs(struct ifaddrs *); +int getifaddrs(struct ifaddrs **); + +COSMOPOLITAN_C_END_ +#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ +#endif /* COSMOPOLITAN_LIBC_SOCK_IFADDRS_H_ */ diff --git a/libc/sysv/consts.sh b/libc/sysv/consts.sh index c5cd41e99dc..3a284ef6213 100755 --- a/libc/sysv/consts.sh +++ b/libc/sysv/consts.sh @@ -1351,7 +1351,7 @@ syscon iff IFF_ALLMULTI 0x0200 0x0200 0x0200 0x0200 0x0200 0x0200 syscon iff IFF_NOARP 0x80 0x80 0x80 0x80 0x80 0x80 0x80 0x80 # faked nt as linux; unix consensus syscon iff IFF_POINTOPOINT 0x10 0x10 0x10 0x10 0x10 0x10 0x10 0x10 # point-to-point; faked nt as linux; unix consensus syscon iff IFF_PROMISC 0x100 0x100 0x100 0x100 0x100 0x100 0x100 0 # in packet capture mode; unix consensus -syscon iff IFF_RUNNING 0x40 0x40 0x40 0x40 0x40 0x40 0x40 0 # unix consensus +syscon iff IFF_RUNNING 0x40 0x40 0x40 0x40 0x40 0x40 0x40 0 # unix consensus (mostly for bsd compatibility?) syscon iff IFF_NOTRAILERS 0x20 0x20 0x20 0x20 0 0 0 0 syscon iff IFF_AUTOMEDIA 0x4000 0x4000 0 0 0 0 0 0 syscon iff IFF_DYNAMIC 0x8000 0x8000 0 0 0 0 0 0 diff --git a/tool/viz/getifaddrs.c b/tool/viz/getifaddrs.c new file mode 100644 index 00000000000..d2fb9e64734 --- /dev/null +++ b/tool/viz/getifaddrs.c @@ -0,0 +1,116 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2023 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/calls.h" +#include "libc/fmt/itoa.h" +#include "libc/runtime/runtime.h" +#include "libc/sock/ifaddrs.h" +#include "libc/sock/sock.h" +#include "libc/sock/struct/sockaddr.h" +#include "libc/sock/struct/sockaddr6.h" +#include "libc/stdio/stdio.h" +#include "libc/str/str.h" +#include "libc/sysv/consts/af.h" +#include "libc/sysv/consts/iff.h" + +/* example output: + + eth0 + addr: 10.10.10.237 + netmask: 255.255.255.0 + broadcast: 255.255.255.0 + flags: IFF_UP IFF_BROADCAST IFF_MULTICAST IFF_RUNNING + + lo + addr: 127.0.0.1 + netmask: 255.0.0.0 + flags: IFF_UP IFF_LOOPBACK IFF_RUNNING */ + +const char *sockaddr2str(const struct sockaddr *sa, char *buf, size_t size) { + if (sa->sa_family == AF_INET) { + return inet_ntop(AF_INET, &(((const struct sockaddr_in *)sa)->sin_addr), + buf, size); + } else if (sa->sa_family == AF_INET6) { + return inet_ntop(AF_INET6, &(((const struct sockaddr_in6 *)sa)->sin6_addr), + buf, size); + } else { + return 0; + } +} + +int main(int argc, char *argv[]) { + + // get network interface list + struct ifaddrs *ifaddrs; + if (getifaddrs(&ifaddrs)) { + perror("getifaddrs"); + exit(1); + } + + // print network interface list + for (struct ifaddrs *ifa = ifaddrs; ifa; ifa = ifa->ifa_next) { + tinyprint(1, ifa->ifa_name, "\n", NULL); + + char buf[128]; + if (sockaddr2str(ifa->ifa_addr, buf, sizeof(buf))) { + tinyprint(1, "addr: ", buf, "\n", NULL); + } + if (sockaddr2str(ifa->ifa_netmask, buf, sizeof(buf))) { + tinyprint(1, "netmask: ", buf, "\n", NULL); + } + if ((ifa->ifa_flags & IFF_BROADCAST) && + sockaddr2str(ifa->ifa_netmask, buf, sizeof(buf))) { + tinyprint(1, "broadcast: ", buf, "\n", NULL); + } else if ((ifa->ifa_flags & IFF_POINTOPOINT) && + sockaddr2str(ifa->ifa_dstaddr, buf, sizeof(buf))) { + tinyprint(1, "dstaddr: ", buf, "\n", NULL); + } + + tinyprint(1, "flags:", NULL); + if (ifa->ifa_flags & IFF_UP) { + tinyprint(1, " IFF_UP", NULL); + } + if (ifa->ifa_flags & IFF_DEBUG) { + tinyprint(1, " IFF_DEBUG", NULL); + } + if (ifa->ifa_flags & IFF_LOOPBACK) { + tinyprint(1, " IFF_LOOPBACK", NULL); + } + if (ifa->ifa_flags & IFF_MULTICAST) { + tinyprint(1, " IFF_MULTICAST", NULL); + } + if (ifa->ifa_flags & IFF_ALLMULTI) { + tinyprint(1, " IFF_ALLMULTI", NULL); + } + if (ifa->ifa_flags & IFF_NOARP) { + tinyprint(1, " IFF_NOARP", NULL); + } + if (ifa->ifa_flags & IFF_PROMISC) { + tinyprint(1, " IFF_PROMISC", NULL); + } + tinyprint(1, "\n", NULL); + + tinyprint(1, "\n", NULL); + } + + // perform cleanup + freeifaddrs(ifaddrs); + + // report success + exit(0); +}