diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2021-05-02 19:15:56 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2021-05-02 19:15:56 +0200 |
commit | 98c68ed8cc11fa8932e480256b2544c50aaa790b (patch) | |
tree | 97e7a5b0a3af20dd3e5014cfbb3e17289cb64c88 | |
parent | 009dc3ba991290de19f0a5e4e4a25a33e82689cc (diff) | |
download | dns-98c68ed8cc11fa8932e480256b2544c50aaa790b.tar.gz |
convert to typedef structs
-rw-r--r-- | src/database.c | 12 | ||||
-rw-r--r-- | src/database.h | 18 | ||||
-rw-r--r-- | src/dns.c | 12 | ||||
-rw-r--r-- | src/dns.h | 24 | ||||
-rw-r--r-- | src/server.c | 14 | ||||
-rw-r--r-- | src/server.h | 2 | ||||
-rw-r--r-- | src/tree.c | 14 | ||||
-rw-r--r-- | src/tree.h | 12 |
8 files changed, 54 insertions, 54 deletions
diff --git a/src/database.c b/src/database.c index ddb3519..ba64a8e 100644 --- a/src/database.c +++ b/src/database.c @@ -6,9 +6,9 @@ #include "database.h" -static int database_init ( struct database* _database ) { +static int database_init ( database_t* _database ) { // Initialize 2D array of tree_node pointers, paranoia style - if ( !( _database->zone = malloc( sizeof( struct tree_node** ) * DB_CLASS_LEN ) ) ) + if ( !( _database->zone = malloc( sizeof( tree_node_t** ) * DB_CLASS_LEN ) ) ) return 1; size_t rr_size = sizeof( struct tree_node* ) * DB_RR_LEN; @@ -23,7 +23,7 @@ static int database_init ( struct database* _database ) { } int database_populate ( - struct database* _database, + database_t* _database, char* _zonefile ) { if ( database_init( _database ) ) { @@ -53,7 +53,7 @@ int database_populate ( return 0; } -int database_destroy ( struct database* _database ) { +int database_destroy ( database_t* _database ) { if ( !_database || !_database->zone ) return 1; @@ -73,8 +73,8 @@ int database_destroy ( struct database* _database ) { } int database_query ( - struct database_rdata* _rdata, - struct database* _database, + database_rdata_t* _rdata, + database_t* _database, const char* _qname, int _qname_len, uint16_t _qtype, diff --git a/src/database.h b/src/database.h index 84e21c9..3fcef80 100644 --- a/src/database.h +++ b/src/database.h @@ -40,26 +40,26 @@ #define DB_CLASS_LEN 3 #define DB_RR_LEN 32 -struct database { - struct tree_node*** zone; -}; +typedef struct database { + tree_node_t*** zone; +} database_t; -struct database_rdata { +typedef struct database_rdata { char* rdata; uint16_t rdlen; uint32_t ttl; -}; +} database_rdata_t; int database_populate ( - struct database* _database, + database_t* _database, char* _zonefile ); -int database_destroy ( struct database* _database ); +int database_destroy ( database_t* _database ); int database_query ( - struct database_rdata* _rdata, - struct database* _database, + database_rdata_t* _rdata, + database_t* _database, const char* _qname, int _qname_len, uint16_t _qtype, @@ -5,7 +5,7 @@ #include "dns.h" -int dns_construct_header ( char* _buffer, int _bufflen, struct dns_header* _header ) +int dns_construct_header ( char* _buffer, int _bufflen, dns_header_t* _header ) { if ( !_buffer || !_header || _bufflen < 12 ) return -1; @@ -32,7 +32,7 @@ int dns_construct_header ( char* _buffer, int _bufflen, struct dns_header* _head int dns_construct_answer ( char* _buffer, int _bufflen, - struct dns_answer* _answer + dns_answer_t* _answer ) { int ret = 0; @@ -62,7 +62,7 @@ int dns_construct_answer ( int dns_construct_questoin ( char* _buffer, int _bufflen, - struct dns_question* _question + dns_question_t* _question ) { //TODO Test int ret = 0; @@ -89,13 +89,13 @@ int dns_construct_questoin ( int dns_construct_packet ( char* _buffer, int _bufflen, - struct dns_message* _message + dns_message_t* _message ) { return -1; } -int dns_destroy_struct ( struct dns_message* _msg ) +int dns_destroy_struct ( dns_message_t* _msg ) { if ( !_msg ) return -1; @@ -113,7 +113,7 @@ int dns_destroy_struct ( struct dns_message* _msg ) return 0; } -int dns_parse_packet ( char* _buffer, int _bufflen, struct dns_message* _msg ) +int dns_parse_packet ( char* _buffer, int _bufflen, dns_message_t* _msg ) { //TODO refactor @@ -76,17 +76,17 @@ enum dns_responsecode { /** * Data is COPIED * */ -struct dns_header; +typedef struct dns_header dns_header_t; /** * QNAME is REFERENCED * */ -struct dns_question; +typedef struct dns_question dns_question_t; /** * NAME is REFERENCED * */ -struct dns_answer; +typedef struct dns_answer dns_answer_t; /** * DNS Message struct @@ -95,7 +95,7 @@ struct dns_answer; * the buffer used to create it remains unchanged as * some values are referenced, not copied. * */ -struct dns_message; +typedef struct dns_message dns_message_t; struct dns_header { uint16_t id; @@ -138,41 +138,41 @@ struct dns_message { struct dns_header header; int question_count; - struct dns_question* question; + dns_question_t* question; int answer_count; - struct dns_answer* answer; + dns_answer_t* answer; }; int dns_construct_header ( char* _buffer, int _bufflen, - struct dns_header* _header + dns_header_t* _header ); int dns_construct_answer ( char* _buffer, int _bufflen, - struct dns_answer* _answer + dns_answer_t* _answer ); int dns_construct_questoin ( char* _buffer, int _bufflen, - struct dns_question* _question + dns_question_t* _question ); // Question and answer count come from header int dns_construct_packet ( char* _buffer, int _bufflen, - struct dns_message* _message + dns_message_t* _message ); /** * Frees all malloced memory * */ -int dns_destroy_struct ( struct dns_message* _msg ); +int dns_destroy_struct ( dns_message_t* _msg ); /** * Parse the packet in _buffer and populate the dns_message struct @@ -182,7 +182,7 @@ int dns_destroy_struct ( struct dns_message* _msg ); * ONLY WRITES QUESTION SECTION. ALL OTHER ARE IGNORED * * */ -int dns_parse_packet ( char* _buffer, int _bufflen, struct dns_message* _msg ); +int dns_parse_packet ( char* _buffer, int _bufflen, dns_message_t* _msg ); /** * Convert a null terminated string containing a diff --git a/src/server.c b/src/server.c index dee6faa..1135a67 100644 --- a/src/server.c +++ b/src/server.c @@ -12,7 +12,7 @@ void run_dns_server ( server_config_t* _config ) char recv_buffer[ UDP_BUFFER_LEN ]; - struct database zone_db; + database_t zone_db; signal ( SIGTERM, signal_term ); signal ( SIGINT, signal_term ); @@ -92,9 +92,9 @@ int handle_connection ( int _socket, socklen_t sockaddr_client_len, char* _buffer, int _bufflen, - struct database* _zone_db ) + database_t* _zone_db ) { - struct dns_message msg; + dns_message_t msg; if (dns_parse_packet (_buffer, _bufflen, &msg) ) { LOGPRINTF (_LOG_DEBUG, "Malformed packet recieved. parsing failed"); @@ -110,8 +110,8 @@ int handle_connection ( int _socket, // Only handles first request // TODO heavy refactoring. major POC vibe - struct database_rdata rdata; - struct dns_question* quest = & msg.question[0]; + database_rdata_t rdata; + dns_question_t* quest = & msg.question[0]; int db_ret = database_query( &rdata, _zone_db, quest->qname, quest->qname_len, quest->qtype, quest->qclass ); if (db_ret) { @@ -120,8 +120,8 @@ int handle_connection ( int _socket, return 1; } - struct dns_header head = {msg.header.id,1,OP_QUERY,0,0,0,0,0,RCODE_NOERR,0,1,0,0}; - struct dns_answer answ = {quest->qname, quest->qname_len, RR_A, CL_IN, rdata.ttl, rdata.rdlen, rdata.rdata }; + dns_header_t head = {msg.header.id,1,OP_QUERY,0,0,0,0,0,RCODE_NOERR,0,1,0,0}; + dns_answer_t answ = {quest->qname, quest->qname_len, RR_A, CL_IN, rdata.ttl, rdata.rdlen, rdata.rdata }; char ret[512]; int hlen = dns_construct_header ( ret, 512, &head ); diff --git a/src/server.h b/src/server.h index 03eea0e..d0139aa 100644 --- a/src/server.h +++ b/src/server.h @@ -42,6 +42,6 @@ int handle_connection ( int _socket, socklen_t sockaddr_client_len, char* _buffer, int _bufflen, - struct database* _zone_db ); + database_t* _zone_db ); void signal_term ( ); @@ -48,9 +48,9 @@ static int string_compare ( const char* _1, const char* _2 ) return 99; } -int tree_insert ( struct tree_node** _root, char* _key, void* _data ) +int tree_insert ( tree_node_t** _root, char* _key, void* _data ) { - struct tree_node** node = _root; + tree_node_t** node = _root; while( *node ) { int ret = string_compare ( (*node)->key, _key ); @@ -74,7 +74,7 @@ int tree_insert ( struct tree_node** _root, char* _key, void* _data ) return 0; } -int tree_balanced_insert ( struct tree_node** _root, void* _data[], char* _key[], unsigned int _len) +int tree_balanced_insert ( tree_node_t** _root, void* _data[], char* _key[], unsigned int _len) { // n is the smallest n, for which 2^(n+1) - 1 >= _len, // thus describes the minimal tree depth required to store @@ -111,14 +111,14 @@ int tree_balanced_insert ( struct tree_node** _root, void* _data[], char* _key[ return 0; } -int tree_destroy ( struct tree_node** _root, uint8_t _options ) +int tree_destroy ( tree_node_t** _root, uint8_t _options ) { //Not efficient, but this code is for testing only. unsigned int max_depth = 0; unsigned int node_cnt = 0; while(*_root) { - struct tree_node** node = _root; + tree_node_t** node = _root; unsigned int depth = 0; while( (*node)->above || (*node)->below ) { @@ -144,9 +144,9 @@ int tree_destroy ( struct tree_node** _root, uint8_t _options ) return 0; } -void* tree_get ( struct tree_node** _root, const char* _query ) +void* tree_get ( tree_node_t** _root, const char* _query ) { - struct tree_node** node = _root; + tree_node_t** node = _root; while(*node) { int ret = string_compare ( (*node)->key, _query ); @@ -54,24 +54,24 @@ #define _TREE_FREE_DATA 0x01 #define _TREE_FREE_KEY 0x02 -struct tree_node { +typedef struct tree_node { char* key; void* data; struct tree_node* above; struct tree_node* below; -}; +} tree_node_t; -int tree_insert ( struct tree_node** _root, char* _key, void* _data ); +int tree_insert ( tree_node_t** _root, char* _key, void* _data ); /** * Inserts the given list into the tree, achieving optimal depth. * Expects a sorted list. * */ -int tree_balanced_insert ( struct tree_node** _root, void* _data[], char* _key[], unsigned int _len); +int tree_balanced_insert ( tree_node_t** _root, void* _data[], char* _key[], unsigned int _len); /** * Returns (void*)node->data on success, NULL on failure * */ -void* tree_get ( struct tree_node** _root, const char* _query ); +void* tree_get ( tree_node_t** _root, const char* _query ); -int tree_destroy( struct tree_node** _root, uint8_t _options ); +int tree_destroy( tree_node_t** _root, uint8_t _options ); |