aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

commit 1b2acd131d2a7fb8eff689946d9c0dbc6b201f58
parent 5dddfab3c2cc868e6fcdf2495916340ed2861301
Author: xplshn <anto@xplshn.com.ar>
Date:   Tue, 24 Nov 2026 14:33:05 -0300

net: tun devices, tunctl over them

Diffstat:
Mcmd/linux/tunctl.c | 30++++++++++--------------------
Mshared/libutil/net_linux.c | 35+++++++++++++++++++++++++++++++++++
Mshared/libutil/net_other.c | 36++++++++++++++++++++++++++++++++++++
Mshared/net.h | 6++++++
4 files changed, 87 insertions(+), 20 deletions(-)

diff --git a/cmd/linux/tunctl.c b/cmd/linux/tunctl.c @@ -1,11 +1,7 @@ /* see LICENSE file for copyright and license details */ -#include <sys/ioctl.h> -#include <sys/socket.h> -#include <sys/stat.h> #include <sys/types.h> -#include <fcntl.h> #include <limits.h> #include <pwd.h> #include <stdio.h> @@ -13,10 +9,7 @@ #include <string.h> #include <unistd.h> -#include <linux/if.h> -#include <linux/if_tun.h> - -#include "paths.h" +#include "net.h" #include "util.h" static int dflag; @@ -36,7 +29,7 @@ usage(void) int main(int argc, char *argv[]) { - struct ifreq ifr; + char name[NET_IFNAME_MAX] = ""; struct passwd *pw; uid_t owner_uid = 0; int fd; @@ -74,31 +67,28 @@ main(int argc, char *argv[]) owner_uid = pw->pw_uid; } - fd = open("/dev/net/tun", O_RDWR); + fd = net_tun_open(); if (fd < 0) eprintf("open /dev/net/tun:"); - memset(&ifr, 0, sizeof(ifr)); - ifr.ifr_flags = (Tflag ? IFF_TAP : IFF_TUN) | IFF_NO_PI; - if (argc > 0) - estrlcpy(ifr.ifr_name, argv[0], sizeof(ifr.ifr_name)); + estrlcpy(name, argv[0], sizeof(name)); - if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) + if (net_tun_attach(fd, name, sizeof(name), Tflag) < 0) eprintf("ioctl TUNSETIFF:"); if (tflag) { - if (ioctl(fd, TUNSETPERSIST, (void *)1) < 0) + if (net_tun_persist(fd, 1) < 0) eprintf("ioctl TUNSETPERSIST:"); if (owner) { - if (ioctl(fd, TUNSETOWNER, (void *)(long)owner_uid) < 0) + if (net_tun_owner(fd, owner_uid) < 0) eprintf("ioctl TUNSETOWNER:"); } - printf("Set '%s' persistent and owned by %u\n", ifr.ifr_name, owner_uid); + printf("Set '%s' persistent and owned by %u\n", name, owner_uid); } else if (dflag) { - if (ioctl(fd, TUNSETPERSIST, (void *)0) < 0) + if (net_tun_persist(fd, 0) < 0) eprintf("ioctl TUNSETPERSIST:"); - printf("Set '%s' non-persistent\n", ifr.ifr_name); + printf("Set '%s' non-persistent\n", name); } close(fd); diff --git a/shared/libutil/net_linux.c b/shared/libutil/net_linux.c @@ -9,7 +9,9 @@ #include <arpa/inet.h> #include <ctype.h> #include <errno.h> +#include <fcntl.h> #include <ifaddrs.h> +#include <linux/if_tun.h> #include <net/if.h> #include <net/route.h> #include <netpacket/packet.h> @@ -660,4 +662,37 @@ net_set_name(const char *name, const char *newname) close(sock); return 0; } + +int +net_tun_open(void) +{ + return open("/dev/net/tun", O_RDWR); +} + +/* name is the one to ask for, or empty, and comes back as the one given */ +int +net_tun_attach(int fd, char *name, size_t namesz, int tap) +{ + struct ifreq ifr; + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_flags = (tap ? IFF_TAP : IFF_TUN) | IFF_NO_PI; + strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); + if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) + return -1; + strlcpy(name, ifr.ifr_name, namesz); + return 0; +} + +int +net_tun_persist(int fd, int on) +{ + return ioctl(fd, TUNSETPERSIST, (void *)(long)on) < 0 ? -1 : 0; +} + +int +net_tun_owner(int fd, uid_t uid) +{ + return ioctl(fd, TUNSETOWNER, (void *)(long)uid) < 0 ? -1 : 0; +} #endif diff --git a/shared/libutil/net_other.c b/shared/libutil/net_other.c @@ -171,4 +171,40 @@ net_set_name(const char *name, const char *newname) errno = ENOTSUP; return -1; } + +int +net_tun_open(void) +{ + errno = ENOTSUP; + return -1; +} + +int +net_tun_attach(int fd, char *name, size_t namesz, int tap) +{ + (void)fd; + (void)name; + (void)namesz; + (void)tap; + errno = ENOTSUP; + return -1; +} + +int +net_tun_persist(int fd, int on) +{ + (void)fd; + (void)on; + errno = ENOTSUP; + return -1; +} + +int +net_tun_owner(int fd, uid_t uid) +{ + (void)fd; + (void)uid; + errno = ENOTSUP; + return -1; +} #endif diff --git a/shared/net.h b/shared/net.h @@ -2,6 +2,8 @@ #define NET_H #include <netinet/in.h> +#include <stddef.h> +#include <sys/types.h> #define NET_IFNAME_MAX 16 #define NET_ROUTE_ADDRSTRLEN 16 @@ -74,5 +76,9 @@ int net_del_route(const char *, const char *, const char *, int, int); int net_flush_addrs(const char *); int net_set_txqueuelen(const char *, int); int net_set_name(const char *, const char *); +int net_tun_open(void); +int net_tun_attach(int, char *, size_t, int); +int net_tun_persist(int, int); +int net_tun_owner(int, uid_t); #endif