mirror of
				https://github.com/pocoproject/poco.git
				synced 2025-10-25 18:22:59 +02:00 
			
		
		
		
	GH #147: NetworkInterface (linux): MAC address not always available
This commit is contained in:
		| @@ -1277,7 +1277,7 @@ NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly) | |||||||
| 				ifIndex = sdl->sdl_index; | 				ifIndex = sdl->sdl_index; | ||||||
| 				intf = NetworkInterface(ifIndex); | 				intf = NetworkInterface(ifIndex); | ||||||
| 				setInterfaceParams(currIface, intf.impl()); | 				setInterfaceParams(currIface, intf.impl()); | ||||||
| 				if ((result.find(ifIndex) == result.end()) && ((upOnly && intf.isUp()) || !upOnly) && !ipOnly) | 				if ((result.find(ifIndex) == result.end()) && ((upOnly && intf.isUp()) || !upOnly)) | ||||||
| 					ifIt = result.insert(Map::value_type(ifIndex, intf)).first; | 					ifIt = result.insert(Map::value_type(ifIndex, intf)).first; | ||||||
| 				break; | 				break; | ||||||
| 			} | 			} | ||||||
| @@ -1340,6 +1340,18 @@ NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly) | |||||||
| 	} | 	} | ||||||
| 	if (ifaces) freeifaddrs(ifaces); | 	if (ifaces) freeifaddrs(ifaces); | ||||||
|  |  | ||||||
|  | 	if (ipOnly) | ||||||
|  | 	{ | ||||||
|  | 		Map::iterator it = result.begin(); | ||||||
|  | 		Map::iterator end = result.end(); | ||||||
|  | 		for (; it != end;) | ||||||
|  | 		{ | ||||||
|  | 			if (!it->second.supportsIPv4() && !it->second.supportsIPv6()) | ||||||
|  | 				result.erase(it++); | ||||||
|  | 			else ++it; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
| 	return result; | 	return result; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -1437,7 +1449,7 @@ NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly) | |||||||
| 				intf = NetworkInterface(ifIndex); | 				intf = NetworkInterface(ifIndex); | ||||||
| 				setInterfaceParams(iface, intf.impl()); | 				setInterfaceParams(iface, intf.impl()); | ||||||
| 				 | 				 | ||||||
| 				if ((result.find(ifIndex) == result.end()) && ((upOnly && intf.isUp()) || !upOnly) && !ipOnly) | 				if ((result.find(ifIndex) == result.end()) && ((upOnly && intf.isUp()) || !upOnly)) | ||||||
| 					ifIt = result.insert(Map::value_type(ifIndex, intf)).first; | 					ifIt = result.insert(Map::value_type(ifIndex, intf)).first; | ||||||
| 				 | 				 | ||||||
| 				break; | 				break; | ||||||
| @@ -1505,6 +1517,18 @@ NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly) | |||||||
| 	 | 	 | ||||||
| 	if (ifaces) freeifaddrs(ifaces); | 	if (ifaces) freeifaddrs(ifaces); | ||||||
|  |  | ||||||
|  | 	if (ipOnly) | ||||||
|  | 	{ | ||||||
|  | 		Map::iterator it = result.begin(); | ||||||
|  | 		Map::iterator end = result.end(); | ||||||
|  | 		for (; it != end;) | ||||||
|  | 		{ | ||||||
|  | 			if (!it->second.supportsIPv4() && !it->second.supportsIPv6()) | ||||||
|  | 				result.erase(it++); | ||||||
|  | 			else ++it; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	return result; | 	return result; | ||||||
| #else | #else | ||||||
| 	throw Poco::NotImplementedException("Not implemented in Android"); | 	throw Poco::NotImplementedException("Not implemented in Android"); | ||||||
|   | |||||||
| @@ -194,9 +194,16 @@ void NetworkInterfaceTest::testMapIpOnly() | |||||||
| { | { | ||||||
| 	NetworkInterface::Map m = NetworkInterface::map(true, false); | 	NetworkInterface::Map m = NetworkInterface::map(true, false); | ||||||
| 	assert (!m.empty()); | 	assert (!m.empty()); | ||||||
|  |  | ||||||
|  | 	std::cout << std::endl; | ||||||
| 	for (NetworkInterface::Map::const_iterator it = m.begin(); it != m.end(); ++it) | 	for (NetworkInterface::Map::const_iterator it = m.begin(); it != m.end(); ++it) | ||||||
| 	{ | 	{ | ||||||
| 		assert(it->second.supportsIPv4() || it->second.supportsIPv6()); | 		assert(it->second.supportsIPv4() || it->second.supportsIPv6()); | ||||||
|  | 		std::cout << "Interface: (" << it->second.index() << ")" << std::endl; | ||||||
|  | 		std::cout << "Address:    " << it->second.address().toString() << std::endl; | ||||||
|  | 		NetworkInterface::MACAddress mac(it->second.macAddress()); | ||||||
|  | 		if (!mac.empty() && (it->second.type() != NetworkInterface::NI_TYPE_SOFTWARE_LOOPBACK)) | ||||||
|  | 			std::cout << "MAC Address:" << mac << std::endl; | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Alex
					Alex