Skip to content

Commit

Permalink
[nozzle] cleanup and document nozzle_get_mac
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 21, 2017
1 parent c5477bb commit 25c3a21
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
37 changes: 27 additions & 10 deletions libnozzle/libnozzle.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ static int _get_mtu(const nozzle_t nozzle)

static int _get_mac(const nozzle_t nozzle, char **ether_addr)
{
int err = 0;
int err = 0, savederrno = 0;
char mac[MAX_MAC_CHAR];
#ifdef KNET_BSD
struct ifaddrs *ifap = NULL;
Expand All @@ -310,8 +310,10 @@ static int _get_mac(const nozzle_t nozzle, char **ether_addr)

#ifdef KNET_LINUX
err = ioctl(lib_cfg.sockfd, SIOCGIFHWADDR, &nozzle->ifr);
if (err)
if (err) {
savederrno = errno;
goto out_clean;
}

ether_ntoa_r((struct ether_addr *)nozzle->ifr.ifr_hwaddr.sa_data, mac);
#endif
Expand All @@ -322,8 +324,10 @@ static int _get_mac(const nozzle_t nozzle, char **ether_addr)
* https://lists.freebsd.org/pipermail/freebsd-hackers/2004-June/007394.html
*/
err = getifaddrs(&ifap);
if (err < 0)
if (err < 0) {
savederrno = errno;
goto out_clean;
}

ifa = ifap;

Expand All @@ -338,20 +342,24 @@ static int _get_mac(const nozzle_t nozzle, char **ether_addr)
if (found) {
ether_ntoa_r((struct ether_addr *)LLADDR((struct sockaddr_dl *)ifa->ifa_addr), mac);
} else {
errno = EINVAL;
err = -1;
}
freeifaddrs(ifap);

if (err)
if (err) {
goto out_clean;
}

#endif
*ether_addr = strdup(mac);
if (!*ether_addr)
if (!*ether_addr) {
savederrno = errno;
err = -1;
}

out_clean:

errno = savederrno;
return err;
}

Expand Down Expand Up @@ -715,11 +723,20 @@ int nozzle_reset_mtu(nozzle_t nozzle, char **error_string)

int nozzle_get_mac(const nozzle_t nozzle, char **ether_addr)
{
int err;
int err = 0, savederrno = 0;

pthread_mutex_lock(&lib_mutex);
if ((!nozzle) || (!ether_addr)) {
errno = EINVAL;
return -1;
}

if ((!_check(nozzle)) || (!ether_addr)) {
savederrno = pthread_mutex_lock(&lib_mutex);
if (savederrno) {
errno = savederrno;
return -1;
}

if (!_check(nozzle)) {
errno = EINVAL;
err = -1;
goto out_clean;
Expand All @@ -729,7 +746,7 @@ int nozzle_get_mac(const nozzle_t nozzle, char **ether_addr)

out_clean:
pthread_mutex_unlock(&lib_mutex);

errno = savederrno;
return err;
}

Expand Down
13 changes: 13 additions & 0 deletions libnozzle/libnozzle.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@ int nozzle_set_mtu(nozzle_t nozzle, const int mtu, char **error_string);

int nozzle_reset_mtu(nozzle_t nozzle, char **error_string);

/**
* nozzle_get_mac
* @brief retrive mac address on a given nozzle interface
*
* nozzle - pointer to the nozzle struct
*
* ether_addr - pointers to string containing the current mac address.
* The string is malloc'ed, the caller needs to free this buffer.
* @return
* 0 on success.
* -1 on error and errno is set.
*/

int nozzle_get_mac(const nozzle_t nozzle, char **ether_addr);
int nozzle_set_mac(nozzle_t nozzle, const char *ether_addr);
int nozzle_reset_mac(nozzle_t nozzle);
Expand Down

0 comments on commit 25c3a21

Please sign in to comment.