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;
#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
* 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;
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;
}
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;
out_clean:
pthread_mutex_unlock(&lib_mutex);
-
+ errno = savederrno;
return err;
}
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);