2018-07-16 12:48:04 +00:00
|
|
|
#ifndef LIBLLARP_DNSD_HPP
|
|
|
|
#define LIBLLARP_DNSD_HPP
|
|
|
|
|
2018-07-25 00:35:11 +00:00
|
|
|
#include <llarp/ev.h> // for sockaadr
|
2018-07-24 01:43:32 +00:00
|
|
|
#include <string>
|
2018-07-25 00:35:11 +00:00
|
|
|
#include "dns.hpp" // question and dnsc
|
|
|
|
#include "dnsc.hpp"
|
2018-07-16 12:48:04 +00:00
|
|
|
|
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-07-21 13:24:47 +00:00
|
|
|
typedef ssize_t (*sendto_dns_hook_func)(void *sock, const struct sockaddr *from,
|
|
|
|
const void *buffer, size_t length);
|
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-07-22 03:34:28 +00:00
|
|
|
// raw or llarp subsystem
|
|
|
|
bool llarp;
|
2018-07-16 12:48:04 +00:00
|
|
|
/// request id
|
|
|
|
int id;
|
2018-07-22 03:34:28 +00:00
|
|
|
/// question being asked
|
|
|
|
dns_msg_question question;
|
|
|
|
// request source socket
|
2018-07-16 12:48:04 +00:00
|
|
|
struct sockaddr *from;
|
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
|
|
|
|
sockaddr *returnThis;
|
|
|
|
};
|
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
/// intercept query hook functor
|
2018-08-08 12:40:54 +00:00
|
|
|
typedef dnsd_query_hook_response *(*intercept_query_hook)(
|
|
|
|
std::string name, const struct sockaddr *from,
|
|
|
|
struct dnsd_question_request *request);
|
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-08-02 12:51:49 +00:00
|
|
|
/// for timers (MAYBEFIXME? maybe we decouple this)
|
|
|
|
struct llarp_logic *logic;
|
2018-08-01 09:04:40 +00:00
|
|
|
/// tracker
|
|
|
|
struct dns_tracker *tracker;
|
|
|
|
/// upstream DNS client context to use
|
2018-07-22 03:34:28 +00:00
|
|
|
dnsc_context client;
|
|
|
|
/// custom data for intercept query hook
|
|
|
|
void *user;
|
|
|
|
/// hook function for intercepting dns requests
|
|
|
|
intercept_query_hook intercept;
|
|
|
|
};
|
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
/// udp event handler
|
2018-07-22 03:34:28 +00:00
|
|
|
void
|
|
|
|
llarp_handle_dnsd_recvfrom(struct llarp_udp_io *udp,
|
2018-08-01 09:04:40 +00:00
|
|
|
const struct sockaddr *saddr, const void *buf,
|
2018-07-22 03:34:28 +00:00
|
|
|
ssize_t sz);
|
|
|
|
|
2018-08-02 12:51:49 +00:00
|
|
|
/// for hook functions to use
|
|
|
|
void
|
|
|
|
writecname_dnss_response(std::string cname, const struct sockaddr *from,
|
|
|
|
dnsd_question_request *request);
|
|
|
|
|
2018-08-08 12:40:54 +00:00
|
|
|
void
|
|
|
|
writesend_dnss_response(struct sockaddr *hostRes, const struct sockaddr *from,
|
|
|
|
dnsd_question_request *request);
|
2018-08-02 12:51:49 +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
|
|
|
|
llarp_dnsd_init(struct dnsd_context *dnsd, struct llarp_ev_loop *netloop,
|
2018-08-08 12:40:54 +00:00
|
|
|
struct llarp_logic *logic, const char *dnsd_ifname,
|
|
|
|
uint16_t dnsd_port, const char *dnsc_hostname,
|
|
|
|
uint16_t dnsc_port);
|
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
|
|
|
|
llarp_dnsd_stop(struct dnsd_context *dnsd);
|
|
|
|
|
2018-07-16 12:48:04 +00:00
|
|
|
#endif
|