Skip to content

Commit 024be3b

Browse files
committed
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
1 parent 6ca5ab4 commit 024be3b

File tree

5 files changed

+260
-1
lines changed

5 files changed

+260
-1
lines changed

libc/isystem/ifaddrs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef _IFADDRS_H
2+
#define _IFADDRS_H
3+
#include "libc/sock/ifaddrs.h"
4+
#endif /* _IFADDRS_H */

libc/sock/ifaddrs.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2023 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/sock/ifaddrs.h"
20+
#include "libc/calls/calls.h"
21+
#include "libc/mem/mem.h"
22+
#include "libc/sock/sock.h"
23+
#include "libc/sock/struct/ifconf.h"
24+
#include "libc/sock/struct/ifreq.h"
25+
#include "libc/str/str.h"
26+
#include "libc/sysv/consts/af.h"
27+
#include "libc/sysv/consts/iff.h"
28+
#include "libc/sysv/consts/sio.h"
29+
#include "libc/sysv/consts/sock.h"
30+
31+
struct IfAddr {
32+
struct ifaddrs ifaddrs;
33+
char name[IFNAMSIZ];
34+
struct sockaddr_in addr;
35+
struct sockaddr_in netmask;
36+
struct sockaddr_in bstaddr;
37+
};
38+
39+
/**
40+
* Frees network interface address list.
41+
*/
42+
void freeifaddrs(struct ifaddrs *ifp) {
43+
struct ifaddrs *next;
44+
while (ifp) {
45+
next = ifp->ifa_next;
46+
free(ifp);
47+
ifp = next;
48+
}
49+
}
50+
51+
/**
52+
* Gets network interface address list.
53+
*
54+
* @return 0 on success, or -1 w/ errno
55+
* @see tool/viz/getifaddrs.c for example code
56+
*/
57+
int getifaddrs(struct ifaddrs **out_ifpp) {
58+
int rc = -1;
59+
int fd;
60+
if ((fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)) != -1) {
61+
char *data;
62+
size_t size;
63+
if ((data = malloc((size = 16384)))) {
64+
struct ifconf conf;
65+
conf.ifc_buf = data;
66+
conf.ifc_len = size;
67+
if (!ioctl(fd, SIOCGIFCONF, &conf)) {
68+
struct ifaddrs *res = 0;
69+
for (struct ifreq *ifr = (struct ifreq *)data;
70+
(char *)ifr < data + conf.ifc_len; ++ifr) {
71+
if (ifr->ifr_addr.sa_family != AF_INET) {
72+
continue; // TODO(jart): IPv6 support
73+
}
74+
struct IfAddr *addr;
75+
if ((addr = calloc(1, sizeof(struct IfAddr)))) {
76+
memcpy(addr->name, ifr->ifr_name, IFNAMSIZ);
77+
addr->ifaddrs.ifa_name = addr->name;
78+
memcpy(&addr->addr, &ifr->ifr_addr, sizeof(struct sockaddr_in));
79+
addr->ifaddrs.ifa_addr = (struct sockaddr *)&addr->addr;
80+
addr->ifaddrs.ifa_netmask = (struct sockaddr *)&addr->netmask;
81+
if (!ioctl(fd, SIOCGIFFLAGS, ifr)) {
82+
addr->ifaddrs.ifa_flags = ifr->ifr_flags;
83+
}
84+
if (!ioctl(fd, SIOCGIFNETMASK, ifr)) {
85+
memcpy(&addr->netmask, &ifr->ifr_addr,
86+
sizeof(struct sockaddr_in));
87+
}
88+
unsigned long op;
89+
if (addr->ifaddrs.ifa_flags & IFF_BROADCAST) {
90+
op = SIOCGIFBRDADDR;
91+
} else if (addr->ifaddrs.ifa_flags & IFF_POINTOPOINT) {
92+
op = SIOCGIFDSTADDR;
93+
} else {
94+
op = 0;
95+
}
96+
if (op && !ioctl(fd, op, ifr)) {
97+
memcpy(&addr->bstaddr, &ifr->ifr_addr,
98+
sizeof(struct sockaddr_in));
99+
addr->ifaddrs.ifa_broadaddr = // is union'd w/ ifu_dstaddr
100+
(struct sockaddr *)&addr->bstaddr;
101+
}
102+
addr->ifaddrs.ifa_next = res;
103+
res = (struct ifaddrs *)addr;
104+
}
105+
}
106+
*out_ifpp = res;
107+
rc = 0;
108+
}
109+
free(data);
110+
}
111+
close(fd);
112+
}
113+
return rc;
114+
}

