lokinet/daemon/dns.cpp

104 lines
2.4 KiB
C++
Raw Normal View History

2018-07-16 01:31:47 +00:00
2018-07-13 13:13:38 +00:00
#include <getopt.h>
2018-07-13 13:36:51 +00:00
#include <signal.h>
#include <stdio.h> /* fprintf, printf */
#include <unistd.h>
2018-07-13 13:13:38 +00:00
2018-07-16 12:48:04 +00:00
#include <llarp/logic.h>
#include "dnsd.hpp"
#include "ev.hpp"
2018-07-13 13:13:38 +00:00
#include "logger.hpp"
2018-07-13 13:36:51 +00:00
#include "net.hpp"
2018-07-13 13:13:38 +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
bool done = false;
void
handle_signal(int sig)
{
printf("got SIGINT\n");
done = true;
}
int
main(int argc, char *argv[])
2018-07-13 13:36:51 +00:00
{
2018-07-16 23:26:58 +00:00
dns_context dns;
2018-07-13 13:13:38 +00:00
int code = 1;
llarp::LogInfo("Starting up server");
2018-07-13 13:36:51 +00:00
// llarp::SetLogLevel(llarp::eLogDebug);
2018-07-13 13:13:38 +00:00
if(1)
2018-07-13 13:36:51 +00:00
{
2018-07-16 12:48:04 +00:00
// libev version
llarp_ev_loop *netloop = nullptr;
2018-07-16 12:48:04 +00:00
llarp_threadpool *worker = nullptr;
llarp_logic *logic = nullptr;
2018-07-16 12:48:04 +00:00
llarp_ev_loop_alloc(&netloop);
2018-07-16 12:48:04 +00:00
// configure main netloop
2018-07-16 23:26:58 +00:00
if(!llarp_dns_init(&dns, netloop, "127.0.0.1", 1052))
2018-07-13 13:36:51 +00:00
{
2018-07-16 23:26:58 +00:00
llarp::LogError("failed to initialize dns subsystem");
return 1;
2018-07-13 13:13:38 +00:00
}
2018-07-16 23:26:58 +00:00
worker = llarp_init_same_process_threadpool();
logic = llarp_init_single_process_logic(worker);
llarp::LogInfo("running dns mainloop");
llarp_ev_loop_run_single_process(netloop, worker, logic);
2018-07-16 12:48:04 +00:00
llarp_ev_loop_free(&netloop);
2018-07-13 13:13:38 +00:00
}
2018-07-16 12:48:04 +00:00
else
{
struct sockaddr_in m_address;
int m_sockfd;
2018-07-13 13:36:51 +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;
m_address.sin_port = htons(1053);
int rbind = bind(m_sockfd, (struct sockaddr *)&m_address,
sizeof(struct sockaddr_in));
2018-07-13 13:13:38 +00:00
if(rbind != 0)
2018-07-13 13:13:38 +00:00
{
2018-07-16 12:48:04 +00:00
llarp::LogError("Could not bind: ", strerror(errno));
return 0;
2018-07-13 13:13:38 +00:00
}
2018-07-13 13:36:51 +00:00
2018-07-16 12:48:04 +00:00
const size_t BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE]; // 1024 is buffer size
2018-07-16 12:48:04 +00:00
struct sockaddr_in clientAddress;
socklen_t addrLen = sizeof(struct sockaddr_in);
2018-07-13 13:36:51 +00:00
2018-07-16 12:48:04 +00:00
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100 * 1000; // 1 sec
if(setsockopt(m_sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
2018-07-13 13:36:51 +00:00
{
2018-07-16 12:48:04 +00:00
perror("Error");
2018-07-13 13:13:38 +00:00
}
2018-07-13 13:36:51 +00:00
2018-07-16 12:48:04 +00:00
signal(SIGINT, handle_signal);
while(!done)
2018-07-13 13:13:38 +00:00
{
2018-07-16 12:48:04 +00:00
// sigint quits after next packet
int nbytes = recvfrom(m_sockfd, buffer, BUFFER_SIZE, 0,
(struct sockaddr *)&clientAddress, &addrLen);
if(nbytes == -1)
continue;
2018-07-16 12:48:04 +00:00
llarp::LogInfo("Received Bytes ", nbytes);
2018-07-16 23:26:58 +00:00
// raw_handle_recvfrom(&m_sockfd, (const struct sockaddr *)&clientAddress,
// buffer, nbytes);
2018-07-13 13:13:38 +00:00
}
}
return code;
}