コード例 #1
0
void EnvironmentImpl::nodeIdImpl(NodeId& id)
{
	std::memset(&id, 0, sizeof(id));
	int ifIndex = 0;
	char ifName[32];
	char ifAddr[4];

	int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (s == -1) return;

	for (;;)
	{
		if (ifIndexToIfName(ifIndex, ifName) == OK)
		{
			if (ifAddrGet(ifName, ifAddr) == OK)
			{			
				struct arpreq ar;
				std::memset(&ar, 0, sizeof(ar));
				struct sockaddr_in* pAddr = reinterpret_cast<struct sockaddr_in*>(&ar.arp_pa);
				pAddr->sin_family = AF_INET;
				std::memcpy(&pAddr->sin_addr, ifAddr, sizeof(struct in_addr));
				int rc = ioctl(s, SIOCGARP, reinterpret_cast<int>(&ar));
				if (rc < 0) continue;
				std::memcpy(&id, ar.arp_ha.sa_data, sizeof(id));
				close(s);
				return;
			}
		}
		else break;	
	}
	close(s);
	throw SystemException("cannot get Ethernet hardware address");
}
コード例 #2
0
void EnvironmentImpl::nodeIdImpl(NodeId& id)
{
	std::memset(&id, 0, sizeof(id));

	int ifIndex = 1;
	char ifName[32];
	for (;;)
	{
		if (ifIndexToIfName(ifIndex, ifName) == OK)
		{
			struct ifnet* pIf = ifunit(ifName);
			if (pIf)
			{
				std::memcpy(&id, ((struct arpcom *) pIf)->ac_enaddr, sizeof(id));
				return;
			}
		}
		else break;	
		++ifIndex;
	}
	throw SystemException("cannot get Ethernet hardware address");
}