当前位置: 首页>>代码示例>>C++>>正文


C++ EthernetInterface::addAddress方法代码示例

本文整理汇总了C++中EthernetInterface::addAddress方法的典型用法代码示例。如果您正苦于以下问题:C++ EthernetInterface::addAddress方法的具体用法?C++ EthernetInterface::addAddress怎么用?C++ EthernetInterface::addAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在EthernetInterface的用法示例。


在下文中一共展示了EthernetInterface::addAddress方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: getLocalInterfaceList

int getLocalInterfaceList(InterfaceRefList& iflist, const bool onlyUp) 
{
	IP_ADAPTER_ADDRESSES *ipAA, *ipAA_head;
	DWORD flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_INCLUDE_PREFIX;
	ULONG bufSize;
        int num = 0;

	// Figure out required buffer size
	DWORD iReturn = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, NULL, &bufSize);

	if (iReturn != ERROR_BUFFER_OVERFLOW) {
		return -1;
	}

	// Allocate the required buffer
	ipAA_head = (IP_ADAPTER_ADDRESSES *)malloc(bufSize);

	if (!ipAA_head) {
		HAGGLE_ERR("Could not allocate IP_ADAPTER_ADDRESSES buffer\n");
		return -1;
	}

	// Now, get the information for real
	iReturn = GetAdaptersAddresses(AF_UNSPEC, flags, NULL, ipAA_head, &bufSize);

	switch (iReturn) {
		case ERROR_SUCCESS:
			break;
		case ERROR_NO_DATA:
		case ERROR_BUFFER_OVERFLOW:
		case ERROR_INVALID_PARAMETER:
		default:
			HAGGLE_ERR("ERROR: %s\n", STRERROR(ERRNO));
			free(ipAA_head);
			return -1;
	}
	
	// Loop through all the adapters and their addresses
	for (ipAA = ipAA_head; ipAA; ipAA = ipAA->Next) {
		
		// Ignore interfaces that are not Ethernet or 802.11
		if (ipAA->IfType != IF_TYPE_IEEE80211 &&
			ipAA->IfType != IF_TYPE_FASTETHER &&
			ipAA->IfType != IF_TYPE_GIGABITETHERNET &&
			ipAA->IfType != IF_TYPE_ETHERNET_CSMACD) {
		
			//HAGGLE_DBG("Skipping interface of wrong type = %d\n", ipAA->IfType);
			continue;
		}

		// Ignore non operational interfaces
		if (onlyUp && ipAA->OperStatus != IfOperStatusUp) {
			//HAGGLE_DBG("Skipping non operational interface status=%d\n", ipAA->OperStatus);
			continue;
		}

		if (ipAA->Mtu > 1500) {
			// Some weird MTU. Some discovered "Ethernet" interfaces have a really high MTU.
			// These are probably virtual serial line interfaces enabled when synching the phone 
			// over, e.g., a USB cable.
			continue;
		}
		// Ok, this interface seems to be interesting
		unsigned char macaddr[ETH_MAC_LEN];
		memcpy(macaddr, ipAA->PhysicalAddress, ETH_MAC_LEN); 
		EthernetAddress addr(macaddr);

		EthernetInterface *ethIface = new EthernetInterface(macaddr, 
								    ipAA->AdapterName, &addr, 
								    IFFLAG_LOCAL | ((ipAA->OperStatus == IfOperStatusUp) ? IFFLAG_UP : 0));
		
		if (!ethIface)
			continue;
		
		/*	
		HAGGLE_DBG("LOCAL Interface type=%d index=%d name=%s mtu=%d mac=%s\n", 
			ipAA->IfType, ipAA->IfIndex, ipAA->AdapterName, ipAA->Mtu, mac.getAddrStr());
		*/	
		
		IP_ADAPTER_UNICAST_ADDRESS *ipAUA;
		IP_ADAPTER_PREFIX *ipAP;

		for (ipAUA = ipAA->FirstUnicastAddress, ipAP = ipAA->FirstPrefix; ipAUA && ipAP; ipAUA = ipAUA->Next, ipAP = ipAP->Next) {
			if (ipAUA->Address.lpSockaddr->sa_family == AF_INET) {
				struct in_addr bc;
				struct sockaddr_in *saddr_v4 = (struct sockaddr_in *)ipAUA->Address.lpSockaddr;
				DWORD mask = 0xFFFFFFFF;

				memcpy(&bc, &saddr_v4->sin_addr, sizeof(bc));

				// Create mask
				mask >>= (32 - ipAP->PrefixLength);
				
				// Create broadcast address
				bc.S_un.S_addr = saddr_v4->sin_addr.S_un.S_addr | ~mask;

				IPv4Address ipv4(*saddr_v4);
				ethIface->addAddress(ipv4);

				IPv4BroadcastAddress ipv4bc(bc);
//.........这里部分代码省略.........
开发者ID:eikoyoneki,项目名称:haggle-label,代码行数:101,代码来源:Utility.cpp


注:本文中的EthernetInterface::addAddress方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。