poco icon indicating copy to clipboard operation
poco copied to clipboard

TryParse scoped ipv6 addressess

Open diridari opened this issue 2 years ago • 2 comments

I would like to parse scoped ipv6 addressees.

Poco::Net::IPAddress addr;
ASSERT_TRUE("fe80::1ff:fe23:4567:890a%eth0", addr)';

i am using poco 1.11.0

diridari avatar Dec 14 '22 11:12 diridari

Hi, @diridari ! You can parse scoped ipv6 addr Look into IPAddressImpl.cpp:653. This is already implemented.

IPv6AddressImpl IPv6AddressImpl::parse(const std::string& addr)
{
	if (addr.empty()) return IPv6AddressImpl();
#if defined(_WIN32)
	struct addrinfo* pAI;
	struct addrinfo hints;
	std::memset(&hints, 0, sizeof(hints));
	hints.ai_flags = AI_NUMERICHOST;
	int rc = getaddrinfo(addr.c_str(), NULL, &hints, &pAI);
	if (rc == 0)
	{
		IPv6AddressImpl result = IPv6AddressImpl(&reinterpret_cast<struct sockaddr_in6*>(pAI->ai_addr)->sin6_addr, static_cast<int>(reinterpret_cast<struct sockaddr_in6*>(pAI->ai_addr)->sin6_scope_id));
		freeaddrinfo(pAI);
		return result;
	}
	else return IPv6AddressImpl();
#else
	struct in6_addr ia;
	std::string::size_type pos = addr.find('%');
	if (std::string::npos != pos)
	{
		std::string::size_type start = ('[' == addr[0]) ? 1 : 0;
		std::string unscopedAddr(addr, start, pos - start);
		std::string scope(addr, pos + 1, addr.size() - start - pos);
		Poco::UInt32 scopeId(0);
		if (!(scopeId = if_nametoindex(scope.c_str())))
			return IPv6AddressImpl();
		if (inet_pton(AF_INET6, unscopedAddr.c_str(), &ia) == 1)
			return IPv6AddressImpl(&ia, scopeId);
		else
			return IPv6AddressImpl();
	}
	else
	{
		if (inet_pton(AF_INET6, addr.c_str(), &ia) == 1)
			return IPv6AddressImpl(&ia);
		else
			return IPv6AddressImpl();
	}
#endif
}

Note, that scoped interface should be correct name from you system, i.e. when ipv6 is parsing, calls if_nametoindex, and if scoped ipv6 contains unexisted scope name, than method returns empty addr

bas524 avatar Jan 29 '23 09:01 bas524

This issue is stale because it has been open for 365 days with no activity.

github-actions[bot] avatar Jan 30 '24 02:01 github-actions[bot]

This issue was closed because it has been inactive for 60 days since being marked as stale.

github-actions[bot] avatar Mar 30 '24 02:03 github-actions[bot]