ipmath.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #include "ipmath.h"
  2. namespace {
  3. constexpr auto IPV4_BITS = 32;
  4. constexpr auto IPV6_BITS = 128;
  5. const ui128 MAX_IPV4_ADDR = Max<ui32>();
  6. const ui128 MAX_IPV6_ADDR = Max<ui128>();
  7. TStringBuf TypeToString(TIpv6Address::TIpType type) {
  8. switch (type) {
  9. case TIpv6Address::Ipv4:
  10. return TStringBuf("IPv4");
  11. case TIpv6Address::Ipv6:
  12. return TStringBuf("IPv6");
  13. default:
  14. return TStringBuf("UNKNOWN");
  15. }
  16. }
  17. size_t MaxPrefixLenForType(TIpv6Address::TIpType type) {
  18. switch (type) {
  19. case TIpv6Address::Ipv4:
  20. return IPV4_BITS;
  21. case TIpv6Address::Ipv6:
  22. return IPV6_BITS;
  23. case TIpv6Address::LAST:
  24. ythrow yexception() << "invalid type";
  25. }
  26. }
  27. template <ui8 ADDR_LEN>
  28. ui128 LowerBoundForPrefix(ui128 value, ui8 prefixLen) {
  29. const int shift = ADDR_LEN - prefixLen;
  30. const ui128 shifted = (shift < 128) ? (ui128{1} << shift) : 0;
  31. ui128 mask = ~(shifted - 1);
  32. return value & mask;
  33. }
  34. template <ui8 ADDR_LEN>
  35. ui128 UpperBoundForPrefix(ui128 value, ui8 prefixLen) {
  36. const int shift = ADDR_LEN - prefixLen;
  37. const ui128 shifted = (shift < 128) ? (ui128{1} << shift) : 0;
  38. ui128 mask = shifted - 1;
  39. return value | mask;
  40. }
  41. auto LowerBoundForPrefix4 = LowerBoundForPrefix<IPV4_BITS>;
  42. auto LowerBoundForPrefix6 = LowerBoundForPrefix<IPV6_BITS>;
  43. auto UpperBoundForPrefix4 = UpperBoundForPrefix<IPV4_BITS>;
  44. auto UpperBoundForPrefix6 = UpperBoundForPrefix<IPV6_BITS>;
  45. TIpv6Address IpFromStringSafe(const TStringBuf s) {
  46. bool ok{};
  47. auto addr = TIpv6Address::FromString(s, ok);
  48. Y_ENSURE(ok, "Failed to parse an IP address from " << s);
  49. return addr;
  50. }
  51. /// it's different from TIpv6Address::IsValid for 0.0.0.0
  52. bool IsValid(TIpv6Address addr) {
  53. switch (addr.Type()) {
  54. case TIpv6Address::Ipv4:
  55. case TIpv6Address::Ipv6:
  56. return true;
  57. case TIpv6Address::LAST:
  58. return false;
  59. }
  60. }
  61. bool HasNext(TIpv6Address addr) {
  62. switch (addr.Type()) {
  63. case TIpv6Address::Ipv4:
  64. return ui128(addr) != MAX_IPV4_ADDR;
  65. case TIpv6Address::Ipv6:
  66. return ui128(addr) != MAX_IPV6_ADDR;
  67. case TIpv6Address::LAST:
  68. return false;
  69. }
  70. }
  71. TIpv6Address Next(TIpv6Address addr) {
  72. return {ui128(addr) + 1, addr.Type()};
  73. }
  74. } // namespace
  75. TIpv6Address LowerBoundForPrefix(TIpv6Address value, ui8 prefixLen) {
  76. auto type = value.Type();
  77. switch (type) {
  78. case TIpv6Address::Ipv4:
  79. return {LowerBoundForPrefix4(value, prefixLen), type};
  80. case TIpv6Address::Ipv6:
  81. return {LowerBoundForPrefix6(value, prefixLen), type};
  82. default:
  83. ythrow yexception() << "invalid type";
  84. }
  85. }
  86. TIpv6Address UpperBoundForPrefix(TIpv6Address value, ui8 prefixLen) {
  87. auto type = value.Type();
  88. switch (type) {
  89. case TIpv6Address::Ipv4:
  90. return {UpperBoundForPrefix4(value, prefixLen), type};
  91. case TIpv6Address::Ipv6:
  92. return {UpperBoundForPrefix6(value, prefixLen), type};
  93. default:
  94. ythrow yexception() << "invalid type";
  95. }
  96. }
  97. TIpAddressRange::TIpAddressRangeBuilder::operator TIpAddressRange() {
  98. return Build();
  99. }
  100. TIpAddressRange TIpAddressRange::TIpAddressRangeBuilder::Build() {
  101. return TIpAddressRange{Start_, End_};
  102. }
  103. TIpAddressRange::TIpAddressRangeBuilder::TIpAddressRangeBuilder(const TStringBuf from)
  104. : TIpAddressRangeBuilder{IpFromStringSafe(from)}
  105. {
  106. }
  107. TIpAddressRange::TIpAddressRangeBuilder::TIpAddressRangeBuilder(TIpv6Address from) {
  108. Y_ENSURE_EX(IsValid(from), TInvalidIpRangeException() << "Address " << from.ToString() << " is invalid");
  109. Start_ = from;
  110. End_ = Start_;
  111. }
  112. TIpAddressRange::TIpAddressRangeBuilder& TIpAddressRange::TIpAddressRangeBuilder::To(const TStringBuf to) {
  113. End_ = IpFromStringSafe(to);
  114. return *this;
  115. }
  116. TIpAddressRange::TIpAddressRangeBuilder& TIpAddressRange::TIpAddressRangeBuilder::To(TIpv6Address to) {
  117. Y_ENSURE_EX(IsValid(to), TInvalidIpRangeException() << "Address " << to.ToString() << " is invalid");
  118. End_ = to;
  119. return *this;
  120. }
  121. TIpAddressRange::TIpAddressRangeBuilder& TIpAddressRange::TIpAddressRangeBuilder::WithPrefixImpl(ui8 len, bool checkLowerBound) {
  122. Y_ENSURE_EX(IsValid(Start_), TInvalidIpRangeException() << "Start value must be set before prefix");
  123. const auto type = Start_.Type();
  124. const auto maxLen = MaxPrefixLenForType(type);
  125. Y_ENSURE_EX(len <= maxLen, TInvalidIpRangeException() << "Maximum prefix length for this address type is "
  126. << maxLen << ", but requested " << (ui32)len);
  127. const auto lowerBound = LowerBoundForPrefix(Start_, len);
  128. if (checkLowerBound) {
  129. Y_ENSURE_EX(Start_ == lowerBound, TInvalidIpRangeException() << "Cannot create IP range from start address "
  130. << Start_ << " with prefix length " << (ui32)len);
  131. }
  132. Start_ = lowerBound;
  133. End_ = UpperBoundForPrefix(Start_, len);
  134. return *this;
  135. }
  136. TIpAddressRange::TIpAddressRangeBuilder& TIpAddressRange::TIpAddressRangeBuilder::WithPrefix(ui8 len) {
  137. return WithPrefixImpl(len, true);
  138. }
  139. TIpAddressRange::TIpAddressRangeBuilder& TIpAddressRange::TIpAddressRangeBuilder::WithMaskedPrefix(ui8 len) {
  140. return WithPrefixImpl(len, false);
  141. }
  142. void TIpAddressRange::Init(TIpv6Address from, TIpv6Address to) {
  143. Start_ = from;
  144. End_ = to;
  145. Y_ENSURE_EX(Start_ <= End_, TInvalidIpRangeException() << "Invalid IP address range: from " << Start_ << " to " << End_);
  146. Y_ENSURE_EX(Start_.Type() == End_.Type(), TInvalidIpRangeException()
  147. << "Address type mismtach: start address type is " << TypeToString(Start_.Type())
  148. << " end type is " << TypeToString(End_.Type()));
  149. }
  150. TIpAddressRange::TIpAddressRange(TIpv6Address start, TIpv6Address end) {
  151. Y_ENSURE_EX(IsValid(start), TInvalidIpRangeException() << "start address " << start.ToString() << " is invalid");
  152. Y_ENSURE_EX(IsValid(end), TInvalidIpRangeException() << "end address " << end.ToString() << " is invalid");
  153. Init(start, end);
  154. }
  155. TIpAddressRange::TIpAddressRange(const TStringBuf start, const TStringBuf end) {
  156. auto startAddr = IpFromStringSafe(start);
  157. auto endAddr = IpFromStringSafe(end);
  158. Init(startAddr, endAddr);
  159. }
  160. TIpAddressRange::~TIpAddressRange() {
  161. }
  162. TIpAddressRange::TIpType TIpAddressRange::Type() const {
  163. return Start_.Type();
  164. }
  165. ui128 TIpAddressRange::Size() const {
  166. return ui128(End_) - ui128(Start_) + 1;
  167. }
  168. bool TIpAddressRange::IsSingle() const {
  169. return Start_ == End_;
  170. }
  171. bool TIpAddressRange::Contains(const TIpAddressRange& other) const {
  172. return Start_ <= other.Start_ && End_ >= other.End_;
  173. }
  174. bool TIpAddressRange::Contains(const TIpv6Address& addr) const {
  175. return Start_ <= addr && End_ >= addr;
  176. }
  177. bool TIpAddressRange::Overlaps(const TIpAddressRange& other) const {
  178. return Start_ <= other.End_ && other.Start_ <= End_;
  179. }
  180. bool TIpAddressRange::IsConsecutive(const TIpAddressRange& other) const {
  181. return (HasNext(End_) && Next(End_) == other.Start_)
  182. || (HasNext(other.End_) && Next(other.End_) == Start_);
  183. }
  184. TIpAddressRange TIpAddressRange::Union(const TIpAddressRange& other) const {
  185. Y_ENSURE(IsConsecutive(other) || Overlaps(other), "Can merge only consecutive or overlapping ranges");
  186. Y_ENSURE(other.Start_.Type() == Start_.Type(), "Cannot merge ranges of addresses of different types");
  187. auto s = Start_;
  188. auto e = End_;
  189. s = {Min<ui128>(Start_, other.Start_), Start_.Type()};
  190. e = {Max<ui128>(End_, other.End_), End_.Type()};
  191. return {s, e};
  192. }
  193. TIpAddressRange TIpAddressRange::FromCidrString(const TStringBuf str) {
  194. if (auto result = TryFromCidrString(str)) {
  195. return *result;
  196. }
  197. ythrow TInvalidIpRangeException() << "Cannot parse " << str << " as a CIDR string";
  198. }
  199. TMaybe<TIpAddressRange> TIpAddressRange::TryFromCidrStringImpl(const TStringBuf str, bool compact) {
  200. auto idx = str.rfind('/');
  201. if (idx == TStringBuf::npos) {
  202. return Nothing();
  203. }
  204. TStringBuf sb{str};
  205. TStringBuf address, prefix;
  206. sb.SplitAt(idx, address, prefix);
  207. prefix.Skip(1);
  208. ui8 prefixLen{};
  209. if (!::TryFromString(prefix, prefixLen)) {
  210. return Nothing();
  211. }
  212. return compact ?
  213. TIpAddressRange::From(address).WithMaskedPrefix(prefixLen) :
  214. TIpAddressRange::From(address).WithPrefix(prefixLen);
  215. }
  216. TMaybe<TIpAddressRange> TIpAddressRange::TryFromCidrString(const TStringBuf str) {
  217. return TryFromCidrStringImpl(str, false);
  218. }
  219. TIpAddressRange TIpAddressRange::FromCompactString(const TStringBuf str) {
  220. if (auto result = TryFromCompactString(str)) {
  221. return *result;
  222. }
  223. ythrow TInvalidIpRangeException() << "Cannot parse " << str << " as a CIDR string";
  224. }
  225. TMaybe<TIpAddressRange> TIpAddressRange::TryFromCompactString(const TStringBuf str) {
  226. return TryFromCidrStringImpl(str, true);
  227. }
  228. TIpAddressRange TIpAddressRange::FromRangeString(const TStringBuf str) {
  229. if (auto result = TryFromRangeString(str)) {
  230. return *result;
  231. }
  232. ythrow TInvalidIpRangeException() << "Cannot parse " << str << " as a range string";
  233. }
  234. TMaybe<TIpAddressRange> TIpAddressRange::TryFromRangeString(const TStringBuf str) {
  235. auto idx = str.find('-');
  236. if (idx == TStringBuf::npos) {
  237. return Nothing();
  238. }
  239. TStringBuf sb{str};
  240. TStringBuf start, end;
  241. sb.SplitAt(idx, start, end);
  242. end.Skip(1);
  243. return TIpAddressRange::From(start).To(end);
  244. }
  245. TIpAddressRange TIpAddressRange::FromString(const TStringBuf str) {
  246. if (auto result = TryFromString(str)) {
  247. return *result;
  248. }
  249. ythrow TInvalidIpRangeException() << "Cannot parse an IP address from " << str;
  250. }
  251. TMaybe<TIpAddressRange> TIpAddressRange::TryFromString(const TStringBuf str) {
  252. if (auto idx = str.find('/'); idx != TStringBuf::npos) {
  253. return TryFromCidrString(str);
  254. } else if (idx = str.find('-'); idx != TStringBuf::npos) {
  255. return TryFromRangeString(str);
  256. } else {
  257. bool ok{};
  258. auto addr = TIpv6Address::FromString(str, ok);
  259. if (!ok) {
  260. return Nothing();
  261. }
  262. return TIpAddressRange::From(addr);
  263. }
  264. }
  265. TString TIpAddressRange::ToRangeString() const {
  266. bool ok{};
  267. return TStringBuilder() << Start_.ToString(ok) << "-" << End_.ToString(ok);
  268. }
  269. TIpAddressRange::TIterator TIpAddressRange::begin() const {
  270. return Begin();
  271. }
  272. TIpAddressRange::TIterator TIpAddressRange::Begin() const {
  273. return TIpAddressRange::TIterator{Start_};
  274. }
  275. TIpAddressRange::TIterator TIpAddressRange::end() const {
  276. return End();
  277. }
  278. TIpAddressRange::TIterator TIpAddressRange::End() const {
  279. return TIpAddressRange::TIterator{{ui128(End_) + 1, End_.Type()}};
  280. }
  281. TIpAddressRange::TIpAddressRangeBuilder TIpAddressRange::From(TIpv6Address from) {
  282. return TIpAddressRangeBuilder{from};
  283. }
  284. TIpAddressRange::TIpAddressRangeBuilder TIpAddressRange::From(const TStringBuf from) {
  285. return TIpAddressRangeBuilder{from};
  286. }
  287. bool operator==(const TIpAddressRange& lhs, const TIpAddressRange& rhs) {
  288. return lhs.Start_ == rhs.Start_ && lhs.End_ == rhs.End_;
  289. }
  290. bool operator!=(const TIpAddressRange& lhs, const TIpAddressRange& rhs) {
  291. return !(lhs == rhs);
  292. }
  293. TIpAddressRange::TIterator::TIterator(TIpv6Address val) noexcept
  294. : Current_{val}
  295. {
  296. }
  297. bool TIpAddressRange::TIterator::operator==(const TIpAddressRange::TIterator& other) noexcept {
  298. return Current_ == other.Current_;
  299. }
  300. bool TIpAddressRange::TIterator::operator!=(const TIpAddressRange::TIterator& other) noexcept {
  301. return !(*this == other);
  302. }
  303. TIpAddressRange::TIterator& TIpAddressRange::TIterator::operator++() noexcept {
  304. ui128 numeric = Current_;
  305. Current_ = {numeric + 1, Current_.Type()};
  306. return *this;
  307. }
  308. const TIpv6Address& TIpAddressRange::TIterator::operator*() noexcept {
  309. return Current_;
  310. }