2019-01-11 01:19:36 +00:00
|
|
|
#ifndef LLARP_DNS_HPP
|
|
|
|
#define LLARP_DNS_HPP
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-12-12 00:58:08 +00:00
|
|
|
#include <dns.h>
|
2018-07-22 03:34:28 +00:00
|
|
|
#include <sys/types.h> // for uint & ssize_t
|
|
|
|
|
2018-09-25 08:31:29 +00:00
|
|
|
#ifndef _WIN32
|
2018-09-20 10:07:16 +00:00
|
|
|
#include <sys/socket.h>
|
2018-09-25 08:31:29 +00:00
|
|
|
#endif
|
2018-09-20 10:07:16 +00:00
|
|
|
|
2019-01-11 01:19:36 +00:00
|
|
|
#include <dns/rectypes.hpp>
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net.hpp> // for llarp::Addr , llarp::huint32_t
|
2018-09-25 13:20:39 +00:00
|
|
|
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-11-22 00:39:09 +00:00
|
|
|
#define LLARP_DNS_RECTYPE_A 1
|
|
|
|
#define LLARP_DNS_RECTYPE_NS 2
|
|
|
|
#define LLARP_DNS_RECTYPE_CNAME 5
|
|
|
|
#define LLARP_DNS_RECTYPE_SOA 6
|
|
|
|
#define LLARP_DNS_RECTYPE_PTR 12
|
|
|
|
#define LLARP_DNS_RECTYPE_MX 15
|
|
|
|
#define LLARP_DNS_RECTYPE_TXT 16
|
2018-11-21 23:23:19 +00:00
|
|
|
|
2019-01-11 01:19:36 +00:00
|
|
|
struct dnsc_answer_request;
|
2018-10-01 09:59:50 +00:00
|
|
|
struct dnsd_context;
|
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
// dnsc can work over any UDP socket
|
|
|
|
// however we can't ignore udp->user
|
|
|
|
// we need to be able to reference the request (being a request or response)
|
|
|
|
// bottom line is we can't use udp->user
|
|
|
|
// so we'll need to track all incoming and outgoing requests
|
2018-07-27 04:07:22 +00:00
|
|
|
struct dns_tracker
|
|
|
|
{
|
|
|
|
// uint c_responses;
|
2018-10-13 01:55:06 +00:00
|
|
|
uint32_t c_requests;
|
2018-08-01 09:04:40 +00:00
|
|
|
// request has to be a pointer
|
2018-10-13 01:55:06 +00:00
|
|
|
std::unordered_map< uint32_t, std::unique_ptr< dnsc_answer_request > >
|
2018-09-28 14:29:34 +00:00
|
|
|
client_request;
|
2018-07-27 04:07:22 +00:00
|
|
|
// FIXME: support multiple dns server contexts
|
|
|
|
dnsd_context *dnsd;
|
2018-09-25 13:20:39 +00:00
|
|
|
// rn we need 1 tracker per DNSd and each DNSd needs it's own IP
|
2018-10-03 10:49:57 +00:00
|
|
|
// actually we can bind once and use the tracker to sort
|
|
|
|
// but no way to tell what DNSd they want...
|
2018-09-28 14:29:34 +00:00
|
|
|
// std::map< llarp::Addr, std::unique_ptr< dnsc_answer_request > > dnsds;
|
2018-07-27 04:07:22 +00:00
|
|
|
// std::map< uint, dnsd_question_request * > daemon_request;
|
|
|
|
};
|
|
|
|
|
2018-07-22 03:34:28 +00:00
|
|
|
// protocol parsing/writing structures & functions
|
|
|
|
struct dns_msg_header
|
|
|
|
{
|
|
|
|
uint16_t id;
|
2018-12-02 18:07:07 +00:00
|
|
|
|
2018-07-25 00:35:11 +00:00
|
|
|
uint8_t qr : 1;
|
|
|
|
uint8_t opcode : 4;
|
|
|
|
uint8_t aa : 1;
|
|
|
|
uint8_t tc : 1;
|
|
|
|
uint8_t rd : 1;
|
|
|
|
|
|
|
|
uint8_t ra : 1;
|
|
|
|
uint8_t z : 1;
|
|
|
|
uint8_t ad : 1;
|
|
|
|
uint8_t cd : 1;
|
|
|
|
uint8_t rcode : 4;
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-12-02 18:07:07 +00:00
|
|
|
uint16_t
|
|
|
|
fields() const
|
2018-12-01 14:35:11 +00:00
|
|
|
{
|
2018-12-02 18:07:07 +00:00
|
|
|
return (qr << 15) | (opcode << 14) | (aa << 10) | (tc << 9)
|
|
|
|
| (rd << 8) << (ra << 7) | (z << 6) | rcode;
|
2018-12-01 14:35:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 03:34:28 +00:00
|
|
|
uint16_t qdCount;
|
|
|
|
uint16_t anCount;
|
|
|
|
uint16_t nsCount;
|
|
|
|
uint16_t arCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dns_msg_question
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
uint16_t type;
|
|
|
|
uint16_t qClass;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dns_msg_answer
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
uint16_t type;
|
|
|
|
uint16_t aClass;
|
|
|
|
uint32_t ttl;
|
|
|
|
uint16_t rdLen;
|
2018-11-11 15:24:08 +00:00
|
|
|
std::vector< byte_t > rData;
|
2018-11-18 23:08:02 +00:00
|
|
|
std::unique_ptr< llarp::dns::record > record;
|
2018-07-22 03:34:28 +00:00
|
|
|
};
|
|
|
|
|
2018-08-02 12:51:49 +00:00
|
|
|
struct dns_packet
|
|
|
|
{
|
2018-12-01 14:35:11 +00:00
|
|
|
struct dns_msg_header header;
|
2018-09-25 13:20:39 +00:00
|
|
|
std::vector< std::unique_ptr< dns_msg_question > > questions;
|
|
|
|
std::vector< std::unique_ptr< dns_msg_answer > > answers;
|
|
|
|
std::vector< std::unique_ptr< dns_msg_answer > > auth_rrs;
|
|
|
|
std::vector< std::unique_ptr< dns_msg_answer > > additional_rrs;
|
2018-08-02 12:51:49 +00:00
|
|
|
};
|
|
|
|
|
2018-11-18 23:08:02 +00:00
|
|
|
std::vector< byte_t >
|
|
|
|
packet2bytes(dns_packet &in);
|
|
|
|
|
2018-10-09 12:40:24 +00:00
|
|
|
std::string
|
2018-11-03 13:19:18 +00:00
|
|
|
getDNSstring(const char *const buffer, uint32_t *pos);
|
2018-10-09 12:40:24 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
code_domain(char *&buffer, const std::string &domain) throw();
|
|
|
|
|
2018-11-18 23:08:02 +00:00
|
|
|
void
|
|
|
|
vcode_domain(std::vector< byte_t > &bytes, const std::string &domain) throw();
|
|
|
|
|
|
|
|
void
|
|
|
|
vput16bits(std::vector< byte_t > &bytes, uint16_t value) throw();
|
|
|
|
|
|
|
|
void
|
|
|
|
vput32bits(std::vector< byte_t > &bytes, uint32_t value) throw();
|
|
|
|
|
2018-07-27 04:07:22 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
uint16_t
|
|
|
|
get16bits(const char *&buffer) throw();
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-07-27 04:07:22 +00:00
|
|
|
uint32_t
|
|
|
|
get32bits(const char *&buffer) throw();
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-12-01 14:35:11 +00:00
|
|
|
bool
|
2018-12-02 18:07:07 +00:00
|
|
|
decode_hdr(llarp_buffer_t *buffer, dns_msg_header *hdr);
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-07-27 04:07:22 +00:00
|
|
|
dns_msg_question *
|
2018-11-03 13:19:18 +00:00
|
|
|
decode_question(const char *buffer, uint32_t *pos);
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-07-27 04:07:22 +00:00
|
|
|
dns_msg_answer *
|
2018-11-03 13:19:18 +00:00
|
|
|
decode_answer(const char *const buffer, uint32_t *pos);
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-07-27 04:07:22 +00:00
|
|
|
void
|
|
|
|
put16bits(char *&buffer, uint16_t value) throw();
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-07-27 04:07:22 +00:00
|
|
|
void
|
|
|
|
put32bits(char *&buffer, uint32_t value) throw();
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-07-27 04:07:22 +00:00
|
|
|
void
|
|
|
|
llarp_handle_dns_recvfrom(struct llarp_udp_io *udp,
|
2019-02-03 00:48:10 +00:00
|
|
|
const struct sockaddr *addr, ManagedBuffer buf);
|
2018-07-27 04:07:22 +00:00
|
|
|
}
|
2018-07-22 03:34:28 +00:00
|
|
|
#endif
|