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
|
|
|
*/
|
|
|
|
|
|
2022-04-14 16:58:40 -06:00
|
|
|
#include "Name.h"
|
2021-05-14 17:17:26 +02:00
|
|
|
#include <AK/Random.h>
|
2023-02-09 03:11:50 +01:00
|
|
|
#include <AK/Stream.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
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
Name::Name(ByteString 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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 22:36:34 +00:00
|
|
|
ErrorOr<Name> Name::parse(ReadonlyBytes data, size_t& offset, size_t recursion_level)
|
2021-02-14 15:10:39 +03:00
|
|
|
{
|
2023-11-13 23:33:23 +00:00
|
|
|
static constexpr size_t MAX_LABEL_SIZE = 63;
|
|
|
|
|
static constexpr size_t MAX_NAME_SIZE = 253;
|
|
|
|
|
|
2021-02-14 15:10:39 +03:00
|
|
|
if (recursion_level > 4)
|
2023-11-13 22:36:34 +00:00
|
|
|
return Name {};
|
2021-02-14 15:10:39 +03:00
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
|
while (true) {
|
2023-11-02 20:04:11 +00:00
|
|
|
if (offset >= data.size())
|
2023-11-13 22:36:34 +00:00
|
|
|
return Error::from_string_literal("Unexpected EOF when parsing name");
|
2021-02-14 15:10:39 +03:00
|
|
|
u8 b = data[offset++];
|
|
|
|
|
if (b == '\0') {
|
2023-11-13 23:33:23 +00:00
|
|
|
if (builder.length() > MAX_NAME_SIZE)
|
|
|
|
|
return Error::from_string_literal("Domain name exceeds maximum allowed length");
|
2021-02-14 15:10:39 +03:00
|
|
|
// This terminates the name.
|
2023-12-16 17:49:34 +03:30
|
|
|
return builder.to_byte_string();
|
2021-02-14 15:10:39 +03:00
|
|
|
} else if ((b & 0xc0) == 0xc0) {
|
|
|
|
|
// The two bytes tell us the offset when to continue from.
|
2023-11-02 20:04:11 +00:00
|
|
|
if (offset >= data.size())
|
2023-11-13 22:36:34 +00:00
|
|
|
return Error::from_string_literal("Unexpected EOF when parsing name");
|
2021-02-14 15:10:39 +03:00
|
|
|
size_t dummy = (b & 0x3f) << 8 | data[offset++];
|
2023-11-13 22:36:34 +00:00
|
|
|
auto rest_of_name = TRY(parse(data, dummy, recursion_level + 1));
|
2021-02-14 15:10:39 +03:00
|
|
|
builder.append(rest_of_name.as_string());
|
2023-11-13 23:33:23 +00:00
|
|
|
if (builder.length() > MAX_NAME_SIZE)
|
|
|
|
|
return Error::from_string_literal("Domain name exceeds maximum allowed length");
|
2023-12-16 17:49:34 +03:30
|
|
|
return builder.to_byte_string();
|
2021-02-14 15:10:39 +03:00
|
|
|
} else {
|
|
|
|
|
// This is the length of a part.
|
2023-11-02 20:04:11 +00:00
|
|
|
if (offset + b >= data.size())
|
2023-11-13 22:36:34 +00:00
|
|
|
return Error::from_string_literal("Unexpected EOF when parsing name");
|
2023-11-13 23:33:23 +00:00
|
|
|
if (b > MAX_LABEL_SIZE)
|
|
|
|
|
return Error::from_string_literal("DNS label exceeds maximum allowed length");
|
2023-11-02 20:04:11 +00:00
|
|
|
builder.append({ data.offset_pointer(offset), b });
|
2021-02-14 15:10:39 +03:00
|
|
|
builder.append('.');
|
2023-11-13 23:33:23 +00:00
|
|
|
if (builder.length() > MAX_NAME_SIZE)
|
|
|
|
|
return Error::from_string_literal("Domain name exceeds maximum allowed length");
|
2021-02-14 15:10:39 +03:00
|
|
|
offset += b;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 16:58:40 -06:00
|
|
|
size_t Name::serialized_size() const
|
2021-02-14 15:17:27 +03:00
|
|
|
{
|
|
|
|
|
if (m_name.is_empty())
|
|
|
|
|
return 1;
|
|
|
|
|
return m_name.length() + 2;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 16:58:40 -06:00
|
|
|
void Name::randomize_case()
|
2021-02-14 16:25:48 +03:00
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
2023-12-16 17:49:34 +03:30
|
|
|
m_name = builder.to_byte_string();
|
2021-02-14 16:25:48 +03:00
|
|
|
}
|
|
|
|
|
|
2023-02-10 01:00:18 +01:00
|
|
|
ErrorOr<void> Name::write_to_stream(Stream& stream) const
|
2021-02-14 15:16:20 +03:00
|
|
|
{
|
2023-01-09 18:57:41 +01:00
|
|
|
auto parts = as_string().split_view('.');
|
2021-02-14 15:16:20 +03:00
|
|
|
for (auto& part : parts) {
|
2023-01-14 21:13:29 +01:00
|
|
|
TRY(stream.write_value<u8>(part.length()));
|
2023-03-01 15:37:45 +01:00
|
|
|
TRY(stream.write_until_depleted(part.bytes()));
|
2021-02-14 15:16:20 +03:00
|
|
|
}
|
2023-01-14 21:13:29 +01:00
|
|
|
TRY(stream.write_value('\0'));
|
2023-01-09 18:57:41 +01:00
|
|
|
return {};
|
2021-02-14 15:16:20 +03:00
|
|
|
}
|
|
|
|
|
|
2022-04-14 16:58:40 -06:00
|
|
|
unsigned Name::Traits::hash(Name const& name)
|
2021-02-14 16:33:16 +03:00
|
|
|
{
|
|
|
|
|
return CaseInsensitiveStringTraits::hash(name.as_string());
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 16:58:40 -06:00
|
|
|
bool Name::Traits::equals(Name const& a, Name 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
|
|
|
}
|