Esempio n. 1
0
 int readMacAddress(char *devName, u_char *buf, int bufLen) {
   // we can only call dlpi_open() with root privileges.  So only try to get
   // the MAC address if we are still root.  We could cache the dlpi handle
   // for each interface as another field in the adaptorNIO structure, but in
   // practice it seems unlikely that the MAC address will change without
   // a reboot of the host,  so it's OK to only read it at the start.  If a
   // new interface somehow appears then it will just be instantiated without
   // a MAC.
   if(getuid() != 0) {
     return NO;
   }
   
   int macaddrlen = DLPI_PHYSADDR_MAX;
   int copied = 0;
   char macaddr[DLPI_PHYSADDR_MAX];
   dlpi_handle_t dh;
   if (DLPI_SUCCESS != dlpi_open(devName, &dh, 0)) {
     myLog(LOG_ERR, "device %s dlpi_open failed : %s", devName, strerror(errno));
     return 0;
   }
   // opened OK
   if (DLPI_SUCCESS != dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR, macaddr, &macaddrlen)) {
     myLog(LOG_ERR, "device %s dlpi_get_physaddr failed :%s", devName, strerror(errno));
   }
   if(macaddrlen <= bufLen) {
     memcpy(buf, macaddr, macaddrlen);
     copied = macaddrlen;
   }
   dlpi_close(dh);
   return copied;
 }
Esempio n. 2
0
static void hwaddr_libdlpi_lookup(sigar_t *sigar, sigar_net_interface_config_t *ifconfig)
{
    dlpi_handle_t handle;
    dlpi_info_t linkinfo;
    uchar_t addr[DLPI_PHYSADDR_MAX];
    uint_t alen = sizeof(addr);

    if (dlpi_open(ifconfig->name, &handle, 0) != DLPI_SUCCESS) {
        return;
    }

    if (dlpi_get_physaddr(handle, DL_CURR_PHYS_ADDR, addr, &alen) == DLPI_SUCCESS &&
        dlpi_info(handle, &linkinfo, 0) == DLPI_SUCCESS) {
        if (alen < sizeof(ifconfig->hwaddr.addr.mac)) {
            sigar_net_address_mac_set(ifconfig->hwaddr, addr, alen);
            SIGAR_SSTRCPY(ifconfig->type, dlpi_mactype(linkinfo.di_mactype));
        }
    }

    dlpi_close(handle);
}
Esempio n. 3
0
static int eth_get(const char *device, u8 ea[ETH_ALEN])
{
#ifdef __sun__
	dlpi_handle_t dh;
	u32 physaddrlen = DLPI_PHYSADDR_MAX;
	u8 physaddr[DLPI_PHYSADDR_MAX];
	int retval;

	retval = dlpi_open(device, &dh, 0);
	if (retval != DLPI_SUCCESS) {
		wpa_printf(MSG_ERROR, "dlpi_open error: %s",
			   dlpi_strerror(retval));
		return -1;
	}

	retval = dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR, physaddr,
				   &physaddrlen);
	if (retval != DLPI_SUCCESS) {
		wpa_printf(MSG_ERROR, "dlpi_get_physaddr error: %s",
			   dlpi_strerror(retval));
		dlpi_close(dh);
		return -1;
	}
	os_memcpy(ea, physaddr, ETH_ALEN);
	dlpi_close(dh);
#else /* __sun__ */
	struct if_msghdr *ifm;
	struct sockaddr_dl *sdl;
	u_char *p, *buf;
	size_t len;
	int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };

	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
		return -1;
	if ((buf = os_malloc(len)) == NULL)
		return -1;
	if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
		os_free(buf);
		return -1;
	}
	for (p = buf; p < buf + len; p += ifm->ifm_msglen) {
		ifm = (struct if_msghdr *)p;
		sdl = (struct sockaddr_dl *)(ifm + 1);
		if (ifm->ifm_type != RTM_IFINFO ||
		    (ifm->ifm_addrs & RTA_IFP) == 0)
			continue;
		if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
		    os_memcmp(sdl->sdl_data, device, sdl->sdl_nlen) != 0)
			continue;
		os_memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
		break;
	}
	os_free(buf);

	if (p >= buf + len) {
		errno = ESRCH;
		return -1;
	}
#endif /* __sun__ */
	return 0;
}