addr_utilities.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef ADDR_UTILITIES_H
  7. #define ADDR_UTILITIES_H
  8. #include <asiolink/io_address.h>
  9. namespace isc {
  10. namespace dhcp {
  11. /// This code is based on similar code from the Dibbler project. I, Tomasz Mrugalski,
  12. /// as a sole creator of that code hereby release it under BSD license for the benefit
  13. /// of the Kea project.
  14. /// @brief returns a first address in a given prefix
  15. ///
  16. /// Example: For 2001:db8:1\::deaf:beef and length /120 the function will return
  17. /// 2001:db8:1\::dead:be00. See also @ref lastAddrInPrefix.
  18. ///
  19. /// @todo It currently works for v6 only and will throw if v4 address is passed.
  20. ///
  21. /// @param prefix and address that belongs to a prefix
  22. /// @param len prefix length
  23. ///
  24. /// @return first address from a prefix
  25. isc::asiolink::IOAddress firstAddrInPrefix(const isc::asiolink::IOAddress& prefix,
  26. uint8_t len);
  27. /// @brief returns a last address in a given prefix
  28. ///
  29. /// Example: For 2001:db8:1\::deaf:beef and length /112 the function will return
  30. /// 2001:db8:1\::dead:ffff. See also @ref firstAddrInPrefix.
  31. ///
  32. /// @todo It currently works for v6 only and will throw if v4 address is passed.
  33. ///
  34. /// @param prefix and address that belongs to a prefix
  35. /// @param len prefix length
  36. ///
  37. /// @return first address from a prefix
  38. isc::asiolink::IOAddress lastAddrInPrefix(const isc::asiolink::IOAddress& prefix,
  39. uint8_t len);
  40. /// @brief Generates an IPv4 netmask of specified length
  41. /// @throw BadValue if len is greater than 32
  42. /// @return netmask
  43. isc::asiolink::IOAddress getNetmask4(uint8_t len);
  44. /// @brief Returns number of available addresses in the specified range (min - max).
  45. ///
  46. /// Note that for min equal max case, the number of addresses is one.
  47. ///
  48. /// @throw BadValue if min and max are not of the same family (both must be
  49. /// either IPv4 or IPv6) or if max < min.
  50. ///
  51. /// @param min the first address in range
  52. /// @param max the last address in range
  53. /// @return number of addresses in range
  54. uint64_t addrsInRange(const isc::asiolink::IOAddress& min,
  55. const isc::asiolink::IOAddress& max);
  56. /// @brief Returns number of available IPv6 prefixes in the specified prefix.
  57. ///
  58. /// Note that if the answer is bigger than uint64_t can hold, it will return
  59. /// the max value of uint64_t type.
  60. ///
  61. /// Example: prefixesInRange(48, 64) returns 65536, as there are /48 pool
  62. /// can be split into 65536 prefixes, each /64 large.
  63. ///
  64. /// @param pool_len length of the pool in bits
  65. /// @param delegated_len length of the prefixes to be delegated from the pool
  66. /// @return number of prefixes in range
  67. uint64_t prefixesInRange(const uint8_t pool_len, const uint8_t delegated_len);
  68. };
  69. };
  70. #endif // ADDR_UTILITIES_H