fix headers, clang-format, favor llarp::Addr

pull/19/head
Ryan Tharp 6 years ago
parent f032a83e37
commit 6bfd55f3f4

@ -1,8 +1,8 @@
#ifndef LIBLLARP_DNSC_HPP #ifndef LIBLLARP_DNSC_HPP
#define LIBLLARP_DNSC_HPP #define LIBLLARP_DNSC_HPP
#include <llarp/ev.h> // for sockaadr #include <llarp/ev.h> // for sockaadr
#include "dns.hpp" // get protocol structs #include <llarp/dns.hpp> // get protocol structs
// internal, non-public functions // internal, non-public functions
// well dnsc init/stop are public... // well dnsc init/stop are public...
@ -27,7 +27,7 @@ struct dnsc_answer_request
dnsc_answer_hook_func resolved; dnsc_answer_hook_func resolved;
/// result /// result
bool found; bool found;
struct sockaddr result; llarp::Addr result;
std::string revDNS; std::string revDNS;
// a reference to dnsc_context incase of multiple contexts // a reference to dnsc_context incase of multiple contexts
struct dnsc_context *context; struct dnsc_context *context;
@ -35,14 +35,19 @@ struct dnsc_answer_request
/// event handler for processing DNS responses /// event handler for processing DNS responses
void void
llarp_handle_dnsc_recvfrom(struct llarp_udp_io *udp, llarp_handle_dnsc_recvfrom(struct llarp_udp_io *const udp,
const struct sockaddr *addr, const void *buf, const struct sockaddr *addr, const void *buf,
ssize_t sz); const ssize_t sz);
/// generic handler for processing DNS responses /// generic handler for processing DNS responses
/// 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
void void
raw_handle_recvfrom(int *sockfd, const struct sockaddr *addr, const void *buf, raw_handle_recvfrom(int *sockfd, const struct sockaddr *addr, const void *buf,
ssize_t sz); const ssize_t sz);
/// DNS client context (one needed per upstream DNS server) /// DNS client context (one needed per upstream DNS server)
struct dnsc_context struct dnsc_context
@ -61,27 +66,28 @@ struct dnsc_context
/// async resolve a hostname using generic socks /// async resolve a hostname using generic socks
void void
raw_resolve_host(struct dnsc_context *dnsc, const char *url, raw_resolve_host(struct dnsc_context *const dnsc, const char *url,
dnsc_answer_hook_func resolved, void *user); dnsc_answer_hook_func resolved, void *const user);
/// async resolve a hostname using llarp platform framework /// async resolve a hostname using llarp platform framework
bool bool
llarp_resolve_host(struct dnsc_context *dns, const char *url, llarp_resolve_host(struct dnsc_context *const dns, const char *url,
dnsc_answer_hook_func resolved, void *user); dnsc_answer_hook_func resolved, void *const user);
/// cleans up request structure allocations /// cleans up request structure allocations
void void
llarp_host_resolved(dnsc_answer_request *request); llarp_host_resolved(dnsc_answer_request *const request);
/// initialize dns subsystem and bind socket /// initialize dns subsystem and bind socket
/// returns true on bind success otherwise returns false /// returns true on bind success otherwise returns false
bool bool
llarp_dnsc_init(struct dnsc_context *dnsc, struct llarp_logic *logic, llarp_dnsc_init(struct dnsc_context *const dnsc,
struct llarp_ev_loop *netloop, struct llarp_logic *const logic,
struct llarp_ev_loop *const netloop,
const llarp::Addr &dnsc_sockaddr); const llarp::Addr &dnsc_sockaddr);
/// shutdowns any events, and deallocates for this context /// shutdowns any events, and deallocates for this context
bool bool
llarp_dnsc_stop(struct dnsc_context *dnsc); llarp_dnsc_stop(struct dnsc_context *const dnsc);
#endif #endif

