aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2021-05-01 15:12:09 +0200
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2021-05-01 15:12:09 +0200
commit009dc3ba991290de19f0a5e4e4a25a33e82689cc (patch)
tree8207ae9b3d72525029088d2cef31460f9daa24f0
parentbd8e73aad283611c096f07e0fc16aa4985f9f2eb (diff)
downloaddns-009dc3ba991290de19f0a5e4e4a25a33e82689cc.tar.gz
improved logging
-rw-r--r--src/database.c18
-rw-r--r--src/database.h1
-rw-r--r--src/log.c3
-rw-r--r--src/log.h24
-rw-r--r--src/server.c10
-rw-r--r--src/server.h2
-rw-r--r--src/test.c3
7 files changed, 41 insertions, 20 deletions
diff --git a/src/database.c b/src/database.c
index a684cff..ddb3519 100644
--- a/src/database.c
+++ b/src/database.c
@@ -26,8 +26,10 @@ int database_populate (
struct database* _database,
char* _zonefile
) {
- if ( database_init( _database ) )
+ if ( database_init( _database ) ) {
+ LOGPRINTF(_LOG_ERROR, "Failed to initialize database.");
return 1;
+ }
// TODO parsing
@@ -46,6 +48,8 @@ int database_populate (
tree_insert( &_database->zone[0][0], qname, data );
+ LOGPRINTF(_LOG_NOTE, "Database initialized and populated");
+
return 0;
}
@@ -80,11 +84,15 @@ int database_query (
// _qtype and _qclass start at 1, so they are invalid when 0.
- if ( !_rdata || !_database || !_qname || !_qtype || !_qclass || _qname_len <= 0 )
+ if ( !_rdata || !_database || !_qname || !_qtype || !_qclass || _qname_len <= 0 ) {
+ LOGPRINTF(_LOG_ERROR, "Invalid arguments");
return 1;
+ }
- if ( _qtype >= DB_RR_LEN || _qclass >= DB_RR_LEN )
+ if ( _qtype >= DB_RR_LEN || _qclass >= DB_RR_LEN ) {
+ LOGPRINTF(_LOG_DEBUG, "Invalid qtype/qclass");
return 1;
+ }
_rdata->ttl = 0;
_rdata->rdlen = 0;
@@ -95,8 +103,10 @@ int database_query (
void* data = tree_get( &_database->zone[class][type], _qname );
- if ( !data )
+ if ( !data ) {
+ LOGPRINTF(_LOG_DEBUG, "No matching RR found");
return 2;
+ }
_rdata->ttl = *( (uint32_t*) data );
_rdata->rdlen = *( (uint16_t*)(data + 4) );
diff --git a/src/database.h b/src/database.h
index e6ba882..84e21c9 100644
--- a/src/database.h
+++ b/src/database.h
@@ -9,6 +9,7 @@
#include <stdlib.h>
#include "tree.h"
+#include "log.h"
// TODO remove
#include "dns.h"
diff --git a/src/log.c b/src/log.c
index 884492e..9122ecb 100644
--- a/src/log.c
+++ b/src/log.c
@@ -8,9 +8,10 @@
unsigned int log_loglevel;
int log_fd;
-const char* log_loglevel_str[5] = {
+const char* log_loglevel_str[6] = {
"---",
"ERROR",
+ "ERROR",
"WARNING",
"NOTE",
"DEBUG"
diff --git a/src/log.h b/src/log.h
index ecbb9e9..0f10745 100644
--- a/src/log.h
+++ b/src/log.h
@@ -13,15 +13,16 @@
#include <fcntl.h>
#include <unistd.h>
-#define _LOG_ERROR 1
-#define _LOG_WARNING 2
-#define _LOG_NOTE 3
-#define _LOG_DEBUG 4
+#define _LOG_ERRNO 1
+#define _LOG_ERROR 2
+#define _LOG_WARNING 3
+#define _LOG_NOTE 4
+#define _LOG_DEBUG 5
extern unsigned int log_loglevel;
extern int log_fd;
-extern const char* log_loglevel_str[5];
+extern const char* log_loglevel_str[6];
#define LOGPRINTF(l,...) {\
if((l) <= log_loglevel){\
@@ -30,8 +31,8 @@ extern const char* log_loglevel_str[5];
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_ERRNO)\
+ printf("%s: ", strerror(errno));\
if((l) == _LOG_DEBUG)\
printf("%s:%d: ", __FILE__, __LINE__);\
printf(__VA_ARGS__);\
@@ -40,6 +41,15 @@ extern const char* log_loglevel_str[5];
}\
}
+// DEBUG Wrapper around LOGPRINTF wich is only compiled in in
+// _DEBUG mode for performance
+
+#ifdef _DEBUG
+#define DEBUG(...) { LOGPRINTF(_LOG_DEBUG, __VA_ARGS__); }
+#else
+#define DEBUG(...) { }
+#endif
+
/**
* Opens logfile, writes filedes to _fd
diff --git a/src/server.c b/src/server.c
index 26b26ca..dee6faa 100644
--- a/src/server.c
+++ b/src/server.c
@@ -24,7 +24,7 @@ void run_dns_server ( server_config_t* _config )
log_init_stdout ( _LOG_DEBUG );
if ( (ret = database_populate ( &zone_db, "/nofile" )) ) {
- LOGPRINTF(_LOG_ERROR, "Failed to populate database from zonefile");
+ LOGPRINTF(_LOG_ERRNO, "Failed to populate database from zonefile");
exit(1);
}
@@ -32,7 +32,7 @@ void run_dns_server ( server_config_t* _config )
sock_server = socket ( AF_INET, SOCK_DGRAM, 0 );
if ( sock_server == -1 ) {
- LOGPRINTF(_LOG_ERROR, "socket() failed");
+ LOGPRINTF(_LOG_ERRNO, "socket() failed");
exit ( errno );
}
@@ -41,7 +41,7 @@ void run_dns_server ( server_config_t* _config )
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_NOTE, "inet_aton(): Invalid bind IP\n" );
+ LOGPRINTF(_LOG_ERROR, "inet_aton(): Invalid bind IP\n" );
exit ( 1 );
}
@@ -49,7 +49,7 @@ void run_dns_server ( server_config_t* _config )
(struct sockaddr*) &sock_server_addr,
sizeof(struct sockaddr_in) );
if ( ret == -1 ) {
- LOGPRINTF(_LOG_ERROR, "bind() failed");
+ LOGPRINTF(_LOG_ERRNO, "bind() failed");
exit ( errno );
}
@@ -70,7 +70,7 @@ void run_dns_server ( server_config_t* _config )
(struct sockaddr*) &sock_client_addr,
&sock_client_addr_len );
if ( ret == -1 ) {
- LOGPRINTF( _LOG_ERROR, "recvfrom()");
+ LOGPRINTF( _LOG_ERRNO, "recvfrom()");
exit ( errno );
}
diff --git a/src/server.h b/src/server.h
index 7f74088..03eea0e 100644
--- a/src/server.h
+++ b/src/server.h
@@ -33,6 +33,8 @@ typedef struct server_config {
static int sock_server;
+int init_socket ( );
+
void run_dns_server ( server_config_t* _config );
int handle_connection ( int _socket,
diff --git a/src/test.c b/src/test.c
index a74edcf..708fff2 100644
--- a/src/test.c
+++ b/src/test.c
@@ -12,9 +12,6 @@ void run_test ()
log_init_stdout(_LOG_DEBUG);
//Space for temporary tests
- test_database();
-
- return;
//tree_balanced_insert(NULL, NULL, NULL, 15 );
//
//Normal tests