labelsequence.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 <dns/labelsequence.h>
  15. #include <dns/name_internal.h>
  16. #include <exceptions/exceptions.h>
  17. #include <boost/functional/hash.hpp>
  18. #include <cstring>
  19. namespace isc {
  20. namespace dns {
  21. LabelSequence::LabelSequence(const uint8_t* data,
  22. const uint8_t* offsets,
  23. size_t offsets_size) :
  24. data_(data),
  25. offsets_(offsets),
  26. first_label_(0),
  27. last_label_(offsets_size - 1)
  28. {
  29. if (data == NULL || offsets == NULL) {
  30. isc_throw(BadValue,
  31. "Null pointer passed to LabelSequence constructor");
  32. }
  33. if (offsets_size == 0) {
  34. isc_throw(BadValue, "Zero offsets to LabelSequence constructor");
  35. }
  36. if (offsets_size > Name::MAX_LABELS) {
  37. isc_throw(BadValue, "MAX_LABELS exceeded");
  38. }
  39. for (size_t cur_offset = 0; cur_offset < offsets_size; ++cur_offset) {
  40. if (offsets[cur_offset] > Name::MAX_LABELLEN) {
  41. isc_throw(BadValue, "MAX_LABEL_LEN exceeded");
  42. }
  43. if (cur_offset > 0 && offsets[cur_offset] <= offsets[cur_offset - 1]) {
  44. isc_throw(BadValue, "Offset smaller than previous offset");
  45. }
  46. }
  47. }
  48. const uint8_t*
  49. LabelSequence::getData(size_t *len) const {
  50. *len = getDataLength();
  51. return (&data_[offsets_[first_label_]]);
  52. }
  53. void
  54. LabelSequence::getOffsetData(size_t* len,
  55. uint8_t placeholder[Name::MAX_LABELS]) const
  56. {
  57. *len = getLabelCount();
  58. for (size_t i = 0; i < *len; ++i) {
  59. placeholder[i] = offsets_[first_label_ + i] - offsets_[first_label_];
  60. }
  61. }
  62. size_t
  63. LabelSequence::getDataLength() const {
  64. const size_t last_label_len = data_[offsets_[last_label_]] + 1;
  65. return (offsets_[last_label_] - offsets_[first_label_] + last_label_len);
  66. }
  67. bool
  68. LabelSequence::equals(const LabelSequence& other, bool case_sensitive) const {
  69. size_t len, other_len;
  70. const uint8_t* data = getData(&len);
  71. const uint8_t* other_data = other.getData(&other_len);
  72. if (len != other_len) {
  73. return (false);
  74. }
  75. if (case_sensitive) {
  76. return (std::memcmp(data, other_data, len) == 0);
  77. }
  78. // As long as the data was originally validated as (part of) a name,
  79. // label length must never be a capital ascii character, so we can
  80. // simply compare them after converting to lower characters.
  81. for (size_t i = 0; i < len; ++i) {
  82. const uint8_t ch = data[i];
  83. const uint8_t other_ch = other_data[i];
  84. if (isc::dns::name::internal::maptolower[ch] !=
  85. isc::dns::name::internal::maptolower[other_ch]) {
  86. return (false);
  87. }
  88. }
  89. return (true);
  90. }
  91. NameComparisonResult
  92. LabelSequence::compare(const LabelSequence& other,
  93. bool case_sensitive) const
  94. {
  95. // Determine the relative ordering under the DNSSEC order relation of
  96. // 'this' and 'other', and also determine the hierarchical relationship
  97. // of the labels.
  98. unsigned int nlabels = 0;
  99. int l1 = getLabelCount();
  100. int l2 = other.getLabelCount();
  101. const int ldiff = static_cast<int>(l1) - static_cast<int>(l2);
  102. unsigned int l = (ldiff < 0) ? l1 : l2;
  103. while (l > 0) {
  104. --l;
  105. --l1;
  106. --l2;
  107. size_t pos1 = offsets_[l1 + first_label_];
  108. size_t pos2 = other.offsets_[l2 + other.first_label_];
  109. unsigned int count1 = data_[pos1++];
  110. unsigned int count2 = other.data_[pos2++];
  111. // We don't support any extended label types including now-obsolete
  112. // bitstring labels.
  113. assert(count1 <= Name::MAX_LABELLEN && count2 <= Name::MAX_LABELLEN);
  114. const int cdiff = static_cast<int>(count1) - static_cast<int>(count2);
  115. unsigned int count = (cdiff < 0) ? count1 : count2;
  116. while (count > 0) {
  117. const uint8_t label1 = data_[pos1];
  118. const uint8_t label2 = other.data_[pos2];
  119. int chdiff;
  120. if (case_sensitive) {
  121. chdiff = static_cast<int>(label1) - static_cast<int>(label2);
  122. } else {
  123. chdiff = static_cast<int>(
  124. isc::dns::name::internal::maptolower[label1]) -
  125. static_cast<int>(
  126. isc::dns::name::internal::maptolower[label2]);
  127. }
  128. if (chdiff != 0) {
  129. return (NameComparisonResult(
  130. chdiff, nlabels,
  131. nlabels == 0 ? NameComparisonResult::NONE :
  132. NameComparisonResult::COMMONANCESTOR));
  133. }
  134. --count;
  135. ++pos1;
  136. ++pos2;
  137. }
  138. if (cdiff != 0) {
  139. return (NameComparisonResult(
  140. cdiff, nlabels,
  141. nlabels == 0 ? NameComparisonResult::NONE :
  142. NameComparisonResult::COMMONANCESTOR));
  143. }
  144. ++nlabels;
  145. }
  146. if (ldiff < 0) {
  147. return (NameComparisonResult(ldiff, nlabels,
  148. NameComparisonResult::SUPERDOMAIN));
  149. } else if (ldiff > 0) {
  150. return (NameComparisonResult(ldiff, nlabels,
  151. NameComparisonResult::SUBDOMAIN));
  152. }
  153. return (NameComparisonResult(ldiff, nlabels, NameComparisonResult::EQUAL));
  154. }
  155. void
  156. LabelSequence::stripLeft(size_t i) {
  157. if (i >= getLabelCount()) {
  158. isc_throw(OutOfRange, "Cannot strip to zero or less labels; " << i <<
  159. " (labelcount: " << getLabelCount() << ")");
  160. }
  161. first_label_ += i;
  162. }
  163. void
  164. LabelSequence::stripRight(size_t i) {
  165. if (i >= getLabelCount()) {
  166. isc_throw(OutOfRange, "Cannot strip to zero or less labels; " << i <<
  167. " (labelcount: " << getLabelCount() << ")");
  168. }
  169. last_label_ -= i;
  170. }
  171. bool
  172. LabelSequence::isAbsolute() const {
  173. return (data_[offsets_[last_label_]] == 0);
  174. }
  175. size_t
  176. LabelSequence::getHash(bool case_sensitive) const {
  177. size_t length;
  178. const uint8_t* s = getData(&length);
  179. if (length > 16) {
  180. length = 16;
  181. }
  182. size_t hash_val = 0;
  183. while (length > 0) {
  184. const uint8_t c = *s++;
  185. boost::hash_combine(hash_val, case_sensitive ? c :
  186. isc::dns::name::internal::maptolower[c]);
  187. --length;
  188. }
  189. return (hash_val);
  190. }
  191. std::string
  192. LabelSequence::toText(bool omit_final_dot) const {
  193. const uint8_t* np = &data_[offsets_[first_label_]];
  194. const uint8_t* np_end = np + getDataLength();
  195. // use for integrity check
  196. unsigned int labels = getLabelCount();
  197. // init with an impossible value to catch error cases in the end:
  198. unsigned int count = Name::MAX_LABELLEN + 1;
  199. // result string: it will roughly have the same length as the wire format
  200. // label sequence data. reserve that length to minimize reallocation.
  201. std::string result;
  202. result.reserve(getDataLength());
  203. while (np != np_end) {
  204. labels--;
  205. count = *np++;
  206. if (count == 0) {
  207. // We've reached the "final dot". If we've not dumped any
  208. // character, the entire label sequence is the root name.
  209. // In that case we don't omit the final dot.
  210. if (!omit_final_dot || result.empty()) {
  211. result.push_back('.');
  212. }
  213. break;
  214. }
  215. if (count <= Name::MAX_LABELLEN) {
  216. assert(np_end - np >= count);
  217. if (!result.empty()) {
  218. // just after a non-empty label. add a separating dot.
  219. result.push_back('.');
  220. }
  221. while (count-- > 0) {
  222. const uint8_t c = *np++;
  223. switch (c) {
  224. case 0x22: // '"'
  225. case 0x28: // '('
  226. case 0x29: // ')'
  227. case 0x2E: // '.'
  228. case 0x3B: // ';'
  229. case 0x5C: // '\\'
  230. // Special modifiers in zone files.
  231. case 0x40: // '@'
  232. case 0x24: // '$'
  233. result.push_back('\\');
  234. result.push_back(c);
  235. break;
  236. default:
  237. if (c > 0x20 && c < 0x7f) {
  238. // append printable characters intact
  239. result.push_back(c);
  240. } else {
  241. // encode non-printable characters in the form of \DDD
  242. result.push_back(0x5c);
  243. result.push_back(0x30 + ((c / 100) % 10));
  244. result.push_back(0x30 + ((c / 10) % 10));
  245. result.push_back(0x30 + (c % 10));
  246. }
  247. }
  248. }
  249. } else {
  250. isc_throw(BadLabelType, "unknown label type in name data");
  251. }
  252. }
  253. // We should be at the end of the data and have consumed all labels.
  254. assert(np == np_end);
  255. assert(labels == 0);
  256. return (result);
  257. }
  258. std::string
  259. LabelSequence::toText() const {
  260. return (toText(!isAbsolute()));
  261. }
  262. std::ostream&
  263. operator<<(std::ostream& os, const LabelSequence& label_sequence) {
  264. os << label_sequence.toText();
  265. return (os);
  266. }
  267. } // end namespace dns
  268. } // end namespace isc