From c84c3f459919aa8db2a24c8461aab9e547be961f Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Tue, 23 Jun 2020 07:39:29 +0200 Subject: Comments, white space errors --- src/dns.c | 2 +- src/dns.h | 2 +- src/log.c | 2 +- src/main.c | 2 +- src/server.c | 2 +- src/test.c | 6 +++--- src/tree.h | 48 +++++++++++++++++++++++++++++++++++++----------- 7 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/dns.c b/src/dns.c index 14f84ea..2bf6add 100644 --- a/src/dns.c +++ b/src/dns.c @@ -11,7 +11,7 @@ int dns_construct_header ( struct dns_header* _header, char* _buffer, int _buffl return -1; *((uint16_t*)_buffer) = _header->id; //Since only copied, no flipping necessary - _buffer[2] = + _buffer[2] = ((_header->QR & 0x01) << 7) | ((_header->OPCODE & 0x0F) << 3) | ((_header->AA & 0x01) << 2) | diff --git a/src/dns.h b/src/dns.h index fa9d81f..73cf9c3 100644 --- a/src/dns.h +++ b/src/dns.h @@ -161,7 +161,7 @@ int dns_destroy_struct ( struct dns_message* _msg ); int dns_parse_packet ( char* _buffer, int _bufflen, struct dns_message* _msg ); /** - * Convert a null terminated string containing a + * Convert a null terminated string containing a * fqdn (eg server.example.com) to the binary format used by DNS records * ( [6]server[7]example[3]com[0] ) * returns: length of string in _sink, < 0 on failure diff --git a/src/log.c b/src/log.c index df1ae6a..5522670 100644 --- a/src/log.c +++ b/src/log.c @@ -24,7 +24,7 @@ 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); diff --git a/src/main.c b/src/main.c index a20dd07..d66b7fb 100644 --- a/src/main.c +++ b/src/main.c @@ -18,6 +18,6 @@ int main(int argc, char* argv[]) #else run_dns_server(); #endif - + } diff --git a/src/server.c b/src/server.c index 4a449da..1b28b14 100644 --- a/src/server.c +++ b/src/server.c @@ -109,7 +109,7 @@ void signal_term ( ) { printf( "Recieved Signal. Terminating active connections and closing socket\n" ); //terminate all children >:) - kill ( 0, SIGTERM ); + kill ( 0, SIGTERM ); shutdown ( sock_server, SHUT_RDWR ); close ( sock_server ); diff --git a/src/test.c b/src/test.c index 974ed8f..07dbb7d 100644 --- a/src/test.c +++ b/src/test.c @@ -10,7 +10,7 @@ void run_test () { //Space for temporary tests - + //tree_balanced_insert(NULL, NULL, NULL, 15 ); //Normal tests test_tree(); @@ -24,10 +24,10 @@ int test_tree () printf("\n-> test_tree()\n======\n\n"); struct tree_node* root = NULL; - + tree_insert ( &root, "eins", "Test eins" ); tree_insert ( &root, "zwei", "Test zwei" ); - + printf("After Insert\n"); printf("%s\n", tree_get(&root, "zwei")); diff --git a/src/tree.h b/src/tree.h index 38b4b38..98019c8 100644 --- a/src/tree.h +++ b/src/tree.h @@ -4,11 +4,43 @@ * License: MIT * */ +/** + * Binary tree designed to serve as a static lookup table. + * On the fly rebalancing is not intended. + * + * The char* _key is used as a primary key, the tree can + * carry any generic payload in void* _data. + * + * The root does not need special initialization. + * + * A query with tree_get() returns _data on an exact match, + * NULL if no matching key is found. + * + * To free the memory, do not use free() on root, but + * tree_destroy() instead. _key and _data can be freed + * by providing _TREE_FREE_DATA | _TREE_FREE_KEY. + * + * Example: + * + * struct tree_node* root = NULL; + * + * // A String is used as data + * tree_insert ( &root, "one", "data one" ); + * tree_insert ( &root, "two", "data two" ); + * + * printf( "%s\n", tree_get( &root, "two" ) ); + * + * tree_destroy ( &root, 0 ); + * + **/ + #pragma once #include #include +#include +//TODO remove #include #define _TREE_FREE_DATA 0x01 @@ -23,7 +55,11 @@ struct tree_node { int tree_insert ( struct tree_node** _root, char* _key, void* _data ); -int tree_balance( struct tree_node** _root ); +/** + * 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); /** * Returns (void*)node->data on success, NULL on failure @@ -31,13 +67,3 @@ int tree_balance( struct tree_node** _root ); void* tree_get ( struct tree_node** _root, char* _query ); int tree_destroy( struct tree_node** _root, uint8_t _options ); - -/** - * ignore-case alphabetical string compare - * returns: - * 0 :: _1 == _2 - * -1 :: _1 < _2 - * +1 :: _1 > _2 - * */ -int string_compare ( char* _1, char* _2 ); - -- cgit v1.2.3