2021-02-14 15:02:02 +03:00
|
|
|
/*
|
2021-02-14 15:10:39 +03:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2021-02-14 15:02:02 +03:00
|
|
|
* Copyright (c) 2021, Sergey Bugaev <bugaevc@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-02-14 15:02:02 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "DNSName.h"
|
2021-05-14 17:17:26 +02:00
|
|
|
#include <AK/Random.h>
|
2021-11-10 11:05:21 +01:00
|
|
|
#include <AK/Vector.h>
|
2021-02-14 16:25:48 +03:00
|
|
|
#include <ctype.h>
|
2021-02-14 15:02:02 +03:00
|
|
|
|
2022-04-12 23:25:07 -06:00
|
|
|
namespace DNS {
|
2021-02-14 15:02:02 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
DNSName::DNSName(String const& name)
|
2021-02-14 15:02:02 +03:00
|
|
|
{
|
|
|
|
|
if (name.ends_with('.'))
|
|
|
|
|
m_name = name.substring(0, name.length() - 1);
|
|
|
|
|
else
|
|
|
|
|
m_name = name;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
DNSName DNSName::parse(u8 const* data, size_t& offset, size_t max_offset, size_t recursion_level)
|
2021-02-14 15:10:39 +03:00
|
|
|
{
|
|
|
|
|
if (recursion_level > 4)
|
2022-04-12 23:38:39 -06:00
|
|
|
return {};
|
2021-02-14 15:10:39 +03:00
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
while (true) {
|
|
|
|
|
if (offset >= max_offset)
|
2022-04-12 23:38:39 -06:00
|
|
|
return {};
|
2021-02-14 15:10:39 +03:00
|
|
|
u8 b = data[offset++];
|
|
|
|
|
if (b == '\0') {
|
|
|
|
|
// This terminates the name.
|
|
|
|
|
return builder.to_string();
|
|
|
|
|
} else if ((b & 0xc0) == 0xc0) {
|
|
|
|
|
// The two bytes tell us the offset when to continue from.
|
|
|
|
|
if (offset >= max_offset)
|
2022-04-12 23:38:39 -06:00
|
|
|
return {};
|
2021-02-14 15:10:39 +03:00
|
|
|
size_t dummy = (b & 0x3f) << 8 | data[offset++];
|
|
|
|
|
auto rest_of_name = parse(data, dummy, max_offset, recursion_level + 1);
|
|
|
|
|
builder.append(rest_of_name.as_string());
|
|
|
|
|
return builder.to_string();
|
|
|
|
|
} else {
|
|
|
|
|
// This is the length of a part.
|
|
|
|
|
if (offset + b >= max_offset)
|
2022-04-12 23:38:39 -06:00
|
|
|
return {};
|
2022-04-01 20:58:27 +03:00
|
|
|
builder.append((char const*)&data[offset], (size_t)b);
|
2021-02-14 15:10:39 +03:00
|
|
|
builder.append('.');
|
|
|
|
|
offset += b;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 15:17:27 +03:00
|
|
|
size_t DNSName::serialized_size() const
|
|
|
|
|
{
|
|
|
|
|
if (m_name.is_empty())
|
|
|
|
|
return 1;
|
|
|
|
|
return m_name.length() + 2;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 16:25:48 +03:00
|
|
|
void DNSName::randomize_case()
|
|
|
|
|
{
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
for (char c : m_name) {
|
|
|
|
|
// Randomize the 0x20 bit in every ASCII character.
|
|
|
|
|
if (isalpha(c)) {
|
2021-05-14 17:17:26 +02:00
|
|
|
if (get_random_uniform(2))
|
2021-02-14 16:25:48 +03:00
|
|
|
c |= 0x20;
|
|
|
|
|
else
|
|
|
|
|
c &= ~0x20;
|
|
|
|
|
}
|
|
|
|
|
builder.append(c);
|
|
|
|
|
}
|
|
|
|
|
m_name = builder.to_string();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
OutputStream& operator<<(OutputStream& stream, DNSName const& name)
|
2021-02-14 15:16:20 +03:00
|
|
|
{
|
|
|
|
|
auto parts = name.as_string().split_view('.');
|
|
|
|
|
for (auto& part : parts) {
|
|
|
|
|
stream << (u8)part.length();
|
|
|
|
|
stream << part.bytes();
|
|
|
|
|
}
|
|
|
|
|
stream << '\0';
|
|
|
|
|
return stream;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
unsigned DNSName::Traits::hash(DNSName const& name)
|
2021-02-14 16:33:16 +03:00
|
|
|
{
|
|
|
|
|
return CaseInsensitiveStringTraits::hash(name.as_string());
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool DNSName::Traits::equals(DNSName const& a, DNSName const& b)
|
2021-02-14 16:33:16 +03:00
|
|
|
{
|
|
|
|
|
return CaseInsensitiveStringTraits::equals(a.as_string(), b.as_string());
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-14 15:02:02 +03:00
|
|
|
}
|