DNSPacket.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "DNSPacket.h"
  28. #include "DNSName.h"
  29. #include "DNSPacketHeader.h"
  30. #include <AK/IPv4Address.h>
  31. #include <AK/MemoryStream.h>
  32. #include <AK/StringBuilder.h>
  33. #include <arpa/inet.h>
  34. #include <ctype.h>
  35. #include <stdlib.h>
  36. namespace LookupServer {
  37. void DNSPacket::add_question(const DNSQuestion& question)
  38. {
  39. m_questions.empend(question);
  40. ASSERT(m_questions.size() <= UINT16_MAX);
  41. }
  42. void DNSPacket::add_answer(const DNSAnswer& answer)
  43. {
  44. m_answers.empend(answer);
  45. ASSERT(m_answers.size() <= UINT16_MAX);
  46. }
  47. ByteBuffer DNSPacket::to_byte_buffer() const
  48. {
  49. DNSPacketHeader header;
  50. header.set_id(m_id);
  51. if (is_query())
  52. header.set_is_query();
  53. else
  54. header.set_is_response();
  55. // FIXME: What should this be?
  56. header.set_opcode(0);
  57. header.set_response_code(m_code);
  58. header.set_truncated(false); // hopefully...
  59. header.set_recursion_desired(true);
  60. // FIXME: what should the be for requests?
  61. header.set_recursion_available(true);
  62. header.set_question_count(m_questions.size());
  63. header.set_answer_count(m_answers.size());
  64. DuplexMemoryStream stream;
  65. stream << ReadonlyBytes { &header, sizeof(header) };
  66. for (auto& question : m_questions) {
  67. stream << question.name();
  68. stream << htons(question.record_type());
  69. stream << htons(question.class_code());
  70. }
  71. for (auto& answer : m_answers) {
  72. stream << answer.name();
  73. stream << htons(answer.type());
  74. stream << htons(answer.class_code());
  75. stream << htonl(answer.ttl());
  76. if (answer.type() == T_PTR) {
  77. DNSName name { answer.record_data() };
  78. stream << htons(name.serialized_size());
  79. stream << name;
  80. } else {
  81. stream << htons(answer.record_data().length());
  82. stream << answer.record_data().bytes();
  83. }
  84. }
  85. return stream.copy_into_contiguous_buffer();
  86. }
  87. class [[gnu::packed]] DNSRecordWithoutName {
  88. public:
  89. DNSRecordWithoutName() { }
  90. u16 type() const { return m_type; }
  91. u16 record_class() const { return m_class; }
  92. u32 ttl() const { return m_ttl; }
  93. u16 data_length() const { return m_data_length; }
  94. void* data() { return this + 1; }
  95. const void* data() const { return this + 1; }
  96. private:
  97. NetworkOrdered<u16> m_type;
  98. NetworkOrdered<u16> m_class;
  99. NetworkOrdered<u32> m_ttl;
  100. NetworkOrdered<u16> m_data_length;
  101. };
  102. static_assert(sizeof(DNSRecordWithoutName) == 10);
  103. Optional<DNSPacket> DNSPacket::from_raw_packet(const u8* raw_data, size_t raw_size)
  104. {
  105. if (raw_size < sizeof(DNSPacketHeader)) {
  106. dbgln("DNS response not large enough ({} out of {}) to be a DNS packet.", raw_size, sizeof(DNSPacketHeader));
  107. return {};
  108. }
  109. auto& header = *(const DNSPacketHeader*)(raw_data);
  110. #ifdef LOOKUPSERVER_DEBUG
  111. dbgln("Got packet (ID: {})", header.id());
  112. dbgln(" Question count: {}", header.question_count());
  113. dbgln(" Answer count: {}", header.answer_count());
  114. dbgln(" Authority count: {}", header.authority_count());
  115. dbgln("Additional count: {}", header.additional_count());
  116. #endif
  117. DNSPacket packet;
  118. packet.m_id = header.id();
  119. packet.m_query_or_response = header.is_response();
  120. packet.m_code = header.response_code();
  121. // FIXME: Should we parse further in this case?
  122. if (packet.code() != Code::NOERROR)
  123. return packet;
  124. size_t offset = sizeof(DNSPacketHeader);
  125. for (u16 i = 0; i < header.question_count(); i++) {
  126. auto name = DNSName::parse(raw_data, offset, raw_size);
  127. struct RawDNSAnswerQuestion {
  128. NetworkOrdered<u16> record_type;
  129. NetworkOrdered<u16> class_code;
  130. };
  131. auto& record_and_class = *(const RawDNSAnswerQuestion*)&raw_data[offset];
  132. packet.m_questions.empend(name, record_and_class.record_type, record_and_class.class_code);
  133. offset += 4;
  134. #ifdef LOOKUPSERVER_DEBUG
  135. auto& question = packet.m_questions.last();
  136. dbgln("Question #{}: name=_{}_, type={}, class={}", i, question.name(), question.record_type(), question.class_code());
  137. #endif
  138. }
  139. for (u16 i = 0; i < header.answer_count(); ++i) {
  140. auto name = DNSName::parse(raw_data, offset, raw_size);
  141. auto& record = *(const DNSRecordWithoutName*)(&raw_data[offset]);
  142. String data;
  143. offset += sizeof(DNSRecordWithoutName);
  144. if (record.type() == T_PTR) {
  145. size_t dummy_offset = offset;
  146. data = DNSName::parse(raw_data, dummy_offset, raw_size).as_string();
  147. } else if (record.type() == T_A) {
  148. data = { record.data(), record.data_length() };
  149. } else {
  150. // FIXME: Parse some other record types perhaps?
  151. dbgln("data=(unimplemented record type {})", record.type());
  152. }
  153. #ifdef LOOKUPSERVER_DEBUG
  154. dbgln("Answer #{}: name=_{}_, type={}, ttl={}, length={}, data=_{}_", i, name, record.type(), record.ttl(), record.data_length(), data);
  155. #endif
  156. packet.m_answers.empend(name, record.type(), record.record_class(), record.ttl(), data);
  157. offset += record.data_length();
  158. }
  159. return packet;
  160. }
  161. }