libc/sock/ifaddrs.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef COSMOPOLITAN_LIBC_SOCK_IFADDRS_H_
2+
#define COSMOPOLITAN_LIBC_SOCK_IFADDRS_H_
3+
#include "libc/sock/struct/sockaddr.h"
4+
#if !(__ASSEMBLER__ + __LINKER__ + 0)
5+
COSMOPOLITAN_C_START_
6+
7+
struct ifaddrs {
8+
struct ifaddrs *ifa_next;
9+
char *ifa_name;
10+
unsigned ifa_flags;
11+
struct sockaddr *ifa_addr;
12+
struct sockaddr *ifa_netmask;
13+
union {
14+
struct sockaddr *ifa_broadaddr;
15+
struct sockaddr *ifa_dstaddr;
16+
};
17+
void *ifa_data;
18+
};
19+
20+
void freeifaddrs(struct ifaddrs *);
21+
int getifaddrs(struct ifaddrs **);
22+
23+
COSMOPOLITAN_C_END_
24+
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
25+
#endif /* COSMOPOLITAN_LIBC_SOCK_IFADDRS_H_ */

libc/sysv/consts.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ syscon iff IFF_ALLMULTI 0x0200 0x0200 0x0200 0x0200 0x0200 0x0200
13511351
syscon iff IFF_NOARP 0x80 0x80 0x80 0x80 0x80 0x80 0x80 0x80 # faked nt as linux; unix consensus
13521352
syscon iff IFF_POINTOPOINT 0x10 0x10 0x10 0x10 0x10 0x10 0x10 0x10 # point-to-point; faked nt as linux; unix consensus
13531353
syscon iff IFF_PROMISC 0x100 0x100 0x100 0x100 0x100 0x100 0x100 0 # in packet capture mode; unix consensus
1354-
syscon iff IFF_RUNNING 0x40 0x40 0x40 0x40 0x40 0x40 0x40 0 # unix consensus
1354+
syscon iff IFF_RUNNING 0x40 0x40 0x40 0x40 0x40 0x40 0x40 0 # unix consensus (mostly for bsd compatibility?)
13551355
syscon iff IFF_NOTRAILERS 0x20 0x20 0x20 0x20 0 0 0 0
13561356
syscon iff IFF_AUTOMEDIA 0x4000 0x4000 0 0 0 0 0 0
13571357
syscon iff IFF_DYNAMIC 0x8000 0x8000 0 0 0 0 0 0

