Skip to content

Commit 082ee66

Browse files
committed
Merge from mysql-5.6 to mysql-trunk
2 parents c2be0f9 + 51db32b commit 082ee66

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

mysys/my_gethwaddr.c

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,61 @@ my_bool my_gethwaddr(uchar *to)
6868
#include <sys/ioctl.h>
6969
#include <net/ethernet.h>
7070

71+
#define MAX_IFS 64
72+
7173
my_bool my_gethwaddr(uchar *to)
7274
{
73-
int fd, res= 1;
75+
int fd= -1;
76+
int res= 1;
7477
struct ifreq ifr;
78+
struct ifreq ifs[MAX_IFS];
79+
struct ifreq *ifri= NULL;
80+
struct ifreq *ifend= NULL;
81+
7582
char zero_array[ETHER_ADDR_LEN] = {0};
83+
struct ifconf ifc;
7684

7785
fd = socket(AF_INET, SOCK_DGRAM, 0);
7886
if (fd < 0)
79-
goto err;
87+
return 1;
88+
89+
/* Retrieve interfaces */
90+
ifc.ifc_len= sizeof(ifs);
91+
ifc.ifc_req= ifs;
92+
if (ioctl(fd, SIOCGIFCONF, &ifc) < 0)
93+
{
94+
close(fd);
95+
return 1;
96+
}
97+
98+
/* Initialize out parameter */
99+
memcpy(to, zero_array, ETHER_ADDR_LEN);
80100

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

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

113+
/* Get HW address, break if not 0 */
114+
if (ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0)
115+
{
116+
memcpy(to, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
117+
if (memcmp(to, zero_array, ETHER_ADDR_LEN))
118+
{
119+
res= 0;
120+
break;
121+
}
122+
}
123+
}
124+
}
93125
close(fd);
94-
err:
95126
return res;
96127
}
97128

0 commit comments

Comments
 (0)