]> git.proxmox.com Git - mirror_kronosnet.git/commitdiff
[nozzle] cleanup and document nozzle_get_mac
authorFabio M. Di Nitto <fdinitto@redhat.com>
Mon, 20 Nov 2017 05:21:55 +0000 (06:21 +0100)
committerFabio M. Di Nitto <fdinitto@redhat.com>
Tue, 18 Dec 2018 04:55:08 +0000 (05:55 +0100)
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
libnozzle/libnozzle.c
libnozzle/libnozzle.h

index 1a5e82f088eb0d7617c6b41aa0ca8314740b26aa..fefb04002e5e4558e795ff3291f5c08ea354dce4 100644 (file)
@@ -296,7 +296,7 @@ out_clean:
 
 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;
@@ -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
@@ -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;
 
@@ -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;
 }
 
@@ -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;
@@ -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;
 }
 
index f1e53671f278ec0fcc9469d64e2c0069893e44ab..5f75cb064e0bf8f882e207e57b16931afef061db 100644 (file)
@@ -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);