tool/viz/getifaddrs.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
2+
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
3+
╞══════════════════════════════════════════════════════════════════════════════╡
4+
│ Copyright 2023 Justine Alexandra Roberts Tunney │
5+
│ │
6+
│ Permission to use, copy, modify, and/or distribute this software for │
7+
│ any purpose with or without fee is hereby granted, provided that the │
8+
│ above copyright notice and this permission notice appear in all copies. │
9+
│ │
10+
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
11+
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
12+
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
13+
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
14+
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
15+
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
16+
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
17+
│ PERFORMANCE OF THIS SOFTWARE. │
18+
╚─────────────────────────────────────────────────────────────────────────────*/
19+
#include "libc/calls/calls.h"
20+
#include "libc/fmt/itoa.h"
21+
#include "libc/runtime/runtime.h"
22+
#include "libc/sock/ifaddrs.h"
23+
#include "libc/sock/sock.h"
24+
#include "libc/sock/struct/sockaddr.h"
25+
#include "libc/sock/struct/sockaddr6.h"
26+
#include "libc/stdio/stdio.h"
27+
#include "libc/str/str.h"
28+
#include "libc/sysv/consts/af.h"
29+
#include "libc/sysv/consts/iff.h"
30+
31+
/* example output:
32+
33+
eth0
34+
addr: 10.10.10.237
35+
netmask: 255.255.255.0
36+
broadcast: 255.255.255.0
37+
flags: IFF_UP IFF_BROADCAST IFF_MULTICAST IFF_RUNNING
38+
39+
lo
40+
addr: 127.0.0.1
41+
netmask: 255.0.0.0
42+
flags: IFF_UP IFF_LOOPBACK IFF_RUNNING */
43+
44+
const char *sockaddr2str(const struct sockaddr *sa, char *buf, size_t size) {
45+
if (sa->sa_family == AF_INET) {
46+
return inet_ntop(AF_INET, &(((const struct sockaddr_in *)sa)->sin_addr),
47+
buf, size);
48+
} else if (sa->sa_family == AF_INET6) {
49+
return inet_ntop(AF_INET6, &(((const struct sockaddr_in6 *)sa)->sin6_addr),
50+
buf, size);
51+
} else {
52+
return 0;
53+
}
54+
}
55+
56+
int main(int argc, char *argv[]) {
57+
58+
// get network interface list
59+
struct ifaddrs *ifaddrs;
60+
if (getifaddrs(&ifaddrs)) {
61+
perror("getifaddrs");
62+
exit(1);
63+
}
64+
65+
// print network interface list
66+
for (struct ifaddrs *ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
67+
tinyprint(1, ifa->ifa_name, "\n", NULL);
68+
69+
char buf[128];
70+
if (sockaddr2str(ifa->ifa_addr, buf, sizeof(buf))) {
71+
tinyprint(1, "addr: ", buf, "\n", NULL);
72+
}
73+
if (sockaddr2str(ifa->ifa_netmask, buf, sizeof(buf))) {
74+
tinyprint(1, "netmask: ", buf, "\n", NULL);
75+
}
76+
if ((ifa->ifa_flags & IFF_BROADCAST) &&
77+
sockaddr2str(ifa->ifa_netmask, buf, sizeof(buf))) {
78+
tinyprint(1, "broadcast: ", buf, "\n", NULL);
79+
} else if ((ifa->ifa_flags & IFF_POINTOPOINT) &&
80+
sockaddr2str(ifa->ifa_dstaddr, buf, sizeof(buf))) {
81+
tinyprint(1, "dstaddr: ", buf, "\n", NULL);
82+
}
83+
84+
tinyprint(1, "flags:", NULL);
85+
if (ifa->ifa_flags & IFF_UP) {
86+
tinyprint(1, " IFF_UP", NULL);
87+
}
88+
if (ifa->ifa_flags & IFF_DEBUG) {
89+
tinyprint(1, " IFF_DEBUG", NULL);
90+
}
91+
if (ifa->ifa_flags & IFF_LOOPBACK) {
92+
tinyprint(1, " IFF_LOOPBACK", NULL);
93+
}
94+
if (ifa->ifa_flags & IFF_MULTICAST) {
95+
tinyprint(1, " IFF_MULTICAST", NULL);
96+
}
97+
if (ifa->ifa_flags & IFF_ALLMULTI) {
98+
tinyprint(1, " IFF_ALLMULTI", NULL);
99+
}
100+
if (ifa->ifa_flags & IFF_NOARP) {
101+
tinyprint(1, " IFF_NOARP", NULL);
102+
}
103+
if (ifa->ifa_flags & IFF_PROMISC) {
104+
tinyprint(1, " IFF_PROMISC", NULL);
105+
}
106+
tinyprint(1, "\n", NULL);
107+
108+
tinyprint(1, "\n", NULL);
109+
}
110+
111+
// perform cleanup
112+
freeifaddrs(ifaddrs);
113+
114+
// report success
115+
exit(0);
116+
}

0 commit comments

Comments
 (0)