Skip to content
Snippets Groups Projects
Commit 6da750c9 authored by dnsadmin's avatar dnsadmin
Browse files

Dokumentáció hozzáadva

parent a89c859f
Branches
No related tags found
No related merge requests found
...@@ -12,20 +12,33 @@ ...@@ -12,20 +12,33 @@
#include "udp_socket_wp.h" #include "udp_socket_wp.h"
/**
* A communicator class to implement IPC between the server
* (running in the background) and a controller task.
* It implements between two pre-set sockets.
*/
class IPC_US_communicator class IPC_US_communicator
{ {
const bool master; const bool server; /** If tha object is a server */
general_sockaddr_wp master_sockaddr; general_sockaddr_wp server_sockaddr; /** Sockaddr for the server */
general_sockaddr_wp slave_sockaddr; general_sockaddr_wp controller_sockaddr; /** Sockaddr for the controller */
udp_socket_wp comm_sock; udp_socket_wp comm_sock; /** Socket to which messages will be sent */
IPC_US_communicator(const IPC_US_communicator& orig); IPC_US_communicator(const IPC_US_communicator& orig);
public: public:
IPC_US_communicator(std::string socket_path, const bool master); IPC_US_communicator(std::string socket_path, const bool master);
virtual ~IPC_US_communicator(); virtual ~IPC_US_communicator();
/**
* Returns the received data from the other side.
* @return Received data in string
*/
std::string recv_data(); std::string recv_data();
/**
* Send a string message to the other side.
* @param data The message to send
*/
void send_data(const std::string& data); void send_data(const std::string& data);
}; };
......
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/* /*
* File: command.h * File: command.h
* Author: Eckl Máté <ecklm94@gmail.com> * Author: Eckl Máté <ecklm94@gmail.com>
...@@ -14,6 +8,9 @@ ...@@ -14,6 +8,9 @@
#ifndef COMMAND_H #ifndef COMMAND_H
#define COMMAND_H #define COMMAND_H
/**
* Enum of possible command which may be sended to a server.
*/
enum command enum command
{ {
STATISTICS, KILL STATISTICS, KILL
......
...@@ -40,8 +40,8 @@ ...@@ -40,8 +40,8 @@
*/ */
class dns_answer: public dns_question class dns_answer: public dns_question
{ {
uint32_t ttl; uint32_t ttl; /** Time To Live */
uint16_t rd_length; uint16_t rd_length; /** Total length of the rdata field in bytes */
char *rdata; /** There is no '\0' at the end. */ char *rdata; /** There is no '\0' at the end. */
protected: protected:
/** /**
...@@ -62,7 +62,7 @@ public: ...@@ -62,7 +62,7 @@ public:
/** /**
* @return Full size of the answer object including the length of rdata * @return Full size of the answer object including the length of rdata
*/ */
virtual size_t get_size() const; virtual size_t get_size() const override;
/** /**
* @return TTL in host byte order * @return TTL in host byte order
......
...@@ -16,6 +16,10 @@ ...@@ -16,6 +16,10 @@
#include "record.h" #include "record.h"
// https://www.sqlite.org/cintro.html // https://www.sqlite.org/cintro.html
/**
* Database handler wrapper class that uses sqlite to execute readonly queries.
*/
class dns_db class dns_db
{ {
static bool map_initialized; /** Helps to mage readable_DNS_types singleton. */ static bool map_initialized; /** Helps to mage readable_DNS_types singleton. */
......
...@@ -23,13 +23,13 @@ ...@@ -23,13 +23,13 @@
*/ */
/** The QR bit indicates whether the header is for a query or a response. */ /** The QR bit indicates whether the header is for a query or a response. */
#define DNS_FLAG_QR_QUERY 0x0000 //#define DNS_FLAG_QR_QUERY 0x0000
#define DNS_FLAG_QR_RESPONSE 0x8000 #define DNS_FLAG_QR_RESPONSE 0x8000
// https://tools.ietf.org/html/rfc6895#section-2.2 // https://tools.ietf.org/html/rfc6895#section-2.2
#define DNS_FLAG_OPCODE_SHIFT 15 #define DNS_FLAG_OPCODE_SHIFT 15
#define DNS_FLAG_OPCODE_MASK 0x7800 // 01111000 00000000 #define DNS_FLAG_OPCODE_MASK 0x7800 // 01111000 00000000
#define DNS_FLAG_OPCODE_STANDARD_QUERY 0x0000<<DNS_FLAG_OPCODE_SHIFT //#define DNS_FLAG_OPCODE_STANDARD_QUERY 0x0000<<DNS_FLAG_OPCODE_SHIFT
#define DNS_FLAG_OPCODE_STATUS 0x0002<<DNS_FLAG_OPCODE_SHIFT #define DNS_FLAG_OPCODE_STATUS 0x0002<<DNS_FLAG_OPCODE_SHIFT
#define DNS_FLAG_OPCODE_UNASSIGNED 0x0003<<DNS_FLAG_OPCODE_SHIFT #define DNS_FLAG_OPCODE_UNASSIGNED 0x0003<<DNS_FLAG_OPCODE_SHIFT
#define DNS_FLAG_OPCODE_NOTIFY 0x0004<<DNS_FLAG_OPCODE_SHIFT #define DNS_FLAG_OPCODE_NOTIFY 0x0004<<DNS_FLAG_OPCODE_SHIFT
...@@ -38,12 +38,12 @@ ...@@ -38,12 +38,12 @@
//-- Only meaningfull in the response packet --// //-- Only meaningfull in the response packet --//
#define DNS_FLAG_AA_MASK 0x0400 #define DNS_FLAG_AA_MASK 0x0400
#define DNS_FLAG_AA_AUTHORITATIVE 0x0400 #define DNS_FLAG_AA_AUTHORITATIVE 0x0400
#define DNS_FLAG_AA_RECURSIVE 0x0000 //#define DNS_FLAG_AA_RECURSIVE 0x0000
//---------------------------------------------// //---------------------------------------------//
#define DNS_FLAG_TC_MASK 0x0200 #define DNS_FLAG_TC_MASK 0x0200
#define DNS_FLAG_TC_TRUNCATED 0x0200 #define DNS_FLAG_TC_TRUNCATED 0x0200
#define DNS_FLAG_TC_NOT_TRUNCATED 0x0000 //#define DNS_FLAG_TC_NOT_TRUNCATED 0x0000
#define DNS_FLAG_RD_MASK 0x0100 #define DNS_FLAG_RD_MASK 0x0100
#define DNS_FLAG_RD_RECURSION_DESIRED 0x0100 #define DNS_FLAG_RD_RECURSION_DESIRED 0x0100
...@@ -64,7 +64,7 @@ ...@@ -64,7 +64,7 @@
// https://tools.ietf.org/html/rfc6895#section-2.3 // https://tools.ietf.org/html/rfc6895#section-2.3
#define DNS_FLAG_RCODE_MASK 0x000F #define DNS_FLAG_RCODE_MASK 0x000F
#define DNS_FLAG_RCODE_NOERR 0x0000 //#define DNS_FLAG_RCODE_NOERR 0x0000
#define DNS_FLAG_RCODE_FORMERR 0x0001 #define DNS_FLAG_RCODE_FORMERR 0x0001
#define DNS_FLAG_RCODE_SERVFAIL 0x0002 #define DNS_FLAG_RCODE_SERVFAIL 0x0002
#define DNS_FLAG_RCODE_NXDOMAIN 0x0003 #define DNS_FLAG_RCODE_NXDOMAIN 0x0003
......
...@@ -15,15 +15,31 @@ ...@@ -15,15 +15,31 @@
class dns_server class dns_server
{ {
private: private:
dns_db db; dns_db db; /** The database that holds the DNS records */
udp_socket_wp socket; udp_socket_wp socket; /** Socket to listen to for the queries */
volatile bool running; // Más jelentést kéne neki adni és akkor egyértelműbb lenne, hogy mikor kell beállítani igazra/hamisra volatile bool running; /** Status flag if the server is still running */
pthread_t *threads; pthread_t *threads; /** Ids of the threads started to serve the requets */
int thread_num; int thread_num; /** Number of the threads started */
dns_server(const dns_db& __db) { throw "Not implementable"; } dns_server(const dns_db& __db) { throw "Not implementable"; }
protected: protected:
/**
* The function that can be started in a thread.
* It calls serve_forever() on server_obj.
* @param server_obj The server object prepared to serve requests
* @return Nothing
*/
static void *thread_func(void *server_obj); static void *thread_func(void *server_obj);
/**
* Serves a single request.
* Gets a query, and if there is a database hit, it sends back a response.
*/
void serve_one_request(); void serve_one_request();
/**
* Deletes the threads array.
* It is only for avoidance of code duplication.
*/
void delete_threads(); void delete_threads();
public: public:
dns_server(); dns_server();
...@@ -31,16 +47,37 @@ public: ...@@ -31,16 +47,37 @@ public:
dns_server(const general_sockaddr_wp& addr, const char *db_path); dns_server(const general_sockaddr_wp& addr, const char *db_path);
dns_server(const general_sockaddr_wp& addr, const dns_db& __db); dns_server(const general_sockaddr_wp& addr, const dns_db& __db);
/**
* Opens an sqlite database.
* @param db_path Path to the sqlite database file.
*/
void connect_db(const char* db_path) { db.close(); db.open(db_path); } void connect_db(const char* db_path) { db.close(); db.open(db_path); }
/**
* Binds the server to the sockaddr specified.
* @param addr Sockaddr to bind to
*/
void init(const general_sockaddr_wp& addr); void init(const general_sockaddr_wp& addr);
/**
* Serves requests one by one until running is set to false.
*/
void serve_forever(); void serve_forever();
/**
* Starts to serve request in n different threads.
*/
void serve_in_threads(int n); void serve_in_threads(int n);
/**
* Stops serving in all threads.
*/
void stop(); void stop();
/**
* Releases every required resource.
*/
void shutdown(); void shutdown();
virtual ~dns_server() { this->shutdown(); } virtual ~dns_server() { this->shutdown(); }
}; };
......
...@@ -12,11 +12,38 @@ ...@@ -12,11 +12,38 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
/**
* Defines the interface of a variable logging function.
* Implement this, and it will be compatible with all the logging used in this software.
*/
#define LOGGER_FUNCTION_HEADER(func_name) void (func_name)(int __pri, const char *__fmt, ...) #define LOGGER_FUNCTION_HEADER(func_name) void (func_name)(int __pri, const char *__fmt, ...)
/**
* On demand wrapper of perror.
* Some of the syscalls or libc functions set errno, but perror would always print to stderr.
* This macro makes it easy write perror messages to the variable log intervace.
*/
#define PERROR_ON_DEMAND(__s) log_on_demand(LOG_ERR, "%s: %s", __s, strerror(errno)) #define PERROR_ON_DEMAND(__s) log_on_demand(LOG_ERR, "%s: %s", __s, strerror(errno))
/**
* Pointer to a variable logging function.
*/
typedef LOGGER_FUNCTION_HEADER(*logger); typedef LOGGER_FUNCTION_HEADER(*logger);
/**
* Logs to syslog.
* @param __pri Priority of the log message
* @param __fmt Format string
* @param ... Additional variable for format string
*/
LOGGER_FUNCTION_HEADER(syslog_on_demand); LOGGER_FUNCTION_HEADER(syslog_on_demand);
/**
* Logs to stderr.
* @param __pri Priority of the log message
* @param __fmt Format string
* @param ... Additional variable for format string
*/
LOGGER_FUNCTION_HEADER(stdlog_on_demand); LOGGER_FUNCTION_HEADER(stdlog_on_demand);
#endif /* LOGGER_H */ #endif /* LOGGER_H */
...@@ -10,6 +10,9 @@ ...@@ -10,6 +10,9 @@
#include <stdint.h> #include <stdint.h>
/**
* Represents a DNS record selected form the database.
*/
struct record struct record
{ {
char name[50] = { 0 }; char name[50] = { 0 };
......
...@@ -22,7 +22,7 @@ class udp_packet_wp ...@@ -22,7 +22,7 @@ class udp_packet_wp
protected: protected:
void set_buf_len(const ssize_t new_buf_len); void set_buf_len(const ssize_t new_buf_len);
public: public:
general_sockaddr_wp address; general_sockaddr_wp address; /** Source address of the packet */
udp_packet_wp(); udp_packet_wp();
udp_packet_wp(const udp_packet_wp& other); udp_packet_wp(const udp_packet_wp& other);
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "../include/IPC_US_communicator.h" #include "../include/IPC_US_communicator.h"
IPC_US_communicator::IPC_US_communicator(std::string socket_path, const bool master): master(master), comm_sock(AF_UNIX) { IPC_US_communicator::IPC_US_communicator(std::string socket_path, const bool master): server(master), comm_sock(AF_UNIX) {
std::string master_sock_path = socket_path + "master.sock"; std::string master_sock_path = socket_path + "master.sock";
std::string slave_sock_path = socket_path + "slave.sock"; std::string slave_sock_path = socket_path + "slave.sock";
...@@ -20,20 +20,20 @@ IPC_US_communicator::IPC_US_communicator(std::string socket_path, const bool mas ...@@ -20,20 +20,20 @@ IPC_US_communicator::IPC_US_communicator(std::string socket_path, const bool mas
memset(&ipc, 0, sizeof(ipc)); memset(&ipc, 0, sizeof(ipc));
ipc.sun_family = AF_UNIX; ipc.sun_family = AF_UNIX;
strncpy(ipc.sun_path+1, master_sock_path.c_str(), sizeof(ipc.sun_path)-1-1); strncpy(ipc.sun_path+1, master_sock_path.c_str(), sizeof(ipc.sun_path)-1-1);
this->master_sockaddr.set_sockaddr((sockaddr*) &ipc, sizeof(ipc)); this->server_sockaddr.set_sockaddr((sockaddr*) &ipc, sizeof(ipc));
strncpy(ipc.sun_path+1, slave_sock_path.c_str(), sizeof(ipc.sun_path)-1-1); strncpy(ipc.sun_path+1, slave_sock_path.c_str(), sizeof(ipc.sun_path)-1-1);
this->slave_sockaddr.set_sockaddr((sockaddr*) &ipc, sizeof(ipc)); this->controller_sockaddr.set_sockaddr((sockaddr*) &ipc, sizeof(ipc));
timeval timeout = { 5, 0 }; timeval timeout = { 5, 0 };
comm_sock.setsockopt_wp(SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); comm_sock.setsockopt_wp(SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
if(master) if(master)
{ {
comm_sock.bind_wp(master_sockaddr); comm_sock.bind_wp(server_sockaddr);
} }
else else
{ {
comm_sock.bind_wp(slave_sockaddr); comm_sock.bind_wp(controller_sockaddr);
} }
} }
...@@ -52,9 +52,9 @@ std::string IPC_US_communicator::recv_data() { ...@@ -52,9 +52,9 @@ std::string IPC_US_communicator::recv_data() {
void IPC_US_communicator::send_data(const std::string& data) { void IPC_US_communicator::send_data(const std::string& data) {
general_sockaddr_wp pair_sockaddr; general_sockaddr_wp pair_sockaddr;
if(master) if(server)
pair_sockaddr = slave_sockaddr; pair_sockaddr = controller_sockaddr;
else else
pair_sockaddr = master_sockaddr; pair_sockaddr = server_sockaddr;
comm_sock.sendto_wp(data.c_str(), data.length()+1, 0, pair_sockaddr);// +1: terminal '\0' comm_sock.sendto_wp(data.c_str(), data.length()+1, 0, pair_sockaddr);// +1: terminal '\0'
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment