2024-02-08 03:14:00 +02:00
|
|
|
#include <BAN/ByteSpan.h>
|
2024-02-08 18:34:15 +02:00
|
|
|
#include <BAN/Debug.h>
|
2024-02-08 03:14:00 +02:00
|
|
|
#include <BAN/Endianness.h>
|
|
|
|
#include <BAN/HashMap.h>
|
|
|
|
#include <BAN/IPv4.h>
|
|
|
|
#include <BAN/String.h>
|
|
|
|
#include <BAN/StringView.h>
|
|
|
|
#include <BAN/Vector.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2024-02-12 23:47:39 +02:00
|
|
|
#include <sys/select.h>
|
2024-02-08 03:14:00 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
struct DNSPacket
|
|
|
|
{
|
|
|
|
BAN::NetworkEndian<uint16_t> identification { 0 };
|
|
|
|
BAN::NetworkEndian<uint16_t> flags { 0 };
|
|
|
|
BAN::NetworkEndian<uint16_t> question_count { 0 };
|
|
|
|
BAN::NetworkEndian<uint16_t> answer_count { 0 };
|
|
|
|
BAN::NetworkEndian<uint16_t> authority_RR_count { 0 };
|
|
|
|
BAN::NetworkEndian<uint16_t> additional_RR_count { 0 };
|
|
|
|
uint8_t data[];
|
|
|
|
};
|
|
|
|
static_assert(sizeof(DNSPacket) == 12);
|
|
|
|
|
|
|
|
struct DNSAnswer
|
|
|
|
{
|
|
|
|
uint8_t __storage[12];
|
|
|
|
BAN::NetworkEndian<uint16_t>& name() { return *reinterpret_cast<BAN::NetworkEndian<uint16_t>*>(__storage + 0x00); };
|
|
|
|
BAN::NetworkEndian<uint16_t>& type() { return *reinterpret_cast<BAN::NetworkEndian<uint16_t>*>(__storage + 0x02); };
|
|
|
|
BAN::NetworkEndian<uint16_t>& class_() { return *reinterpret_cast<BAN::NetworkEndian<uint16_t>*>(__storage + 0x04); };
|
|
|
|
BAN::NetworkEndian<uint32_t>& ttl() { return *reinterpret_cast<BAN::NetworkEndian<uint32_t>*>(__storage + 0x06); };
|
|
|
|
BAN::NetworkEndian<uint16_t>& data_len() { return *reinterpret_cast<BAN::NetworkEndian<uint16_t>*>(__storage + 0x0A); };
|
|
|
|
uint8_t data[];
|
|
|
|
};
|
|
|
|
static_assert(sizeof(DNSAnswer) == 12);
|
|
|
|
|
2024-02-08 12:03:54 +02:00
|
|
|
enum QTYPE : uint16_t
|
|
|
|
{
|
|
|
|
A = 0x0001,
|
|
|
|
CNAME = 0x0005,
|
|
|
|
AAAA = 0x001C,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DNSEntry
|
|
|
|
{
|
|
|
|
time_t valid_until { 0 };
|
|
|
|
BAN::IPv4Address address { 0 };
|
|
|
|
};
|
|
|
|
|
2024-02-12 23:47:39 +02:00
|
|
|
struct DNSResponse
|
|
|
|
{
|
|
|
|
uint16_t id;
|
|
|
|
DNSEntry entry;
|
|
|
|
};
|
|
|
|
|
2024-02-08 03:14:00 +02:00
|
|
|
bool send_dns_query(int socket, BAN::StringView domain, uint16_t id)
|
|
|
|
{
|
|
|
|
static uint8_t buffer[4096];
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
|
|
|
|
DNSPacket& request = *reinterpret_cast<DNSPacket*>(buffer);
|
|
|
|
request.identification = id;
|
|
|
|
request.flags = 0x0100;
|
|
|
|
request.question_count = 1;
|
|
|
|
|
|
|
|
size_t idx = 0;
|
|
|
|
|
|
|
|
auto labels = MUST(BAN::StringView(domain).split('.'));
|
|
|
|
for (auto label : labels)
|
|
|
|
{
|
|
|
|
ASSERT(label.size() <= 0xFF);
|
|
|
|
request.data[idx++] = label.size();
|
|
|
|
for (char c : label)
|
|
|
|
request.data[idx++] = c;
|
|
|
|
}
|
|
|
|
request.data[idx++] = 0x00;
|
|
|
|
|
2024-02-08 12:03:54 +02:00
|
|
|
*(uint16_t*)&request.data[idx] = htons(QTYPE::A); idx += 2;
|
|
|
|
*(uint16_t*)&request.data[idx] = htons(0x0001); idx += 2;
|
2024-02-08 03:14:00 +02:00
|
|
|
|
|
|
|
sockaddr_in nameserver;
|
|
|
|
nameserver.sin_family = AF_INET;
|
|
|
|
nameserver.sin_port = htons(53);
|
|
|
|
nameserver.sin_addr.s_addr = inet_addr("8.8.8.8");
|
|
|
|
if (sendto(socket, &request, sizeof(DNSPacket) + idx, 0, (sockaddr*)&nameserver, sizeof(nameserver)) == -1)
|
|
|
|
{
|
2024-02-08 18:34:15 +02:00
|
|
|
dprintln("sendto: {}", strerror(errno));
|
2024-02-08 03:14:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-12 23:47:39 +02:00
|
|
|
BAN::Optional<DNSResponse> read_dns_response(int socket)
|
2024-02-08 03:14:00 +02:00
|
|
|
{
|
|
|
|
static uint8_t buffer[4096];
|
|
|
|
|
|
|
|
ssize_t nrecv = recvfrom(socket, buffer, sizeof(buffer), 0, nullptr, nullptr);
|
|
|
|
if (nrecv == -1)
|
|
|
|
{
|
2024-02-08 18:34:15 +02:00
|
|
|
dprintln("recvfrom: {}", strerror(errno));
|
2024-02-08 03:14:00 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
DNSPacket& reply = *reinterpret_cast<DNSPacket*>(buffer);
|
|
|
|
if (reply.flags & 0x0F)
|
|
|
|
{
|
2024-02-08 18:34:15 +02:00
|
|
|
dprintln("DNS error (rcode {})", (unsigned)(reply.flags & 0xF));
|
2024-02-08 03:14:00 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t idx = 0;
|
|
|
|
for (size_t i = 0; i < reply.question_count; i++)
|
|
|
|
{
|
|
|
|
while (reply.data[idx])
|
|
|
|
idx += reply.data[idx] + 1;
|
|
|
|
idx += 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
DNSAnswer& answer = *reinterpret_cast<DNSAnswer*>(&reply.data[idx]);
|
2024-02-08 12:03:54 +02:00
|
|
|
if (answer.type() != QTYPE::A)
|
|
|
|
{
|
2024-02-08 18:34:15 +02:00
|
|
|
dprintln("Not A record");
|
2024-02-08 12:03:54 +02:00
|
|
|
return {};
|
|
|
|
}
|
2024-02-08 03:14:00 +02:00
|
|
|
if (answer.data_len() != 4)
|
|
|
|
{
|
2024-02-08 18:34:15 +02:00
|
|
|
dprintln("corrupted package");
|
2024-02-08 03:14:00 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-02-12 23:47:39 +02:00
|
|
|
DNSResponse result;
|
|
|
|
result.id = reply.identification;
|
|
|
|
result.entry.valid_until = time(nullptr) + answer.ttl();
|
|
|
|
result.entry.address = BAN::IPv4Address(*reinterpret_cast<uint32_t*>(answer.data));
|
2024-02-08 12:03:54 +02:00
|
|
|
|
|
|
|
return result;
|
2024-02-08 03:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int create_service_socket()
|
|
|
|
{
|
|
|
|
int socket = ::socket(AF_UNIX, SOCK_SEQPACKET, 0);
|
|
|
|
if (socket == -1)
|
|
|
|
{
|
2024-02-08 18:34:15 +02:00
|
|
|
dprintln("socket: {}", strerror(errno));
|
2024-02-08 03:14:00 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sockaddr_un addr;
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strcpy(addr.sun_path, "/tmp/resolver.sock");
|
|
|
|
if (bind(socket, (sockaddr*)&addr, sizeof(addr)) == -1)
|
|
|
|
{
|
2024-02-08 18:34:15 +02:00
|
|
|
dprintln("bind: {}", strerror(errno));
|
2024-02-08 03:14:00 +02:00
|
|
|
close(socket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chmod("/tmp/resolver.sock", 0777) == -1)
|
|
|
|
{
|
2024-02-08 18:34:15 +02:00
|
|
|
dprintln("chmod: {}", strerror(errno));
|
2024-02-08 03:14:00 +02:00
|
|
|
close(socket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(socket, 10) == -1)
|
|
|
|
{
|
2024-02-08 18:34:15 +02:00
|
|
|
dprintln("listen: {}", strerror(errno));
|
2024-02-08 03:14:00 +02:00
|
|
|
close(socket);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
BAN::Optional<BAN::String> read_service_query(int socket)
|
|
|
|
{
|
|
|
|
static char buffer[4096];
|
|
|
|
ssize_t nrecv = recv(socket, buffer, sizeof(buffer), 0);
|
|
|
|
if (nrecv == -1)
|
|
|
|
{
|
2024-02-08 18:34:15 +02:00
|
|
|
dprintln("recv: {}", strerror(errno));
|
2024-02-08 03:14:00 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
buffer[nrecv] = '\0';
|
|
|
|
return BAN::String(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int, char**)
|
|
|
|
{
|
|
|
|
srand(time(nullptr));
|
|
|
|
|
|
|
|
int service_socket = create_service_socket();
|
|
|
|
if (service_socket == -1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
int dns_socket = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (dns_socket == -1)
|
|
|
|
{
|
2024-02-08 18:34:15 +02:00
|
|
|
dprintln("socket: {}", strerror(errno));
|
2024-02-08 03:14:00 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-02-08 12:03:54 +02:00
|
|
|
BAN::HashMap<BAN::String, DNSEntry> dns_cache;
|
|
|
|
|
2024-02-12 23:47:39 +02:00
|
|
|
struct Client
|
|
|
|
{
|
|
|
|
const int socket;
|
|
|
|
bool close { false };
|
|
|
|
uint16_t query_id { 0 };
|
|
|
|
BAN::String query;
|
|
|
|
};
|
|
|
|
|
|
|
|
BAN::LinkedList<Client> clients;
|
|
|
|
|
2024-02-08 03:14:00 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
2024-02-12 23:47:39 +02:00
|
|
|
int max_sock = BAN::Math::max(service_socket, dns_socket);
|
|
|
|
|
|
|
|
fd_set fds;
|
|
|
|
FD_ZERO(&fds);
|
|
|
|
FD_SET(service_socket, &fds);
|
|
|
|
FD_SET(dns_socket, &fds);
|
|
|
|
for (auto& client : clients)
|
2024-02-08 03:14:00 +02:00
|
|
|
{
|
2024-02-12 23:47:39 +02:00
|
|
|
FD_SET(client.socket, &fds);
|
|
|
|
max_sock = BAN::Math::max(max_sock, client.socket);
|
2024-02-08 03:14:00 +02:00
|
|
|
}
|
|
|
|
|
2024-02-12 23:47:39 +02:00
|
|
|
int nselect = select(max_sock + 1, &fds, nullptr, nullptr, nullptr);
|
|
|
|
if (nselect == -1)
|
2024-02-08 12:03:54 +02:00
|
|
|
{
|
2024-02-12 23:47:39 +02:00
|
|
|
perror("select");
|
2024-02-08 03:14:00 +02:00
|
|
|
continue;
|
2024-02-08 12:03:54 +02:00
|
|
|
}
|
2024-02-08 03:14:00 +02:00
|
|
|
|
2024-02-12 23:47:39 +02:00
|
|
|
if (FD_ISSET(service_socket, &fds))
|
2024-02-08 03:14:00 +02:00
|
|
|
{
|
2024-02-12 23:47:39 +02:00
|
|
|
int client = accept(service_socket, nullptr, nullptr);
|
|
|
|
if (client == -1)
|
|
|
|
{
|
|
|
|
perror("accept");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MUST(clients.emplace_back(client));
|
2024-02-08 12:03:54 +02:00
|
|
|
}
|
|
|
|
|
2024-02-12 23:47:39 +02:00
|
|
|
if (FD_ISSET(dns_socket, &fds))
|
2024-02-08 12:03:54 +02:00
|
|
|
{
|
2024-02-12 23:47:39 +02:00
|
|
|
auto result = read_dns_response(dns_socket);
|
|
|
|
if (!result.has_value())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (auto& client : clients)
|
2024-02-08 03:14:00 +02:00
|
|
|
{
|
2024-02-12 23:47:39 +02:00
|
|
|
if (client.query_id != result->id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
(void)dns_cache.insert(client.query, result->entry);
|
|
|
|
|
|
|
|
sockaddr_storage storage;
|
|
|
|
storage.ss_family = AF_INET;
|
|
|
|
memcpy(storage.ss_storage, &result->entry.address.raw, sizeof(result->entry.address.raw));
|
|
|
|
if (send(client.socket, &storage, sizeof(storage), 0) == -1)
|
|
|
|
dprintln("send: {}", strerror(errno));
|
|
|
|
client.close = true;
|
|
|
|
break;
|
2024-02-08 03:14:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 23:47:39 +02:00
|
|
|
for (auto& client : clients)
|
|
|
|
{
|
|
|
|
if (!FD_ISSET(client.socket, &fds))
|
|
|
|
continue;
|
2024-02-08 12:03:54 +02:00
|
|
|
|
2024-02-12 23:47:39 +02:00
|
|
|
if (!client.query.empty())
|
|
|
|
{
|
|
|
|
dprintln("Client already has a query");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto query = read_service_query(client.socket);
|
|
|
|
if (!query.has_value())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
BAN::Optional<DNSEntry> result;
|
|
|
|
|
|
|
|
if (dns_cache.contains(*query))
|
|
|
|
{
|
|
|
|
auto& cached = dns_cache[*query];
|
|
|
|
if (time(nullptr) <= cached.valid_until)
|
|
|
|
result = cached;
|
|
|
|
else
|
|
|
|
dns_cache.remove(*query);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.has_value())
|
|
|
|
{
|
|
|
|
sockaddr_storage storage;
|
|
|
|
storage.ss_family = AF_INET;
|
|
|
|
memcpy(storage.ss_storage, &result->address.raw, sizeof(result->address.raw));
|
|
|
|
if (send(client.socket, &storage, sizeof(storage), 0) == -1)
|
|
|
|
dprintln("send: {}", strerror(errno));
|
|
|
|
client.close = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
client.query = query.release_value();
|
|
|
|
client.query_id = rand() % 0xFFFF;
|
|
|
|
send_dns_query(dns_socket, client.query, client.query_id);
|
|
|
|
}
|
2024-02-08 12:03:54 +02:00
|
|
|
|
2024-02-12 23:47:39 +02:00
|
|
|
for (auto it = clients.begin(); it != clients.end();)
|
|
|
|
{
|
|
|
|
if (!it->close)
|
|
|
|
{
|
|
|
|
it++;
|
|
|
|
continue;
|
|
|
|
}
|
2024-02-08 12:03:54 +02:00
|
|
|
|
2024-02-12 23:47:39 +02:00
|
|
|
close(it->socket);
|
|
|
|
it = clients.remove(it);
|
|
|
|
}
|
2024-02-08 03:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|