question.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (C) 2010 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 <iostream>
  15. #include <string>
  16. #include <util/buffer.h>
  17. #include <dns/messagerenderer.h>
  18. #include <dns/name.h>
  19. #include <dns/question.h>
  20. #include <dns/rrclass.h>
  21. #include <dns/rrtype.h>
  22. using namespace std;
  23. using namespace isc::util;
  24. namespace isc {
  25. namespace dns {
  26. Question::Question(InputBuffer& buffer) :
  27. name_(buffer), rrtype_(0), rrclass_(0)
  28. {
  29. // In theory, we could perform this in the member initialization list,
  30. // and it would be a little bit more efficient. We don't do this, however,
  31. // because the initialization ordering is crucial (type must be first)
  32. // and the ordering in the initialization list depends on the appearance
  33. // order of member variables. It's fragile to rely on such an implicit
  34. // dependency, so we make the initialization order explicit.
  35. rrtype_ = RRType(buffer);
  36. rrclass_ = RRClass(buffer);
  37. }
  38. string
  39. Question::toText() const {
  40. return (name_.toText() + " " + rrclass_.toText() + " " +
  41. rrtype_.toText() + "\n");
  42. }
  43. unsigned int
  44. Question::toWire(OutputBuffer& buffer) const {
  45. name_.toWire(buffer);
  46. rrtype_.toWire(buffer);
  47. rrclass_.toWire(buffer); // number of "entries", which is always 1
  48. return (1);
  49. }
  50. unsigned int
  51. Question::toWire(AbstractMessageRenderer& renderer) const {
  52. const size_t pos0 = renderer.getLength();
  53. renderer.writeName(name_);
  54. rrtype_.toWire(renderer);
  55. rrclass_.toWire(renderer);
  56. // Make sure the renderer has a room for the question
  57. if (renderer.getLength() > renderer.getLengthLimit()) {
  58. renderer.trim(renderer.getLength() - pos0);
  59. renderer.setTruncated();
  60. return (0);
  61. }
  62. return (1); // number of "entries"
  63. }
  64. ostream&
  65. operator<<(std::ostream& os, const Question& question) {
  66. os << question.toText();
  67. return (os);
  68. }
  69. }
  70. }