Network: Fix minor issues with IP address parsing and add tests
This commit is contained in:
@@ -353,7 +353,7 @@ namespace Nz
|
||||
// validation of ipv4 subspace ::ffff:x.x
|
||||
if (mappedIPv4)
|
||||
{
|
||||
static const UInt8 abyPfx[] = {0,0, 0,0, 0,0, 0,0, 0,0, 0xFF,0xFF};
|
||||
const UInt8 abyPfx[] = {0,0, 0,0, 0,0, 0,0, 0,0, 0xFF,0xFF};
|
||||
if (std::memcmp(result, abyPfx, sizeof(abyPfx)) != 0)
|
||||
return false;
|
||||
}
|
||||
@@ -376,11 +376,10 @@ namespace Nz
|
||||
++addressPtr; // past the colon
|
||||
|
||||
unsigned int portValue;
|
||||
if (!Detail::ParseDecimal(addressPtr, &portValue, nullptr) || portValue > 65535)
|
||||
if (!Detail::ParseDecimal(addressPtr, &portValue, &addressPtr) || portValue > 65535)
|
||||
return false;
|
||||
|
||||
if (port)
|
||||
*port = static_cast<UInt16>(portValue);
|
||||
*port = static_cast<UInt16>(portValue);
|
||||
}
|
||||
else // finished just with IP address
|
||||
*port = 0; // indicate we have no port part
|
||||
|
||||
@@ -33,16 +33,20 @@ namespace Nz
|
||||
* \brief Builds the IP from a hostname
|
||||
* \return true If successful
|
||||
*
|
||||
* \remark address C-string symbolizing the IP address or hostname
|
||||
* \remark address C-string symbolizing the IP address
|
||||
*/
|
||||
|
||||
bool IpAddress::BuildFromAddress(const char* address)
|
||||
{
|
||||
m_isValid = false;
|
||||
|
||||
bool isIPv6;
|
||||
UInt8 result[16];
|
||||
if (!ParseIPAddress(address, result, &m_port, &isIPv6, nullptr))
|
||||
const char* endOfRead;
|
||||
if (!ParseIPAddress(address, result, &m_port, &isIPv6, &endOfRead))
|
||||
return false;
|
||||
|
||||
// was everything parsed?
|
||||
if (*endOfRead != '\0')
|
||||
return false;
|
||||
|
||||
m_isValid = true;
|
||||
@@ -154,20 +158,42 @@ namespace Nz
|
||||
if (m_port != 0)
|
||||
stream << '[';
|
||||
|
||||
for (unsigned int i = 0; i < 8; ++i)
|
||||
// IPv4-mapped IPv6?
|
||||
if (f0 == 0 && l0 == 5 && m_ipv6[5] == 0xFFFF)
|
||||
{
|
||||
if (i == f0)
|
||||
{
|
||||
stream << "::";
|
||||
i = l0;
|
||||
if (i >= 8)
|
||||
break;
|
||||
}
|
||||
else if (i != 0)
|
||||
stream << ':';
|
||||
IPv4 ipv4 = {
|
||||
m_ipv6[6] >> 8,
|
||||
m_ipv6[6] & 0xFF,
|
||||
m_ipv6[7] >> 8,
|
||||
m_ipv6[7] & 0xFF,
|
||||
};
|
||||
|
||||
stream << ToLower(NumberToString(m_ipv6[i], 16));
|
||||
stream << "::ffff:";
|
||||
for (unsigned int i = 0; i < 4; ++i)
|
||||
{
|
||||
stream << int(ipv4[i]);
|
||||
if (i != 3)
|
||||
stream << '.';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (unsigned int i = 0; i < 8; ++i)
|
||||
{
|
||||
if (i == f0)
|
||||
{
|
||||
stream << "::";
|
||||
i = l0;
|
||||
if (i >= 8)
|
||||
break;
|
||||
}
|
||||
else if (i != 0)
|
||||
stream << ':';
|
||||
|
||||
stream << ToLower(NumberToString(m_ipv6[i], 16));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (m_port != 0)
|
||||
stream << ']';
|
||||
|
||||
@@ -57,6 +57,9 @@ namespace Nz
|
||||
|
||||
std::string TranslateCanonicalName(const wchar_t* str)
|
||||
{
|
||||
if (!str)
|
||||
return {};
|
||||
|
||||
return FromWideString(str);
|
||||
}
|
||||
#else
|
||||
@@ -92,6 +95,9 @@ namespace Nz
|
||||
|
||||
std::string TranslateCanonicalName(const char* str)
|
||||
{
|
||||
if (!str)
|
||||
return {};
|
||||
|
||||
return str;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user