Packet.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "Packet.h"
  8. #include "Name.h"
  9. #include "PacketHeader.h"
  10. #include <AK/Debug.h>
  11. #include <AK/MemoryStream.h>
  12. #include <AK/StringBuilder.h>
  13. #include <arpa/inet.h>
  14. namespace DNS {
  15. void Packet::add_question(Question const& question)
  16. {
  17. m_questions.empend(question);
  18. VERIFY(m_questions.size() <= UINT16_MAX);
  19. }
  20. void Packet::add_answer(Answer const& answer)
  21. {
  22. m_answers.empend(answer);
  23. VERIFY(m_answers.size() <= UINT16_MAX);
  24. }
  25. ByteBuffer Packet::to_byte_buffer() const
  26. {
  27. PacketHeader header;
  28. header.set_id(m_id);
  29. if (is_query())
  30. header.set_is_query();
  31. else
  32. header.set_is_response();
  33. header.set_authoritative_answer(m_authoritative_answer);
  34. // FIXME: What should this be?
  35. header.set_opcode(0);
  36. header.set_response_code(m_code);
  37. header.set_truncated(false); // hopefully...
  38. header.set_recursion_desired(m_recursion_desired);
  39. // FIXME: what should the be for requests?
  40. header.set_recursion_available(m_recursion_available);
  41. header.set_question_count(m_questions.size());
  42. header.set_answer_count(m_answers.size());
  43. DuplexMemoryStream stream;
  44. stream << ReadonlyBytes { &header, sizeof(header) };
  45. for (auto& question : m_questions) {
  46. stream << question.name();
  47. stream << htons((u16)question.record_type());
  48. stream << htons(question.raw_class_code());
  49. }
  50. for (auto& answer : m_answers) {
  51. stream << answer.name();
  52. stream << htons((u16)answer.type());
  53. stream << htons(answer.raw_class_code());
  54. stream << htonl(answer.ttl());
  55. if (answer.type() == RecordType::PTR) {
  56. Name name { answer.record_data() };
  57. stream << htons(name.serialized_size());
  58. stream << name;
  59. } else {
  60. stream << htons(answer.record_data().length());
  61. stream << answer.record_data().bytes();
  62. }
  63. }
  64. return stream.copy_into_contiguous_buffer();
  65. }
  66. class [[gnu::packed]] DNSRecordWithoutName {
  67. public:
  68. DNSRecordWithoutName() = default;
  69. u16 type() const { return m_type; }
  70. u16 record_class() const { return m_class; }
  71. u32 ttl() const { return m_ttl; }
  72. u16 data_length() const { return m_data_length; }
  73. void* data() { return this + 1; }
  74. void const* data() const { return this + 1; }
  75. private:
  76. NetworkOrdered<u16> m_type;
  77. NetworkOrdered<u16> m_class;
  78. NetworkOrdered<u32> m_ttl;
  79. NetworkOrdered<u16> m_data_length;
  80. };
  81. static_assert(sizeof(DNSRecordWithoutName) == 10);
  82. Optional<Packet> Packet::from_raw_packet(u8 const* raw_data, size_t raw_size)
  83. {
  84. if (raw_size < sizeof(PacketHeader)) {
  85. dbgln("DNS response not large enough ({} out of {}) to be a DNS packet.", raw_size, sizeof(PacketHeader));
  86. return {};
  87. }
  88. auto& header = *(PacketHeader const*)(raw_data);
  89. dbgln_if(LOOKUPSERVER_DEBUG, "Got packet (ID: {})", header.id());
  90. dbgln_if(LOOKUPSERVER_DEBUG, " Question count: {}", header.question_count());
  91. dbgln_if(LOOKUPSERVER_DEBUG, " Answer count: {}", header.answer_count());
  92. dbgln_if(LOOKUPSERVER_DEBUG, " Authority count: {}", header.authority_count());
  93. dbgln_if(LOOKUPSERVER_DEBUG, "Additional count: {}", header.additional_count());
  94. Packet packet;
  95. packet.m_id = header.id();
  96. packet.m_query_or_response = header.is_response();
  97. packet.m_code = header.response_code();
  98. // FIXME: Should we parse further in this case?
  99. if (packet.code() != Code::NOERROR)
  100. return packet;
  101. size_t offset = sizeof(PacketHeader);
  102. for (u16 i = 0; i < header.question_count(); i++) {
  103. auto name = Name::parse(raw_data, offset, raw_size);
  104. struct RawDNSAnswerQuestion {
  105. NetworkOrdered<u16> record_type;
  106. NetworkOrdered<u16> class_code;
  107. };
  108. auto& record_and_class = *(RawDNSAnswerQuestion const*)&raw_data[offset];
  109. u16 class_code = record_and_class.class_code & ~MDNS_WANTS_UNICAST_RESPONSE;
  110. bool mdns_wants_unicast_response = record_and_class.class_code & MDNS_WANTS_UNICAST_RESPONSE;
  111. packet.m_questions.empend(name, (RecordType)(u16)record_and_class.record_type, (RecordClass)class_code, mdns_wants_unicast_response);
  112. offset += 4;
  113. auto& question = packet.m_questions.last();
  114. dbgln_if(LOOKUPSERVER_DEBUG, "Question #{}: name=_{}_, type={}, class={}", i, question.name(), question.record_type(), question.class_code());
  115. }
  116. for (u16 i = 0; i < header.answer_count(); ++i) {
  117. auto name = Name::parse(raw_data, offset, raw_size);
  118. auto& record = *(DNSRecordWithoutName const*)(&raw_data[offset]);
  119. DeprecatedString data;
  120. offset += sizeof(DNSRecordWithoutName);
  121. switch ((RecordType)record.type()) {
  122. case RecordType::PTR: {
  123. size_t dummy_offset = offset;
  124. data = Name::parse(raw_data, dummy_offset, raw_size).as_string();
  125. break;
  126. }
  127. case RecordType::CNAME:
  128. // Fall through
  129. case RecordType::A:
  130. // Fall through
  131. case RecordType::TXT:
  132. // Fall through
  133. case RecordType::AAAA:
  134. // Fall through
  135. case RecordType::SRV:
  136. data = { record.data(), record.data_length() };
  137. break;
  138. default:
  139. // FIXME: Parse some other record types perhaps?
  140. dbgln("data=(unimplemented record type {})", (u16)record.type());
  141. }
  142. dbgln_if(LOOKUPSERVER_DEBUG, "Answer #{}: name=_{}_, type={}, ttl={}, length={}, data=_{}_", i, name, record.type(), record.ttl(), record.data_length(), data);
  143. u16 class_code = record.record_class() & ~MDNS_CACHE_FLUSH;
  144. bool mdns_cache_flush = record.record_class() & MDNS_CACHE_FLUSH;
  145. packet.m_answers.empend(name, (RecordType)record.type(), (RecordClass)class_code, record.ttl(), data, mdns_cache_flush);
  146. offset += record.data_length();
  147. }
  148. return packet;
  149. }
  150. }