diff options
author | dimanne <dimanne@yandex-team.ru> | 2022-02-10 16:49:48 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:48 +0300 |
commit | 79b3cf0f1d6edfa0268153eddcb48f3467527461 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp | |
parent | 93d66104be406c5b8cdfce7be5546d72d83b547e (diff) | |
download | ydb-79b3cf0f1d6edfa0268153eddcb48f3467527461.tar.gz |
Restoring authorship annotation for <dimanne@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/actors/core/events.h | 2 | ||||
-rw-r--r-- | library/cpp/actors/core/log.cpp | 2 | ||||
-rw-r--r-- | library/cpp/actors/core/log.h | 20 | ||||
-rw-r--r-- | library/cpp/actors/dnscachelib/dnscache.h | 2 | ||||
-rw-r--r-- | library/cpp/actors/dnscachelib/ya.make | 26 | ||||
-rw-r--r-- | library/cpp/containers/comptrie/comptrie_builder.inl | 14 | ||||
-rw-r--r-- | library/cpp/http/fetch/exthttpcodes.h | 2 | ||||
-rw-r--r-- | library/cpp/http/fetch/httpfetcher.h | 32 | ||||
-rw-r--r-- | library/cpp/http/fetch/httpparser.h | 8 | ||||
-rw-r--r-- | library/cpp/int128/ut/int128_old_ut.cpp | 134 | ||||
-rw-r--r-- | library/cpp/ipv6_address/ipv6_address.cpp | 516 | ||||
-rw-r--r-- | library/cpp/ipv6_address/ipv6_address.h | 228 | ||||
-rw-r--r-- | library/cpp/ipv6_address/ipv6_address_p.h | 10 | ||||
-rw-r--r-- | library/cpp/ipv6_address/ut/ipv6_address_ut.cpp | 14 |
14 files changed, 505 insertions, 505 deletions
diff --git a/library/cpp/actors/core/events.h b/library/cpp/actors/core/events.h index 6aacb096ae..702cf50fad 100644 --- a/library/cpp/actors/core/events.h +++ b/library/cpp/actors/core/events.h @@ -19,7 +19,7 @@ namespace NActors { ES_MON = 7, ES_INTERCONNECT_TCP = 8, ES_PROFILER = 9, - ES_YF = 10, + ES_YF = 10, ES_HTTP = 11, ES_USERSPACE = 4096, diff --git a/library/cpp/actors/core/log.cpp b/library/cpp/actors/core/log.cpp index d8a3993916..5f63b5af58 100644 --- a/library/cpp/actors/core/log.cpp +++ b/library/cpp/actors/core/log.cpp @@ -695,7 +695,7 @@ namespace NActors { } while (!isOk); } - void ReopenLog() override { + void ReopenLog() override { } private: diff --git a/library/cpp/actors/core/log.h b/library/cpp/actors/core/log.h index a5219b5811..c11a7cf3c1 100644 --- a/library/cpp/actors/core/log.h +++ b/library/cpp/actors/core/log.h @@ -32,22 +32,22 @@ 0ull) \ ) -#define LOG_LOG_SAMPLED_BY(actorCtxOrSystem, priority, component, sampleBy, ...) \ - do { \ - ::NActors::NLog::TSettings* mSettings = static_cast<::NActors::NLog::TSettings*>((actorCtxOrSystem).LoggerSettings()); \ - ::NActors::NLog::EPriority mPriority = static_cast<::NActors::NLog::EPriority>(priority); \ - ::NActors::NLog::EComponent mComponent = static_cast<::NActors::NLog::EComponent>(component); \ - if (mSettings && mSettings->Satisfies(mPriority, mComponent, sampleBy)) { \ - ::NActors::MemLogAdapter( \ - actorCtxOrSystem, priority, component, __VA_ARGS__); \ - } \ +#define LOG_LOG_SAMPLED_BY(actorCtxOrSystem, priority, component, sampleBy, ...) \ + do { \ + ::NActors::NLog::TSettings* mSettings = static_cast<::NActors::NLog::TSettings*>((actorCtxOrSystem).LoggerSettings()); \ + ::NActors::NLog::EPriority mPriority = static_cast<::NActors::NLog::EPriority>(priority); \ + ::NActors::NLog::EComponent mComponent = static_cast<::NActors::NLog::EComponent>(component); \ + if (mSettings && mSettings->Satisfies(mPriority, mComponent, sampleBy)) { \ + ::NActors::MemLogAdapter( \ + actorCtxOrSystem, priority, component, __VA_ARGS__); \ + } \ } while (0) /**/ #define LOG_LOG_S_SAMPLED_BY(actorCtxOrSystem, priority, component, sampleBy, stream) \ LOG_LOG_SAMPLED_BY(actorCtxOrSystem, priority, component, sampleBy, "%s", [&]() { \ TStringBuilder logStringBuilder; \ logStringBuilder << stream; \ - return static_cast<TString>(logStringBuilder); \ + return static_cast<TString>(logStringBuilder); \ }().data()) #define LOG_LOG(actorCtxOrSystem, priority, component, ...) LOG_LOG_SAMPLED_BY(actorCtxOrSystem, priority, component, 0ull, __VA_ARGS__) diff --git a/library/cpp/actors/dnscachelib/dnscache.h b/library/cpp/actors/dnscachelib/dnscache.h index f34b9003d9..3313a251a1 100644 --- a/library/cpp/actors/dnscachelib/dnscache.h +++ b/library/cpp/actors/dnscachelib/dnscache.h @@ -55,7 +55,7 @@ private: struct THost { THost() noexcept { } - + TVector<TIpHost> AddrsV4; time_t ResolvedV4 = 0; time_t NotFoundV4 = 0; diff --git a/library/cpp/actors/dnscachelib/ya.make b/library/cpp/actors/dnscachelib/ya.make index 60a46cc208..e3a6ad6202 100644 --- a/library/cpp/actors/dnscachelib/ya.make +++ b/library/cpp/actors/dnscachelib/ya.make @@ -1,5 +1,5 @@ -LIBRARY() - +LIBRARY() + OWNER( davenger fomichev @@ -8,17 +8,17 @@ OWNER( single ) -SRCS( - dnscache.cpp - dnscache.h +SRCS( + dnscache.cpp + dnscache.h probes.cpp probes.h - timekeeper.h -) - -PEERDIR( - contrib/libs/c-ares + timekeeper.h +) + +PEERDIR( + contrib/libs/c-ares library/cpp/lwtrace -) - -END() +) + +END() diff --git a/library/cpp/containers/comptrie/comptrie_builder.inl b/library/cpp/containers/comptrie/comptrie_builder.inl index ebb00a57af..f273fa6571 100644 --- a/library/cpp/containers/comptrie/comptrie_builder.inl +++ b/library/cpp/containers/comptrie/comptrie_builder.inl @@ -142,7 +142,7 @@ public: const_iterator Find(char ch) const; void Add(const TBlob& s, TNode* node); - bool IsLast() const override { + bool IsLast() const override { return this->Empty(); } @@ -151,7 +151,7 @@ public: return Find(key, value, result, packer); } - ui64 Measure(const TBuilderImpl* builder) const override { + ui64 Measure(const TBuilderImpl* builder) const override { return MeasureRange(builder, 0, this->size()); } @@ -200,7 +200,7 @@ public: return written; } - void Destroy(TBuilderImpl* builder) override { + void Destroy(TBuilderImpl* builder) override { // Delete all nodes down the stream. for (iterator it = this->begin(); it != this->end(); ++it) { builder->DestroyNode(it->Node); @@ -221,7 +221,7 @@ public: Y_ASSERT(reinterpret_cast<ISubtree*>(this) == static_cast<void*>(this)); // This assumption is used in TNode::Subtree() } - bool IsLast() const override { + bool IsLast() const override { return Buffer.Empty(); } @@ -251,7 +251,7 @@ public: return nullptr; } - ui64 Measure(const TBuilderImpl*) const override { + ui64 Measure(const TBuilderImpl*) const override { return Buffer.Size(); } @@ -287,7 +287,7 @@ public: Y_ASSERT(reinterpret_cast<ISubtree*>(this) == static_cast<void*>(this)); // This assumption is used in TNode::Subtree() } - bool IsLast() const override { + bool IsLast() const override { return Data->Size == 0; } @@ -316,7 +316,7 @@ public: return nullptr; } - ui64 Measure(const TBuilderImpl*) const override { + ui64 Measure(const TBuilderImpl*) const override { return Data->Size; } diff --git a/library/cpp/http/fetch/exthttpcodes.h b/library/cpp/http/fetch/exthttpcodes.h index 793d286c23..6b525052cd 100644 --- a/library/cpp/http/fetch/exthttpcodes.h +++ b/library/cpp/http/fetch/exthttpcodes.h @@ -45,7 +45,7 @@ enum ExtHttpCodes { HTTP_PROXY_ERROR = 1036, HTTP_SSL_ERROR = 1037, HTTP_CACHED_COPY_NOT_FOUND = 1038, - HTTP_TIMEDOUT_WHILE_BYTES_RECEIVING = 1039, + HTTP_TIMEDOUT_WHILE_BYTES_RECEIVING = 1039, HTTP_FETCHER_BAD_RESPONSE = 1040, HTTP_FETCHER_MB_ERROR = 1041, HTTP_SSL_CERT_ERROR = 1042, diff --git a/library/cpp/http/fetch/httpfetcher.h b/library/cpp/http/fetch/httpfetcher.h index b6bd038a94..7fc251afd2 100644 --- a/library/cpp/http/fetch/httpfetcher.h +++ b/library/cpp/http/fetch/httpfetcher.h @@ -96,12 +96,12 @@ public: if ((got = TAgent::read(bufptr, buffree)) < 0) { fetcherr = errno; if (errno == EINTR) - header->error = HTTP_INTERRUPTED; + header->error = HTTP_INTERRUPTED; else if (errno == ETIMEDOUT) - header->error = HTTP_TIMEDOUT_WHILE_BYTES_RECEIVING; - else - header->error = HTTP_CONNECTION_LOST; - + header->error = HTTP_TIMEDOUT_WHILE_BYTES_RECEIVING; + else + header->error = HTTP_CONNECTION_LOST; + break; } @@ -111,7 +111,7 @@ public: buffree -= got; THttpParser<TCheck>::Parse(parsebuf, got); - + if (header->error) break; //if ANY error ocurred we will stop download that file or will have unprognosed stream position until MAX size reached @@ -139,20 +139,20 @@ public: } } while (THttpParser<TCheck>::GetState() > THttpParser<TCheck>::hp_eof); - i64 Adjustment = 0; - if (!header->error) { + i64 Adjustment = 0; + if (!header->error) { if (header->transfer_chunked) { - Adjustment = header->header_size + header->entity_size - bufsize - 1; + Adjustment = header->header_size + header->entity_size - bufsize - 1; } else if (header->content_length >= 0) { - Adjustment = header->header_size + header->content_length - bufsize; - } + Adjustment = header->header_size + header->content_length - bufsize; + } if (Adjustment > 0) - Adjustment = 0; - } - + Adjustment = 0; + } + if (buf) { - TAlloc::Shrink(buf, buflen - buffree + Adjustment); - + TAlloc::Shrink(buf, buflen - buffree + Adjustment); + if (TWriter::Write(buf, buflen - buffree) < 0) ret = EIO; } diff --git a/library/cpp/http/fetch/httpparser.h b/library/cpp/http/fetch/httpparser.h index cd7228055b..769828e4ae 100644 --- a/library/cpp/http/fetch/httpparser.h +++ b/library/cpp/http/fetch/httpparser.h @@ -174,10 +174,10 @@ protected: case hp_read_alive: Header->entity_size += size; - if (Header->entity_size >= Header->content_length) { + if (Header->entity_size >= Header->content_length) { State = hp_eof; } - + TCheck::CheckDocPart(buf, size, Header); if (isReader) return size; @@ -314,8 +314,8 @@ public: buf = Ptr; Ptr = (char*)Ptr + Chunk; Size -= Chunk; - if (State == hp_eof) { - Size = 0; + if (State == hp_eof) { + Size = 0; Eoferr = 0; } else if (State == hp_error) return Eoferr = -1; diff --git a/library/cpp/int128/ut/int128_old_ut.cpp b/library/cpp/int128/ut/int128_old_ut.cpp index 20ff30943e..2c5b9e9610 100644 --- a/library/cpp/int128/ut/int128_old_ut.cpp +++ b/library/cpp/int128/ut/int128_old_ut.cpp @@ -1,10 +1,10 @@ #include <library/cpp/testing/unittest/registar.h> #include <library/cpp/int128/int128.h> - + #include "int128_ut_helpers.h" -class TUInt128Test: public TTestBase { - UNIT_TEST_SUITE(TUInt128Test); +class TUInt128Test: public TTestBase { + UNIT_TEST_SUITE(TUInt128Test); UNIT_TEST(Create); UNIT_TEST(Minus); UNIT_TEST(Plus); @@ -16,100 +16,100 @@ class TUInt128Test: public TTestBase { #if defined(Y_HAVE_INT128) UNIT_TEST(FromSystemUint128); #endif - UNIT_TEST_SUITE_END(); - -private: - void Create(); - void Minus(); - void Plus(); - void Shift(); - void Overflow(); - void Underflow(); + UNIT_TEST_SUITE_END(); + +private: + void Create(); + void Minus(); + void Plus(); + void Shift(); + void Overflow(); + void Underflow(); void ToStringTest(); void FromStringTest(); #if defined(Y_HAVE_INT128) void FromSystemUint128(); #endif -}; - -UNIT_TEST_SUITE_REGISTRATION(TUInt128Test); - -void TUInt128Test::Create() { +}; + +UNIT_TEST_SUITE_REGISTRATION(TUInt128Test); + +void TUInt128Test::Create() { const ui128 n1 = 10; - UNIT_ASSERT_EQUAL(n1, 10); - + UNIT_ASSERT_EQUAL(n1, 10); + const ui128 n2 = n1; - UNIT_ASSERT_EQUAL(n2, 10); - + UNIT_ASSERT_EQUAL(n2, 10); + const ui128 n3(10); - UNIT_ASSERT_EQUAL(n3, 10); -} -void TUInt128Test::Minus() { + UNIT_ASSERT_EQUAL(n3, 10); +} +void TUInt128Test::Minus() { const ui128 n2 = 20; const ui128 n3 = 30; - + ui128 n4 = n3 - n2; - UNIT_ASSERT_EQUAL(n4, 10); - - n4 = n4 - 2; - UNIT_ASSERT_EQUAL(n4, 8); - - n4 -= 2; - UNIT_ASSERT_EQUAL(n4, 6); - - n4 = 10 - n4; - UNIT_ASSERT_EQUAL(n4, 4); -} -void TUInt128Test::Plus() { + UNIT_ASSERT_EQUAL(n4, 10); + + n4 = n4 - 2; + UNIT_ASSERT_EQUAL(n4, 8); + + n4 -= 2; + UNIT_ASSERT_EQUAL(n4, 6); + + n4 = 10 - n4; + UNIT_ASSERT_EQUAL(n4, 4); +} +void TUInt128Test::Plus() { const ui128 n2 = 20; const ui128 n3 = 30; - + ui128 n4 = n3 + n2; - UNIT_ASSERT_EQUAL(n4, 50); - - n4 = n4 + 2; - UNIT_ASSERT_EQUAL(n4, 52); - - n4 += 2; - UNIT_ASSERT_EQUAL(n4, 54); - - n4 = 10 + n4; - UNIT_ASSERT_EQUAL(n4, 64); -} -void TUInt128Test::Shift() { + UNIT_ASSERT_EQUAL(n4, 50); + + n4 = n4 + 2; + UNIT_ASSERT_EQUAL(n4, 52); + + n4 += 2; + UNIT_ASSERT_EQUAL(n4, 54); + + n4 = 10 + n4; + UNIT_ASSERT_EQUAL(n4, 64); +} +void TUInt128Test::Shift() { ui128 n = 1; - + const ui128 n4 = n << 4; UNIT_ASSERT_EQUAL(n4, ui128(0x0, 0x0000000000000010)); - UNIT_ASSERT_EQUAL(n4 >> 4, 1); - + UNIT_ASSERT_EQUAL(n4 >> 4, 1); + const ui128 n8 = n << 8; UNIT_ASSERT_EQUAL(n8, ui128(0x0, 0x0000000000000100)); - UNIT_ASSERT_EQUAL(n8 >> 8, 1); - + UNIT_ASSERT_EQUAL(n8 >> 8, 1); + const ui128 n60 = n << 60; UNIT_ASSERT_EQUAL(n60, ui128(0x0, 0x1000000000000000)); - UNIT_ASSERT_EQUAL(n60 >> 60, 1); - + UNIT_ASSERT_EQUAL(n60 >> 60, 1); + const ui128 n64 = n << 64; UNIT_ASSERT_EQUAL(n64, ui128(0x1, 0x0000000000000000)); - UNIT_ASSERT_EQUAL(n64 >> 64, 1); - + UNIT_ASSERT_EQUAL(n64 >> 64, 1); + const ui128 n124 = n << 124; UNIT_ASSERT_EQUAL(n124, ui128(0x1000000000000000, 0x0000000000000000)); - UNIT_ASSERT_EQUAL(n124 >> 124, 1); -} - -void TUInt128Test::Overflow() { + UNIT_ASSERT_EQUAL(n124 >> 124, 1); +} + +void TUInt128Test::Overflow() { ui128 n = ui128(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF); const ui128 n2 = n + 2; - UNIT_ASSERT_EQUAL(n2, 1); -} -void TUInt128Test::Underflow() { + UNIT_ASSERT_EQUAL(n2, 1); +} +void TUInt128Test::Underflow() { ui128 n = 1; const ui128 n128 = n - 2; UNIT_ASSERT_EQUAL(n128, ui128(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF)); -} +} void TUInt128Test::ToStringTest() { ui128 n(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF); diff --git a/library/cpp/ipv6_address/ipv6_address.cpp b/library/cpp/ipv6_address/ipv6_address.cpp index adb24cfb36..be8fcbae13 100644 --- a/library/cpp/ipv6_address/ipv6_address.cpp +++ b/library/cpp/ipv6_address/ipv6_address.cpp @@ -1,17 +1,17 @@ -#include "ipv6_address.h" -#include "ipv6_address_p.h" - +#include "ipv6_address.h" +#include "ipv6_address_p.h" + #ifdef _unix_ -#include <netinet/in.h> +#include <netinet/in.h> #endif - -#include <util/network/address.h> -#include <util/network/init.h> -#include <util/string/cast.h> -#include <util/string/split.h> + +#include <util/network/address.h> +#include <util/network/init.h> +#include <util/string/cast.h> +#include <util/string/split.h> #include <util/system/byteorder.h> -#include <util/ysaveload.h> - +#include <util/ysaveload.h> + #include <array> namespace { @@ -40,79 +40,79 @@ void TIpv6Address::InitFrom(const in6_addr& addr) { const ui64 raw[2] = {SwapBytes(*ui64Ptr), SwapBytes(*(ui64Ptr + 1))}; Ip = FromMemMSF(reinterpret_cast<const char*>(raw)); Type_ = Ipv6; -} +} void TIpv6Address::InitFrom(const in_addr& addr) { unsigned long swapped = SwapBytes(addr.s_addr); Ip = ui128{0, swapped}; - Type_ = Ipv4; -} - + Type_ = Ipv4; +} + void TIpv6Address::InitFrom(const sockaddr_in6& Addr) { - InitFrom(Addr.sin6_addr); - ScopeId_ = Addr.sin6_scope_id; -} + InitFrom(Addr.sin6_addr); + ScopeId_ = Addr.sin6_scope_id; +} void TIpv6Address::InitFrom(const sockaddr_in& Addr) { - InitFrom(Addr.sin_addr); -} - + InitFrom(Addr.sin_addr); +} + TIpv6Address::TIpv6Address(const NAddr::IRemoteAddr& addr) { if (addr.Addr()->sa_family == AF_INET) { // IPv4 const sockaddr_in* Tmp = reinterpret_cast<const sockaddr_in*>(addr.Addr()); - InitFrom(*Tmp); - } else { // IPv6 + InitFrom(*Tmp); + } else { // IPv6 const sockaddr_in6* Tmp = reinterpret_cast<const sockaddr_in6*>(addr.Addr()); - InitFrom(*Tmp); - } -} + InitFrom(*Tmp); + } +} TIpv6Address::TIpv6Address(const sockaddr_in6& Addr) { - InitFrom(Addr); -} + InitFrom(Addr); +} TIpv6Address::TIpv6Address(const sockaddr_in& Addr) { - InitFrom(Addr); -} + InitFrom(Addr); +} TIpv6Address::TIpv6Address(const in6_addr& addr, ui32 Scope) { - InitFrom(addr); - ScopeId_ = Scope; -} + InitFrom(addr); + ScopeId_ = Scope; +} TIpv6Address::TIpv6Address(const in_addr& addr) { - InitFrom(addr); -} - + InitFrom(addr); +} + 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); - } - + } + const auto buf = AddrBuf<INET6_ADDRSTRLEN>(str); - in6_addr addr; + in6_addr addr; if (inet_pton(AF_INET6, buf.data(), &addr) != 1) { - ok = false; - return TIpv6Address(); - } + ok = false; + return TIpv6Address(); + } - ok = true; + ok = true; return TIpv6Address(addr, scopeId); - } else { // if (ipType == Ipv4) { + } else { // if (ipType == Ipv4) { const auto buf = AddrBuf<INET_ADDRSTRLEN>(str); - in_addr addr; + in_addr addr; if (inet_pton(AF_INET, buf.data(), &addr) != 1) { - ok = false; - return TIpv6Address(); - } + ok = false; + return TIpv6Address(); + } - ok = true; + ok = true; return TIpv6Address(addr); - } -} - + } +} + TString TIpv6Address::ToString(bool* ok) const noexcept { - return ToString(true, ok); -} + return ToString(true, ok); +} TString TIpv6Address::ToString(bool PrintScopeId, bool* ok) const noexcept { TString result; bool isOk = true; @@ -120,17 +120,17 @@ TString TIpv6Address::ToString(bool PrintScopeId, bool* ok) const noexcept { if (Type_ == TIpv6Address::Ipv4) { result.resize(INET_ADDRSTRLEN + 2); in_addr addr; - ToInAddr(addr); + ToInAddr(addr); isOk = inet_ntop(AF_INET, &addr, result.begin(), INET_ADDRSTRLEN); result.resize(result.find('\0')); } else if (Type_ == TIpv6Address::Ipv6) { result.resize(INET6_ADDRSTRLEN + 2); in6_addr addr; - ToIn6Addr(addr); + ToIn6Addr(addr); isOk = inet_ntop(AF_INET6, &addr, result.begin(), INET6_ADDRSTRLEN); result.resize(result.find('\0')); if (PrintScopeId) - result += "%" + ::ToString(ScopeId_); + result += "%" + ::ToString(ScopeId_); } else { result = "null"; isOk = true; @@ -141,106 +141,106 @@ TString TIpv6Address::ToString(bool PrintScopeId, bool* ok) const noexcept { } return result; -} - +} + void TIpv6Address::ToSockaddrAndSocklen(sockaddr_in& sockAddrIPv4, sockaddr_in6& sockAddrIPv6, // in const sockaddr*& sockAddrPtr, socklen_t& sockAddrSize, ui16 Port) const { // out - + if (Type_ == Ipv4) { - memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4)); - sockAddrIPv4.sin_family = AF_INET; + memset(&sockAddrIPv4, 0, sizeof(sockAddrIPv4)); + sockAddrIPv4.sin_family = AF_INET; sockAddrIPv4.sin_port = htons(Port); - ToInAddr(sockAddrIPv4.sin_addr); - - sockAddrSize = sizeof(sockAddrIPv4); + ToInAddr(sockAddrIPv4.sin_addr); + + sockAddrSize = sizeof(sockAddrIPv4); sockAddrPtr = reinterpret_cast<sockaddr*>(&sockAddrIPv4); - + } else if (Type_ == Ipv6) { - memset(&sockAddrIPv6, 0, sizeof(sockAddrIPv6)); - sockAddrIPv6.sin6_family = AF_INET6; + memset(&sockAddrIPv6, 0, sizeof(sockAddrIPv6)); + sockAddrIPv6.sin6_family = AF_INET6; sockAddrIPv6.sin6_port = htons(Port); - ToIn6Addr(sockAddrIPv6.sin6_addr); - sockAddrIPv6.sin6_scope_id = ScopeId_; - sockAddrIPv6.sin6_flowinfo = 0; - - sockAddrSize = sizeof(sockAddrIPv6); + ToIn6Addr(sockAddrIPv6.sin6_addr); + sockAddrIPv6.sin6_scope_id = ScopeId_; + sockAddrIPv6.sin6_flowinfo = 0; + + sockAddrSize = sizeof(sockAddrIPv6); sockAddrPtr = reinterpret_cast<sockaddr*>(&sockAddrIPv6); - } else + } else Y_VERIFY(false); -} - +} + void TIpv6Address::ToInAddr(in_addr& Addr4) const { Y_VERIFY(Type_ == TIpv6Address::Ipv4); - - Zero(Addr4); + + Zero(Addr4); ui32 Value = GetLow(Ip); Y_VERIFY(Value == GetLow(Ip), " "); Y_VERIFY(GetHigh(Ip) == 0, " "); - Addr4.s_addr = SwapBytes(Value); -} + Addr4.s_addr = SwapBytes(Value); +} void TIpv6Address::ToIn6Addr(in6_addr& Addr6) const { Y_VERIFY(Type_ == TIpv6Address::Ipv6); - - Zero(Addr6); + + Zero(Addr6); ui64 Raw[2] = {GetHigh(Ip), GetLow(Ip)}; *Raw = SwapBytes(*Raw); Raw[1] = SwapBytes(1 [Raw]); - memcpy(&Addr6, Raw, sizeof(Raw)); -} - + memcpy(&Addr6, Raw, sizeof(Raw)); +} + void TIpv6Address::Save(IOutputStream* out) const { ::Save(out, Ip); - ::Save(out, static_cast<ui8>(Type_)); - ::Save(out, ScopeId_); -} + ::Save(out, static_cast<ui8>(Type_)); + ::Save(out, ScopeId_); +} void TIpv6Address::Load(IInputStream* in) { ::Load(in, Ip); - ui8 num; - ::Load(in, num); - Type_ = static_cast<TIpType>(num); - ::Load(in, ScopeId_); -} - -bool TIpv6Address::Isv4MappedTov6() const noexcept { - /// http://en.wikipedia.org/wiki/IPv6 - /// Hybrid dual-stack IPv6/IPv4 implementations recognize a special class of addresses, - /// the IPv4-mapped IPv6 addresses. In these addresses, the first 80 bits are zero, the next 16 bits are one, - /// and the remaining 32 bits are the IPv4 address. - + ui8 num; + ::Load(in, num); + Type_ = static_cast<TIpType>(num); + ::Load(in, ScopeId_); +} + +bool TIpv6Address::Isv4MappedTov6() const noexcept { + /// http://en.wikipedia.org/wiki/IPv6 + /// Hybrid dual-stack IPv6/IPv4 implementations recognize a special class of addresses, + /// the IPv4-mapped IPv6 addresses. In these addresses, the first 80 bits are zero, the next 16 bits are one, + /// and the remaining 32 bits are the IPv4 address. + if (Type_ != Ipv6) - return false; - + return false; + if (GetHigh(Ip) != 0) - return false; // First 64 bit are not zero -> it is not ipv4-mapped-ipv6 address - + return false; // First 64 bit are not zero -> it is not ipv4-mapped-ipv6 address + const ui64 Low = GetLow(Ip) >> 32; if (Low != 0x0000ffff) - return false; - - return true; -} - -TIpv6Address TIpv6Address::TryToExtractIpv4From6() const noexcept { + return false; + + return true; +} + +TIpv6Address TIpv6Address::TryToExtractIpv4From6() const noexcept { if (Isv4MappedTov6() == false) - return TIpv6Address(); - + return TIpv6Address(); + const ui64 NewLow = GetLow(Ip) & 0x00000000ffffffff; TIpv6Address Result(ui128(0, NewLow), Ipv4); - return Result; -} - -TIpv6Address TIpv6Address::Normalized() const noexcept { + return Result; +} + +TIpv6Address TIpv6Address::Normalized() const noexcept { if (Isv4MappedTov6() == false) - return *this; - - TIpv6Address Result = TryToExtractIpv4From6(); + return *this; + + TIpv6Address Result = TryToExtractIpv4From6(); Y_VERIFY(Result.IsNull() == false); - return Result; -} - + return Result; +} + IOutputStream& operator<<(IOutputStream& Out, const TIpv6Address::TIpType Type) noexcept { switch (Type) { case TIpv6Address::Ipv4: @@ -250,188 +250,188 @@ IOutputStream& operator<<(IOutputStream& Out, const TIpv6Address::TIpType Type) Out << "Ipv6"; return Out; default: - Out << "UnknownType"; - return Out; - } -} - + Out << "UnknownType"; + return Out; + } +} + IOutputStream& operator<<(IOutputStream& out, const TIpv6Address& ipv6Address) noexcept { bool ok; const TString& strIp = ipv6Address.ToString(&ok); if (!ok) { return out << "Can not convert ip to string"; - } else { + } else { return out << strIp; - } -} - -TString THostAddressAndPort::ToString() const noexcept { - TStringStream Str; - Str << *this; - return Str.Str(); -} - + } +} + +TString THostAddressAndPort::ToString() const noexcept { + TStringStream Str; + Str << *this; + return Str.Str(); +} + IOutputStream& operator<<(IOutputStream& Out, const THostAddressAndPort& HostAddressAndPort) noexcept { - Out << HostAddressAndPort.Ip << ":" << HostAddressAndPort.Port; - return Out; -} - -namespace { + Out << HostAddressAndPort.Ip << ":" << HostAddressAndPort.Port; + return Out; +} + +namespace { class TRemoteAddr: public NAddr::IRemoteAddr { - public: + public: TRemoteAddr(const TIpv6Address& Address, TIpPort Port); const sockaddr* Addr() const override; socklen_t Len() const override; - - private: + + private: sockaddr_in SockAddrIPv4; sockaddr_in6 SockAddrIPv6; const sockaddr* SockAddrPtr = nullptr; socklen_t SockAddrSize = 0; - }; - + }; + TRemoteAddr::TRemoteAddr(const TIpv6Address& Address, TIpPort Port) { - Address.ToSockaddrAndSocklen(SockAddrIPv4, SockAddrIPv6, SockAddrPtr, SockAddrSize, Port); - } + Address.ToSockaddrAndSocklen(SockAddrIPv4, SockAddrIPv6, SockAddrPtr, SockAddrSize, Port); + } const sockaddr* TRemoteAddr::Addr() const { - return SockAddrPtr; - } - socklen_t TRemoteAddr::Len() const { - return SockAddrSize; - } -} - + return SockAddrPtr; + } + socklen_t TRemoteAddr::Len() const { + return SockAddrSize; + } +} + NAddr::IRemoteAddr* ToIRemoteAddr(const TIpv6Address& Address, TIpPort Port) { - return new TRemoteAddr(Address, Port); -} - + return new TRemoteAddr(Address, Port); +} + std::tuple<THostAddressAndPort, TString, TIpPort> ParseHostAndMayBePortFromString(const TString& RawStr, TIpPort DefaultPort, bool& Ok) noexcept { - // Cout << "ParseHostAndMayBePortFromString: " << RawStr << ", Port: " << DefaultPort << Endl; - + // Cout << "ParseHostAndMayBePortFromString: " << RawStr << ", Port: " << DefaultPort << Endl; + using TRes = std::tuple<THostAddressAndPort, TString, TIpPort>; - - // --------------------------------------------------------------------- - - const size_t BracketColPos = RawStr.find("]:"); + + // --------------------------------------------------------------------- + + const size_t BracketColPos = RawStr.find("]:"); if (BracketColPos != TString::npos) { - // [ipv6]:port + // [ipv6]:port if (!RawStr.StartsWith('[')) { - Ok = false; - return {}; - } + Ok = false; + return {}; + } 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); + const TIpv6Address Ip = TIpv6Address::FromString(StrIpv6, IpConverted); if (!IpConverted) { - Ok = false; - return {}; - } + Ok = false; + return {}; + } if (Ip.Type() != TIpv6Address::Ipv6) { - Ok = false; - return {}; - } + Ok = false; + return {}; + } TIpPort Port {}; if (!::TryFromString(StrPort, Port)) { - Ok = false; - return {}; - } - - Ok = true; - TRes Res{{Ip, Port}, {}, {}}; - return Res; - } - - // --------------------------------------------------------------------- - + Ok = false; + return {}; + } + + Ok = true; + TRes Res{{Ip, Port}, {}, {}}; + return Res; + } + + // --------------------------------------------------------------------- + if (RawStr.StartsWith('[')) { - // [ipv6] + // [ipv6] if (!RawStr.EndsWith(']')) { - Ok = false; - return {}; - } + Ok = false; + return {}; + } const TStringBuf StrIpv6(RawStr.begin() + 1, RawStr.end() - 1); - + bool IpConverted; - const TIpv6Address Ip = TIpv6Address::FromString(StrIpv6, IpConverted); + const TIpv6Address Ip = TIpv6Address::FromString(StrIpv6, IpConverted); if (!IpConverted || Ip.Type() != TIpv6Address::Ipv6) { - Ok = false; - return {}; - } - - Ok = true; - TRes Res{{Ip, DefaultPort}, {}, {}}; - return Res; - } - - // --------------------------------------------------------------------- - + Ok = false; + return {}; + } + + Ok = true; + TRes Res{{Ip, DefaultPort}, {}, {}}; + return Res; + } + + // --------------------------------------------------------------------- + const size_t ColPos = RawStr.find(':'); if (ColPos != TString::npos) { - // host:port - // ipv4:port - // ipv6 - - { + // host:port + // ipv4:port + // ipv6 + + { bool IpConverted; - const TIpv6Address Ipv6 = TIpv6Address::FromString(RawStr, IpConverted); + const TIpv6Address Ipv6 = TIpv6Address::FromString(RawStr, IpConverted); if (IpConverted && Ipv6.Type() == TIpv6Address::Ipv6) { - // ipv6 - Ok = true; - TRes Res{{Ipv6, DefaultPort}, {}, {}}; - return Res; - } - } - + // ipv6 + Ok = true; + TRes Res{{Ipv6, DefaultPort}, {}, {}}; + return Res; + } + } + const TStringBuf StrPort(RawStr.begin() + ColPos + 1, RawStr.end()); TIpPort Port {}; if (!::TryFromString(StrPort, Port)) { - Ok = false; - return {}; - } - + Ok = false; + return {}; + } + const TStringBuf StrIpv4OrHost(RawStr.begin(), RawStr.begin() + ColPos); - { + { bool IpConverted; - const TIpv6Address Ipv4 = TIpv6Address::FromString(StrIpv4OrHost, IpConverted); + const TIpv6Address Ipv4 = TIpv6Address::FromString(StrIpv4OrHost, IpConverted); if (IpConverted && Ipv4.Type() == TIpv6Address::Ipv4) { - // ipv4:port - Ok = true; - TRes Res{{Ipv4, Port}, {}, {}}; - return Res; - } - } - - { - // host:port - Ok = true; + // ipv4:port + Ok = true; + TRes Res{{Ipv4, Port}, {}, {}}; + return Res; + } + } + + { + // host:port + Ok = true; TRes Res{THostAddressAndPort{}, TString(StrIpv4OrHost), Port}; - return Res; - } - } - - // --------------------------------------------------------------------- - - { - // ipv4 + return Res; + } + } + + // --------------------------------------------------------------------- + + { + // ipv4 bool IpConverted; - const TIpv6Address Ipv4 = TIpv6Address::FromString(RawStr, IpConverted); + const TIpv6Address Ipv4 = TIpv6Address::FromString(RawStr, IpConverted); if (IpConverted && Ipv4.Type() == TIpv6Address::Ipv4) { - Ok = true; - TRes Res{{Ipv4, DefaultPort}, {}, {}}; - return Res; - } - } - - // --------------------------------------------------------------------- - - { - // host - Ok = true; + Ok = true; + TRes Res{{Ipv4, DefaultPort}, {}, {}}; + return Res; + } + } + + // --------------------------------------------------------------------- + + { + // host + Ok = true; TRes Res{THostAddressAndPort{}, TString(RawStr), DefaultPort}; - return Res; - } -} + return Res; + } +} diff --git a/library/cpp/ipv6_address/ipv6_address.h b/library/cpp/ipv6_address/ipv6_address.h index 3852768354..1d7eb0b65f 100644 --- a/library/cpp/ipv6_address/ipv6_address.h +++ b/library/cpp/ipv6_address/ipv6_address.h @@ -1,37 +1,37 @@ -#pragma once - +#pragma once + #include <util/generic/hash_set.h> -#include <util/network/ip.h> -#include <util/stream/input.h> - +#include <util/network/ip.h> +#include <util/stream/input.h> + #include <library/cpp/int128/int128.h> - + #if defined(_freebsd_) -// #include required to avoid problem with undefined 'socklen_t' on FreeBSD -#include <sys/socket.h> +// #include required to avoid problem with undefined 'socklen_t' on FreeBSD +#include <sys/socket.h> #endif #if defined(_win_) -// #include required to avoid problem with undefined 'socklen_t' on Windows -#include <util/network/socket.h> +// #include required to avoid problem with undefined 'socklen_t' on Windows +#include <util/network/socket.h> #endif -namespace NAddr { - class IRemoteAddr; -} +namespace NAddr { + class IRemoteAddr; +} struct in6_addr; struct in_addr; -struct sockaddr; -struct sockaddr_in; -struct sockaddr_in6; - -// TODO (dimanne): rename to something like TIntInetAddress or THostAddress -class TIpv6Address { -public: +struct sockaddr; +struct sockaddr_in; +struct sockaddr_in6; + +// TODO (dimanne): rename to something like TIntInetAddress or THostAddress +class TIpv6Address { +public: enum TIpType { Ipv6, Ipv4, LAST }; - + constexpr TIpv6Address() noexcept = default; constexpr TIpv6Address(const TIpv6Address&) noexcept = default; constexpr TIpv6Address& operator=(const TIpv6Address&) noexcept = default; @@ -52,68 +52,68 @@ public: 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); explicit TIpv6Address(const sockaddr_in6& Addr); explicit TIpv6Address(const sockaddr_in& Addr); explicit TIpv6Address(const in6_addr& addr, ui32 Scope); explicit TIpv6Address(const in_addr& addr); - + static TIpv6Address FromString(TStringBuf srcStr, bool& ok) noexcept; constexpr bool IsNull() const noexcept { - return Ip == 0; - } - + return Ip == 0; + } + constexpr bool IsValid() const noexcept { return Ip != 0 && (Type_ == Ipv6 || Type_ == Ipv4); } - + explicit constexpr operator bool() const noexcept { return IsValid(); - } - + } + constexpr bool operator ! () const noexcept { return !IsValid(); } bool Isv4MappedTov6() const noexcept; - TIpv6Address TryToExtractIpv4From6() const noexcept; - TIpv6Address Normalized() const noexcept; - + TIpv6Address TryToExtractIpv4From6() const noexcept; + TIpv6Address Normalized() const noexcept; + TString ToString(bool* ok = nullptr) const noexcept; TString ToString(bool PrintScopeId, bool* ok = nullptr) const noexcept; - + void ToSockaddrAndSocklen(sockaddr_in& sockAddrIPv4, sockaddr_in6& sockAddrIPv6, // in const sockaddr*& sockAddrPtr, socklen_t& sockAddrSize, ui16 Port) const; // out - + void ToInAddr(in_addr& Addr4) const; void ToIn6Addr(in6_addr& Addr6) const; - // int SocketFamily() const; - + // int SocketFamily() const; + 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 { if (Type_ != other.Type_) return Type_ < other.Type_; else return Ip > other.Ip; - } + } constexpr bool operator==(const TIpv6Address& other) const noexcept { return Type_ == other.Type_ && Ip == other.Ip; - } + } constexpr bool operator!=(const TIpv6Address& other) const noexcept { return Type_ != other.Type_ || Ip != other.Ip; - } - + } + constexpr bool operator<=(const TIpv6Address& other) const noexcept { return !(*this > other); } @@ -123,116 +123,116 @@ public: } constexpr operator ui128() const noexcept { - return Ip; - } - + return Ip; + } + constexpr TIpType Type() const noexcept { - return Type_; - } - + return Type_; + } + void SetScopeId(ui32 New) noexcept { - ScopeId_ = New; - } + ScopeId_ = New; + } constexpr ui32 ScopeId() const noexcept { - return ScopeId_; - } - + return ScopeId_; + } + void Save(IOutputStream* out) const; void Load(IInputStream* in); - -private: + +private: void InitFrom(const in6_addr& addr); void InitFrom(const in_addr& addr); - + void InitFrom(const sockaddr_in6& Addr); void InitFrom(const sockaddr_in& Addr); - + ui128 Ip{}; TIpType Type_ = LAST; ui32 ScopeId_ = 0; -}; +}; 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 Get1() noexcept { return {1, TIpv6Address::Ipv6}; -} - -struct THostAddressAndPort { +} + +struct THostAddressAndPort { constexpr THostAddressAndPort() noexcept = default; constexpr THostAddressAndPort(const TIpv6Address& i, TIpPort p) noexcept { - Ip = i; - Port = p; - } - + Ip = i; + Port = p; + } + constexpr bool operator==(const THostAddressAndPort& Other) const noexcept { - return Ip == Other.Ip && Port == Other.Port; - } + return Ip == Other.Ip && Port == Other.Port; + } constexpr bool operator!=(const THostAddressAndPort& Other) const noexcept { - return !(*this == Other); - } + return !(*this == Other); + } constexpr bool IsValid() const noexcept { - return Ip.IsValid() && Port != 0; - } - - TString ToString() const noexcept; - + return Ip.IsValid() && Port != 0; + } + + TString ToString() const noexcept; + TIpv6Address Ip {}; TIpPort Port {0}; -}; +}; IOutputStream& operator<<(IOutputStream& Out, const THostAddressAndPort& HostAddressAndPort) noexcept; - -/// -/// Returns -/// 1. either valid THostAddressAndPort + +/// +/// Returns +/// 1. either valid THostAddressAndPort /// 2. or TString with hostname (which you should resolve) and TIpPort with port -/// 3. or error, if Ok == false -/// -/// Supported RawStrs are -/// 1.2.3.4 // port wil be equal to DefaultPort -/// 1.2.3.4:80 -/// [2001::7348] // port wil be equal to DefaultPort -/// 2001::7348 // port wil be equal to DefaultPort -/// [2001::7348]:80 -/// +/// 3. or error, if Ok == false +/// +/// Supported RawStrs are +/// 1.2.3.4 // port wil be equal to DefaultPort +/// 1.2.3.4:80 +/// [2001::7348] // port wil be equal to DefaultPort +/// 2001::7348 // port wil be equal to DefaultPort +/// [2001::7348]:80 +/// std::tuple<THostAddressAndPort, TString, TIpPort> ParseHostAndMayBePortFromString(const TString& RawStr, TIpPort DefaultPort, bool& Ok) noexcept; - + using TIpv6AddressesSet = THashSet<TIpv6Address>; -template <> -struct THash<TIpv6Address> { +template <> +struct THash<TIpv6Address> { inline size_t operator()(const TIpv6Address& ip) const { const ui128& Tmp = static_cast<ui128>(ip); return CombineHashes(THash<ui128>()(Tmp), THash<ui8>()(static_cast<ui8>(ip.Type()))); - } -}; -template <> -struct THash<THostAddressAndPort> { + } +}; +template <> +struct THash<THostAddressAndPort> { inline size_t operator()(const THostAddressAndPort& IpAndPort) const { - return CombineHashes(THash<TIpv6Address>()(IpAndPort.Ip), THash<TIpPort>()(IpAndPort.Port)); - } -}; - -namespace std { - template <> - struct hash<TIpv6Address> { + return CombineHashes(THash<TIpv6Address>()(IpAndPort.Ip), THash<TIpPort>()(IpAndPort.Port)); + } +}; + +namespace std { + template <> + struct hash<TIpv6Address> { std::size_t operator()(const TIpv6Address& Ip) const noexcept { - return THash<TIpv6Address>()(Ip); - } - }; -} - + return THash<TIpv6Address>()(Ip); + } + }; +} + NAddr::IRemoteAddr* ToIRemoteAddr(const TIpv6Address& Address, TIpPort Port); - -// template <> -// class TSerializer<TIpv6Address> { -// public: + +// template <> +// class TSerializer<TIpv6Address> { +// public: // static void Save(IOutputStream *out, const TIpv6Address &ip); // static void Load(IInputStream *in, TIpv6Address &ip); -//}; +//}; diff --git a/library/cpp/ipv6_address/ipv6_address_p.h b/library/cpp/ipv6_address/ipv6_address_p.h index 1175f07902..44bec74579 100644 --- a/library/cpp/ipv6_address/ipv6_address_p.h +++ b/library/cpp/ipv6_address/ipv6_address_p.h @@ -1,7 +1,7 @@ -#pragma once - -#include "ipv6_address.h" - +#pragma once + +#include "ipv6_address.h" + 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 789637cb35..73bb7cffdd 100644 --- a/library/cpp/ipv6_address/ut/ipv6_address_ut.cpp +++ b/library/cpp/ipv6_address/ut/ipv6_address_ut.cpp @@ -1,6 +1,6 @@ #include <library/cpp/testing/unittest/registar.h> #include <library/cpp/ipv6_address/ipv6_address.h> -#include <unordered_set> +#include <unordered_set> class TIpv6AddressTest: public TTestBase { UNIT_TEST_SUITE(TIpv6AddressTest); @@ -11,7 +11,7 @@ class TIpv6AddressTest: public TTestBase { private: void ParseHostAndMayBePortFromString_data(); void CheckAddressValidity(); - void HashCompileTest(); + void HashCompileTest(); }; UNIT_TEST_SUITE_REGISTRATION(TIpv6AddressTest); @@ -78,7 +78,7 @@ void TIpv6AddressTest::ParseHostAndMayBePortFromString_data() { CheckIpDefPortAgainstIpPortDefPort("ya.ru", 123, HostRes("ya.ru", 123), true); } - + void TIpv6AddressTest::CheckAddressValidity() { bool Ok; @@ -106,7 +106,7 @@ void TIpv6AddressTest::CheckAddressValidity() { static_assert(Get1() == TIpv6Address(0, 0, 0, 0, 0, 0, 0, 1)); } -void TIpv6AddressTest::HashCompileTest() { - std::unordered_set<TIpv6Address> test; - Y_UNUSED(test); -} +void TIpv6AddressTest::HashCompileTest() { + std::unordered_set<TIpv6Address> test; + Y_UNUSED(test); +} |