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.
This commit is contained in:
Erik Ekman 2021-05-13 20:11:49 +02:00
parent 25e4caa105
commit 02173b8352
1 changed files with 8 additions and 1 deletions

View File

@ -341,6 +341,9 @@ utun_unit(const char *dev)
const char *unit_str = dev; const char *unit_str = dev;
int unit = 0; int unit = 0;
if (!dev)
return -1;
while (*unit_str != '\0' && !isdigit(*unit_str)) while (*unit_str != '\0' && !isdigit(*unit_str))
unit_str++; unit_str++;
@ -383,6 +386,10 @@ open_utun(const char *dev)
addr.ss_sysaddr = AF_SYS_CONTROL; addr.ss_sysaddr = AF_SYS_CONTROL;
addr.sc_id = info.ctl_id; addr.sc_id = info.ctl_id;
addr.sc_unit = utun_unit(dev); addr.sc_unit = utun_unit(dev);
if (addr.sc_unit < 0) {
close(fd);
return -1;
}
err = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); err = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
if (err != 0) { if (err != 0) {
@ -457,7 +464,7 @@ open_tun(const char *tun_device)
fprintf(stderr, "No tun devices found, trying utun\n"); fprintf(stderr, "No tun devices found, trying utun\n");
for (i = 0; i < TUN_MAX_TRY; i++) { for (i = 0; i < TUN_MAX_TRY; i++) {
snprintf(tun_name, sizeof(tun_name), "utun%d", 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) { if (tun_fd >= 0) {
return tun_fd; return tun_fd;
} }