Serenity Operating System
at hosted 147 lines 5.9 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "DNSResponse.h" 28#include "DNSPacket.h" 29#include "DNSRequest.h" 30#include <AK/IPv4Address.h> 31#include <AK/StringBuilder.h> 32 33static String parse_dns_name(const u8* data, size_t& offset, size_t max_offset, size_t recursion_level = 0); 34 35class [[gnu::packed]] DNSRecordWithoutName 36{ 37public: 38 DNSRecordWithoutName() {} 39 40 u16 type() const { return m_type; } 41 u16 record_class() const { return m_class; } 42 u32 ttl() const { return m_ttl; } 43 u16 data_length() const { return m_data_length; } 44 45 void* data() { return this + 1; } 46 const void* data() const { return this + 1; } 47 48private: 49 NetworkOrdered<u16> m_type; 50 NetworkOrdered<u16> m_class; 51 NetworkOrdered<u32> m_ttl; 52 NetworkOrdered<u16> m_data_length; 53}; 54 55static_assert(sizeof(DNSRecordWithoutName) == 10); 56 57Optional<DNSResponse> DNSResponse::from_raw_response(const u8* raw_data, size_t raw_size) 58{ 59 if (raw_size < sizeof(DNSPacket)) { 60 dbg() << "DNS response not large enough (" << raw_size << " out of " << sizeof(DNSPacket) << ") to be a DNS packet."; 61 return {}; 62 } 63 64 auto& response_header = *(const DNSPacket*)(raw_data); 65 dbgprintf("Got response (ID: %u)\n", response_header.id()); 66 dbgprintf(" Question count: %u\n", response_header.question_count()); 67 dbgprintf(" Answer count: %u\n", response_header.answer_count()); 68 dbgprintf(" Authority count: %u\n", response_header.authority_count()); 69 dbgprintf("Additional count: %u\n", response_header.additional_count()); 70 71 DNSResponse response; 72 response.m_id = response_header.id(); 73 response.m_code = response_header.response_code(); 74 75 if (response.code() != DNSResponse::Code::NOERROR) 76 return response; 77 78 size_t offset = sizeof(DNSPacket); 79 80 for (u16 i = 0; i < response_header.question_count(); ++i) { 81 auto name = parse_dns_name(raw_data, offset, raw_size); 82 struct RawDNSAnswerQuestion { 83 NetworkOrdered<u16> record_type; 84 NetworkOrdered<u16> class_code; 85 }; 86 auto& record_and_class = *(const RawDNSAnswerQuestion*)&raw_data[offset]; 87 response.m_questions.empend(name, record_and_class.record_type, record_and_class.class_code); 88 auto& question = response.m_questions.last(); 89 offset += 4; 90 dbg() << "Question #" << i << ": _" << question.name() << "_ type: " << question.record_type() << ", class: " << question.class_code(); 91 } 92 93 for (u16 i = 0; i < response_header.answer_count(); ++i) { 94 auto name = parse_dns_name(raw_data, offset, raw_size); 95 96 auto& record = *(const DNSRecordWithoutName*)(&raw_data[offset]); 97 98 String data; 99 100 offset += sizeof(DNSRecordWithoutName); 101 if (record.type() == T_PTR) { 102 size_t dummy_offset = offset; 103 data = parse_dns_name(raw_data, dummy_offset, raw_size); 104 } else if (record.type() == T_A) { 105 auto ipv4_address = IPv4Address((const u8*)record.data()); 106 data = ipv4_address.to_string(); 107 } else { 108 // FIXME: Parse some other record types perhaps? 109 dbg() << " data=(unimplemented record type " << record.type() << ")"; 110 } 111 dbg() << "Answer #" << i << ": name=_" << name << "_, type=" << record.type() << ", ttl=" << record.ttl() << ", length=" << record.data_length() << ", data=_" << data << "_"; 112 response.m_answers.empend(name, record.type(), record.record_class(), record.ttl(), data); 113 offset += record.data_length(); 114 } 115 116 return response; 117} 118 119String parse_dns_name(const u8* data, size_t& offset, size_t max_offset, size_t recursion_level) 120{ 121 if (recursion_level > 4) 122 return {}; 123 Vector<char, 128> buf; 124 while (offset < max_offset) { 125 u8 ch = data[offset]; 126 if (ch == '\0') { 127 ++offset; 128 break; 129 } 130 if ((ch & 0xc0) == 0xc0) { 131 if ((offset + 1) >= max_offset) 132 return {}; 133 size_t dummy = (ch & 0x3f) << 8 | data[offset + 1]; 134 offset += 2; 135 StringBuilder builder; 136 builder.append(buf.data(), buf.size()); 137 auto okay = parse_dns_name(data, dummy, max_offset, recursion_level + 1); 138 builder.append(okay); 139 return builder.to_string(); 140 } 141 for (size_t i = 0; i < ch; ++i) 142 buf.append(data[offset + i + 1]); 143 buf.append('.'); 144 offset += ch + 1; 145 } 146 return String::copy(buf); 147}