nsec_bitmap.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (C) 2011 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 <stdint.h>
  15. #include <vector>
  16. #include <dns/exceptions.h>
  17. using namespace std;
  18. namespace isc {
  19. namespace dns {
  20. namespace rdata {
  21. namespace generic {
  22. namespace detail {
  23. namespace nsec {
  24. void
  25. checkRRTypeBitmaps(const char* const rrtype_name,
  26. const vector<uint8_t>& typebits)
  27. {
  28. bool first = true;
  29. unsigned int lastblock = 0;
  30. const size_t total_len = typebits.size();
  31. size_t i = 0;
  32. while (i < total_len) {
  33. if (i + 2 > total_len) {
  34. isc_throw(DNSMessageFORMERR, rrtype_name <<
  35. " RDATA from wire: incomplete bit map field");
  36. }
  37. const unsigned int block = typebits[i];
  38. const size_t len = typebits[i + 1];
  39. // Check that bitmap window blocks are in the correct order.
  40. if (!first && block <= lastblock) {
  41. isc_throw(DNSMessageFORMERR, rrtype_name <<
  42. " RDATA from wire: Disordered window blocks found: "
  43. << lastblock << " then " << block);
  44. }
  45. // Check for legal length
  46. if (len < 1 || len > 32) {
  47. isc_throw(DNSMessageFORMERR, rrtype_name <<
  48. " RDATA from wire: Invalid bitmap length: " << len);
  49. }
  50. // Check for overflow.
  51. i += 2;
  52. if (i + len > total_len) {
  53. isc_throw(DNSMessageFORMERR, rrtype_name <<
  54. " RDATA from wire: bitmap length too large: " << len);
  55. }
  56. // The last octet of the bitmap must be non zero.
  57. if (typebits[i + len - 1] == 0) {
  58. isc_throw(DNSMessageFORMERR, rrtype_name <<
  59. " RDATA from wire: bitmap ending an all-zero byte");
  60. }
  61. i += len;
  62. lastblock = block;
  63. first = false;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }