Skip to content

Commit

Permalink
[nozzle] cleanup nozzle_open coding style and return codes
Browse files Browse the repository at this point in the history
also update the header file for Doxygen man pages

Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Dec 16, 2017
1 parent e878778 commit 84e02db
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 30 deletions.
77 changes: 48 additions & 29 deletions libnozzle/libnozzle.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,27 +384,28 @@ nozzle_t nozzle_find(char *dev, size_t dev_size)
return nozzle;
}

nozzle_t nozzle_open(char *dev, size_t dev_size, const char *updownpath)
nozzle_t nozzle_open(char *devname, size_t devname_size, const char *updownpath)
{
nozzle_t nozzle;
int savederrno = 0;
nozzle_t nozzle = NULL;
char *temp_mac = NULL;
#ifdef KNET_BSD
uint16_t i;
long int nozzlenum = 0;
char curnozzle[IFNAMSIZ];
#endif

if (dev == NULL) {
if (devname == NULL) {
errno = EINVAL;
return NULL;
}

if (dev_size < IFNAMSIZ) {
if (devname_size < IFNAMSIZ) {
errno = EINVAL;
return NULL;
}

if (strlen(dev) > IFNAMSIZ) {
if (strlen(devname) > IFNAMSIZ) {
errno = E2BIG;
return NULL;
}
Expand All @@ -415,13 +416,13 @@ nozzle_t nozzle_open(char *dev, size_t dev_size, const char *updownpath)
* but it is possible to force a nozzleX device number
* where X is 0 to 255.
*/
if (strlen(dev)) {
if (strncmp(dev, "tap", 3)) {
if (strlen(devname)) {
if (strncmp(devname, "tap", 3)) {
errno = EINVAL;
return NULL;
}
errno = 0;
nozzlenum = strtol(dev+3, NULL, 10);
nozzlenum = strtol(devname+3, NULL, 10);
if (errno) {
errno = EINVAL;
return NULL;
Expand All @@ -440,21 +441,18 @@ nozzle_t nozzle_open(char *dev, size_t dev_size, const char *updownpath)
return NULL;
}
/* 14: 2 for /, 1 for \0 + 11 (post-down.d) */
if (strlen(updownpath) >= (PATH_MAX - (strlen(dev) + 14))) {
if (strlen(updownpath) >= (PATH_MAX - (strlen(devname) + 14))) {
errno = E2BIG;
return NULL;
}
}

nozzle = malloc(sizeof(struct nozzle_iface));
if (!nozzle) {
savederrno = pthread_mutex_lock(&lib_mutex);
if (savederrno) {
errno = savederrno;
return NULL;
}

memset(nozzle, 0, sizeof(struct nozzle_iface));

pthread_mutex_lock(&lib_mutex);

if (!lib_init) {
lib_cfg.head = NULL;
#ifdef KNET_LINUX
Expand All @@ -463,61 +461,80 @@ nozzle_t nozzle_open(char *dev, size_t dev_size, const char *updownpath)
#ifdef KNET_BSD
lib_cfg.sockfd = socket(AF_LOCAL, SOCK_DGRAM, 0);
#endif
if (lib_cfg.sockfd < 0)
if (lib_cfg.sockfd < 0) {
savederrno = errno;
goto out_error;
}
lib_init = 1;
}

nozzle = malloc(sizeof(struct nozzle_iface));
if (!nozzle) {
savederrno = ENOMEM;
goto out_error;
}

memset(nozzle, 0, sizeof(struct nozzle_iface));

#ifdef KNET_BSD
if (!strlen(dev)) {
if (!strlen(devname)) {
for (i = 0; i < 256; i++) {
snprintf(curnozzle, sizeof(curnozzle) - 1, "/dev/nozzle%u", i);
snprintf(curnozzle, sizeof(curnozzle) - 1, "/dev/tap%u", i);
nozzle->fd = open(curnozzle, O_RDWR);
savederrno = errno;
if (nozzle->fd > 0) {
break;
}
}
snprintf(curnozzle, sizeof(curnozzle) -1 , "nozzle%u", i);
snprintf(curnozzle, sizeof(curnozzle) -1 , "tap%u", i);
} else {
snprintf(curnozzle, sizeof(curnozzle) - 1, "/dev/%s", dev);
snprintf(curnozzle, sizeof(curnozzle) - 1, "/dev/%s", devname);
nozzle->fd = open(curnozzle, O_RDWR);
snprintf(curnozzle, sizeof(curnozzle) - 1, "%s", dev);
savederrno = errno;
snprintf(curnozzle, sizeof(curnozzle) - 1, "%s", devname);
}
if (nozzle->fd < 0) {
errno = EBUSY;
goto out_error;
}
strncpy(dev, curnozzle, IFNAMSIZ);
strncpy(devname, curnozzle, IFNAMSIZ);
strncpy(nozzle->nozzlename, curnozzle, IFNAMSIZ);
#endif

#ifdef KNET_LINUX
if ((nozzle->fd = open("/dev/net/tun", O_RDWR)) < 0)
if ((nozzle->fd = open("/dev/net/tun", O_RDWR)) < 0) {
savederrno = errno;
goto out_error;
}

memset(&nozzle->ifr, 0, sizeof(struct ifreq));
strncpy(nozzle->ifname, dev, IFNAMSIZ);
strncpy(nozzle->ifname, devname, IFNAMSIZ);
nozzle->ifr.ifr_flags = IFF_TAP | IFF_NO_PI;

if (ioctl(nozzle->fd, TUNSETIFF, &nozzle->ifr) < 0)
if (ioctl(nozzle->fd, TUNSETIFF, &nozzle->ifr) < 0) {
savederrno = errno;
goto out_error;
}

if ((strlen(dev) > 0) && (strcmp(dev, nozzle->ifname) != 0)) {
if ((strlen(devname) > 0) && (strcmp(devname, nozzle->ifname) != 0)) {
errno = EBUSY;
goto out_error;
}

strncpy(dev, nozzle->ifname, IFNAMSIZ);
strncpy(devname, nozzle->ifname, IFNAMSIZ);
strncpy(nozzle->nozzlename, nozzle->ifname, IFNAMSIZ);
#endif

nozzle->default_mtu = _get_mtu(nozzle);
if (nozzle->default_mtu < 0)
if (nozzle->default_mtu < 0) {
savederrno = errno;
goto out_error;
}

if (_get_mac(nozzle, &temp_mac) < 0)
if (_get_mac(nozzle, &temp_mac) < 0) {
savederrno = errno;
goto out_error;
}

strncpy(nozzle->default_mac, temp_mac, 18);
free(temp_mac);
Expand All @@ -536,12 +553,14 @@ nozzle_t nozzle_open(char *dev, size_t dev_size, const char *updownpath)
lib_cfg.head = nozzle;

pthread_mutex_unlock(&lib_mutex);
errno = savederrno;
return nozzle;

out_error:
_close(nozzle);
_close_cfg();
pthread_mutex_unlock(&lib_mutex);
errno = savederrno;
return NULL;
}

Expand Down
31 changes: 30 additions & 1 deletion libnozzle/libnozzle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,36 @@

typedef struct nozzle_iface *nozzle_t;

nozzle_t nozzle_open(char *dev, size_t dev_size, const char *updownpath);
/**
* nozzle_open
* @brief create a new tap device on the system.
*
* devname - pointer to device name of at least size IFNAMSIZ.
* if the dev strlen is 0, then the system will assign a name automatically.
* if a string is specified, the system will try to create a device with
* the specified name.
* NOTE: on FreeBSD the tap device names can only be tapX where X is a
* number from 0 to 255. On Linux such limitation does not apply.
* The name must be unique to the system. If an interface with the same
* name is already configured on the system, an error will be returned.
*
* devname_size - lenght of the buffer provided in dev (has to be at least IFNAMSIZ).
*
* updownpath - nozzle supports the typical filesystem structure to execute
* actions for: down.d post-down.d pre-up.d up.d
* in the form of:
* updownpath/<action>/<interface_name>
* updownpath specifies where to find those directories on the
* filesystem and it must be an absolute path.
*
* @return
* nozzle_open returns
* a pointer to a nozzle struct on success
* NULL on error and errno is set.
*/

nozzle_t nozzle_open(char *devname, size_t devname_size, const char *updownpath);

int nozzle_close(nozzle_t nozzle);

nozzle_t nozzle_find(char *dev, size_t dev_size);
Expand Down

0 comments on commit 84e02db

Please sign in to comment.