lokinet/daemon/dns.cpp

194 lines
4.7 KiB
C++
Raw Normal View History

2018-07-13 13:13:38 +00:00
#include <getopt.h>
2018-07-21 13:24:47 +00:00
#include <signal.h>
#include <stdio.h> /* fprintf, printf */
#ifndef _MSC_VER
2018-07-13 13:36:51 +00:00
#include <unistd.h>
#endif
2018-07-13 13:13:38 +00:00
#include <llarp.h>
2018-07-16 12:48:04 +00:00
#include <llarp/logic.h>
#include "dnsd.hpp"
2018-07-21 13:24:47 +00:00
#include "ev.hpp"
#include "llarp/net.hpp"
2018-07-21 13:24:47 +00:00
#include "logger.hpp"
2018-07-16 12:48:04 +00:00
2018-07-21 13:24:47 +00:00
#include <thread> // for multithreaded version
2018-07-16 12:48:04 +00:00
#include <vector>
2018-07-13 13:13:38 +00:00
// keep this once jeff reenables concurrency
#ifdef _MSC_VER
extern "C" void
SetThreadName(DWORD dwThreadID, LPCSTR szThreadName);
#endif
#ifdef _WIN32
#define uint UINT
#endif
#if(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
#include <pthread_np.h>
#endif
struct llarp_main *ctx = 0;
bool done = false;
2018-07-13 13:13:38 +00:00
void
handle_signal(int sig)
{
printf("got SIGINT\n");
done = true;
}
sockaddr *
hookChecker(std::string name, struct dnsd_context *context)
{
llarp::LogInfo("Hooked ", name);
// cast your context->user;
return nullptr;
}
// FIXME: make configurable
#define SERVER "8.8.8.8"
#define PORT 53
struct dns_relay_config
{
std::string upstream_host;
uint16_t upstream_port;
};
void
dns_iter_config(llarp_config_iterator *itr, const char *section,
const char *key, const char *val)
{
dns_relay_config *config = (dns_relay_config *)itr->user;
if(!strcmp(section, "dns"))
{
if(!strcmp(key, "upstream-server"))
{
config->upstream_host = strdup(val);
llarp::LogDebug("Config file setting dns server to ",
config->upstream_host);
}
if(!strcmp(key, "upstream-port"))
{
config->upstream_port = atoi(val);
llarp::LogDebug("Config file setting dns server port to ",
config->upstream_port);
}
}
}
2018-07-13 13:13:38 +00:00
int
main(int argc, char *argv[])
{
int code = 1;
llarp::LogInfo("Starting up server");
const char *conffname = handleBaseCmdLineArgs(argc, argv);
dns_relay_config dnsr_config;
dnsr_config.upstream_host = "8.8.8.8";
dnsr_config.upstream_port = 53;
llarp_config *config_reader;
llarp_new_config(&config_reader);
// ctx = llarp_main_init(conffname, multiThreaded);
if(llarp_load_config(config_reader, conffname))
{
llarp_free_config(&config_reader);
llarp::LogError("failed to load config file ", conffname);
return false;
}
llarp_config_iterator iter;
iter.user = &dnsr_config;
iter.visit = &dns_iter_config;
llarp_config_iter(config_reader, &iter);
llarp::LogInfo("config [", conffname, "] loaded");
2018-07-13 13:13:38 +00:00
// llarp::SetLogLevel(llarp::eLogDebug);
2018-07-21 13:24:47 +00:00
if(1)
2018-07-13 13:13:38 +00:00
{
2018-07-16 12:48:04 +00:00
// libev version
2018-07-21 13:24:47 +00:00
llarp_ev_loop *netloop = nullptr;
2018-07-16 12:48:04 +00:00
llarp_threadpool *worker = nullptr;
llarp_logic *logic = nullptr;
2018-07-21 13:24:47 +00:00
2018-07-16 12:48:04 +00:00
llarp_ev_loop_alloc(&netloop);
2018-07-21 13:24:47 +00:00
2018-07-16 12:48:04 +00:00
// configure main netloop
struct dnsd_context dnsd;
if(!llarp_dnsd_init(&dnsd, netloop, "*", 1053,
(const char *)dnsr_config.upstream_host.c_str(),
dnsr_config.upstream_port))
{
// llarp::LogError("failed to initialize dns subsystem");
llarp::LogError("Couldnt init dns daemon");
return 0;
}
// Configure intercept
dnsd.intercept = &hookChecker;
2018-07-21 13:24:47 +00:00
llarp::LogInfo("singlethread start");
worker = llarp_init_same_process_threadpool();
logic = llarp_init_single_process_logic(worker);
llarp_ev_loop_run_single_process(netloop, worker, logic);
llarp::LogInfo("singlethread end");
2018-07-16 12:48:04 +00:00
llarp_ev_loop_free(&netloop);
}
else
{
struct sockaddr_in m_address;
int m_sockfd;
2018-07-21 13:24:47 +00:00
m_sockfd = socket(AF_INET, SOCK_DGRAM, 0);
m_address.sin_family = AF_INET;
2018-07-16 12:48:04 +00:00
m_address.sin_addr.s_addr = INADDR_ANY;
2018-07-21 13:24:47 +00:00
m_address.sin_port = htons(1053);
int rbind = bind(m_sockfd, (struct sockaddr *)&m_address,
sizeof(struct sockaddr_in));
if(rbind != 0)
{
2018-07-16 12:48:04 +00:00
llarp::LogError("Could not bind: ", strerror(errno));
return 0;
}
2018-07-21 13:24:47 +00:00
2018-07-16 12:48:04 +00:00
const size_t BUFFER_SIZE = 1024;
2018-07-21 13:24:47 +00:00
char buffer[BUFFER_SIZE]; // 1024 is buffer size
2018-07-16 12:48:04 +00:00
struct sockaddr_in clientAddress;
2018-07-21 13:24:47 +00:00
socklen_t addrLen = sizeof(struct sockaddr_in);
2018-07-16 12:48:04 +00:00
struct timeval tv;
2018-07-21 13:24:47 +00:00
tv.tv_sec = 0;
tv.tv_usec = 100 * 1000; // 1 sec
#ifndef _WIN32
2018-07-21 13:24:47 +00:00
if(setsockopt(m_sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
#else
if(setsockopt(m_sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv,
sizeof(tv))
< 0)
#endif
2018-07-21 13:24:47 +00:00
{
2018-07-16 12:48:04 +00:00
perror("Error");
}
2018-07-21 13:24:47 +00:00
2018-07-16 12:48:04 +00:00
signal(SIGINT, handle_signal);
while(!done)
{
// sigint quits after next packet
int nbytes = recvfrom(m_sockfd, buffer, BUFFER_SIZE, 0,
2018-07-21 13:24:47 +00:00
(struct sockaddr *)&clientAddress, &addrLen);
if(nbytes == -1)
continue;
2018-07-16 12:48:04 +00:00
llarp::LogInfo("Received Bytes ", nbytes);
2018-07-21 13:24:47 +00:00
raw_handle_recvfrom(&m_sockfd, (const struct sockaddr *)&clientAddress,
buffer, nbytes);
2018-07-16 12:48:04 +00:00
}
2018-07-13 13:13:38 +00:00
}
return code;
}