Browse Source

1

nodeinfo-routing-update
Evgeny 7 months ago
parent
commit
ea9d6d8870
  1. 28
      src/tun_freebsd.c

28
src/tun_freebsd.c

@ -117,22 +117,22 @@ static int tun_set_ip(const char *ifname, const char *ip_addr) {
return 0; return 0;
} }
// Bring TUN interface up // Bring TUN interface up (using ifconfig - more reliable on FreeBSD)
static int tun_set_up(const char *ifname) { static int tun_set_up(const char *ifname) {
if (!ifname) { if (!ifname) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
struct ifreq ifr; // Use ifconfig to bring up the interface
memset(&ifr, 0, sizeof(ifr)); char cmd[256];
snprintf(cmd, sizeof(cmd), "ifconfig %s up 2>/dev/null", ifname);
// Get current flags
if (if_ioctl(ifname, SIOCGIFFLAGS, &ifr) < 0) return -1;
// Set UP and RUNNING int ret = system(cmd);
ifr.ifr_flags |= IFF_UP | IFF_RUNNING; if (ret != 0) {
if (if_ioctl(ifname, SIOCSIFFLAGS, &ifr) < 0) return -1; DEBUG_ERROR(DEBUG_CATEGORY_TUN, "ifconfig %s up failed", ifname);
return -1;
}
return 0; return 0;
} }
@ -164,15 +164,7 @@ int tun_platform_init(struct tun_if* tun, const char* ifname, const char* ip_str
return -1; return -1;
} }
// Bring interface UP first (required before setting IP on FreeBSD) // Configure IP address using ifconfig (also brings up the interface)
if (tun_set_up(tun->ifname) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to bring up TUN interface %s", tun->ifname);
close(tun->fd);
tun->fd = -1;
return -1;
}
// Configure IP address using ifconfig
if (tun_set_ip(tun->ifname, ip_str) < 0) { if (tun_set_ip(tun->ifname, ip_str) < 0) {
DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to set TUN IP: %s", ip_str); DEBUG_ERROR(DEBUG_CATEGORY_TUN, "Failed to set TUN IP: %s", ip_str);
close(tun->fd); close(tun->fd);

Loading…
Cancel
Save