Skip to content

Commit

Permalink
[libtap] BSD: switch to ioctl to destroy tap devices
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Nov 28, 2017
1 parent fa6375d commit 5a80dd0
Showing 1 changed file with 17 additions and 37 deletions.
54 changes: 17 additions & 37 deletions libtap/libtap.c
Expand Up @@ -248,27 +248,17 @@ static int _check(const tap_t tap)

static void _close(tap_t tap)
{
#ifdef KNET_BSD
char command[PATH_MAX];
char *error_string = NULL;
#endif

if (!tap)
return;

if (tap->fd)
close(tap->fd);

#ifdef KNET_BSD
memset(command, 0, PATH_MAX);

snprintf(command, PATH_MAX, "ifconfig %s destroy", tap->tapname);
memset(&tap->ifr, 0, sizeof(struct ifreq));
strncpy(tap->ifname, tap->tapname, IFNAMSIZ);

_execute_shell(command, &error_string);
if (error_string) {
free(error_string);
error_string = NULL;
}
ioctl(lib_cfg.sockfd, SIOCIFDESTROY, &tap->ifr);
#endif

free(tap);
Expand Down Expand Up @@ -465,6 +455,20 @@ tap_t tap_open(char *dev, size_t dev_size, const char *updownpath)

pthread_mutex_lock(&lib_mutex);

if (!lib_init) {
lib_cfg.head = NULL;
#ifdef KNET_LINUX
lib_cfg.sockfd = socket(AF_INET, SOCK_STREAM, 0);
#endif
#ifdef KNET_BSD
lib_cfg.sockfd = socket(AF_LOCAL, SOCK_DGRAM, 0);
#endif
if (lib_cfg.sockfd < 0)
goto out_error;
lib_init = 1;
}


#ifdef KNET_BSD
if (!strlen(dev)) {
for (i = 0; i < 256; i++) {
Expand Down Expand Up @@ -508,30 +512,6 @@ tap_t tap_open(char *dev, size_t dev_size, const char *updownpath)
strncpy(tap->tapname, tap->ifname, IFNAMSIZ);
#endif

if (!lib_init) {
lib_cfg.head = NULL;
#ifdef KNET_LINUX
lib_cfg.sockfd = socket(AF_INET, SOCK_STREAM, 0);
#endif
#ifdef KNET_BSD
lib_cfg.sockfd = socket(AF_LOCAL, SOCK_DGRAM, 0);
#endif
if (lib_cfg.sockfd < 0)
goto out_error;
lib_init = 1;
}

memset(&tap->ifr, 0, sizeof(struct ifreq));
strncpy(tap->ifname, tap->tapname, IFNAMSIZ);
#ifdef KNET_LINUX
if (ioctl(lib_cfg.sockfd, SIOGIFINDEX, &tap->ifr) < 0)
goto out_error;
#endif
#ifdef KNET_BSD
if (ioctl(lib_cfg.sockfd, SIOCGIFINDEX, &tap->ifr) < 0)
goto out_error;
#endif

tap->default_mtu = _get_mtu(tap);
if (tap->default_mtu < 0)
goto out_error;
Expand Down

0 comments on commit 5a80dd0

Please sign in to comment.