On all places is just needed hardware address for the first ethernet-style
interface. So provide it by new get_first_ether_hwaddr() function.
Signed-off-by: Pali Rohár <pali@kernel.org>
ether_to_eui64(eui64_t *p_eui64)
{
u_char addr[6];
ether_to_eui64(eui64_t *p_eui64)
{
u_char addr[6];
- char *if_name;
-
- if (get_if_hwaddr(addr, devnam) < 0) {
- if ((if_name = get_first_ethernet()) == NULL) {
- error("no persistent id can be found");
- return 0;
- }
-
- if (get_if_hwaddr(addr, if_name) < 0) {
- error("could not obtain hardware address for %s", if_name);
- return 0;
- }
+
+ if (get_if_hwaddr(addr, devnam) < 0 || get_first_ether_hwaddr(addr) < 0) {
+ error("ipv6cp: no persistent id can be found");
+ return 0;
static int
get_default_epdisc(struct epdisc *ep)
{
static int
get_default_epdisc(struct epdisc *ep)
{
struct hostent *hp;
u_int32_t addr;
/* First try for an ethernet MAC address */
struct hostent *hp;
u_int32_t addr;
/* First try for an ethernet MAC address */
- p = get_first_ethernet();
- if (p != 0 && get_if_hwaddr(ep->value, p) >= 0) {
+ if (get_first_ether_hwaddr(ep->value) >= 0) {
ep->class = EPD_MAC;
ep->length = 6;
return 1;
ep->class = EPD_MAC;
ep->length = 6;
return 1;
int cipxfaddr(int);
#endif
int get_if_hwaddr(u_char *addr, char *name);
int cipxfaddr(int);
#endif
int get_if_hwaddr(u_char *addr, char *name);
-char *get_first_ethernet(void);
+int get_first_ether_hwaddr(u_char *addr);
int get_time(struct timeval *);
/* Get current time, monotonic if possible. */
int get_time(struct timeval *);
/* Get current time, monotonic if possible. */
- * get_first_ethernet - return the name of the first ethernet-style
- * interface on this system.
+ * get_first_ether_hwaddr - get the hardware address for the first
+ * ethernet-style interface on this system.
-static char first_ether_name[IF_NAMESIZE];
-char *
-get_first_ethernet(void)
+int
+get_first_ether_hwaddr(u_char *addr)
{
struct if_nameindex *if_ni, *i;
struct ifreq ifreq;
{
struct if_nameindex *if_ni, *i;
struct ifreq ifreq;
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd < 0)
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd < 0)
if_ni = if_nameindex();
if (!if_ni) {
close(sock_fd);
if_ni = if_nameindex();
if (!if_ni) {
close(sock_fd);
- first_ether_name[0] = 0;
for (i = if_ni; !(i->if_index == 0 && i->if_name == NULL); i++) {
memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
strlcpy(ifreq.ifr_name, i->if_name, sizeof(ifreq.ifr_name));
ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
if (ret >= 0 && ifreq.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
for (i = if_ni; !(i->if_index == 0 && i->if_name == NULL); i++) {
memset(&ifreq.ifr_hwaddr, 0, sizeof(struct sockaddr));
strlcpy(ifreq.ifr_name, i->if_name, sizeof(ifreq.ifr_name));
ret = ioctl(sock_fd, SIOCGIFHWADDR, &ifreq);
if (ret >= 0 && ifreq.ifr_hwaddr.sa_family == ARPHRD_ETHER) {
- strlcpy(first_ether_name, i->if_name, sizeof(first_ether_name));
+ memcpy(addr, ifreq.ifr_hwaddr.sa_data, 6);
}
if_freenameindex(if_ni);
close(sock_fd);
}
if_freenameindex(if_ni);
close(sock_fd);
- if (!first_ether_name[0])
- return NULL;
-
- return first_ether_name;
}
/********************************************************************
}
/********************************************************************
#endif /* defined(INET6) && defined(SOL2) */
#endif /* defined(INET6) && defined(SOL2) */
-#if defined(INET6) && defined(SOL2)
-static char first_ether_name[LIFNAMSIZ]; /* Solaris 8 and above */
-#else
-static char first_ether_name[IFNAMSIZ]; /* Before Solaris 8 */
+#if !defined(INET6) || !defined(SOL2)
#define MAXIFS 256 /* Max # of interfaces */
#endif /* defined(INET6) && defined(SOL2) */
#define MAXIFS 256 /* Max # of interfaces */
#endif /* defined(INET6) && defined(SOL2) */
#if defined(SOL2) && defined(INET6)
/*
#if defined(SOL2) && defined(INET6)
/*
- * get_first_ethernet - returns the first Ethernet interface name found in
- * the system, or NULL if none is found
+ * get_first_ether_hwaddr - get the hardware address for the first
+ * ethernet-style interface on this system.
*
* NOTE: This is the lifreq version (Solaris 8 and above)
*/
*
* NOTE: This is the lifreq version (Solaris 8 and above)
*/
-char *
-get_first_ethernet(void)
+int
+get_first_ether_hwaddr(u_char *addr)
{
struct lifnum lifn;
struct lifconf lifc;
{
struct lifnum lifn;
struct lifconf lifc;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
if (ioctl(fd, SIOCGLIFNUM, &lifn) < 0) {
close(fd);
error("could not determine number of interfaces: %m");
if (ioctl(fd, SIOCGLIFNUM, &lifn) < 0) {
close(fd);
error("could not determine number of interfaces: %m");
}
num_ifs = lifn.lifn_count;
}
num_ifs = lifn.lifn_count;
if (req == NULL) {
close(fd);
error("out of memory");
if (req == NULL) {
close(fd);
error("out of memory");
close(fd);
free(req);
error("SIOCGLIFCONF: %m");
close(fd);
free(req);
error("SIOCGLIFCONF: %m");
memset(&lifr, 0, sizeof(lifr));
strncpy(lifr.lifr_name, plifreq->lifr_name, sizeof(lifr.lifr_name));
if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) {
memset(&lifr, 0, sizeof(lifr));
strncpy(lifr.lifr_name, plifreq->lifr_name, sizeof(lifr.lifr_name));
if (ioctl(fd, SIOCGLIFFLAGS, &lifr) < 0) {
- close(fd);
- free(req);
error("SIOCGLIFFLAGS: %m");
error("SIOCGLIFFLAGS: %m");
!= (IFF_UP | IFF_BROADCAST))
continue;
!= (IFF_UP | IFF_BROADCAST))
continue;
+ if (get_if_hwaddr(addr, lifr.lifr_name) < 0)
+ continue;
+
found = 1;
break;
}
free(req);
close(fd);
found = 1;
break;
}
free(req);
close(fd);
- if (found) {
- strncpy(first_ether_name, lifr.lifr_name, sizeof(first_ether_name));
- return (char *)first_ether_name;
- } else
- return NULL;
+ if (found)
+ return 0;
+ else
+ return -1;
- * get_first_ethernet - returns the first Ethernet interface name found in
- * the system, or NULL if none is found
+ * get_first_ether_hwaddr - get the hardware address for the first
+ * ethernet-style interface on this system.
*
* NOTE: This is the ifreq version (before Solaris 8).
*/
*
* NOTE: This is the ifreq version (before Solaris 8).
*/
-char *
-get_first_ethernet(void)
+int
+get_first_ether_hwaddr(u_char *addr)
{
struct ifconf ifc;
struct ifreq *pifreq;
{
struct ifconf ifc;
struct ifreq *pifreq;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
if (req == NULL) {
close(fd);
error("out of memory");
if (req == NULL) {
close(fd);
error("out of memory");
close(fd);
free(req);
error("SIOCGIFCONF: %m");
close(fd);
free(req);
error("SIOCGIFCONF: %m");
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, pifreq->ifr_name, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, pifreq->ifr_name, sizeof(ifr.ifr_name));
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
- close(fd);
- free(req);
error("SIOCGIFFLAGS: %m");
error("SIOCGIFFLAGS: %m");
!= (IFF_UP | IFF_BROADCAST))
continue;
!= (IFF_UP | IFF_BROADCAST))
continue;
+ if (get_if_hwaddr(addr, ifr.ifr_name) < 0)
+ continue;
+
found = 1;
break;
}
free(req);
close(fd);
found = 1;
break;
}
free(req);
close(fd);
- if (found) {
- strncpy(first_ether_name, ifr.ifr_name, sizeof(first_ether_name));
- return (char *)first_ether_name;
- } else
- return NULL;
+ if (found)
+ return 0;
+ else
+ return -1;
}
#endif /* defined(SOL2) && defined(INET6) */
}
#endif /* defined(SOL2) && defined(INET6) */