aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2021-05-05 10:39:22 +0200
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2021-05-05 10:39:22 +0200
commit57e6cf6b01e981ce159d472f3e3ed92f893ad443 (patch)
treeec90834b00596e8a2992182341263f242fcfc753 /src
parent98c68ed8cc11fa8932e480256b2544c50aaa790b (diff)
downloaddns-57e6cf6b01e981ce159d472f3e3ed92f893ad443.tar.gz
changing server logic
Diffstat (limited to 'src')
-rw-r--r--src/main.c7
-rw-r--r--src/server.c117
-rw-r--r--src/server.h6
3 files changed, 67 insertions, 63 deletions
diff --git a/src/main.c b/src/main.c
index 36a37c6..bb1a4ac 100644
--- a/src/main.c
+++ b/src/main.c
@@ -16,15 +16,20 @@
int main(int argc, char* argv[])
{
+ log_init_stdout(_LOG_DEBUG);
//CMD line arg parsing goes in here
#ifdef _TEST
run_test();
#else
server_config_t config;
+
+ memset(&config, 0, sizeof config);
+
config.bind_ip = "0.0.0.0";
config.bind_port = 53;
+ config.zonefile = "/nofile";
- run_dns_server( &config );
+ server_start( &config );
#endif
}
diff --git a/src/server.c b/src/server.c
index 1135a67..8cd378a 100644
--- a/src/server.c
+++ b/src/server.c
@@ -5,86 +5,53 @@
#include "server.h"
-void run_dns_server ( server_config_t* _config )
+void server_start ( server_config_t* _config )
{
- int ret;
- struct sockaddr_in sock_server_addr;
-
char recv_buffer[ UDP_BUFFER_LEN ];
-
database_t zone_db;
signal ( SIGTERM, signal_term );
signal ( SIGINT, signal_term );
- //Avoid zombie processes
- //TODO currently useless, since no forking is done.
- signal (SIGCHLD, SIG_IGN);
-
- log_init_stdout ( _LOG_DEBUG );
-
- if ( (ret = database_populate ( &zone_db, "/nofile" )) ) {
+ if ( database_populate( &zone_db, _config->zonefile ) ) {
LOGPRINTF(_LOG_ERRNO, "Failed to populate database from zonefile");
exit(1);
}
- LOGPRINTF(_LOG_NOTE, "Initializing DNS Server on %s:%i", _config->bind_ip, _config->bind_port);
-
- sock_server = socket ( AF_INET, SOCK_DGRAM, 0 );
- if ( sock_server == -1 ) {
- LOGPRINTF(_LOG_ERRNO, "socket() failed");
- exit ( errno );
- }
-
- memset( &sock_server_addr, '\0', sizeof(struct sockaddr_in) );
- sock_server_addr.sin_family = AF_INET;
- sock_server_addr.sin_port = htons( _config->bind_port );
- ret = inet_aton ( _config->bind_ip, & sock_server_addr.sin_addr );
- if( ret == 0 ) { //Error on 0, no errno!
- LOGPRINTF(_LOG_ERROR, "inet_aton(): Invalid bind IP\n" );
- exit ( 1 );
- }
-
- ret = bind ( sock_server,
- (struct sockaddr*) &sock_server_addr,
- sizeof(struct sockaddr_in) );
- if ( ret == -1 ) {
- LOGPRINTF(_LOG_ERRNO, "bind() failed");
- exit ( errno );
- }
+ sock_server = server_get_socket( _config->bind_ip, _config->bind_port );
LOGPRINTF(_LOG_NOTE, "Done!");
- while( 1 )
- {
- struct sockaddr_in sock_client_addr;
- socklen_t sock_client_addr_len;
+ while( 1 ) {
+ struct sockaddr_in sock_client_addr;
+ socklen_t sock_client_addr_len = sizeof( struct sockaddr_in );
+ int recv_len = 0;
- sock_client_addr_len = sizeof ( struct sockaddr_in );
- memset ( &sock_client_addr, '\0', sock_client_addr_len );
+ memset ( &sock_client_addr, 0, sock_client_addr_len );
- ret = recvfrom (sock_server,
+ recv_len = recvfrom (sock_server,
recv_buffer,
UDP_BUFFER_LEN,
0,
(struct sockaddr*) &sock_client_addr,
&sock_client_addr_len );
- if ( ret == -1 ) {
+ if ( recv_len == -1 ) {
LOGPRINTF( _LOG_ERRNO, "recvfrom()");
exit ( errno );
}
- LOGPRINTF(_LOG_DEBUG, "UDP Packet size %i", ret);
+ DEBUG("Packet size %i from %s:%i", recv_len, inet_ntoa(sock_client_addr.sin_addr), sock_client_addr.sin_port );
handle_connection ( sock_server,
&sock_client_addr,
sock_client_addr_len,
recv_buffer,
- ret,
+ recv_len,
&zone_db );
}
close( sock_server );
+ exit(0);
}
int handle_connection ( int _socket,
@@ -92,18 +59,22 @@ int handle_connection ( int _socket,
socklen_t sockaddr_client_len,
char* _buffer,
int _bufflen,
- database_t* _zone_db )
-{
+ database_t* _zone_db ) {
dns_message_t msg;
- if (dns_parse_packet (_buffer, _bufflen, &msg) ) {
- LOGPRINTF (_LOG_DEBUG, "Malformed packet recieved. parsing failed");
+ if ( dns_parse_packet (_buffer, _bufflen, &msg) ) {
+ DEBUG("Malformed packet recieved. parsing failed");
return 1;
}
- if(msg.question_count > 0) {
+ if ( ! msg.question_count ) {
+ DEBUG("No questions in request.");
+ return 1;
+ }
+
+ if (msg.question_count > 0) {
char out[128];
- qname_to_fqdn( (char*) msg.question[0].qname, 100, out, 128);
+ qname_to_fqdn( (char*) msg.question[0].qname, msg.question[0].qname_len, out, 128);
LOGPRINTF(_LOG_DEBUG, "Request for %s QTYPE %i", out, msg.question[0].qtype);
}
@@ -115,8 +86,8 @@ int handle_connection ( int _socket,
int db_ret = database_query( &rdata, _zone_db, quest->qname, quest->qname_len, quest->qtype, quest->qclass );
if (db_ret) {
-
LOGPRINTF(_LOG_DEBUG, "DB Query exited with code %i", db_ret);
+ dns_destroy_struct ( &msg );
return 1;
}
@@ -126,23 +97,49 @@ int handle_connection ( int _socket,
char ret[512];
int hlen = dns_construct_header ( ret, 512, &head );
int alen = dns_construct_answer ( ret + hlen, 512-hlen, &answ );
- sendto (_socket, ret, hlen + alen, 0, (struct sockaddr*) sockaddr_client, sockaddr_client_len);
+ sendto( _socket, ret, hlen + alen, 0, (struct sockaddr*) sockaddr_client, sockaddr_client_len );
dns_destroy_struct ( &msg );
return 0;
}
-void signal_term ( ) {
- printf( "Recieved Signal. Terminating active connections and closing socket\n" );
+int server_get_socket ( char* _bind_ip, uint16_t _bind_port ) {
+ struct sockaddr_in socket_addr;
+ int server_socket;
+
+ LOGPRINTF(_LOG_NOTE, "Binding on %s:%i", _bind_ip, _bind_port);
+
+ server_socket = socket ( AF_INET, SOCK_DGRAM, 0 );
+ if ( server_socket == -1 ) {
+ LOGPRINTF(_LOG_ERRNO, "failed to create socket");
+ exit ( errno );
+ }
- //terminate all children >:)
- kill ( 0, SIGTERM );
+ memset( &socket_addr, '\0', sizeof(struct sockaddr_in) );
+
+ socket_addr.sin_family = AF_INET;
+ socket_addr.sin_port = htons( _bind_port );
+
+ if ( ! inet_aton ( _bind_ip, & socket_addr.sin_addr ) ) {
+ LOGPRINTF(_LOG_ERROR, "inet_aton(): Invalid bind IP\n" );
+ exit ( 1 );
+ }
+
+ if ( bind( server_socket, (struct sockaddr*) &socket_addr, sizeof(struct sockaddr_in) ) ) {
+ LOGPRINTF(_LOG_ERRNO, "bind() failed");
+ exit ( errno );
+ }
+
+ return server_socket;
+}
+
+void signal_term ( ) {
+ LOGPRINTF(_LOG_NOTE, "Server shutting down" );
shutdown ( sock_server, SHUT_RDWR );
close ( sock_server );
- printf( "done\n" );
-
+ LOGPRINTF(_LOG_NOTE, "Done!" );
exit( 0 );
}
diff --git a/src/server.h b/src/server.h
index d0139aa..1751fae 100644
--- a/src/server.h
+++ b/src/server.h
@@ -29,13 +29,15 @@
typedef struct server_config {
char* bind_ip;
uint16_t bind_port;
+ char* zonefile;
+ char* user;
} server_config_t;
static int sock_server;
-int init_socket ( );
+void server_start ( server_config_t* _config );
-void run_dns_server ( server_config_t* _config );
+int server_get_socket ( char* _bind_ip, uint16_t _bind_port );
int handle_connection ( int _socket,
struct sockaddr_in *sockaddr_client,