2019-01-11 01:19:36 +00:00
|
|
|
#ifndef LLARP_DNSD_HPP
|
|
|
|
#define LLARP_DNSD_HPP
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-12-12 00:58:08 +00:00
|
|
|
#include <dns.hpp> // question and dnsc
|
|
|
|
#include <dnsc.hpp>
|
2019-01-11 01:19:36 +00:00
|
|
|
#include <ev/ev.h> // for sockaadr
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <string>
|
|
|
|
|
2018-10-09 12:42:21 +00:00
|
|
|
//
|
|
|
|
// Input structures/functions:
|
|
|
|
//
|
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
// fwd declaration
|
2018-07-23 23:56:26 +00:00
|
|
|
struct dnsd_context;
|
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
/// sendto hook functor
|
2018-11-26 13:30:03 +00:00
|
|
|
using sendto_dns_hook_func = std::function< ssize_t(
|
2019-02-03 00:48:10 +00:00
|
|
|
void *sock, const struct sockaddr *from, ManagedBuffer) >;
|
2018-09-29 10:22:00 +00:00
|
|
|
// FIXME: llarp::Addr
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
/// DNS server query request
|
2018-07-22 03:34:28 +00:00
|
|
|
struct dnsd_question_request
|
2018-07-16 12:48:04 +00:00
|
|
|
{
|
|
|
|
/// sock type
|
|
|
|
void *user;
|
2018-10-09 12:42:21 +00:00
|
|
|
// raw or llarp subsystem (is this used? does this matter?)
|
2018-07-22 03:34:28 +00:00
|
|
|
bool llarp;
|
2018-12-01 14:35:11 +00:00
|
|
|
/// request header
|
|
|
|
dns_msg_header hdr;
|
2018-07-22 03:34:28 +00:00
|
|
|
/// question being asked
|
|
|
|
dns_msg_question question;
|
|
|
|
// request source socket
|
2018-09-29 10:22:00 +00:00
|
|
|
struct sockaddr *from; // FIXME: convert to llarp::Addr
|
2018-08-01 12:51:28 +00:00
|
|
|
sendto_dns_hook_func sendto_hook; // sendto hook tbh
|
2018-07-22 03:34:28 +00:00
|
|
|
// maybe a reference to dnsd_context incase of multiple
|
2018-07-25 00:35:11 +00:00
|
|
|
dnsd_context *context; // or you can access it via user (udp)
|
2018-07-16 12:48:04 +00:00
|
|
|
};
|
|
|
|
|
2018-08-08 12:40:54 +00:00
|
|
|
// FIXME: made better as a two way structure, collapse the request and response
|
|
|
|
// together
|
2018-08-02 12:51:49 +00:00
|
|
|
struct dnsd_query_hook_response
|
|
|
|
{
|
|
|
|
/// turn off communication
|
|
|
|
bool dontSendResponse;
|
|
|
|
/// turn off recursion
|
|
|
|
bool dontLookUp;
|
|
|
|
/// potential address
|
2018-11-29 14:01:13 +00:00
|
|
|
llarp::huint32_t returnThis;
|
2018-08-02 12:51:49 +00:00
|
|
|
};
|
|
|
|
|
2018-10-09 12:42:21 +00:00
|
|
|
/// builds and fires a request based based on llarp_udp_io udp event
|
|
|
|
/// called by the llarp_handle_dns_recvfrom generic (dnsd/dnsc) handler in dns
|
|
|
|
void
|
|
|
|
llarp_handle_dnsd_recvfrom(struct llarp_udp_io *udp,
|
2019-02-03 00:48:10 +00:00
|
|
|
const struct sockaddr *addr, ManagedBuffer buf);
|
2018-10-09 12:42:21 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// output structures/functions:
|
|
|
|
//
|
|
|
|
// we may want to pass dnsd_question_request to these,
|
|
|
|
// incase we need to send an error back up through the pipeline
|
|
|
|
|
2018-11-18 23:08:02 +00:00
|
|
|
// FIXME: just use the from in the request
|
|
|
|
|
2018-10-09 12:42:21 +00:00
|
|
|
/// NXDOMAIN not found
|
|
|
|
void
|
2018-12-01 14:35:11 +00:00
|
|
|
write404_dnss_response(const dnsd_question_request *request);
|
2018-10-09 12:42:21 +00:00
|
|
|
|
|
|
|
/// for hook functions to use
|
|
|
|
void
|
2018-12-02 18:07:07 +00:00
|
|
|
writecname_dnss_response(std::string cname,
|
|
|
|
const dnsd_question_request *request);
|
2018-10-09 12:42:21 +00:00
|
|
|
// FIXME: llarp::Addr
|
|
|
|
|
|
|
|
/// send an A record found response
|
|
|
|
void
|
2018-11-18 23:48:50 +00:00
|
|
|
writesend_dnss_response(llarp::huint32_t *hostRes,
|
2018-12-01 14:35:11 +00:00
|
|
|
const dnsd_question_request *request);
|
2018-10-09 12:42:21 +00:00
|
|
|
// FIXME: llarp::Addr
|
|
|
|
|
|
|
|
/// send an PTR record found response
|
|
|
|
void
|
2018-12-02 18:07:07 +00:00
|
|
|
writesend_dnss_revresponse(std::string reverse,
|
|
|
|
const dnsd_question_request *request);
|
2018-10-09 12:42:21 +00:00
|
|
|
// FIXME: llarp::Addr
|
|
|
|
|
|
|
|
//
|
|
|
|
// setup/teardown functions/structure:
|
|
|
|
//
|
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
/// intercept query hook functor
|
2018-11-22 23:59:03 +00:00
|
|
|
using intercept_query_hook = std::function< dnsd_query_hook_response *(
|
2018-12-01 14:35:11 +00:00
|
|
|
std::string name, const dnsd_question_request *request) >;
|
2018-09-29 10:22:00 +00:00
|
|
|
// FIXME: llarp::Addr
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
/// DNS Server context
|
2018-07-22 03:34:28 +00:00
|
|
|
struct dnsd_context
|
|
|
|
{
|
|
|
|
/// DNS daemon socket to listen on
|
|
|
|
struct llarp_udp_io udp;
|
2018-09-22 10:22:18 +00:00
|
|
|
/// udp tracker
|
2018-08-01 09:04:40 +00:00
|
|
|
struct dns_tracker *tracker;
|
|
|
|
/// upstream DNS client context to use
|
2018-07-22 03:34:28 +00:00
|
|
|
dnsc_context client;
|
2018-09-22 10:22:18 +00:00
|
|
|
/// custom data for intercept query hook (used for configuration of hook)
|
2018-07-22 03:34:28 +00:00
|
|
|
void *user;
|
|
|
|
/// hook function for intercepting dns requests
|
|
|
|
intercept_query_hook intercept;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// initialize dns subsystem and bind socket
|
|
|
|
/// returns true on bind success otherwise returns false
|
|
|
|
bool
|
2018-12-10 14:14:55 +00:00
|
|
|
llarp_dnsd_init(struct dnsd_context *const dnsd, llarp::Logic *const logic,
|
2018-10-03 10:59:49 +00:00
|
|
|
struct llarp_ev_loop *const netloop,
|
|
|
|
const llarp::Addr &dnsd_sockaddr,
|
2018-09-29 10:22:00 +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:59:49 +00:00
|
|
|
llarp_dnsd_stop(struct dnsd_context *const dnsd);
|
2018-07-22 03:34:28 +00:00
|
|
|
|
2018-07-16 12:48:04 +00:00
|
|
|
#endif
|