From d84a301a63c768afa3aafeede302e809af93f351 Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Mon, 2 Dec 2019 22:53:38 +0100 Subject: memory leak fixes, now answers removed forking to improve performance fixed memory leaks added header construct function now return MXDOMAIN to all valid DNS queries --- src/main.c | 53 ++++++++++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 33 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 1e96f33..75574a0 100644 --- a/src/main.c +++ b/src/main.c @@ -158,17 +158,11 @@ int main( int argc, LOGPRINTF(_LOG_DEBUG, "UDP Packet size %i", ret); - if ( (handle_connection (sock_server, - &sock_client_addr, - sock_client_addr_len, - recv_buffer, - ret ) ) ) { - LOGPRINTF(_LOG_ERROR, "handle_connection() failed"); - return errno; - } - else { - LOGPRINTF ( _LOG_DEBUG, "forked " ); - } + handle_connection ( sock_server, + &sock_client_addr, + sock_client_addr_len, + recv_buffer, + ret ); } close( sock_server ); @@ -182,35 +176,28 @@ int handle_connection ( int _socket, char* _buffer, int _bufflen ) { - pid_t pid = fork(); - - if ( pid > 0) - return 0; - else if ( pid < 0 ) - return errno; - - //Change signal handler. signal_term shuts down entire socket on TERM - signal ( SIGTERM, signal_term_child ); - - //echo request - sendto (_socket, _buffer, _bufflen, 0, (struct sockaddr*) sockaddr_client, sockaddr_client_len); - - //Testing recieved question - //TODO remove - struct dns_message msg; if (dns_parse_packet (_buffer, _bufflen, &msg) ) { LOGPRINTF (_LOG_DEBUG, "Malformed packet recieved. parsing failed"); - close ( _socket ); - exit( 1 ); + return 1; } - char out[128]; - qname_to_fqdn( msg.question[0].qname, 100, out, 128); - printf("%s\n", out); + if(msg.question_count > 0) { + char out[128]; + qname_to_fqdn( msg.question[0].qname, 100, out, 128); + printf("%s %i\n", out, msg.question[0].qtype); + } + + dns_destroy_struct ( &msg ); + + //Always return NXDOMAIN + struct dns_header head = {msg.header.id,1,OP_Q,0,0,0,0,0,RCODE_NAMEERR,0,0,0,0}; + char ret[20]; + int retlen = dns_construct_header ( &head, ret, 20 ); + sendto (_socket, ret, retlen, 0, (struct sockaddr*) sockaddr_client, sockaddr_client_len); - exit ( 0 ); + return 0; } void signal_term ( ) { -- cgit v1.2.3