aboutsummaryrefslogtreecommitdiffstats
path: root/util/network/ip.h
diff options
context:
space:
mode:
authorAnton Samokhvalov <pg83@yandex.ru>2022-02-10 16:45:15 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:45:15 +0300
commit72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch)
treeda2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/network/ip.h
parent778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff)
downloadydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/network/ip.h')
-rw-r--r--util/network/ip.h158
1 files changed, 79 insertions, 79 deletions
diff --git a/util/network/ip.h b/util/network/ip.h
index dc7c2d24a0..9307231465 100644
--- a/util/network/ip.h
+++ b/util/network/ip.h
@@ -1,119 +1,119 @@
#pragma once
-
-#include "socket.h"
-#include "hostip.h"
-
-#include <util/system/error.h>
-#include <util/system/byteorder.h>
+
+#include "socket.h"
+#include "hostip.h"
+
+#include <util/system/error.h>
+#include <util/system/byteorder.h>
#include <util/generic/string.h>
-#include <util/generic/yexception.h>
-
+#include <util/generic/yexception.h>
+
/// IPv4 address in network format
using TIpHost = ui32;
/// Port number in host format
using TIpPort = ui16;
-
+
/*
- * ipStr is in 'ddd.ddd.ddd.ddd' format
- * returns IPv4 address in inet format
- */
+ * ipStr is in 'ddd.ddd.ddd.ddd' format
+ * returns IPv4 address in inet format
+ */
static inline TIpHost IpFromString(const char* ipStr) {
in_addr ia;
-
+
if (inet_aton(ipStr, &ia) == 0) {
- ythrow TSystemError() << "Failed to convert (" << ipStr << ") to ip address";
+ ythrow TSystemError() << "Failed to convert (" << ipStr << ") to ip address";
}
-
+
return (ui32)ia.s_addr;
}
-static inline char* IpToString(TIpHost ip, char* buf, size_t len) {
+static inline char* IpToString(TIpHost ip, char* buf, size_t len) {
if (!inet_ntop(AF_INET, (void*)&ip, buf, (socklen_t)len)) {
- ythrow TSystemError() << "Failed to get ip address string";
+ ythrow TSystemError() << "Failed to get ip address string";
}
- return buf;
+ return buf;
}
static inline TString IpToString(TIpHost ip) {
- char buf[INET_ADDRSTRLEN];
-
+ char buf[INET_ADDRSTRLEN];
+
return TString(IpToString(ip, buf, sizeof(buf)));
-}
-
-static inline TIpHost ResolveHost(const char* data, size_t len) {
- TIpHost ret;
+}
+
+static inline TIpHost ResolveHost(const char* data, size_t len) {
+ TIpHost ret;
const TString s(data, len);
-
+
if (NResolver::GetHostIP(s.data(), &ret) != 0) {
- ythrow TSystemError(NResolver::GetDnsError()) << "can not resolve(" << s << ")";
- }
-
- return HostToInet(ret);
-}
-
+ ythrow TSystemError(NResolver::GetDnsError()) << "can not resolve(" << s << ")";
+ }
+
+ return HostToInet(ret);
+}
+
/// socket address
-struct TIpAddress: public sockaddr_in {
+struct TIpAddress: public sockaddr_in {
inline TIpAddress() noexcept {
- Clear();
- }
-
+ Clear();
+ }
+
inline TIpAddress(const sockaddr_in& addr) noexcept
- : sockaddr_in(addr)
+ : sockaddr_in(addr)
, tmp(0)
- {
- }
-
+ {
+ }
+
inline TIpAddress(TIpHost ip, TIpPort port) noexcept {
- Set(ip, port);
- }
-
+ Set(ip, port);
+ }
+
inline TIpAddress(TStringBuf ip, TIpPort port) {
Set(ResolveHost(ip.data(), ip.size()), port);
- }
-
- inline TIpAddress(const char* ip, TIpPort port) {
- Set(ResolveHost(ip, strlen(ip)), port);
- }
-
+ }
+
+ inline TIpAddress(const char* ip, TIpPort port) {
+ Set(ResolveHost(ip, strlen(ip)), port);
+ }
+
inline operator sockaddr*() const noexcept {
- return (sockaddr*)(sockaddr_in*)this;
- }
-
+ return (sockaddr*)(sockaddr_in*)this;
+ }
+
inline operator socklen_t*() const noexcept {
- tmp = sizeof(sockaddr_in);
-
- return (socklen_t*)&tmp;
- }
-
+ tmp = sizeof(sockaddr_in);
+
+ return (socklen_t*)&tmp;
+ }
+
inline operator socklen_t() const noexcept {
- tmp = sizeof(sockaddr_in);
-
- return tmp;
- }
-
+ tmp = sizeof(sockaddr_in);
+
+ return tmp;
+ }
+
inline void Clear() noexcept {
- Zero((sockaddr_in&)(*this));
- }
-
+ Zero((sockaddr_in&)(*this));
+ }
+
inline void Set(TIpHost ip, TIpPort port) noexcept {
- Clear();
-
- sin_family = AF_INET;
- sin_addr.s_addr = ip;
- sin_port = HostToInet(port);
- }
-
+ Clear();
+
+ sin_family = AF_INET;
+ sin_addr.s_addr = ip;
+ sin_port = HostToInet(port);
+ }
+
inline TIpHost Host() const noexcept {
- return sin_addr.s_addr;
- }
-
+ return sin_addr.s_addr;
+ }
+
inline TIpPort Port() const noexcept {
- return InetToHost(sin_port);
- }
-
+ return InetToHost(sin_port);
+ }
+
private:
// required for "operator socklen_t*()"
- mutable socklen_t tmp;
-};
+ mutable socklen_t tmp;
+};