aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/ipv6_address
diff options
context:
space:
mode:
authorantervis <antervis@yandex-team.ru>2022-02-10 16:49:48 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:49:48 +0300
commitb3eef5b52437f05851a3e8fb489a68db0132657b (patch)
tree5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/ipv6_address
parent0942f9eeaefcfc84db83fa240d8ed52b99345e7d (diff)
downloadydb-b3eef5b52437f05851a3e8fb489a68db0132657b.tar.gz
Restoring authorship annotation for <antervis@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/ipv6_address')
-rw-r--r--library/cpp/ipv6_address/ipv6_address.cpp98
-rw-r--r--library/cpp/ipv6_address/ipv6_address.h106
-rw-r--r--library/cpp/ipv6_address/ipv6_address_p.h4
-rw-r--r--library/cpp/ipv6_address/ut/ipv6_address_ut.cpp50
4 files changed, 129 insertions, 129 deletions
diff --git a/library/cpp/ipv6_address/ipv6_address.cpp b/library/cpp/ipv6_address/ipv6_address.cpp
index a3d0e0a859..be8fcbae13 100644
--- a/library/cpp/ipv6_address/ipv6_address.cpp
+++ b/library/cpp/ipv6_address/ipv6_address.cpp
@@ -23,16 +23,16 @@ namespace {
*(reinterpret_cast<const ui64*>(memPtr) + 1)
};
}
-
- // zero-terminated copy of address string
- template <size_t N>
- inline auto AddrBuf(TStringBuf str) noexcept {
- std::array<char, N+1> res;
- auto len = Min(str.size(), N);
- CopyN(str.begin(), len, res.begin());
- res[len] = '\0';
- return res;
- }
+
+ // zero-terminated copy of address string
+ template <size_t N>
+ inline auto AddrBuf(TStringBuf str) noexcept {
+ std::array<char, N+1> res;
+ auto len = Min(str.size(), N);
+ CopyN(str.begin(), len, res.begin());
+ res[len] = '\0';
+ return res;
+ }
}
void TIpv6Address::InitFrom(const in6_addr& addr) {
@@ -78,35 +78,35 @@ TIpv6Address::TIpv6Address(const in_addr& addr) {
InitFrom(addr);
}
-TIpv6Address TIpv6Address::FromString(TStringBuf str, bool& ok) noexcept {
- const TIpType ipType = FigureOutType(str);
+TIpv6Address TIpv6Address::FromString(TStringBuf str, bool& ok) noexcept {
+ const TIpType ipType = FigureOutType(str);
if (ipType == Ipv6) {
- ui32 scopeId = 0;
- if (size_t pos = str.find('%'); pos != TStringBuf::npos) {
- ::TryFromString(str.substr(pos + 1), scopeId);
- str.Trunc(pos);
+ ui32 scopeId = 0;
+ if (size_t pos = str.find('%'); pos != TStringBuf::npos) {
+ ::TryFromString(str.substr(pos + 1), scopeId);
+ str.Trunc(pos);
}
- const auto buf = AddrBuf<INET6_ADDRSTRLEN>(str);
+ const auto buf = AddrBuf<INET6_ADDRSTRLEN>(str);
in6_addr addr;
- if (inet_pton(AF_INET6, buf.data(), &addr) != 1) {
+ if (inet_pton(AF_INET6, buf.data(), &addr) != 1) {
ok = false;
return TIpv6Address();
}
-
+
ok = true;
- return TIpv6Address(addr, scopeId);
+ return TIpv6Address(addr, scopeId);
} else { // if (ipType == Ipv4) {
- const auto buf = AddrBuf<INET_ADDRSTRLEN>(str);
+ const auto buf = AddrBuf<INET_ADDRSTRLEN>(str);
in_addr addr;
- if (inet_pton(AF_INET, buf.data(), &addr) != 1) {
+ if (inet_pton(AF_INET, buf.data(), &addr) != 1) {
ok = false;
return TIpv6Address();
}
-
+
ok = true;
- return TIpv6Address(addr);
+ return TIpv6Address(addr);
}
}
@@ -192,12 +192,12 @@ void TIpv6Address::ToIn6Addr(in6_addr& Addr6) const {
}
void TIpv6Address::Save(IOutputStream* out) const {
- ::Save(out, Ip);
+ ::Save(out, Ip);
::Save(out, static_cast<ui8>(Type_));
::Save(out, ScopeId_);
}
void TIpv6Address::Load(IInputStream* in) {
- ::Load(in, Ip);
+ ::Load(in, Ip);
ui8 num;
::Load(in, num);
Type_ = static_cast<TIpType>(num);
@@ -242,14 +242,14 @@ TIpv6Address TIpv6Address::Normalized() const noexcept {
}
IOutputStream& operator<<(IOutputStream& Out, const TIpv6Address::TIpType Type) noexcept {
- switch (Type) {
- case TIpv6Address::Ipv4:
- Out << "Ipv4";
- return Out;
- case TIpv6Address::Ipv6:
- Out << "Ipv6";
- return Out;
- default:
+ switch (Type) {
+ case TIpv6Address::Ipv4:
+ Out << "Ipv4";
+ return Out;
+ case TIpv6Address::Ipv6:
+ Out << "Ipv6";
+ return Out;
+ default:
Out << "UnknownType";
return Out;
}
@@ -317,16 +317,16 @@ std::tuple<THostAddressAndPort, TString, TIpPort> ParseHostAndMayBePortFromStrin
const size_t BracketColPos = RawStr.find("]:");
if (BracketColPos != TString::npos) {
// [ipv6]:port
- if (!RawStr.StartsWith('[')) {
+ if (!RawStr.StartsWith('[')) {
Ok = false;
return {};
}
- const TStringBuf StrIpv6(RawStr.begin() + 1, RawStr.begin() + BracketColPos);
- const TStringBuf StrPort(RawStr.begin() + BracketColPos + 2, RawStr.end());
+ const TStringBuf StrIpv6(RawStr.begin() + 1, RawStr.begin() + BracketColPos);
+ const TStringBuf StrPort(RawStr.begin() + BracketColPos + 2, RawStr.end());
bool IpConverted;
const TIpv6Address Ip = TIpv6Address::FromString(StrIpv6, IpConverted);
- if (!IpConverted) {
+ if (!IpConverted) {
Ok = false;
return {};
}
@@ -334,8 +334,8 @@ std::tuple<THostAddressAndPort, TString, TIpPort> ParseHostAndMayBePortFromStrin
Ok = false;
return {};
}
- TIpPort Port {};
- if (!::TryFromString(StrPort, Port)) {
+ TIpPort Port {};
+ if (!::TryFromString(StrPort, Port)) {
Ok = false;
return {};
}
@@ -349,15 +349,15 @@ std::tuple<THostAddressAndPort, TString, TIpPort> ParseHostAndMayBePortFromStrin
if (RawStr.StartsWith('[')) {
// [ipv6]
- if (!RawStr.EndsWith(']')) {
+ if (!RawStr.EndsWith(']')) {
Ok = false;
return {};
}
- const TStringBuf StrIpv6(RawStr.begin() + 1, RawStr.end() - 1);
+ const TStringBuf StrIpv6(RawStr.begin() + 1, RawStr.end() - 1);
bool IpConverted;
const TIpv6Address Ip = TIpv6Address::FromString(StrIpv6, IpConverted);
- if (!IpConverted || Ip.Type() != TIpv6Address::Ipv6) {
+ if (!IpConverted || Ip.Type() != TIpv6Address::Ipv6) {
Ok = false;
return {};
}
@@ -386,14 +386,14 @@ std::tuple<THostAddressAndPort, TString, TIpPort> ParseHostAndMayBePortFromStrin
}
}
- const TStringBuf StrPort(RawStr.begin() + ColPos + 1, RawStr.end());
- TIpPort Port {};
- if (!::TryFromString(StrPort, Port)) {
+ const TStringBuf StrPort(RawStr.begin() + ColPos + 1, RawStr.end());
+ TIpPort Port {};
+ if (!::TryFromString(StrPort, Port)) {
Ok = false;
return {};
}
- const TStringBuf StrIpv4OrHost(RawStr.begin(), RawStr.begin() + ColPos);
+ const TStringBuf StrIpv4OrHost(RawStr.begin(), RawStr.begin() + ColPos);
{
bool IpConverted;
const TIpv6Address Ipv4 = TIpv6Address::FromString(StrIpv4OrHost, IpConverted);
@@ -408,7 +408,7 @@ std::tuple<THostAddressAndPort, TString, TIpPort> ParseHostAndMayBePortFromStrin
{
// host:port
Ok = true;
- TRes Res{THostAddressAndPort{}, TString(StrIpv4OrHost), Port};
+ TRes Res{THostAddressAndPort{}, TString(StrIpv4OrHost), Port};
return Res;
}
}
@@ -431,7 +431,7 @@ std::tuple<THostAddressAndPort, TString, TIpPort> ParseHostAndMayBePortFromStrin
{
// host
Ok = true;
- TRes Res{THostAddressAndPort{}, TString(RawStr), DefaultPort};
+ TRes Res{THostAddressAndPort{}, TString(RawStr), DefaultPort};
return Res;
}
}
diff --git a/library/cpp/ipv6_address/ipv6_address.h b/library/cpp/ipv6_address/ipv6_address.h
index 3c9db90d15..1d7eb0b65f 100644
--- a/library/cpp/ipv6_address/ipv6_address.h
+++ b/library/cpp/ipv6_address/ipv6_address.h
@@ -32,26 +32,26 @@ public:
Ipv4,
LAST };
- constexpr TIpv6Address() noexcept = default;
- constexpr TIpv6Address(const TIpv6Address&) noexcept = default;
- constexpr TIpv6Address& operator=(const TIpv6Address&) noexcept = default;
-
- constexpr TIpv6Address(const ui128& ip, TIpType Type) noexcept
- : Ip(ip), Type_(Type)
- {}
-
- constexpr TIpv6Address(ui8 a, ui8 b, ui8 c, ui8 d) noexcept
- : Ip((ui32(a) << 24) | (ui32(b) << 16) | (ui32(c) << 8) | ui32(d))
- , Type_(TIpv6Address::Ipv4)
- {}
-
- constexpr TIpv6Address(ui16 a, ui16 b, ui16 c, ui16 d, ui16 e, ui16 f, ui16 g, ui16 h, ui32 scope = 0) noexcept
- : Type_(TIpv6Address::Ipv6)
- , ScopeId_(scope)
- {
- auto hi = (ui64(a) << 48) | (ui64(b) << 32) | (ui64(c) << 16) | ui64(d);
- auto lo = (ui64(e) << 48) | (ui64(f) << 32) | (ui64(g) << 16) | ui64(h);
- Ip = {hi, lo};
+ constexpr TIpv6Address() noexcept = default;
+ constexpr TIpv6Address(const TIpv6Address&) noexcept = default;
+ constexpr TIpv6Address& operator=(const TIpv6Address&) noexcept = default;
+
+ constexpr TIpv6Address(const ui128& ip, TIpType Type) noexcept
+ : Ip(ip), Type_(Type)
+ {}
+
+ constexpr TIpv6Address(ui8 a, ui8 b, ui8 c, ui8 d) noexcept
+ : Ip((ui32(a) << 24) | (ui32(b) << 16) | (ui32(c) << 8) | ui32(d))
+ , Type_(TIpv6Address::Ipv4)
+ {}
+
+ constexpr TIpv6Address(ui16 a, ui16 b, ui16 c, ui16 d, ui16 e, ui16 f, ui16 g, ui16 h, ui32 scope = 0) noexcept
+ : Type_(TIpv6Address::Ipv6)
+ , ScopeId_(scope)
+ {
+ auto hi = (ui64(a) << 48) | (ui64(b) << 32) | (ui64(c) << 16) | ui64(d);
+ auto lo = (ui64(e) << 48) | (ui64(f) << 32) | (ui64(g) << 16) | ui64(h);
+ Ip = {hi, lo};
}
explicit TIpv6Address(const NAddr::IRemoteAddr& addr);
@@ -60,24 +60,24 @@ public:
explicit TIpv6Address(const in6_addr& addr, ui32 Scope);
explicit TIpv6Address(const in_addr& addr);
- static TIpv6Address FromString(TStringBuf srcStr, bool& ok) noexcept;
+ static TIpv6Address FromString(TStringBuf srcStr, bool& ok) noexcept;
- constexpr bool IsNull() const noexcept {
+ constexpr bool IsNull() const noexcept {
return Ip == 0;
}
- constexpr bool IsValid() const noexcept {
- return Ip != 0 && (Type_ == Ipv6 || Type_ == Ipv4);
- }
+ constexpr bool IsValid() const noexcept {
+ return Ip != 0 && (Type_ == Ipv6 || Type_ == Ipv4);
+ }
- explicit constexpr operator bool() const noexcept {
- return IsValid();
+ explicit constexpr operator bool() const noexcept {
+ return IsValid();
+ }
+
+ constexpr bool operator ! () const noexcept {
+ return !IsValid();
}
- constexpr bool operator ! () const noexcept {
- return !IsValid();
- }
-
bool Isv4MappedTov6() const noexcept;
TIpv6Address TryToExtractIpv4From6() const noexcept;
TIpv6Address Normalized() const noexcept;
@@ -95,45 +95,45 @@ public:
void ToIn6Addr(in6_addr& Addr6) const;
// int SocketFamily() const;
- constexpr bool operator<(const TIpv6Address& other) const noexcept {
+ constexpr bool operator<(const TIpv6Address& other) const noexcept {
if (Type_ != other.Type_)
return Type_ > other.Type_;
else
return Ip < other.Ip;
}
- constexpr bool operator>(const TIpv6Address& other) const noexcept {
+ constexpr bool operator>(const TIpv6Address& other) const noexcept {
if (Type_ != other.Type_)
return Type_ < other.Type_;
else
return Ip > other.Ip;
}
- constexpr bool operator==(const TIpv6Address& other) const noexcept {
+ constexpr bool operator==(const TIpv6Address& other) const noexcept {
return Type_ == other.Type_ && Ip == other.Ip;
}
- constexpr bool operator!=(const TIpv6Address& other) const noexcept {
+ constexpr bool operator!=(const TIpv6Address& other) const noexcept {
return Type_ != other.Type_ || Ip != other.Ip;
}
- constexpr bool operator<=(const TIpv6Address& other) const noexcept {
+ constexpr bool operator<=(const TIpv6Address& other) const noexcept {
return !(*this > other);
}
- constexpr bool operator>=(const TIpv6Address& other) const noexcept {
+ constexpr bool operator>=(const TIpv6Address& other) const noexcept {
return !(*this < other);
}
- constexpr operator ui128() const noexcept {
+ constexpr operator ui128() const noexcept {
return Ip;
}
- constexpr TIpType Type() const noexcept {
+ constexpr TIpType Type() const noexcept {
return Type_;
}
- void SetScopeId(ui32 New) noexcept {
+ void SetScopeId(ui32 New) noexcept {
ScopeId_ = New;
}
- constexpr ui32 ScopeId() const noexcept {
+ constexpr ui32 ScopeId() const noexcept {
return ScopeId_;
}
@@ -154,35 +154,35 @@ private:
IOutputStream& operator<<(IOutputStream& Out, const TIpv6Address::TIpType Type) noexcept;
IOutputStream& operator<<(IOutputStream& Out, const TIpv6Address& ipv6Address) noexcept;
-constexpr TIpv6Address Get127001() noexcept {
- return {127, 0, 0, 1};
+constexpr TIpv6Address Get127001() noexcept {
+ return {127, 0, 0, 1};
}
-
-constexpr TIpv6Address Get1() noexcept {
- return {1, TIpv6Address::Ipv6};
+
+constexpr TIpv6Address Get1() noexcept {
+ return {1, TIpv6Address::Ipv6};
}
struct THostAddressAndPort {
- constexpr THostAddressAndPort() noexcept = default;
- constexpr THostAddressAndPort(const TIpv6Address& i, TIpPort p) noexcept {
+ constexpr THostAddressAndPort() noexcept = default;
+ constexpr THostAddressAndPort(const TIpv6Address& i, TIpPort p) noexcept {
Ip = i;
Port = p;
}
- constexpr bool operator==(const THostAddressAndPort& Other) const noexcept {
+ constexpr bool operator==(const THostAddressAndPort& Other) const noexcept {
return Ip == Other.Ip && Port == Other.Port;
}
- constexpr bool operator!=(const THostAddressAndPort& Other) const noexcept {
+ constexpr bool operator!=(const THostAddressAndPort& Other) const noexcept {
return !(*this == Other);
}
- constexpr bool IsValid() const noexcept {
+ constexpr bool IsValid() const noexcept {
return Ip.IsValid() && Port != 0;
}
TString ToString() const noexcept;
- TIpv6Address Ip {};
- TIpPort Port {0};
+ TIpv6Address Ip {};
+ TIpPort Port {0};
};
IOutputStream& operator<<(IOutputStream& Out, const THostAddressAndPort& HostAddressAndPort) noexcept;
diff --git a/library/cpp/ipv6_address/ipv6_address_p.h b/library/cpp/ipv6_address/ipv6_address_p.h
index 29405af946..44bec74579 100644
--- a/library/cpp/ipv6_address/ipv6_address_p.h
+++ b/library/cpp/ipv6_address/ipv6_address_p.h
@@ -2,6 +2,6 @@
#include "ipv6_address.h"
-inline TIpv6Address::TIpType FigureOutType(TStringBuf srcStr) noexcept {
- return srcStr.Contains(':') ? TIpv6Address::Ipv6 : TIpv6Address::Ipv4;
+inline TIpv6Address::TIpType FigureOutType(TStringBuf srcStr) noexcept {
+ return srcStr.Contains(':') ? TIpv6Address::Ipv6 : TIpv6Address::Ipv4;
}
diff --git a/library/cpp/ipv6_address/ut/ipv6_address_ut.cpp b/library/cpp/ipv6_address/ut/ipv6_address_ut.cpp
index 4a97ff4eab..73bb7cffdd 100644
--- a/library/cpp/ipv6_address/ut/ipv6_address_ut.cpp
+++ b/library/cpp/ipv6_address/ut/ipv6_address_ut.cpp
@@ -5,12 +5,12 @@
class TIpv6AddressTest: public TTestBase {
UNIT_TEST_SUITE(TIpv6AddressTest);
UNIT_TEST(ParseHostAndMayBePortFromString_data);
- UNIT_TEST(CheckAddressValidity)
+ UNIT_TEST(CheckAddressValidity)
UNIT_TEST_SUITE_END();
private:
void ParseHostAndMayBePortFromString_data();
- void CheckAddressValidity();
+ void CheckAddressValidity();
void HashCompileTest();
};
@@ -79,33 +79,33 @@ void TIpv6AddressTest::ParseHostAndMayBePortFromString_data() {
CheckIpDefPortAgainstIpPortDefPort("ya.ru", 123, HostRes("ya.ru", 123), true);
}
-void TIpv6AddressTest::CheckAddressValidity() {
- bool Ok;
-
- constexpr TIpv6Address partsV4 {12, 34, 56, 78};
- static_assert(partsV4.Type() == TIpv6Address::Ipv4);
-
- constexpr TIpv6Address intV4 {0x0C22384E, TIpv6Address::Ipv4};
- static_assert(partsV4 == intV4);
-
- const auto parsedV4 = TIpv6Address::FromString("12.34.56.78", Ok);
+void TIpv6AddressTest::CheckAddressValidity() {
+ bool Ok;
+
+ constexpr TIpv6Address partsV4 {12, 34, 56, 78};
+ static_assert(partsV4.Type() == TIpv6Address::Ipv4);
+
+ constexpr TIpv6Address intV4 {0x0C22384E, TIpv6Address::Ipv4};
+ static_assert(partsV4 == intV4);
+
+ const auto parsedV4 = TIpv6Address::FromString("12.34.56.78", Ok);
UNIT_ASSERT(Ok);
UNIT_ASSERT_EQUAL(parsedV4, partsV4);
-
- constexpr TIpv6Address partsV6 {0xFB, 0x1634, 0x19, 0xABED, 0, 0x8001, 0x1670, 0x742};
- static_assert(partsV6.Type() == TIpv6Address::Ipv6);
-
- constexpr TIpv6Address intV6 {{0x00FB16340019ABED, 0x0000800116700742}, TIpv6Address::Ipv6};
- static_assert(partsV6 == intV6);
-
- const auto parsedV6 = TIpv6Address::FromString("FB:1634:19:ABED:0:8001:1670:742", Ok);
+
+ constexpr TIpv6Address partsV6 {0xFB, 0x1634, 0x19, 0xABED, 0, 0x8001, 0x1670, 0x742};
+ static_assert(partsV6.Type() == TIpv6Address::Ipv6);
+
+ constexpr TIpv6Address intV6 {{0x00FB16340019ABED, 0x0000800116700742}, TIpv6Address::Ipv6};
+ static_assert(partsV6 == intV6);
+
+ const auto parsedV6 = TIpv6Address::FromString("FB:1634:19:ABED:0:8001:1670:742", Ok);
UNIT_ASSERT(Ok);
UNIT_ASSERT_EQUAL(parsedV6, partsV6);
-
- static_assert(Get127001() == TIpv6Address(0x7F000001, TIpv6Address::Ipv4));
- static_assert(Get1() == TIpv6Address(0, 0, 0, 0, 0, 0, 0, 1));
-}
-
+
+ static_assert(Get127001() == TIpv6Address(0x7F000001, TIpv6Address::Ipv4));
+ static_assert(Get1() == TIpv6Address(0, 0, 0, 0, 0, 0, 0, 1));
+}
+
void TIpv6AddressTest::HashCompileTest() {
std::unordered_set<TIpv6Address> test;
Y_UNUSED(test);