2018-07-30 04:38:14 +00:00
|
|
|
#ifndef _MSC_VER
|
2018-07-13 13:36:51 +00:00
|
|
|
#include <unistd.h>
|
2018-07-30 04:38:14 +00:00
|
|
|
#endif
|
2018-07-13 13:13:38 +00:00
|
|
|
|
2018-07-27 00:21:57 +00:00
|
|
|
#include <llarp.h>
|
2018-09-24 13:09:01 +00:00
|
|
|
#include <llarp/dns_iptracker.hpp>
|
|
|
|
#include <llarp/dnsd.hpp>
|
|
|
|
#include <llarp/dns_dotlokilookup.hpp>
|
2018-07-16 12:48:04 +00:00
|
|
|
|
2018-09-24 13:09:01 +00:00
|
|
|
#include <llarp/threading.hpp> // for multithreaded version (multiplatorm)
|
2018-07-13 13:13:38 +00:00
|
|
|
|
2018-09-24 13:24:08 +00:00
|
|
|
#include <signal.h> // Linux needs this for SIGINT
|
2018-07-30 04:38:14 +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
|
2018-07-13 13:13:38 +00:00
|
|
|
|
2018-07-26 01:00:15 +00:00
|
|
|
#if(__FreeBSD__) || (__OpenBSD__) || (__NetBSD__)
|
|
|
|
#include <pthread_np.h>
|
|
|
|
#endif
|
|
|
|
|
2018-08-09 11:22:57 +00:00
|
|
|
// CHECK: is multiprocess still a thing?
|
|
|
|
#ifndef TESTNET
|
|
|
|
#define TESTNET 0
|
|
|
|
#endif
|
|
|
|
|
2018-07-26 10:53:43 +00:00
|
|
|
struct llarp_main *ctx = 0;
|
2018-07-27 00:21:57 +00:00
|
|
|
bool done = false;
|
2018-07-13 13:13:38 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
handle_signal(int sig)
|
|
|
|
{
|
|
|
|
printf("got SIGINT\n");
|
|
|
|
done = true;
|
2018-09-22 10:18:56 +00:00
|
|
|
// if using router, signal it
|
|
|
|
if(ctx)
|
|
|
|
llarp_main_signal(ctx, sig);
|
2018-07-13 13:13:38 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 00:21:57 +00:00
|
|
|
struct dns_relay_config
|
|
|
|
{
|
2018-07-26 10:53:43 +00:00
|
|
|
std::string upstream_host;
|
|
|
|
uint16_t upstream_port;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_iter_config(llarp_config_iterator *itr, const char *section,
|
2018-07-27 00:21:57 +00:00
|
|
|
const char *key, const char *val)
|
2018-07-26 10:53:43 +00:00
|
|
|
{
|
|
|
|
dns_relay_config *config = (dns_relay_config *)itr->user;
|
|
|
|
if(!strcmp(section, "dns"))
|
|
|
|
{
|
|
|
|
if(!strcmp(key, "upstream-server"))
|
|
|
|
{
|
|
|
|
config->upstream_host = strdup(val);
|
2018-07-27 00:21:57 +00:00
|
|
|
llarp::LogDebug("Config file setting dns server to ",
|
|
|
|
config->upstream_host);
|
2018-07-26 10:53:43 +00:00
|
|
|
}
|
|
|
|
if(!strcmp(key, "upstream-port"))
|
|
|
|
{
|
|
|
|
config->upstream_port = atoi(val);
|
2018-07-27 00:21:57 +00:00
|
|
|
llarp::LogDebug("Config file setting dns server port to ",
|
|
|
|
config->upstream_port);
|
2018-07-26 10:53:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 13:13:38 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int code = 1;
|
2018-09-22 10:18:56 +00:00
|
|
|
char cwd[1024];
|
|
|
|
getcwd(cwd, sizeof(cwd));
|
|
|
|
llarp::LogInfo("Starting up server at ", cwd);
|
2018-07-27 00:21:57 +00:00
|
|
|
|
2018-07-26 10:53:43 +00:00
|
|
|
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);
|
2018-07-27 00:21:57 +00:00
|
|
|
|
2018-07-26 10:53:43 +00:00
|
|
|
if(llarp_load_config(config_reader, conffname))
|
|
|
|
{
|
|
|
|
llarp_free_config(&config_reader);
|
|
|
|
llarp::LogError("failed to load config file ", conffname);
|
2018-08-13 08:47:24 +00:00
|
|
|
return 0;
|
2018-07-26 10:53:43 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2018-09-20 12:37:45 +00:00
|
|
|
const uint16_t server_port = 53;
|
2018-07-13 13:13:38 +00:00
|
|
|
|
2018-08-08 12:50:03 +00:00
|
|
|
dns_iptracker_init();
|
|
|
|
|
2018-07-16 21:26:16 +00:00
|
|
|
// llarp::SetLogLevel(llarp::eLogDebug);
|
2018-07-21 13:24:47 +00:00
|
|
|
|
|
|
|
if(1)
|
2018-08-09 11:22:57 +00:00
|
|
|
{
|
|
|
|
// libev version w/router context
|
|
|
|
ctx = llarp_main_init(conffname, !TESTNET);
|
|
|
|
if(!ctx)
|
|
|
|
{
|
|
|
|
llarp::LogError("Cant set up context");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
llarp_main_setup(ctx);
|
|
|
|
signal(SIGINT, handle_signal);
|
|
|
|
|
|
|
|
struct dnsd_context dnsd;
|
|
|
|
if(!llarp_main_init_dnsd(ctx, &dnsd, server_port,
|
|
|
|
(const char *)dnsr_config.upstream_host.c_str(),
|
|
|
|
dnsr_config.upstream_port))
|
|
|
|
{
|
|
|
|
llarp::LogError("Couldnt init dns daemon");
|
|
|
|
}
|
|
|
|
// Configure intercept
|
2018-09-22 10:39:53 +00:00
|
|
|
dnsd.intercept = &llarp_dotlokilookup_handler;
|
2018-09-22 10:18:56 +00:00
|
|
|
dotLokiLookup dll;
|
|
|
|
// should be a function...
|
|
|
|
// dll.tunEndpoint = main_router_getFirstTunEndpoint(ctx);
|
|
|
|
// dll.ip_tracker = &g_dns_iptracker;
|
|
|
|
llarp_main_init_dotLokiLookup(ctx, &dll);
|
|
|
|
dnsd.user = &dll;
|
2018-08-09 11:22:57 +00:00
|
|
|
|
2018-09-20 12:37:45 +00:00
|
|
|
// check tun set up
|
|
|
|
llarp_tun_io *tun = main_router_getRange(ctx);
|
|
|
|
llarp::LogDebug("TunNetmask: ", tun->netmask);
|
|
|
|
llarp::LogDebug("TunIfAddr: ", tun->ifaddr);
|
|
|
|
|
|
|
|
// configure dns_ip_tracker to use this
|
|
|
|
// well our routes table should already be set up
|
|
|
|
|
|
|
|
// mark our TunIfAddr as used
|
2018-09-22 10:18:56 +00:00
|
|
|
if(tun)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
addr.sin_addr.s_addr = inet_addr(tun->ifaddr);
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
|
|
|
|
llarp::Addr tunIp(addr);
|
|
|
|
llarp::LogDebug("llarp::TunIfAddr: ", tunIp);
|
|
|
|
dns_iptracker_setup_dotLokiLookup(&dll, tunIp);
|
|
|
|
dns_iptracker_setup(tunIp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
llarp::LogWarn("No tun interface, can't look up .loki");
|
|
|
|
}
|
2018-09-20 12:37:45 +00:00
|
|
|
|
2018-08-09 11:22:57 +00:00
|
|
|
// run system and wait
|
|
|
|
llarp_main_run(ctx);
|
|
|
|
llarp_main_free(ctx);
|
|
|
|
}
|
|
|
|
else if(0)
|
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-08-08 12:50:03 +00:00
|
|
|
llarp_ev_loop_alloc(&netloop); // set up netio worker
|
2018-08-02 12:51:49 +00:00
|
|
|
worker = llarp_init_same_process_threadpool();
|
2018-08-08 12:50:03 +00:00
|
|
|
logic = llarp_init_single_process_logic(worker); // set up logic worker
|
2018-07-21 13:24:47 +00:00
|
|
|
|
2018-07-16 12:48:04 +00:00
|
|
|
// configure main netloop
|
2018-07-20 10:28:21 +00:00
|
|
|
struct dnsd_context dnsd;
|
2018-09-22 10:39:53 +00:00
|
|
|
if(!llarp_dnsd_init(&dnsd, logic, netloop, "*", server_port,
|
2018-07-27 00:21:57 +00:00
|
|
|
(const char *)dnsr_config.upstream_host.c_str(),
|
|
|
|
dnsr_config.upstream_port))
|
2018-07-20 10:28:21 +00:00
|
|
|
{
|
2018-07-25 00:35:11 +00:00
|
|
|
// llarp::LogError("failed to initialize dns subsystem");
|
2018-07-20 10:28:21 +00:00
|
|
|
llarp::LogError("Couldnt init dns daemon");
|
|
|
|
return 0;
|
|
|
|
}
|
2018-07-23 23:56:26 +00:00
|
|
|
// Configure intercept
|
2018-09-22 10:39:53 +00:00
|
|
|
dnsd.intercept = &llarp_dotlokilookup_handler;
|
2018-07-21 13:24:47 +00:00
|
|
|
|
2018-07-29 22:20:31 +00:00
|
|
|
llarp::LogInfo("singlethread start");
|
|
|
|
llarp_ev_loop_run_single_process(netloop, worker, logic);
|
|
|
|
llarp::LogInfo("singlethread end");
|
2018-07-30 04:38:14 +00:00
|
|
|
|
2018-07-16 12:48:04 +00:00
|
|
|
llarp_ev_loop_free(&netloop);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-02 12:51:49 +00:00
|
|
|
// need this for timer stuff
|
|
|
|
llarp_threadpool *worker = nullptr;
|
|
|
|
llarp_logic *logic = nullptr;
|
2018-08-08 12:50:03 +00:00
|
|
|
worker = llarp_init_same_process_threadpool();
|
|
|
|
logic = llarp_init_single_process_logic(worker); // set up logic worker
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-08-01 09:04:40 +00:00
|
|
|
// configure main netloop
|
|
|
|
struct dnsd_context dnsd;
|
2018-09-22 10:39:53 +00:00
|
|
|
if(!llarp_dnsd_init(&dnsd, logic, nullptr, "*", server_port,
|
2018-08-01 09:04:40 +00:00
|
|
|
(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
|
2018-09-22 10:39:53 +00:00
|
|
|
dnsd.intercept = &llarp_dotlokilookup_handler;
|
2018-08-06 12:02:00 +00:00
|
|
|
|
2018-07-16 12:48:04 +00:00
|
|
|
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-09-20 10:08:07 +00:00
|
|
|
m_address.sin_port = htons(server_port);
|
2018-07-21 13:24:47 +00:00
|
|
|
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
|
2018-07-30 04:38:14 +00:00
|
|
|
#ifndef _WIN32
|
2018-07-21 13:24:47 +00:00
|
|
|
if(setsockopt(m_sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
|
2018-07-30 04:38:14 +00:00
|
|
|
#else
|
2018-08-08 12:50:03 +00:00
|
|
|
if(setsockopt(m_sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv,
|
|
|
|
sizeof(tv))
|
|
|
|
< 0)
|
2018-07-30 04:38:14 +00:00
|
|
|
#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;
|
|
|
|
}
|