@ -133,7 +133,8 @@ answer_request_alloc(struct dnsc_context *dnsc, void *sock, const char *url,
if(id == 65535) if(id == 65535)
id = 0; id = 0;
// conflict: do we need this? // conflict: do we need this?
//tracker->client_request[id] = std::unique_ptr< dnsc_answer_request >(request); // tracker->client_request[id] = std::unique_ptr< dnsc_answer_request
// >(request);
dns_query *dns_packet = build_dns_packet( dns_query *dns_packet = build_dns_packet(
(char *)request->question.name.c_str(), id, request->question.type); (char *)request->question.name.c_str(), id, request->question.type);
@ -264,8 +265,8 @@ generic_handle_dnsc_recvfrom(dnsc_answer_request *request,
llarp::LogDebug("Read answer, bytes left ", diff); llarp::LogDebug("Read answer, bytes left ", diff);
if(diff > sz) if(diff > sz)
{ {
llarp::LogWarn("Would read past end of dns packet. for ", // llarp::LogWarn("Would read past end of dns packet. for ",
request->question.name); // request->question.name);
break; break;
} }
} }
@ -319,7 +320,8 @@ generic_handle_dnsc_recvfrom(dnsc_answer_request *request,
if(answer == nullptr) if(answer == nullptr)
{ {
llarp::LogWarn("nameserver ", upstreamAddr, " didnt return any answers:"); llarp::LogWarn("nameserver ", upstreamAddr,
" didnt return any answers for ", question->name);
request->resolved(request); request->resolved(request);
return; return;
} }
@ -370,6 +372,7 @@ generic_handle_dnsc_recvfrom(dnsc_answer_request *request,
if(answer->rdLen == 4) if(answer->rdLen == 4)
{ {
/*
request->result.sa_family = AF_INET; request->result.sa_family = AF_INET;
#if((__APPLE__ && __MACH__) || __FreeBSD__) #if((__APPLE__ && __MACH__) || __FreeBSD__)
request->result.sa_len = sizeof(in_addr); request->result.sa_len = sizeof(in_addr);
@ -382,9 +385,12 @@ generic_handle_dnsc_recvfrom(dnsc_answer_request *request,
ip[1] = answer->rData[1]; ip[1] = answer->rData[1];
ip[2] = answer->rData[2]; ip[2] = answer->rData[2];
ip[3] = answer->rData[3]; ip[3] = answer->rData[3];
*/
request->result.from_4int(answer->rData[0], answer->rData[1],
answer->rData[2], answer->rData[3]);
llarp::Addr test(request->result); // llarp::Addr test(request->result);
llarp::LogDebug(test); // llarp::LogDebug(request->result);
request->found = true; request->found = true;
request->resolved(request); request->resolved(request);
return; return;
@ -408,8 +414,8 @@ generic_handle_dnsc_recvfrom(dnsc_answer_request *request,
} }
void void
raw_resolve_host(struct dnsc_context *dnsc, const char *url, raw_resolve_host(struct dnsc_context *const dnsc, const char *url,
dnsc_answer_hook_func resolved, void *user) dnsc_answer_hook_func resolved, void *const user)
{ {
dns_query *dns_packet = dns_query *dns_packet =
answer_request_alloc(dnsc, nullptr, url, resolved, user); answer_request_alloc(dnsc, nullptr, url, resolved, user);
@ -451,13 +457,8 @@ raw_resolve_host(struct dnsc_context *dnsc, const char *url,
// hexdump("sending packet", &dnsQuery.request, dnsQuery.length); // hexdump("sending packet", &dnsQuery.request, dnsQuery.length);
#ifdef _WIN32
ret = sendto(sockfd, (const char *)dns_packet->request, dns_packet->length, 0, ret = sendto(sockfd, (const char *)dns_packet->request, dns_packet->length, 0,
(struct sockaddr *)&addr, size); (struct sockaddr *)&addr, size);
#else
ret = sendto(sockfd, (const char *)dns_packet->request, dns_packet->length, 0,
(struct sockaddr *)&addr, size);
#endif
delete dns_packet; delete dns_packet;
if(ret < 0) if(ret < 0)
@ -502,9 +503,9 @@ raw_resolve_host(struct dnsc_context *dnsc, const char *url,
/// intermediate udp_io handler /// intermediate udp_io handler
void void
llarp_handle_dnsc_recvfrom(struct llarp_udp_io *udp, llarp_handle_dnsc_recvfrom(struct llarp_udp_io *const udp,
const struct sockaddr *saddr, const void *buf, const struct sockaddr *saddr, const void *buf,
ssize_t sz) const ssize_t sz)
{ {
unsigned char *castBuf = (unsigned char *)buf; unsigned char *castBuf = (unsigned char *)buf;
// auto buffer = llarp::StackBuffer< decltype(castBuf) >(castBuf); // auto buffer = llarp::StackBuffer< decltype(castBuf) >(castBuf);
@ -528,8 +529,8 @@ llarp_handle_dnsc_recvfrom(struct llarp_udp_io *udp,
} }
bool bool
llarp_resolve_host(struct dnsc_context *dnsc, const char *url, llarp_resolve_host(struct dnsc_context *const dnsc, const char *url,
dnsc_answer_hook_func resolved, void *user) dnsc_answer_hook_func resolved, void *const user)
{ {
// FIXME: probably can be stack allocated // FIXME: probably can be stack allocated
dns_query *dns_packet = dns_query *dns_packet =
@ -594,7 +595,7 @@ llarp_resolve_host(struct dnsc_context *dnsc, const char *url,
} }
void void
llarp_host_resolved(dnsc_answer_request *request) llarp_host_resolved(dnsc_answer_request *const request)
{ {
dns_tracker *tracker = (dns_tracker *)request->context->tracker; dns_tracker *tracker = (dns_tracker *)request->context->tracker;
auto val = std::find_if( auto val = std::find_if(
@ -612,8 +613,10 @@ llarp_host_resolved(dnsc_answer_request *request)
} }
bool bool
llarp_dnsc_init(struct dnsc_context *dnsc, struct llarp_logic *logic, llarp_dnsc_init(struct dnsc_context *const dnsc,
struct llarp_ev_loop *netloop, const llarp::Addr &dnsc_sockaddr) struct llarp_logic *const logic,
struct llarp_ev_loop *const netloop,
const llarp::Addr &dnsc_sockaddr)
{ {
// create client socket // create client socket
if(netloop) if(netloop)
@ -640,7 +643,7 @@ llarp_dnsc_init(struct dnsc_context *dnsc, struct llarp_logic *logic,
} }
bool bool
llarp_dnsc_stop(struct dnsc_context *dnsc) llarp_dnsc_stop(struct dnsc_context *const dnsc)
{ {
// delete(sockaddr_in *)dnsc->server; // deallocation // delete(sockaddr_in *)dnsc->server; // deallocation
return true; return true;

Loading…
Cancel
Save