aboutsummaryrefslogtreecommitdiffstats
path: root/util/network/address.cpp
diff options
context:
space:
mode:
authorAnton Samokhvalov <pg83@yandex.ru>2022-02-10 16:45:17 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:45:17 +0300
commitd3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch)
treedd4bd3ca0f36b817e96812825ffaf10d645803f2 /util/network/address.cpp
parent72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff)
downloadydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'util/network/address.cpp')
-rw-r--r--util/network/address.cpp206
1 files changed, 103 insertions, 103 deletions
diff --git a/util/network/address.cpp b/util/network/address.cpp
index 8352743fdf..a81a9e6994 100644
--- a/util/network/address.cpp
+++ b/util/network/address.cpp
@@ -1,65 +1,65 @@
#include <util/stream/str.h>
-#include "address.h"
-
-#if defined(_unix_)
- #include <sys/types.h>
- #include <sys/un.h>
-#endif
-
-using namespace NAddr;
-
-template <bool printPort>
+#include "address.h"
+
+#if defined(_unix_)
+ #include <sys/types.h>
+ #include <sys/un.h>
+#endif
+
+using namespace NAddr;
+
+template <bool printPort>
static inline void PrintAddr(IOutputStream& out, const IRemoteAddr& addr) {
- const sockaddr* a = addr.Addr();
- char buf[INET6_ADDRSTRLEN + 10];
-
- switch (a->sa_family) {
- case AF_INET: {
- const TIpAddress sa(*(const sockaddr_in*)a);
-
- out << IpToString(sa.Host(), buf, sizeof(buf));
-
- if (printPort) {
- out << ":" << sa.Port();
- }
-
- break;
- }
-
- case AF_INET6: {
- const sockaddr_in6* sa = (const sockaddr_in6*)a;
-
- if (!inet_ntop(AF_INET6, (void*)&sa->sin6_addr.s6_addr, buf, sizeof(buf))) {
- ythrow TSystemError() << "inet_ntop() failed";
- }
-
- if (printPort) {
- out << "[" << buf << "]"
- << ":" << InetToHost(sa->sin6_port);
+ const sockaddr* a = addr.Addr();
+ char buf[INET6_ADDRSTRLEN + 10];
+
+ switch (a->sa_family) {
+ case AF_INET: {
+ const TIpAddress sa(*(const sockaddr_in*)a);
+
+ out << IpToString(sa.Host(), buf, sizeof(buf));
+
+ if (printPort) {
+ out << ":" << sa.Port();
+ }
+
+ break;
+ }
+
+ case AF_INET6: {
+ const sockaddr_in6* sa = (const sockaddr_in6*)a;
+
+ if (!inet_ntop(AF_INET6, (void*)&sa->sin6_addr.s6_addr, buf, sizeof(buf))) {
+ ythrow TSystemError() << "inet_ntop() failed";
+ }
+
+ if (printPort) {
+ out << "[" << buf << "]"
+ << ":" << InetToHost(sa->sin6_port);
} else {
out << buf;
- }
-
- break;
- }
-
+ }
+
+ break;
+ }
+
#if defined(AF_UNIX)
case AF_UNIX: {
- const sockaddr_un* sa = (const sockaddr_un*)a;
-
- out << TStringBuf(sa->sun_path);
-
- break;
- }
-#endif
-
- default: {
+ const sockaddr_un* sa = (const sockaddr_un*)a;
+
+ out << TStringBuf(sa->sun_path);
+
+ break;
+ }
+#endif
+
+ default: {
size_t len = addr.Len();
- const char* b = (const char*)a;
+ const char* b = (const char*)a;
const char* e = b + len;
-
+
bool allZeros = true;
for (size_t i = 0; i < len; ++i) {
if (b[i] != 0) {
@@ -67,11 +67,11 @@ static inline void PrintAddr(IOutputStream& out, const IRemoteAddr& addr) {
break;
}
}
-
+
if (allZeros) {
out << "(raw all zeros)";
} else {
- out << "(raw " << (int)a->sa_family << " ";
+ out << "(raw " << (int)a->sa_family << " ";
while (b != e) {
//just print raw bytes
@@ -82,18 +82,18 @@ static inline void PrintAddr(IOutputStream& out, const IRemoteAddr& addr) {
}
out << ")";
- }
-
- break;
- }
- }
-}
-
-template <>
+ }
+
+ break;
+ }
+ }
+}
+
+template <>
void Out<IRemoteAddr>(IOutputStream& out, const IRemoteAddr& addr) {
- PrintAddr<true>(out, addr);
-}
-
+ PrintAddr<true>(out, addr);
+}
+
template <>
void Out<NAddr::TAddrInfo>(IOutputStream& out, const NAddr::TAddrInfo& addr) {
PrintAddr<true>(out, addr);
@@ -115,9 +115,9 @@ void Out<NAddr::TOpaqueAddr>(IOutputStream& out, const NAddr::TOpaqueAddr& addr)
}
void NAddr::PrintHost(IOutputStream& out, const IRemoteAddr& addr) {
- PrintAddr<false>(out, addr);
-}
-
+ PrintAddr<false>(out, addr);
+}
+
TString NAddr::PrintHost(const IRemoteAddr& addr) {
TStringStream ss;
PrintAddr<false>(ss, addr);
@@ -130,15 +130,15 @@ TString NAddr::PrintHostAndPort(const IRemoteAddr& addr) {
return ss.Str();
}
-IRemoteAddrPtr NAddr::GetSockAddr(SOCKET s) {
+IRemoteAddrPtr NAddr::GetSockAddr(SOCKET s) {
auto addr = MakeHolder<TOpaqueAddr>();
-
- if (getsockname(s, addr->MutableAddr(), addr->LenPtr()) < 0) {
- ythrow TSystemError() << "getsockname() failed";
- }
-
+
+ if (getsockname(s, addr->MutableAddr(), addr->LenPtr()) < 0) {
+ ythrow TSystemError() << "getsockname() failed";
+ }
+
return addr;
-}
+}
IRemoteAddrPtr NAddr::GetPeerAddr(SOCKET s) {
auto addr = MakeHolder<TOpaqueAddr>();
@@ -161,44 +161,44 @@ static const in6_addr& In6Addr(const IRemoteAddr& addr) {
bool NAddr::IsLoopback(const IRemoteAddr& addr) {
if (addr.Addr()->sa_family == AF_INET) {
return ((ntohl(InAddr(addr).s_addr) >> 24) & 0xff) == 127;
- }
-
- if (addr.Addr()->sa_family == AF_INET6) {
+ }
+
+ if (addr.Addr()->sa_family == AF_INET6) {
return 0 == memcmp(&In6Addr(addr), &in6addr_loopback, sizeof(in6_addr));
}
-
- return false;
+
+ return false;
}
bool NAddr::IsSame(const IRemoteAddr& lhs, const IRemoteAddr& rhs) {
if (lhs.Addr()->sa_family != rhs.Addr()->sa_family) {
return false;
- }
-
- if (lhs.Addr()->sa_family == AF_INET) {
+ }
+
+ if (lhs.Addr()->sa_family == AF_INET) {
return InAddr(lhs).s_addr == InAddr(rhs).s_addr;
- }
-
- if (lhs.Addr()->sa_family == AF_INET6) {
+ }
+
+ if (lhs.Addr()->sa_family == AF_INET6) {
return 0 == memcmp(&In6Addr(lhs), &In6Addr(rhs), sizeof(in6_addr));
}
ythrow yexception() << "unsupported addr family: " << lhs.Addr()->sa_family;
}
-
-socklen_t NAddr::SockAddrLength(const sockaddr* addr) {
- switch (addr->sa_family) {
- case AF_INET:
- return sizeof(sockaddr_in);
-
- case AF_INET6:
- return sizeof(sockaddr_in6);
-
-#if defined(AF_LOCAL)
- case AF_LOCAL:
- return sizeof(sockaddr_un);
-#endif
- }
-
- ythrow yexception() << "unsupported address family: " << addr->sa_family;
-}
+
+socklen_t NAddr::SockAddrLength(const sockaddr* addr) {
+ switch (addr->sa_family) {
+ case AF_INET:
+ return sizeof(sockaddr_in);
+
+ case AF_INET6:
+ return sizeof(sockaddr_in6);
+
+#if defined(AF_LOCAL)
+ case AF_LOCAL:
+ return sizeof(sockaddr_un);
+#endif
+ }
+
+ ythrow yexception() << "unsupported address family: " << addr->sa_family;
+}