Commit eaea0f45 authored by aligungr's avatar aligungr

IPv6 support

parent ac447032
...@@ -30,7 +30,7 @@ static_assert(sizeof(long long) == sizeof(uint64_t)); ...@@ -30,7 +30,7 @@ static_assert(sizeof(long long) == sizeof(uint64_t));
static std::atomic<int> g_idCounter = 1; static std::atomic<int> g_idCounter = 1;
static bool IPv6FromString(const char *szAddress, std::vector<uint8_t>& address) static bool IPv6FromString(const char *szAddress, uint8_t *address)
{ {
auto asciiToHex = [](char c) -> int { auto asciiToHex = [](char c) -> int {
c |= 0x20; c |= 0x20;
...@@ -46,9 +46,7 @@ static bool IPv6FromString(const char *szAddress, std::vector<uint8_t>& address) ...@@ -46,9 +46,7 @@ static bool IPv6FromString(const char *szAddress, std::vector<uint8_t>& address)
uint8_t colons = 0; uint8_t colons = 0;
uint8_t pos = 0; uint8_t pos = 0;
address.clear(); memset(address, 0, 16);
std::vector<uint8_t> emptyAddress{16};
address.insert(address.begin(), emptyAddress.begin(), emptyAddress.end());
for (uint8_t i = 1; i <= 39; i++) for (uint8_t i = 1; i <= 39; i++)
{ {
...@@ -62,12 +60,12 @@ static bool IPv6FromString(const char *szAddress, std::vector<uint8_t>& address) ...@@ -62,12 +60,12 @@ static bool IPv6FromString(const char *szAddress, std::vector<uint8_t>& address)
else if (szAddress[i] == '\0') else if (szAddress[i] == '\0')
break; break;
} }
for (uint8_t i = 0; i <= 39 && pos < address.size(); i++) for (uint8_t i = 0; i <= 39 && pos < 16; i++)
{ {
if (szAddress[i] == ':' || szAddress[i] == '\0') if (szAddress[i] == ':' || szAddress[i] == '\0')
{ {
address.at(pos) = acc >> 8; address[pos] = acc >> 8;
address.at(pos + 1) = acc; address[pos + 1] = acc;
acc = 0; acc = 0;
if (colons && i && szAddress[i - 1] == ':') if (colons && i && szAddress[i - 1] == ':')
...@@ -199,7 +197,7 @@ OctetString utils::IpToOctetString(const std::string &address) ...@@ -199,7 +197,7 @@ OctetString utils::IpToOctetString(const std::string &address)
else if (ipVersion == 6) else if (ipVersion == 6)
{ {
std::vector<uint8_t> data{16}; std::vector<uint8_t> data{16};
if (!IPv6FromString(address.c_str(), data)) if (!IPv6FromString(address.c_str(), data.data()))
return {}; return {};
return OctetString(std::move(data)); return OctetString(std::move(data));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment