ipv6_address.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #pragma once
  2. #include <util/generic/hash_set.h>
  3. #include <util/generic/strbuf.h>
  4. #include <util/network/ip.h>
  5. #include <util/stream/input.h>
  6. #include <library/cpp/int128/int128.h>
  7. #if defined(_freebsd_)
  8. // #include required to avoid problem with undefined 'socklen_t' on FreeBSD
  9. #include <sys/socket.h>
  10. #endif
  11. #if defined(_win_)
  12. // #include required to avoid problem with undefined 'socklen_t' on Windows
  13. #include <util/network/socket.h>
  14. #endif
  15. namespace NAddr {
  16. class IRemoteAddr;
  17. }
  18. struct in6_addr;
  19. struct in_addr;
  20. struct sockaddr;
  21. struct sockaddr_in;
  22. struct sockaddr_in6;
  23. // TODO (dimanne): rename to something like TIntInetAddress or THostAddress
  24. class TIpv6Address {
  25. public:
  26. enum TIpType { Ipv6,
  27. Ipv4,
  28. LAST };
  29. constexpr TIpv6Address() noexcept = default;
  30. constexpr TIpv6Address(const TIpv6Address&) noexcept = default;
  31. constexpr TIpv6Address& operator=(const TIpv6Address&) noexcept = default;
  32. constexpr TIpv6Address(const ui128& ip, TIpType Type) noexcept
  33. : Ip(ip), Type_(Type)
  34. {}
  35. constexpr TIpv6Address(ui8 a, ui8 b, ui8 c, ui8 d) noexcept
  36. : Ip((ui32(a) << 24) | (ui32(b) << 16) | (ui32(c) << 8) | ui32(d))
  37. , Type_(TIpv6Address::Ipv4)
  38. {}
  39. constexpr TIpv6Address(ui16 a, ui16 b, ui16 c, ui16 d, ui16 e, ui16 f, ui16 g, ui16 h, ui32 scope = 0) noexcept
  40. : Type_(TIpv6Address::Ipv6)
  41. , ScopeId_(scope)
  42. {
  43. auto hi = (ui64(a) << 48) | (ui64(b) << 32) | (ui64(c) << 16) | ui64(d);
  44. auto lo = (ui64(e) << 48) | (ui64(f) << 32) | (ui64(g) << 16) | ui64(h);
  45. Ip = {hi, lo};
  46. }
  47. explicit TIpv6Address(const NAddr::IRemoteAddr& addr);
  48. explicit TIpv6Address(const sockaddr_in6& Addr);
  49. explicit TIpv6Address(const sockaddr_in& Addr);
  50. explicit TIpv6Address(const in6_addr& addr, ui32 Scope);
  51. explicit TIpv6Address(const in_addr& addr);
  52. static TIpv6Address FromString(TStringBuf srcStr, bool& ok) noexcept;
  53. static TIpv6Address FromString(TStringBuf str) noexcept;
  54. constexpr bool IsNull() const noexcept {
  55. return Ip == 0;
  56. }
  57. constexpr bool IsValid() const noexcept {
  58. return Ip != 0 && (Type_ == Ipv6 || Type_ == Ipv4);
  59. }
  60. explicit constexpr operator bool() const noexcept {
  61. return IsValid();
  62. }
  63. constexpr bool operator ! () const noexcept {
  64. return !IsValid();
  65. }
  66. bool Isv4MappedTov6() const noexcept;
  67. TIpv6Address TryToExtractIpv4From6() const noexcept;
  68. TIpv6Address Normalized() const noexcept;
  69. TString ToString(bool* ok = nullptr) const noexcept;
  70. TString ToString(bool PrintScopeId, bool* ok = nullptr) const noexcept;
  71. void ToSockaddrAndSocklen(sockaddr_in& sockAddrIPv4,
  72. sockaddr_in6& sockAddrIPv6, // in
  73. const sockaddr*& sockAddrPtr,
  74. socklen_t& sockAddrSize,
  75. ui16 Port) const; // out
  76. void ToInAddr(in_addr& Addr4) const;
  77. void ToIn6Addr(in6_addr& Addr6) const;
  78. // int SocketFamily() const;
  79. constexpr bool operator<(const TIpv6Address& other) const noexcept {
  80. if (Type_ != other.Type_)
  81. return Type_ > other.Type_;
  82. else
  83. return Ip < other.Ip;
  84. }
  85. constexpr bool operator>(const TIpv6Address& other) const noexcept {
  86. if (Type_ != other.Type_)
  87. return Type_ < other.Type_;
  88. else
  89. return Ip > other.Ip;
  90. }
  91. constexpr bool operator==(const TIpv6Address& other) const noexcept {
  92. return Type_ == other.Type_ && Ip == other.Ip;
  93. }
  94. constexpr bool operator!=(const TIpv6Address& other) const noexcept {
  95. return Type_ != other.Type_ || Ip != other.Ip;
  96. }
  97. constexpr bool operator<=(const TIpv6Address& other) const noexcept {
  98. return !(*this > other);
  99. }
  100. constexpr bool operator>=(const TIpv6Address& other) const noexcept {
  101. return !(*this < other);
  102. }
  103. constexpr operator ui128() const noexcept {
  104. return Ip;
  105. }
  106. constexpr TIpType Type() const noexcept {
  107. return Type_;
  108. }
  109. void SetScopeId(ui32 New) noexcept {
  110. ScopeId_ = New;
  111. }
  112. constexpr ui32 ScopeId() const noexcept {
  113. return ScopeId_;
  114. }
  115. void Save(IOutputStream* out) const;
  116. void Load(IInputStream* in);
  117. private:
  118. void InitFrom(const in6_addr& addr);
  119. void InitFrom(const in_addr& addr);
  120. void InitFrom(const sockaddr_in6& Addr);
  121. void InitFrom(const sockaddr_in& Addr);
  122. ui128 Ip{};
  123. TIpType Type_ = LAST;
  124. ui32 ScopeId_ = 0;
  125. };
  126. IOutputStream& operator<<(IOutputStream& Out, const TIpv6Address::TIpType Type) noexcept;
  127. IOutputStream& operator<<(IOutputStream& Out, const TIpv6Address& ipv6Address) noexcept;
  128. constexpr TIpv6Address Get127001() noexcept {
  129. return {127, 0, 0, 1};
  130. }
  131. constexpr TIpv6Address Get1() noexcept {
  132. return {1, TIpv6Address::Ipv6};
  133. }
  134. struct THostAddressAndPort {
  135. constexpr THostAddressAndPort() noexcept = default;
  136. constexpr THostAddressAndPort(const TIpv6Address& i, TIpPort p) noexcept {
  137. Ip = i;
  138. Port = p;
  139. }
  140. constexpr bool operator==(const THostAddressAndPort& Other) const noexcept {
  141. return Ip == Other.Ip && Port == Other.Port;
  142. }
  143. constexpr bool operator!=(const THostAddressAndPort& Other) const noexcept {
  144. return !(*this == Other);
  145. }
  146. constexpr bool IsValid() const noexcept {
  147. return Ip.IsValid() && Port != 0;
  148. }
  149. TString ToString() const noexcept;
  150. TIpv6Address Ip {};
  151. TIpPort Port {0};
  152. };
  153. IOutputStream& operator<<(IOutputStream& Out, const THostAddressAndPort& HostAddressAndPort) noexcept;
  154. ///
  155. /// Returns
  156. /// 1. either valid THostAddressAndPort
  157. /// 2. or TString with hostname (which you should resolve) and TIpPort with port
  158. /// 3. or error, if Ok == false
  159. ///
  160. /// Supported RawStrs are
  161. /// 1.2.3.4 // port wil be equal to DefaultPort
  162. /// 1.2.3.4:80
  163. /// [2001::7348] // port wil be equal to DefaultPort
  164. /// 2001::7348 // port wil be equal to DefaultPort
  165. /// [2001::7348]:80
  166. ///
  167. std::tuple<THostAddressAndPort, TString, TIpPort> ParseHostAndMayBePortFromString(const TStringBuf RawStr,
  168. TIpPort DefaultPort,
  169. bool& Ok) noexcept;
  170. using TIpv6AddressesSet = THashSet<TIpv6Address>;
  171. template <>
  172. struct THash<TIpv6Address> {
  173. inline size_t operator()(const TIpv6Address& ip) const {
  174. const ui128& Tmp = static_cast<ui128>(ip);
  175. return CombineHashes(THash<ui128>()(Tmp), THash<ui8>()(static_cast<ui8>(ip.Type())));
  176. }
  177. };
  178. template <>
  179. struct THash<THostAddressAndPort> {
  180. inline size_t operator()(const THostAddressAndPort& IpAndPort) const {
  181. return CombineHashes(THash<TIpv6Address>()(IpAndPort.Ip), THash<TIpPort>()(IpAndPort.Port));
  182. }
  183. };
  184. namespace std {
  185. template <>
  186. struct hash<TIpv6Address> {
  187. std::size_t operator()(const TIpv6Address& Ip) const noexcept {
  188. return THash<TIpv6Address>()(Ip);
  189. }
  190. };
  191. }
  192. NAddr::IRemoteAddr* ToIRemoteAddr(const TIpv6Address& Address, TIpPort Port);
  193. // template <>
  194. // class TSerializer<TIpv6Address> {
  195. // public:
  196. // static void Save(IOutputStream *out, const TIpv6Address &ip);
  197. // static void Load(IInputStream *in, TIpv6Address &ip);
  198. //};