ipv6_address.h 7.2 KB

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