diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2019-12-02 22:53:38 +0100 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2019-12-02 22:53:38 +0100 |
commit | d84a301a63c768afa3aafeede302e809af93f351 (patch) | |
tree | 8e7bf34b73c430fef4efff5f290858a371408aa1 /src/main.c | |
parent | 1629c3621e02a8e176172f44a72d155030172d35 (diff) | |
download | dns-d84a301a63c768afa3aafeede302e809af93f351.tar.gz |
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
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 53 |
1 files changed, 20 insertions, 33 deletions
@@ -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 ( ) { |