aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2021-09-20 13:01:39 +0200
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2021-09-20 13:01:39 +0200
commita21c91dc23ca0267f4547bc07c872d9ac4937cb4 (patch)
tree6b108d08f415f2ffe3635318cd4b07496f6b2e48
parentf7101248f706d48b2deaefd337c45a245589ea7d (diff)
downloaddns-a21c91dc23ca0267f4547bc07c872d9ac4937cb4.tar.gz
restructuring
-rw-r--r--src/database.c34
-rw-r--r--src/database.h5
-rw-r--r--src/server.c9
-rw-r--r--src/server.h1
-rw-r--r--src/zonefile.c46
-rw-r--r--tests/zonefile.c3
6 files changed, 46 insertions, 52 deletions
diff --git a/src/database.c b/src/database.c
index 5c865d8..21174f7 100644
--- a/src/database.c
+++ b/src/database.c
@@ -6,7 +6,7 @@
#include "database.h"
-static int database_init ( database_t* _database ) {
+int database_init ( database_t* _database ) {
unsigned int i = 0;
size_t rr_size;
@@ -25,38 +25,6 @@ static int database_init ( database_t* _database ) {
return 0;
}
-int database_populate ( database_t* _database, char* _zonefile ) {
- char* qname;
- int len;
- void* data;
-
- if ( database_init( _database ) ) {
- LOGPRINTF(_LOG_ERROR, "Failed to initialize database.");
- return 1;
- }
-
- /* TODO parsing */
-
- qname = malloc(32);
-
- len = fqdn_to_qname( "test.example.com", 17, qname, 32 );
-
- if ( len <= 0 )
- return 1;
-
- data = malloc( 10 );
-
- *((uint32_t*)data) = 1800;
- *((uint16_t*)(data+4)) = 4;
- *((uint32_t*)(data+6)) = 0x45454545;
-
- tree_insert( &_database->zone[0][0], qname, data );
-
- LOGPRINTF(_LOG_NOTE, "Database initialized and populated");
-
- return 0;
-}
-
int database_destroy ( database_t* _database ) {
unsigned int i, o;
diff --git a/src/database.h b/src/database.h
index 363f29c..2231410 100644
--- a/src/database.h
+++ b/src/database.h
@@ -50,10 +50,7 @@ typedef struct database_rdata {
uint32_t ttl;
} database_rdata_t;
-int database_populate (
- database_t* _database,
- char* _zonefile
-);
+int database_init ( database_t* _database );
int database_destroy ( database_t* _database );
diff --git a/src/server.c b/src/server.c
index 3796b87..da6755e 100644
--- a/src/server.c
+++ b/src/server.c
@@ -18,8 +18,13 @@ void server_start ( server_config_t* _config )
signal ( SIGTERM, signal_term );
signal ( SIGINT, signal_term );
- if ( database_populate( &zone_db, _config->zonefile ) ) {
- LOGPRINTF(_LOG_ERRNO, "Failed to populate database from zonefile");
+ if ( database_init( &zone_db ) ) {
+ LOGPRINTF(_LOG_ERROR, "Failed create database");
+ exit(1);
+ }
+
+ if ( zonefile_to_database( &zone_db, _config->zonefile ) ) {
+ LOGPRINTF(_LOG_ERROR, "Failed populate database");
exit(1);
}
diff --git a/src/server.h b/src/server.h
index d9fc5a0..3c3b794 100644
--- a/src/server.h
+++ b/src/server.h
@@ -26,6 +26,7 @@
#include "dns.h"
#include "log.h"
#include "database.h"
+#include "zonefile.h"
#define UDP_BUFFER_LEN 512
diff --git a/src/zonefile.c b/src/zonefile.c
index 13a2a34..07e89d7 100644
--- a/src/zonefile.c
+++ b/src/zonefile.c
@@ -28,25 +28,47 @@ int zonefile_string_split(char* _parts[], ssize_t _max, char* _str, char _delim)
int zonefile_parse_line(database_t *_database, char *_line) {
char *parts[5];
- int parts_len;
+ int parts_len, ret, fqdn_len;
+
+ char* qname;
+ uint32_t ttl;
+ uint16_t type, class;
+ void* data;
- /* Does this work? */
memset(&parts, 0, sizeof(parts));
- parts_len = zonefile_string_split(parts, 4, _line, ' ');
- if (parts_len != 4) {
- LOGPRINTF(_LOG_ERROR, "Incomplete");
+ parts_len = zonefile_string_split(parts, 5, _line, ' ');
+ if (parts_len != 5) {
+ LOGPRINTF(_LOG_ERROR, "Statement is incomplete");
return -1;
}
- /* parts is the first 5 space-seperated parts of _line */
+ fqdn_len = strlen(parts[0]);
+ if ( (ret = fqdn_check(parts[0], fqdn_len)) ) {
+ LOGPRINTF(_LOG_ERROR, "FQDN Contains invalid char at pos %i", ret);
+ return -1;
+ }
+ qname = malloc( (unsigned)fqdn_len+1 );
+ if ( fqdn_to_qname(parts[0], fqdn_len, qname, fqdn_len+1) < 0) {
+ LOGPRINTF(_LOG_ERROR, "Failed to convert to QNAME. This is a bug.");
+ return -1;
+ }
+ if ( !(ttl = (uint16_t) atoi(parts[1])) ) {
+ LOGPRINTF(_LOG_ERROR, "Invalid TTL");
+ return -1;
+ }
+
+ DEBUG("value %s", parts[4]);
+
+ return 0;
return -1;
}
int zonefile_to_database (database_t *_database, char* _file) {
FILE *zfile = NULL;
- char *line = NULL;
+ char *line = NULL;
+ size_t llen = 0;
ssize_t line_len = 0;
unsigned int line_cnt = 0;
@@ -56,11 +78,13 @@ int zonefile_to_database (database_t *_database, char* _file) {
return -1;
}
- DEBUG("Parsing zonefile %s", _file)
+ DEBUG("Parsing zonefile %s", _file);
+ /* TODO Make resilient to evil empty lines */
- while(!feof(zfile)) {
+ while( (line_len = getline(&line, &llen, zfile)) >= 0 ) {
line_cnt ++;
- line_len = getline(&line, 0, zfile);
+
+ DEBUG("line %u, length %li, allocated %lu", line_cnt, line_len, llen);
/* getline includes the line break. ONLY UNIX ENDINGS!! */
if( line[line_len - 2] == '\n' )
@@ -76,6 +100,6 @@ int zonefile_to_database (database_t *_database, char* _file) {
}
fclose(zfile);
- return -1;
+ return 0;
}
diff --git a/tests/zonefile.c b/tests/zonefile.c
index a8004f9..b142f8e 100644
--- a/tests/zonefile.c
+++ b/tests/zonefile.c
@@ -23,8 +23,7 @@ START_TEST (test_zonefile_string_split) {
str = malloc(str_len);
strncpy(str, refstr, str_len);
- for (i=0; i<4; i++)
- parts[i] = NULL;
+ memset(&parts, 0, sizeof(parts));
len = zonefile_string_split(parts, 4, str, ' ');
ck_assert_int_eq(len, 4);