question.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // $Id$
  15. #include <iostream>
  16. #include <string>
  17. #include "buffer.h"
  18. #include "messagerenderer.h"
  19. #include "name.h"
  20. #include "question.h"
  21. #include "rrclass.h"
  22. #include "rrtype.h"
  23. using namespace std;
  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. {
  41. return (name_.toText() + " " + rrclass_.toText() + " " +
  42. rrtype_.toText() + "\n");
  43. }
  44. unsigned int
  45. Question::toWire(OutputBuffer& buffer) const
  46. {
  47. name_.toWire(buffer);
  48. rrtype_.toWire(buffer);
  49. rrclass_.toWire(buffer); // number of "entries", which is always 1
  50. return (1);
  51. }
  52. unsigned int
  53. Question::toWire(MessageRenderer& renderer) const
  54. {
  55. renderer.writeName(name_);
  56. rrtype_.toWire(renderer);
  57. rrclass_.toWire(renderer);
  58. return (1); // number of "entries"
  59. }
  60. ostream&
  61. operator<<(std::ostream& os, const Question& question)
  62. {
  63. os << question.toText();
  64. return (os);
  65. }
  66. }
  67. }