poco
poco copied to clipboard
TryParse scoped ipv6 addressess
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
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
This issue is stale because it has been open for 365 days with no activity.
This issue was closed because it has been inactive for 60 days since being marked as stale.