ipv6_address.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. static TIpv6Address FromString(TStringBuf str) noexcept;
  53. constexpr bool IsNull() const noexcept {
  54. return Ip == 0;
  55. }
  56. constexpr bool IsValid() const noexcept {
  57. return Ip != 0 && (Type_ == Ipv6 || Type_ == Ipv4);
  58. }
  59. explicit constexpr operator bool() const noexcept {
  60. return IsValid();
  61. }
  62. constexpr bool operator ! () const noexcept {
  63. return !IsValid();
  64. }
  65. bool Isv4MappedTov6() const noexcept;
  66. TIpv6Address TryToExtractIpv4From6() const noexcept;
  67. TIpv6Address Normalized() const noexcept;
  68. TString ToString(bool* ok = nullptr) const noexcept;
  69. TString ToString(bool PrintScopeId, bool* ok = nullptr) const noexcept;
  70. void ToSockaddrAndSocklen(sockaddr_in& sockAddrIPv4,
  71. sockaddr_in6& sockAddrIPv6, // in
  72. const sockaddr*& sockAddrPtr,
  73. socklen_t& sockAddrSize,
  74. ui16 Port) const; // out
  75. void ToInAddr(in_addr& Addr4) const;
  76. void ToIn6Addr(in6_addr& Addr6) const;
  77. // int SocketFamily() const;
  78. constexpr bool operator<(const TIpv6Address& other) const noexcept {
  79. if (Type_ != other.Type_)
  80. return Type_ > other.Type_;
  81. else
  82. return Ip < other.Ip;
  83. }
  84. constexpr bool operator>(const TIpv6Address& other) const noexcept {
  85. if (Type_ != other.Type_)
  86. return Type_ < other.Type_;
  87. else
  88. return Ip > other.Ip;
  89. }
  90. constexpr bool operator==(const TIpv6Address& other) const noexcept {
  91. return Type_ == other.Type_ && Ip == other.Ip;
  92. }
  93. constexpr bool operator!=(const TIpv6Address& other) const noexcept {
  94. return Type_ != other.Type_ || Ip != other.Ip;
  95. }
  96. constexpr bool operator<=(const TIpv6Address& other) const noexcept {
  97. return !(*this > other);
  98. }
  99. constexpr bool operator>=(const TIpv6Address& other) const noexcept {
  100. return !(*this < other);
  101. }
  102. constexpr operator ui128() const noexcept {
  103. return Ip;
  104. }
  105. constexpr TIpType Type() const noexcept {
  106. return Type_;
  107. }
  108. void SetScopeId(ui32 New) noexcept {
  109. ScopeId_ = New;
  110. }
  111. constexpr ui32 ScopeId() const noexcept {
  112. return ScopeId_;
  113. }
  114. void Save(IOutputStream* out) const;
  115. void Load(IInputStream* in);
  116. private:
  117. void InitFrom(const in6_addr& addr);
  118. void InitFrom(const in_addr& addr);
  119. void InitFrom(const sockaddr_in6& Addr);
  120. void InitFrom(const sockaddr_in& Addr);
  121. ui128 Ip{};
  122. TIpType Type_ = LAST;
  123. ui32 ScopeId_ = 0;
  124. };
  125. IOutputStream& operator<<(IOutputStream& Out, const TIpv6Address::TIpType Type) noexcept;
  126. IOutputStream& operator<<(IOutputStream& Out, const TIpv6Address& ipv6Address) noexcept;
  127. constexpr TIpv6Address Get127001() noexcept {
  128. return {127, 0, 0, 1};
  129. }
  130. constexpr TIpv6Address Get1() noexcept {
  131. return {1, TIpv6Address::Ipv6};
  132. }
  133. struct THostAddressAndPort {
  134. constexpr THostAddressAndPort() noexcept = default;
  135. constexpr THostAddressAndPort(const TIpv6Address& i, TIpPort p) noexcept {
  136. Ip = i;
  137. Port = p;
  138. }
  139. constexpr bool operator==(const THostAddressAndPort& Other) const noexcept {
  140. return Ip == Other.Ip && Port == Other.Port;
  141. }
  142. constexpr bool operator!=(const THostAddressAndPort& Other) const noexcept {
  143. return !(*this == Other);
  144. }
  145. constexpr bool IsValid() const noexcept {
  146. return Ip.IsValid() && Port != 0;
  147. }
  148. TString ToString() const noexcept;
  149. TIpv6Address Ip {};
  150. TIpPort Port {0};
  151. };
  152. IOutputStream& operator<<(IOutputStream& Out, const THostAddressAndPort& HostAddressAndPort) noexcept;
  153. ///
  154. /// Returns
  155. /// 1. either valid THostAddressAndPort
  156. /// 2. or TString with hostname (which you should resolve) and TIpPort with port
  157. /// 3. or error, if Ok == false
  158. ///
  159. /// Supported RawStrs are
  160. /// 1.2.3.4 // port wil be equal to DefaultPort
  161. /// 1.2.3.4:80
  162. /// [2001::7348] // port wil be equal to DefaultPort
  163. /// 2001::7348 // port wil be equal to DefaultPort
  164. /// [2001::7348]:80
  165. ///
  166. std::tuple<THostAddressAndPort, TString, TIpPort> ParseHostAndMayBePortFromString(const TString& RawStr,
  167. TIpPort DefaultPort,
  168. bool& Ok) noexcept;
  169. using TIpv6AddressesSet = THashSet<TIpv6Address>;
  170. template <>
  171. struct THash<TIpv6Address> {
  172. inline size_t operator()(const TIpv6Address& ip) const {
  173. const ui128& Tmp = static_cast<ui128>(ip);
  174. return CombineHashes(THash<ui128>()(Tmp), THash<ui8>()(static_cast<ui8>(ip.Type())));
  175. }
  176. };
  177. template <>
  178. struct THash<THostAddressAndPort> {
  179. inline size_t operator()(const THostAddressAndPort& IpAndPort) const {
  180. return CombineHashes(THash<TIpv6Address>()(IpAndPort.Ip), THash<TIpPort>()(IpAndPort.Port));
  181. }
  182. };
  183. namespace std {
  184. template <>
  185. struct hash<TIpv6Address> {
  186. std::size_t operator()(const TIpv6Address& Ip) const noexcept {
  187. return THash<TIpv6Address>()(Ip);
  188. }
  189. };
  190. }
  191. NAddr::IRemoteAddr* ToIRemoteAddr(const TIpv6Address& Address, TIpPort Port);
  192. // template <>
  193. // class TSerializer<TIpv6Address> {
  194. // public:
  195. // static void Save(IOutputStream *out, const TIpv6Address &ip);
  196. // static void Load(IInputStream *in, TIpv6Address &ip);
  197. //};