From: Steven Barth Date: Thu, 17 Jan 2013 07:53:48 +0000 (+0100) Subject: Detect a suitable MAC if the main interface doesn't have one (e.g. ppp) X-Git-Tag: debian/1.1+git20160131-1~200 X-Git-Url: https://git.decadent.org.uk/gitweb/?a=commitdiff_plain;h=43a052c9a684c1475aca444722fbefd127325aa7;p=odhcp6c.git Detect a suitable MAC if the main interface doesn't have one (e.g. ppp) --- diff --git a/src/dhcpv6.c b/src/dhcpv6.c index 9cb55ad..a3d4223 100644 --- a/src/dhcpv6.c +++ b/src/dhcpv6.c @@ -110,6 +110,27 @@ int init_dhcpv6(const char *ifname, int request_pd) uint8_t duid[14] = {0, DHCPV6_OPT_CLIENTID, 0, 10, 0, DHCPV6_DUID_LLADDR, 0, 1}; memcpy(&duid[8], ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN); + + uint8_t zero[ETHER_ADDR_LEN] = {0, 0, 0, 0, 0, 0}; + struct ifreq ifs[100], *ifp, *ifend; + struct ifconf ifc; + ifc.ifc_req = ifs; + ifc.ifc_len = sizeof(ifs); + + if (!memcmp(&duid[8], zero, ETHER_ADDR_LEN) && + ioctl(sock, SIOCGIFCONF, &ifc) >= 0) { + // If our interface doesn't have an address... + ifend = ifs + (ifc.ifc_len / sizeof(struct ifreq)); + for (ifp = ifc.ifc_req; ifp < ifend && + !memcmp(&duid[8], zero, 6); ifp++) { + memcpy(ifr.ifr_name, ifp->ifr_name, + sizeof(ifr.ifr_name)); + ioctl(sock, SIOCGIFHWADDR, &ifr); + memcpy(&duid[8], ifr.ifr_hwaddr.sa_data, + ETHER_ADDR_LEN); + } + } + odhcp6c_add_state(STATE_CLIENT_ID, duid, sizeof(duid)); }