diff options
-rw-r--r-- | src/dns.c | 6 | ||||
-rw-r--r-- | src/log.c | 32 | ||||
-rw-r--r-- | src/log.h | 49 | ||||
-rw-r--r-- | src/main.c | 51 |
4 files changed, 125 insertions, 13 deletions
@@ -3,7 +3,7 @@ int fqdn_to_qname( char* _source, int _sourcelen, char* _sink ,int _sinklen ) { // TODO Opttimize - int i = 0; + int i; int lastdot = 0; if (_sourcelen < 1 || _sinklen < 1) @@ -11,11 +11,10 @@ int fqdn_to_qname( char* _source, int _sourcelen, char* _sink ,int _sinklen ) _sink[0] = ' '; //Set to known value - while ( (i < _sourcelen) && (i < (_sinklen - 1))) { //Copy offset 1 + for(i = 0; ((i < _sourcelen) && (i < (_sinklen - 1))); i++) { //Copy offset 1 if(! _source[i]) break; _sink[i+1] = _source[i]; - i++; } if( _source[i] ) // _source not terminated @@ -34,6 +33,7 @@ int fqdn_to_qname( char* _source, int _sourcelen, char* _sink ,int _sinklen ) return i+2; } + int qname_to_fqdn( char* _source, int _sourcelen, char* _sink, int _sinklen ) { return -1; diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..197582c --- /dev/null +++ b/src/log.c @@ -0,0 +1,32 @@ +#include "log.h" + +const char* log_loglevel_str[5] = { + "---", + "ERROR", + "WARNING", + "NOTE", + "DEBUG" +}; + +int log_init_file(char* _file, unsigned int _verbosity) +{ + log_fd = open(_file, O_WRONLY | O_APPEND | O_CREAT | O_DSYNC); + + return log_init_stdout(_verbosity);; +} + +int log_init_stdout(unsigned int _verbosity) +{ + log_loglevel = _verbosity;// > _LOG_DEBUG ? _LOG_DEBUG : _verbosity; + log_fd = STDIN_FILENO; + + LOGPRINTF(0, "=== RESTART ==="); + LOGPRINTF(0, "Verbosity: %i", _verbosity); + + return 0; +} + +int log_close() +{ + return close(log_fd); +} diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..8b2c881 --- /dev/null +++ b/src/log.h @@ -0,0 +1,49 @@ +#pragma once + +#include <stdio.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> +#include <time.h> +#include <fcntl.h> +#include <unistd.h> + +#define _LOG_ERROR 1 +#define _LOG_WARNING 2 +#define _LOG_NOTE 3 +#define _LOG_DEBUG 4 + +unsigned int log_loglevel; +int log_fd; + +const char* log_loglevel_str[5]; + +#define LOGPRINTF(l,...) {\ + if((l) <= log_loglevel){\ + time_t current = time (NULL);\ + struct tm *tma = localtime(¤t);\ + char* date = asctime(tma);\ + date[strlen(date) - 1] = '\0';\ + printf("[%s] %s: ", date, log_loglevel_str[(l)]);\ + if((l) == _LOG_ERROR)\ + printf("%s:", strerror(errno));\ + if((l) == _LOG_DEBUG)\ + printf("%s:%d: ", __FILE__, __LINE__);\ + printf(__VA_ARGS__);\ + printf("\n");\ + fsync(STDOUT_FILENO);\ + }\ +} + + +/** + * Opens logfile, writes filedes to _fd + * */ +int log_init_file(char* _file, unsigned int _verbosity); + +/** + * Configures LOG macros for stdout + */ +int log_init_stdout(unsigned int _verbosity); + +int log_close(); @@ -12,10 +12,34 @@ #include <signal.h> +#include "dns.h" +#include "log.h" + #define PRINT_ERRNO() {printf("%s:%i %i:%s\n", __FILE__, __LINE__, errno, strerror(errno));} #define UDP_BUFFER_LEN 512 +struct dns_message { + //Header + uint16_t id; + + uint8_t QR; //Query:0 Reply:1 + uint8_t OPCODE; //Query:0 Iquery:1 Status:2 + uint8_t AA; //Authorative answer + uint8_t TC; //Truncation + uint8_t RD; //Recursion Desired + uint8_t RA; //Recursion Available + uint8_t RCODE; //Response Code + + uint16_t question_count; + uint16_t answer_count; + uint16_t authorative_count; + uint16_t additional_count; + + //Question + //Answer + }; + int sock_server; int dns_parse_packet ( int _socket, @@ -36,7 +60,7 @@ int test_main( int argc, char in[128]; char out[128]; - strncpy ( in, "www.example.com\0", 127); + strncpy ( in, "aaa.aaaaa.aa\0", 127); printf("%s\n", in); @@ -65,6 +89,8 @@ int main( int argc, signal ( SIGTERM, signal_term ); signal ( SIGINT, signal_term ); + log_init_stdout ( _LOG_DEBUG ); + sock_server = socket ( AF_INET, SOCK_DGRAM, 0 ); if ( sock_server == -1 ) { PRINT_ERRNO(); @@ -76,11 +102,11 @@ int main( int argc, 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( "inet_aton(): Invalid IP\n" ); + LOGPRINTF(_LOG_NOTE, "inet_aton(): Invalid IP\n" ); return 1; } - ret = bind( sock_server, + ret = bind ( sock_server, (struct sockaddr*) &sock_server_addr, sizeof(struct sockaddr_in) ); if ( ret == -1 ) { @@ -103,22 +129,23 @@ int main( int argc, (struct sockaddr*) &sock_client_addr, &sock_client_addr_len ); if ( ret == -1 ) { - PRINT_ERRNO(); + LOGPRINTF( _LOG_ERROR, "recvfrom()"); return errno; } - printf ( "recieved\n" ); + LOGPRINTF(_LOG_NOTE, "Connection"); if ( dns_parse_packet ( sock_server, &sock_client_addr, sock_client_addr_len, recv_buffer, ret ) ) { - PRINT_ERRNO(); + LOGPRINTF(_LOG_ERROR, "dns_parse_packet()"); return errno; } - - printf ( "forked\n" ); + else { + LOGPRINTF ( _LOG_DEBUG, "forked\n" ); + } } close( sock_server ); @@ -130,7 +157,8 @@ int dns_parse_packet ( int _socket, struct sockaddr_in *sockaddr_client, socklen_t sockaddr_client_len, char* buffer, - int bufflen ) { + int bufflen ) +{ pid_t pid = fork(); if ( pid > 0) @@ -140,7 +168,10 @@ int dns_parse_packet ( int _socket, signal ( SIGTERM, signal_term_child); - sleep ( 999 ); //Keep child active + struct dns_message msg; + + msg.id = *( (uint16_t*) buffer ); + msg.QR = 0x80 & *( (uint8_t*) (buffer + 2)); exit ( 0 ); } |