summaryrefslogtreecommitdiff
path: root/Userland/Services/LookupServer
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@serenityos.org>2021-02-14 16:25:48 +0300
committerAndreas Kling <kling@serenityos.org>2021-02-15 09:14:42 +0100
commitbacbde31f3e4e2de48bce6848f6d7c610fe6a7be (patch)
treedfde9c3b7d07b4a328b66023e73ce89ae38e0a88 /Userland/Services/LookupServer
parent89f718c4c51cfbd635377c38103f220520fb901b (diff)
downloadserenity-bacbde31f3e4e2de48bce6848f6d7c610fe6a7be.zip
LookupServer: Move case randomization into DNSName
* DNSName knows how to randomize itself * DNSPacket no longer constructs DNSQuestion instances, it receives an already built DNSQuestion and just adds it to the list * LookupServer::lookup() explicitly calls randomize_case() if it needs to randomize the case.
Diffstat (limited to 'Userland/Services/LookupServer')
-rw-r--r--Userland/Services/LookupServer/DNSName.cpp17
-rw-r--r--Userland/Services/LookupServer/DNSName.h2
-rw-r--r--Userland/Services/LookupServer/DNSPacket.cpp24
-rw-r--r--Userland/Services/LookupServer/DNSPacket.h2
-rw-r--r--Userland/Services/LookupServer/LookupServer.cpp5
5 files changed, 27 insertions, 23 deletions
diff --git a/Userland/Services/LookupServer/DNSName.cpp b/Userland/Services/LookupServer/DNSName.cpp
index edf8cb9fa7..0bbafd73ad 100644
--- a/Userland/Services/LookupServer/DNSName.cpp
+++ b/Userland/Services/LookupServer/DNSName.cpp
@@ -27,6 +27,7 @@
#include "DNSName.h"
#include <AK/Vector.h>
+#include <ctype.h>
namespace LookupServer {
@@ -77,6 +78,22 @@ size_t DNSName::serialized_size() const
return m_name.length() + 2;
}
+void DNSName::randomize_case()
+{
+ StringBuilder builder;
+ for (char c : m_name) {
+ // Randomize the 0x20 bit in every ASCII character.
+ if (isalpha(c)) {
+ if (arc4random_uniform(2))
+ c |= 0x20;
+ else
+ c &= ~0x20;
+ }
+ builder.append(c);
+ }
+ m_name = builder.to_string();
+}
+
OutputStream& operator<<(OutputStream& stream, const DNSName& name)
{
auto parts = name.as_string().split_view('.');
diff --git a/Userland/Services/LookupServer/DNSName.h b/Userland/Services/LookupServer/DNSName.h
index 08ad651648..f3afa9a8a4 100644
--- a/Userland/Services/LookupServer/DNSName.h
+++ b/Userland/Services/LookupServer/DNSName.h
@@ -41,6 +41,8 @@ public:
size_t serialized_size() const;
const String& as_string() const { return m_name; }
+ void randomize_case();
+
private:
String m_name;
};
diff --git a/Userland/Services/LookupServer/DNSPacket.cpp b/Userland/Services/LookupServer/DNSPacket.cpp
index 4723553f45..afd2b745b4 100644
--- a/Userland/Services/LookupServer/DNSPacket.cpp
+++ b/Userland/Services/LookupServer/DNSPacket.cpp
@@ -37,29 +37,11 @@
namespace LookupServer {
-void DNSPacket::add_question(const String& name, u16 record_type, ShouldRandomizeCase should_randomize_case)
+void DNSPacket::add_question(const DNSQuestion& question)
{
- ASSERT(m_questions.size() <= UINT16_MAX);
+ m_questions.empend(question);
- if (name.is_empty())
- return;
-
- StringBuilder builder;
- for (size_t i = 0; i < name.length(); ++i) {
- u8 ch = name[i];
- if (should_randomize_case == ShouldRandomizeCase::Yes) {
- // Randomize the 0x20 bit in every ASCII character.
- if (isalpha(ch)) {
- if (arc4random_uniform(2))
- ch |= 0x20;
- else
- ch &= ~0x20;
- }
- }
- builder.append(ch);
- }
-
- m_questions.empend(builder.to_string(), record_type, (u16)C_IN);
+ ASSERT(m_questions.size() <= UINT16_MAX);
}
ByteBuffer DNSPacket::to_byte_buffer() const
diff --git a/Userland/Services/LookupServer/DNSPacket.h b/Userland/Services/LookupServer/DNSPacket.h
index c0e5ccd8a3..41912c9f2d 100644
--- a/Userland/Services/LookupServer/DNSPacket.h
+++ b/Userland/Services/LookupServer/DNSPacket.h
@@ -79,7 +79,7 @@ public:
return m_answers.size();
}
- void add_question(const String& name, u16 record_type, ShouldRandomizeCase);
+ void add_question(const DNSQuestion&);
enum class Code : u8 {
NOERROR = 0,
diff --git a/Userland/Services/LookupServer/LookupServer.cpp b/Userland/Services/LookupServer/LookupServer.cpp
index c050268ce4..826094a0eb 100644
--- a/Userland/Services/LookupServer/LookupServer.cpp
+++ b/Userland/Services/LookupServer/LookupServer.cpp
@@ -172,7 +172,10 @@ Vector<String> LookupServer::lookup(const DNSName& name, const String& nameserve
DNSPacket request;
request.set_is_query();
request.set_id(arc4random_uniform(UINT16_MAX));
- request.add_question(name.as_string(), record_type, should_randomize_case);
+ DNSName name_in_question = name;
+ if (should_randomize_case == ShouldRandomizeCase::Yes)
+ name_in_question.randomize_case();
+ request.add_question({ name_in_question, record_type, C_IN });
auto buffer = request.to_byte_buffer();