diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2019-09-12 18:51:38 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2019-09-12 18:51:38 +0200 |
commit | feb3a1f1d9df66976a108a27db6b64d35c3dd988 (patch) | |
tree | 7fd9dfcacf51dfac590a02cc0c06a151958412f9 /src/main.c | |
parent | 9e5b432b18b80e3206cad70c64618b462291d84f (diff) | |
download | dns-feb3a1f1d9df66976a108a27db6b64d35c3dd988.tar.gz |
asdf
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 89 |
1 files changed, 74 insertions, 15 deletions
@@ -10,20 +10,34 @@ #include <unistd.h> +#include <signal.h> + #define PRINT_ERRNO() {printf("%s:%i %i:%s\n", __FILE__, __LINE__, errno, strerror(errno));} #define UDP_BUFFER_LEN 512 -int dns_parse_packet(int _socket, struct sockaddr_in *sockaddr_client, socklen_t sockaddr_client_len, char* buffer); +int sock_server; + +int dns_parse_packet ( int _socket, + struct sockaddr_in *sockaddr_client, + socklen_t sockaddr_client_len, + char* buffer, + int bufflen ); + +void signal_term ( ); -int main(int argc, char* argv[]) -{ +void signal_term_child ( ); + +int main( int argc, + char* argv[] ) { int ret; - int sock_server; struct sockaddr_in sock_server_addr; char recv_buffer[ UDP_BUFFER_LEN ]; + signal ( SIGTERM, signal_term ); + signal ( SIGINT, signal_term ); + sock_server = socket ( AF_INET, SOCK_DGRAM, 0 ); if ( sock_server == -1 ) { PRINT_ERRNO(); @@ -34,18 +48,20 @@ int main(int argc, char* argv[]) sock_server_addr.sin_family = AF_INET; sock_server_addr.sin_port = htons( 53 ); ret = inet_aton ( "0.0.0.0", & sock_server_addr.sin_addr ); - if( ret == 0 ) {//Error on 0, no errno - printf("Invalid IP\n"); + if( ret == 0 ) { //Error on 0, no errno! + printf( "inet_aton(): Invalid IP\n" ); return 1; } - ret = bind( sock_server , &sock_server_addr, sizeof(struct sockaddr_in) ); + ret = bind( sock_server, + (struct sockaddr*) &sock_server_addr, + sizeof(struct sockaddr_in) ); if ( ret == -1 ) { PRINT_ERRNO(); return errno; } - //while( 1 ) + while( 1 ) { struct sockaddr_in sock_client_addr; socklen_t sock_client_addr_len; @@ -53,11 +69,29 @@ int main(int argc, char* argv[]) sock_client_addr_len = sizeof ( struct sockaddr_in ); memset ( &sock_client_addr, '\0', sock_client_addr_len ); - ret = recvfrom ( sock_server, recv_buffer, UDP_BUFFER_LEN, 0, &sock_client_addr, &sock_client_addr_len ); + ret = recvfrom (sock_server, + recv_buffer, + UDP_BUFFER_LEN, + 0, + (struct sockaddr*) &sock_client_addr, + &sock_client_addr_len ); if ( ret == -1 ) { PRINT_ERRNO(); return errno; } + + printf ( "recieved\n" ); + + if ( dns_parse_packet ( sock_server, + &sock_client_addr, + sock_client_addr_len, + recv_buffer, + ret ) ) { + PRINT_ERRNO(); + return errno; + } + + printf ( "forked\n" ); } close( sock_server ); @@ -65,15 +99,40 @@ int main(int argc, char* argv[]) return 0; } - -int dns_parse_packet(int _socket, struct sockaddr_in *sockaddr_client, socklen_t sockaddr_client_len, char* buffer) -{ - pid_t = pid fork(); +int dns_parse_packet ( int _socket, + struct sockaddr_in *sockaddr_client, + socklen_t sockaddr_client_len, + char* buffer, + int bufflen ) { + pid_t pid = fork(); - if ( pid == 0) + if ( pid > 0) return 0; else if ( pid < 0 ) return errno; - exit (0); + signal ( SIGTERM, signal_term_child); + + sleep ( 999 ); //Keep child active + + exit ( 0 ); +} + +void signal_term ( ) { + printf( "Recieved Signal. Terminating active connections and closing socket\n" ); + + kill ( 0, SIGTERM ); //terminate all child processes + + shutdown ( sock_server, SHUT_RDWR ); + close ( sock_server ); + + printf( "done\n" ); + + exit( 0 ); +} + +void signal_term_child ( ) { + close ( sock_server ); + printf ( "%i: Active connection terminated\n", getpid() ); + exit( 0 ); } |