Serenity Operating System
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 "DNSRequest.h"
28#include "DNSPacket.h"
29#include <AK/BufferStream.h>
30#include <AK/StringBuilder.h>
31#include <arpa/inet.h>
32#include <ctype.h>
33#include <stdlib.h>
34
35#define C_IN 1
36
37DNSRequest::DNSRequest()
38 : m_id(arc4random_uniform(UINT16_MAX))
39{
40}
41
42void DNSRequest::add_question(const String& name, u16 record_type, ShouldRandomizeCase should_randomize_case)
43{
44 ASSERT(m_questions.size() <= UINT16_MAX);
45
46 if (name.is_empty())
47 return;
48
49 StringBuilder builder;
50 for (size_t i = 0; i < name.length(); ++i) {
51 u8 ch = name[i];
52 if (should_randomize_case == ShouldRandomizeCase::Yes) {
53 // Randomize the 0x20 bit in every ASCII character.
54 if (isalpha(ch)) {
55 if (arc4random_uniform(2))
56 ch |= 0x20;
57 else
58 ch &= ~0x20;
59 }
60 }
61 builder.append(ch);
62 }
63
64 if (name[name.length() - 1] != '.')
65 builder.append('.');
66
67 m_questions.empend(builder.to_string(), record_type, C_IN);
68}
69
70ByteBuffer DNSRequest::to_byte_buffer() const
71{
72 DNSPacket request_header;
73 request_header.set_id(m_id);
74 request_header.set_is_query();
75 request_header.set_opcode(0);
76 request_header.set_truncated(false);
77 request_header.set_recursion_desired(true);
78 request_header.set_question_count(m_questions.size());
79
80 auto buffer = ByteBuffer::create_uninitialized(m_questions.size() * 4096);
81 BufferStream stream(buffer);
82
83 stream << ByteBuffer::wrap(&request_header, sizeof(request_header));
84
85 for (auto& question : m_questions) {
86 auto parts = question.name().split('.');
87 for (auto& part : parts) {
88 stream << (u8)part.length();
89 stream << part;
90 }
91 stream << '\0';
92 stream << htons(question.record_type());
93 stream << htons(question.class_code());
94 }
95 stream.snip();
96
97 return buffer;
98}