2018-07-16 12:48:04 +00:00
|
|
|
#ifndef LIBLLARP_DNSC_HPP
|
|
|
|
#define LIBLLARP_DNSC_HPP
|
|
|
|
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <ev.h> // for sockaadr
|
2018-12-12 00:58:08 +00:00
|
|
|
#include <dns.hpp> // get protocol structs
|
2018-07-22 03:34:28 +00:00
|
|
|
|
|
|
|
// internal, non-public functions
|
|
|
|
// well dnsc init/stop are public...
|
|
|
|
|
|
|
|
struct dnsc_answer_request;
|
|
|
|
|
2018-10-09 12:41:33 +00:00
|
|
|
#define DNC_BUF_SIZE 512
|
|
|
|
/// a question to be asked remotely (the actual bytes to send on the wire)
|
|
|
|
// header, question
|
|
|
|
struct dns_query
|
|
|
|
{
|
|
|
|
uint16_t length;
|
|
|
|
unsigned char request[DNC_BUF_SIZE];
|
2018-11-18 23:08:02 +00:00
|
|
|
// char *url;
|
2018-10-09 12:41:33 +00:00
|
|
|
// uint16_t reqType;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dns_query *
|
|
|
|
build_dns_packet(char *url, uint16_t id, uint16_t reqType);
|
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
/// hook function to handle an dns client request
|
2018-07-22 03:34:28 +00:00
|
|
|
// should we pass by llarp::Addr
|
|
|
|
// not as long as we're supporting raw
|
|
|
|
typedef void (*dnsc_answer_hook_func)(dnsc_answer_request *request);
|
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
/// struct for dns client requests
|
2018-07-22 03:34:28 +00:00
|
|
|
struct dnsc_answer_request
|
|
|
|
{
|
|
|
|
/// sock type
|
2018-11-18 23:08:02 +00:00
|
|
|
void *sock; // points to udp that sent the request to DNSc...
|
2018-07-22 03:34:28 +00:00
|
|
|
/// customizable (used for hook (outer request))
|
|
|
|
void *user;
|
2018-11-18 23:08:02 +00:00
|
|
|
/// request storage
|
2018-07-22 03:34:28 +00:00
|
|
|
dns_msg_question question;
|
2018-11-18 23:08:02 +00:00
|
|
|
/// response storage
|
|
|
|
dns_packet packet;
|
2018-07-22 03:34:28 +00:00
|
|
|
/// hook
|
|
|
|
dnsc_answer_hook_func resolved;
|
|
|
|
/// result
|
|
|
|
bool found;
|
2018-11-18 23:08:02 +00:00
|
|
|
|
2018-11-18 23:48:50 +00:00
|
|
|
// llarp::huint32_t result;
|
|
|
|
// std::string revDNS;
|
|
|
|
|
2018-07-22 03:34:28 +00:00
|
|
|
// a reference to dnsc_context incase of multiple contexts
|
|
|
|
struct dnsc_context *context;
|
|
|
|
};
|
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
/// event handler for processing DNS responses
|
2018-07-22 03:34:28 +00:00
|
|
|
void
|
2018-10-03 10:54:12 +00:00
|
|
|
llarp_handle_dnsc_recvfrom(struct llarp_udp_io *const udp,
|
2018-11-23 14:37:26 +00:00
|
|
|
const struct sockaddr *addr, llarp_buffer_t buf);
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
/// generic handler for processing DNS responses
|
2018-10-03 10:54:12 +00:00
|
|
|
/// this doesn't look like it exists
|
|
|
|
/// that's because raw_resolve_host calls generic_handle_dnsc_recvfrom directly
|
|
|
|
/// because we don't need a callback like recvfrom
|
|
|
|
/// because we're not evented
|
|
|
|
/// however daemon/dns expects this
|
2018-11-22 00:29:42 +00:00
|
|
|
/*
|
2018-07-22 03:34:28 +00:00
|
|
|
void
|
2018-08-24 00:39:01 +00:00
|
|
|
raw_handle_recvfrom(int *sockfd, const struct sockaddr *addr, const void *buf,
|
2018-10-03 10:54:12 +00:00
|
|
|
const ssize_t sz);
|
2018-11-22 00:29:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
// removed saddr, if needed get through request
|
|
|
|
void
|
|
|
|
generic_handle_dnsc_recvfrom(dnsc_answer_request *request,
|
|
|
|
llarp_buffer_t buffer, dns_msg_header *hdr);
|
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
/// DNS client context (one needed per upstream DNS server)
|
2018-07-22 03:34:28 +00:00
|
|
|
struct dnsc_context
|
|
|
|
{
|
2018-09-29 10:17:13 +00:00
|
|
|
/// Target: DNS servers to use
|
|
|
|
std::vector< llarp::Addr > resolvers;
|
|
|
|
/// udp tracker
|
2018-08-01 09:04:40 +00:00
|
|
|
struct dns_tracker *tracker;
|
|
|
|
/// sock type
|
|
|
|
void *sock;
|
2018-07-22 03:34:28 +00:00
|
|
|
// where to create the new sockets
|
|
|
|
struct llarp_udp_io *udp;
|
2018-09-22 10:21:26 +00:00
|
|
|
/// We will likely need something for timing events (timeouts)
|
2018-12-10 14:14:55 +00:00
|
|
|
llarp::Logic *logic;
|
2018-07-22 03:34:28 +00:00
|
|
|
};
|
|
|
|
|
2018-11-21 23:34:26 +00:00
|
|
|
/// async (blocking w/callback) resolve a hostname using generic socks
|
2018-08-01 09:04:40 +00:00
|
|
|
void
|
2018-10-03 10:54:12 +00:00
|
|
|
raw_resolve_host(struct dnsc_context *const dnsc, const char *url,
|
2018-11-03 13:19:18 +00:00
|
|
|
dnsc_answer_hook_func resolved, void *const user,
|
|
|
|
uint16_t type);
|
2018-08-01 09:04:40 +00:00
|
|
|
|
2018-11-22 00:39:09 +00:00
|
|
|
/// async (non blocking w/callback) resolve a hostname using llarp platform
|
|
|
|
/// framework
|
2018-08-01 09:04:40 +00:00
|
|
|
bool
|
2018-10-03 10:54:12 +00:00
|
|
|
llarp_resolve_host(struct dnsc_context *const dns, const char *url,
|
2018-11-03 13:19:18 +00:00
|
|
|
dnsc_answer_hook_func resolved, void *const user,
|
|
|
|
uint16_t type);
|
2018-08-01 09:04:40 +00:00
|
|
|
|
|
|
|
/// cleans up request structure allocations
|
|
|
|
void
|
2018-10-03 10:54:12 +00:00
|
|
|
llarp_host_resolved(dnsc_answer_request *const request);
|
2018-08-01 09:04:40 +00:00
|
|
|
|
2018-07-22 03:34:28 +00:00
|
|
|
/// initialize dns subsystem and bind socket
|
|
|
|
/// returns true on bind success otherwise returns false
|
|
|
|
bool
|
2018-12-10 14:14:55 +00:00
|
|
|
llarp_dnsc_init(struct dnsc_context *const dnsc, llarp::Logic *const logic,
|
2018-10-03 10:54:12 +00:00
|
|
|
struct llarp_ev_loop *const netloop,
|
2018-09-29 10:17:13 +00:00
|
|
|
const llarp::Addr &dnsc_sockaddr);
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
/// shutdowns any events, and deallocates for this context
|
2018-07-22 03:34:28 +00:00
|
|
|
bool
|
2018-10-03 10:54:12 +00:00
|
|
|
llarp_dnsc_stop(struct dnsc_context *const dnsc);
|
2018-07-16 12:48:04 +00:00
|
|
|
|
|
|
|
#endif
|