Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TryParse scoped ipv6 addressess #3896

Closed
diridari opened this issue Dec 14, 2022 · 6 comments
Closed

TryParse scoped ipv6 addressess #3896

diridari opened this issue Dec 14, 2022 · 6 comments
Labels

Comments

@diridari
Copy link

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

@bas524
Copy link
Contributor

bas524 commented Jan 29, 2023

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

Copy link

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

@github-actions github-actions bot added the stale label Jan 30, 2024
Copy link

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

@clauderobi
Copy link

Can you reopen? The code is buggy. When using a valid IP address like: [fe80::1592:96a0:88bf:d2d7%brv301] the code properly detect the opening '[' at line 674. But when it builds the scope string at line 676 forget to not include the ']' at the end.

@aleks-f aleks-f added bug and removed stale labels Sep 10, 2024
@aleks-f aleks-f added this to 1.14 Sep 10, 2024
@aleks-f aleks-f added this to the Release 1.14.0 milestone Sep 10, 2024
@aleks-f aleks-f reopened this Sep 10, 2024
@aleks-f
Copy link
Member

aleks-f commented Sep 10, 2024

can someone write some test cases reproducing the problem and create a PR where this can be fixed?

@aleks-f
Copy link
Member

aleks-f commented Sep 10, 2024

#4644

@aleks-f aleks-f closed this as completed Sep 10, 2024
@github-project-automation github-project-automation bot moved this to Done in 1.14 Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

No branches or pull requests

4 participants