From 02173b83521deff47c9cf07c906905cf787fc270 Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Thu, 13 May 2021 20:11:49 +0200 Subject: [PATCH] Fix iteration of utun devices on macos Pass the constructed name instead of NULL to open_utun Also add some error handling to utun_unit. --- src/tun.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/tun.c b/src/tun.c index 2ac3b2d..a6b65b8 100644 --- a/src/tun.c +++ b/src/tun.c @@ -341,6 +341,9 @@ utun_unit(const char *dev) const char *unit_str = dev; int unit = 0; + if (!dev) + return -1; + while (*unit_str != '\0' && !isdigit(*unit_str)) unit_str++; @@ -383,6 +386,10 @@ open_utun(const char *dev) addr.ss_sysaddr = AF_SYS_CONTROL; addr.sc_id = info.ctl_id; addr.sc_unit = utun_unit(dev); + if (addr.sc_unit < 0) { + close(fd); + return -1; + } err = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); if (err != 0) { @@ -457,7 +464,7 @@ open_tun(const char *tun_device) fprintf(stderr, "No tun devices found, trying utun\n"); for (i = 0; i < TUN_MAX_TRY; i++) { snprintf(tun_name, sizeof(tun_name), "utun%d", i); - tun_fd = open_utun(tun_device); + tun_fd = open_utun(tun_name); if (tun_fd >= 0) { return tun_fd; }