addr_utilities.cc 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <dhcp/addr_utilities.h>
  15. using namespace isc::asiolink;
  16. namespace isc {
  17. namespace dhcp {
  18. isc::asiolink::IOAddress firstAddrInPrefix(const isc::asiolink::IOAddress& prefix,
  19. uint8_t len) {
  20. const static uint8_t bitMask[]= { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
  21. uint8_t packed[V6ADDRESS_LEN];
  22. // First we copy the whole address as 16 bytes.
  23. memcpy(packed, prefix.getAddress().to_v6().to_bytes().data(), 16);
  24. // If the length is divisible by 8, it is simple. We just zero out the host
  25. // part. Otherwise we need to handle the byte that has to be partially
  26. // zeroed.
  27. if (len % 8 != 0) {
  28. // Get the appropriate mask. It has relevant bits (those that should
  29. // stay) set and irrelevant (those that should be wiped) cleared.
  30. uint8_t mask = bitMask[len % 8];
  31. // Let's leave only whatever the mask says should not be cleared.
  32. packed[len / 8] = packed[len / 8] & mask;
  33. // Since we have just dealt with this byte, let's move the prefix length
  34. // to the beginning of the next byte (len is expressed in bits).
  35. len = (len / 8 + 1) * 8;
  36. }
  37. // Clear out the remaining bits.
  38. for (int i = len / 8; i < sizeof(packed); ++i) {
  39. packed[i] = 0x0;
  40. }
  41. // Finally, let's wrap this into nice and easy IOAddress object.
  42. return (isc::asiolink::IOAddress::from_bytes(AF_INET6, packed));
  43. }
  44. isc::asiolink::IOAddress lastAddrInPrefix(const isc::asiolink::IOAddress& prefix,
  45. uint8_t len) {
  46. const static uint8_t bitMask[]= { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
  47. uint8_t packed[V6ADDRESS_LEN];
  48. // First we copy the whole address as 16 bytes.
  49. memcpy(packed, prefix.getAddress().to_v6().to_bytes().data(), 16);
  50. // if the length is divisible by 8, it is simple. We just fill the host part
  51. // with ones. Otherwise we need to handle the byte that has to be partially
  52. // zeroed.
  53. if (len % 8 != 0) {
  54. // Get the appropriate mask. It has relevant bits (those that should
  55. // stay) set and irrelevant (those that should be set to 1) cleared.
  56. uint8_t mask = bitMask[len % 8];
  57. // Let's set those irrelevant bits with 1. It would be perhaps
  58. // easier to not use negation here and invert bitMask content. However,
  59. // with this approach, we can use the same mask in first and last
  60. // address calculations.
  61. packed[len / 8] = packed[len / 8] | ~mask;
  62. // Since we have just dealt with this byte, let's move the prefix length
  63. // to the beginning of the next byte (len is expressed in bits).
  64. len = (len / 8 + 1) * 8;
  65. }
  66. // Finally set remaining bits to 1.
  67. for (int i = len / 8; i < sizeof(packed); ++i) {
  68. packed[i] = 0xff;
  69. }
  70. // Finally, let's wrap this into nice and easy IOAddress object.
  71. return (isc::asiolink::IOAddress::from_bytes(AF_INET6, packed));
  72. }
  73. };
  74. };