addr_utilities.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <string.h>
  15. #include <dhcp/addr_utilities.h>
  16. using namespace isc::asiolink;
  17. namespace isc {
  18. namespace dhcp {
  19. isc::asiolink::IOAddress firstAddrInPrefix(const isc::asiolink::IOAddress& prefix,
  20. uint8_t len) {
  21. const static uint8_t bitMask[]= { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
  22. uint8_t packed[V6ADDRESS_LEN];
  23. // First we copy the whole address as 16 bytes.
  24. memcpy(packed, prefix.getAddress().to_v6().to_bytes().data(), 16);
  25. // If the length is divisible by 8, it is simple. We just zero out the host
  26. // part. Otherwise we need to handle the byte that has to be partially
  27. // zeroed.
  28. if (len % 8 != 0) {
  29. // Get the appropriate mask. It has relevant bits (those that should
  30. // stay) set and irrelevant (those that should be wiped) cleared.
  31. uint8_t mask = bitMask[len % 8];
  32. // Let's leave only whatever the mask says should not be cleared.
  33. packed[len / 8] = packed[len / 8] & mask;
  34. // Since we have just dealt with this byte, let's move the prefix length
  35. // to the beginning of the next byte (len is expressed in bits).
  36. len = (len / 8 + 1) * 8;
  37. }
  38. // Clear out the remaining bits.
  39. for (int i = len / 8; i < sizeof(packed); ++i) {
  40. packed[i] = 0x0;
  41. }
  42. // Finally, let's wrap this into nice and easy IOAddress object.
  43. return (isc::asiolink::IOAddress::from_bytes(AF_INET6, packed));
  44. }
  45. isc::asiolink::IOAddress lastAddrInPrefix(const isc::asiolink::IOAddress& prefix,
  46. uint8_t len) {
  47. const static uint8_t bitMask[]= { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
  48. uint8_t packed[V6ADDRESS_LEN];
  49. // First we copy the whole address as 16 bytes.
  50. memcpy(packed, prefix.getAddress().to_v6().to_bytes().data(), 16);
  51. // if the length is divisible by 8, it is simple. We just fill the host part
  52. // with ones. Otherwise we need to handle the byte that has to be partially
  53. // zeroed.
  54. if (len % 8 != 0) {
  55. // Get the appropriate mask. It has relevant bits (those that should
  56. // stay) set and irrelevant (those that should be set to 1) cleared.
  57. uint8_t mask = bitMask[len % 8];
  58. // Let's set those irrelevant bits with 1. It would be perhaps
  59. // easier to not use negation here and invert bitMask content. However,
  60. // with this approach, we can use the same mask in first and last
  61. // address calculations.
  62. packed[len / 8] = packed[len / 8] | ~mask;
  63. // Since we have just dealt with this byte, let's move the prefix length
  64. // to the beginning of the next byte (len is expressed in bits).
  65. len = (len / 8 + 1) * 8;
  66. }
  67. // Finally set remaining bits to 1.
  68. for (int i = len / 8; i < sizeof(packed); ++i) {
  69. packed[i] = 0xff;
  70. }
  71. // Finally, let's wrap this into nice and easy IOAddress object.
  72. return (isc::asiolink::IOAddress::from_bytes(AF_INET6, packed));
  73. }
  74. };
  75. };