Skip to content

Commit

Permalink
Merge from mysql-5.6 to mysql-trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
ssorumgard committed Oct 30, 2013
2 parents c2be0f9 + 51db32b commit 082ee66
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions mysys/my_gethwaddr.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,61 @@ my_bool my_gethwaddr(uchar *to)
#include <sys/ioctl.h>
#include <net/ethernet.h>

#define MAX_IFS 64

my_bool my_gethwaddr(uchar *to)
{
int fd, res= 1;
int fd= -1;
int res= 1;
struct ifreq ifr;
struct ifreq ifs[MAX_IFS];
struct ifreq *ifri= NULL;
struct ifreq *ifend= NULL;

char zero_array[ETHER_ADDR_LEN] = {0};
struct ifconf ifc;

fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
goto err;
return 1;

/* Retrieve interfaces */
ifc.ifc_len= sizeof(ifs);
ifc.ifc_req= ifs;
if (ioctl(fd, SIOCGIFCONF, &ifc) < 0)
{
close(fd);
return 1;
}

/* Initialize out parameter */
memcpy(to, zero_array, ETHER_ADDR_LEN);

memset(&ifr, 0, sizeof(ifr));
my_stpncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name) - 1);
/* Calculate first address after array */
ifend= ifs + (ifc.ifc_len / sizeof(struct ifreq));

do
/* Loop over all interfaces */
for (ifri= ifc.ifc_req; ifri < ifend; ifri++)
{
if (ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0)
if (ifri->ifr_addr.sa_family == AF_INET)
{
memcpy(to, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
res= memcmp(to, zero_array, ETHER_ADDR_LEN) ? 0 : 1;
}
} while (res && (errno == 0 || errno == ENODEV) && ifr.ifr_name[3]++ < '6');
/* Reset struct, copy interface name */
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, ifri->ifr_name, sizeof(ifr.ifr_name));

/* Get HW address, break if not 0 */
if (ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0)
{
memcpy(to, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
if (memcmp(to, zero_array, ETHER_ADDR_LEN))
{
res= 0;
break;
}
}
}
}
close(fd);
err:
return res;
}

Expand Down

0 comments on commit 082ee66

Please sign in to comment.