Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/include/join/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ namespace join
template <class Protocol>
class BasicTlsResolver;

template <class Protocol>
class BasicDatagramNameServer;

template <class Protocol>
class BasicHttpClient;
template <class Protocol>
Expand Down Expand Up @@ -685,6 +688,7 @@ namespace join
using Endpoint = BasicInternetEndpoint<Dns>;
using Socket = BasicDatagramSocket<Dns>;
using Resolver = BasicDatagramResolver<Dns>;
using NameServer = BasicDatagramNameServer<Dns>;

/**
* @brief construct the DNS protocol instance.
Expand Down
1 change: 1 addition & 0 deletions fabric/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.22.1)
set(PUBLIC_HEADERS
include/join/dnsmessage.hpp
include/join/resolver.hpp
include/join/nameserver.hpp
include/join/netlinkmanager.hpp
include/join/neighbor.hpp
include/join/neighbormanager.hpp
Expand Down
58 changes: 32 additions & 26 deletions fabric/include/join/dnsmessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,9 @@ namespace join

for (std::string token; std::getline (iss, token, '.');)
{
data << static_cast<uint8_t> (token.size ());
data << token;
uint8_t len = static_cast<uint8_t> (token.size ());
data.write (reinterpret_cast<const char*> (&len), 1);
data.write (token.data (), len);
}

data << '\0';
Expand All @@ -405,43 +406,48 @@ namespace join

for (;;)
{
auto pos = data.tellg ();

uint16_t offset = 0;
data.read (reinterpret_cast<char*> (&offset), sizeof (offset));
offset = ntohs (offset);
uint8_t first = 0;
if (!data.read (reinterpret_cast<char*> (&first), sizeof (first)))
{
return -1;
}

if (offset & 0xC000)
if ((first & 0xC0) == 0xC0)
{
pos = data.tellg ();
data.seekg (offset & 0x3FFF);
uint8_t second = 0;
if (!data.read (reinterpret_cast<char*> (&second), sizeof (second)))
{
return -1;
}

uint16_t ptr = ((first & 0x3F) << 8) | second;
auto saved = data.tellg ();

data.seekg (ptr);
if (decodeName (name, data, depth + 1) == -1)
{
return -1;
}
data.seekg (pos);
data.seekg (saved);
break;
}
else
{
data.seekg (pos);

uint8_t size = 0;
data.read (reinterpret_cast<char*> (&size), sizeof (size));

if (size == 0)
if (first == 0)
{
if (!name.empty () && name.back () == '.')
{
if (!name.empty () && name.back () == '.')
{
name.pop_back ();
}
break;
name.pop_back ();
}
break;
}

name.resize (name.size () + size);
data.read (&name[name.size () - size], size);
name += '.';
name.reserve (name.size () + first + 1);
name.resize (name.size () + first);
if (!data.read (&name[name.size () - first], first))
{
return -1; // LCOV_EXCL_LINE
}
name += '.';
}

return 0;
Expand Down
206 changes: 206 additions & 0 deletions fabric/include/join/nameserver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/**
* MIT License
*
* Copyright (c) 2026 Mathieu Rabine
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef JOIN_FABRIC_NAMESERVER_HPP
#define JOIN_FABRIC_NAMESERVER_HPP

// libjoin.
#include <join/dnsmessage.hpp>
#include <join/reactor.hpp>

namespace join
{
/**
* @brief basic DNS name server over datagram socket.
*/
template <typename Protocol>
class BasicDatagramNameServer : public Protocol::Socket
{
public:
using Socket = typename Protocol::Socket;
using Endpoint = typename Protocol::Endpoint;

/**
* @brief construct the name server instance.
* @param reactor event loop reactor.
*/
explicit BasicDatagramNameServer (Reactor* reactor = nullptr)
: _reactor (reactor ? reactor : ReactorThread::reactor ())
, _buffer (std::make_unique<char[]> (Protocol::maxMsgSize))
{
}

/**
* @brief copy constructor.
* @param other other object to copy.
*/
BasicDatagramNameServer (const BasicDatagramNameServer& other) = delete;

/**
* @brief copy assignment operator.
* @param other other object to copy.
* @return a reference to the current object.
*/
BasicDatagramNameServer& operator= (const BasicDatagramNameServer& other) = delete;

/**
* @brief move constructor.
* @param other other object to move.
*/
BasicDatagramNameServer (BasicDatagramNameServer&& other) = delete;

/**
* @brief move assignment operator.
* @param other other object to move.
* @return a reference to the current object.
*/
BasicDatagramNameServer& operator= (BasicDatagramNameServer&& other) = delete;

/**
* @brief destroy instance.
*/
virtual ~BasicDatagramNameServer () noexcept = default;

/**
* @brief bind the socket to the given endpoint and register with the reactor.
* @param endpoint endpoint to bind to.
* @return 0 on success, -1 on failure.
*/
virtual int bind (const Endpoint& endpoint) noexcept override
{
if (Socket::bind (endpoint) == -1)
{
return -1; // LCOV_EXCL_LINE
}

_reactor->addHandler (this->handle (), this);

return 0;
}

/**
* @brief close the socket and unregister from the reactor.
*/
virtual void close () noexcept override
{
_reactor->delHandler (this->handle ());
Socket::close ();
}

/**
* @brief reply to a DNS query.
* @param query original query packet.
* @param answers answer records.
* @param authorities authority records.
* @param additionals additional records.
* @param rcode response code (default: 0 = no error).
* @return 0 on success, -1 on error.
*/
int reply (const DnsPacket& query, const std::vector<ResourceRecord>& answers = {},
const std::vector<ResourceRecord>& authorities = {},
const std::vector<ResourceRecord>& additionals = {}, uint16_t rcode = 0)
{
DnsPacket response{};
response.id = query.id;
response.flags = (uint16_t (1) << 15) | (query.flags & 0x7800) | (uint16_t (1) << 10) | (rcode & 0x000F);
response.questions = query.questions;
response.answers = answers;
response.authorities = authorities;
response.additionals = additionals;

std::stringstream data;
if (_message.serialize (response, data) == -1)
{
// LCOV_EXCL_START
lastError = make_error_code (Errc::InvalidParam);
return -1;
// LCOV_EXCL_STOP
}

std::string buffer = data.str ();
if (buffer.size () > Protocol::maxMsgSize)
{
// LCOV_EXCL_START
lastError = make_error_code (Errc::MessageTooLong);
return -1;
// LCOV_EXCL_STOP
}

Endpoint to (query.src, query.port);
if (this->writeTo (buffer.data (), buffer.size (), to) == -1)
{
return -1; // LCOV_EXCL_LINE
}

return 0;
}

/**
* @brief method called when a DNS query is received.
* @param packet parsed DNS query received.
*/
virtual void onQuery (const DnsPacket& packet) = 0;

protected:
/**
* @brief method called when data are ready to be read on handle.
* @param fd file descriptor.
*/
virtual void onReceive ([[maybe_unused]] int fd) override
{
Endpoint from;
int size = this->readFrom (_buffer.get (), Protocol::maxMsgSize, &from);
if (size >= int (_headerSize))
{
std::stringstream data;
data.rdbuf ()->pubsetbuf (_buffer.get (), size);

DnsPacket packet;
_message.deserialize (packet, data);
packet.src = from.ip ();
packet.port = from.port ();
packet.dest = this->localEndpoint ().ip ();

if ((packet.flags & 0x8000) == 0)
{
this->onQuery (packet);
}
}
}

/// DNS message header size.
static constexpr size_t _headerSize = 12;

/// DNS message codec.
DnsMessage _message;

/// event loop reactor.
Reactor* _reactor;

/// reception buffer.
std::unique_ptr<char[]> _buffer;
};
}

#endif
Loading
Loading