resolve.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 <resolve/resolve.h>
  15. #include <dns/message.h>
  16. #include <dns/opcode.h>
  17. using namespace isc::dns;
  18. namespace {
  19. class SectionInserter {
  20. public:
  21. SectionInserter(MessagePtr message, const Message::Section sect) :
  22. message_(message), section_(sect)
  23. {}
  24. void operator()(const RRsetPtr rrset) {
  25. message_->addRRset(section_, rrset, true);
  26. }
  27. MessagePtr message_;
  28. const Message::Section section_;
  29. };
  30. }
  31. namespace isc {
  32. namespace resolve {
  33. void
  34. makeErrorMessage(MessagePtr answer_message,
  35. const Rcode& error_code)
  36. {
  37. answer_message->clearSection(Message::SECTION_ANSWER);
  38. answer_message->clearSection(Message::SECTION_AUTHORITY);
  39. answer_message->clearSection(Message::SECTION_ADDITIONAL);
  40. answer_message->setRcode(error_code);
  41. }
  42. void initResponseMessage(const isc::dns::Message& query_message,
  43. isc::dns::Message& response_message)
  44. {
  45. response_message.setOpcode(query_message.getOpcode());
  46. response_message.setQid(query_message.getQid());
  47. assert(response_message.getRRCount(Message::SECTION_QUESTION) == 0);
  48. response_message.appendSection(Message::SECTION_QUESTION,
  49. query_message);
  50. }
  51. void initResponseMessage(const isc::dns::Question& question,
  52. isc::dns::Message& response_message)
  53. {
  54. response_message.setOpcode(isc::dns::Opcode::QUERY());
  55. response_message.addQuestion(question);
  56. }
  57. void copyResponseMessage(const Message& source, MessagePtr target) {
  58. target->setRcode(source.getRcode());
  59. target->appendSection(Message::SECTION_ANSWER, source);
  60. target->appendSection(Message::SECTION_AUTHORITY, source);
  61. target->appendSection(Message::SECTION_ADDITIONAL, source);
  62. }
  63. } // namespace resolve
  64. } // namespace isc