From abd0244aba263fd8894a77dd830dda223e70627a Mon Sep 17 00:00:00 2001 From: Christian Huitema Date: Fri, 20 Jan 2017 19:33:12 -0800 Subject: [PATCH 01/25] Fixing a potential bug in the RB tree for netreq_by_id --- src/context.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/context.c b/src/context.c index 34432780..41a194f7 100644 --- a/src/context.c +++ b/src/context.c @@ -747,7 +747,14 @@ tls_only_is_in_transports_list(getdns_context *context) { static int net_req_query_id_cmp(const void *id1, const void *id2) { - return (intptr_t)id1 - (intptr_t)id2; + int ret = 0; + + if (id1 != id2) + { + ret = ((intptr_t)id1 < (intptr_t)id2) ? -1 : 1; + } + + return ret; } static getdns_tsig_info const tsig_info[] = { From 31eee9c7d10f239a46e2b3881241e600b30b0579 Mon Sep 17 00:00:00 2001 From: Christian Huitema Date: Fri, 20 Jan 2017 19:44:05 -0800 Subject: [PATCH 02/25] Intermediate commit of context.h, mdns.[ch] --- src/context.h | 16 +++++++ src/mdns.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/mdns.h | 27 +++++++++++ 3 files changed, 168 insertions(+) diff --git a/src/context.h b/src/context.h index 86bfb817..6a1d6458 100644 --- a/src/context.h +++ b/src/context.h @@ -287,6 +287,22 @@ struct getdns_context { /* We need to run WSAStartup() to be able to use getaddrinfo() */ WSADATA wsaData; #endif + + /* MDNS */ +#ifdef HAVE_MDNS_SUPPORT + /* + * If supporting MDNS, context may be instantiated either in basic mode + * or in full mode. If working in extended mode, two multicast sockets are + * left open, for IPv4 and IPv6. Data can be received on either socket. + * The context also keeps a list of open queries, characterized by a + * name and an RR type. + */ + int mdns_extended_support; + int fd_mdns_v4; + int fd_mdns_v6; + _getdns_rbtree_t mdns_continuous_queries_by_name_rrtype; + +#endif /* HAVE_MDNS_SUPPORT */ }; /* getdns_context */ /** internal functions **/ diff --git a/src/mdns.c b/src/mdns.c index 36950f36..44027d58 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -78,6 +78,131 @@ static uint8_t mdns_suffix_b_e_f_ip6_arpa[] = { 7, 'i', 'p', 'v', '6', 4, 'a', 'r', 'p', 'a', 0 }; + +/* +* Compare function for the netreq_by_query_id, +* used in the red-black tree of all netreq by continuous query. +*/ +static int mdns_cmp_netreq_by_query_id(const void * id1, const void * id2) +{ + int ret = 0; + + if (id1 != id2) + { + ret = (((intptr_t)id1) < ((intptr_t)id2)) ? -1 : 1; + } + return ret; +} + +/* + * Compare function for the getdns_mdns_known_record type, + * used in the red-black tree of known records per query. + */ + +static int mdns_cmp_known_records(const void * nkr1, const void * nkr2) +{ + int ret = 0; + getdns_mdns_known_record * kr1 = (getdns_mdns_known_record *)nkr1; + getdns_mdns_known_record * kr2 = (getdns_mdns_known_record *)nkr2; + + if (kr1->record_length != kr2->record_length) + { + ret = (kr1->record_length < kr2->record_length) ? -1 : 1; + } + else + { + ret = memcmp((const void*)kr1->record_data, (const void*)kr2->record_data, kr1->record_length); + } + + return ret; +} + +/* + * Compare function for the mdns_continuous_query_by_name_rrtype, + * used in the red-black tree of all ongoing queries. + */ +static int mdns_cmp_continuous_queries_by_name_rrtype(const void * nqnr1, const void * nqnr2) +{ + int ret = 0; + getdns_mdns_continuous_query * qnr1 = (getdns_mdns_continuous_query *)nqnr1; + getdns_mdns_continuous_query * qnr2 = (getdns_mdns_continuous_query *)nqnr2; + + if (qnr1->request_class != qnr2->request_class) + { + ret = (qnr1->request_class < qnr2->request_class) ? -1 : 1; + } + else if (qnr1->request_type != qnr2->request_type) + { + ret = (qnr1->request_type < qnr2->request_type) ? -1 : 1; + } + else if (qnr1->name_len != qnr2->name_len) + { + ret = (qnr1->name_len < qnr2->name_len) ? -1 : 1; + } + else + { + ret = memcmp((void*)qnr1->name, (void*)qnr2->name, qnr1->name_len); + } + return ret; +} + +/* + * Initialize a continuous query from netreq + */ +static int mdns_initialize_continuous_request(getdns_network_req *netreq) +{ + int ret = 0; + getdns_mdns_continuous_query temp_query, *continuous_query, *inserted_query; + getdns_dns_req *dnsreq = netreq->owner; + /* + * Fill the target request, but only initialize name and request_type + */ + temp_query.request_class = dnsreq->request_class; + temp_query.request_type = netreq->request_type; + temp_query.name_len = dnsreq->name_len; + /* TODO: check that dnsreq is in canonical form */ + memcpy(temp_query.name, dnsreq->name, dnsreq->name_len); + /* + * Check whether the continuous query is already in the RB tree. + * if there is not, create one. + * TODO: should lock the context object when doing that. + */ + continuous_query = (getdns_mdns_continuous_query *) + _getdns_rbtree_search(&dnsreq->context->mdns_continuous_queries_by_name_rrtype, &temp_query); + if (continuous_query == NULL) + { + continuous_query = (getdns_mdns_continuous_query *) + GETDNS_MALLOC(dnsreq->context->mf, getdns_mdns_continuous_query); + if (continuous_query != NULL) + { + continuous_query->request_class = temp_query.request_class; + continuous_query->request_type = temp_query.request_type; + continuous_query->name_len = temp_query.name_len; + memcpy(continuous_query->name, temp_query.name, temp_query.name_len); + _getdns_rbtree_init(&continuous_query->known_records_by_value, mdns_cmp_known_records); + /* Tracking of network requests on this socket */ + _getdns_rbtree_init(&continuous_query->netreq_by_query_id, mdns_cmp_netreq_by_query_id); + /* Add the new continuous query to the context */ + inserted_query = _getdns_rbtree_insert(&dnsreq->context->mdns_continuous_queries_by_name_rrtype, + continuous_query); + if (inserted_query == NULL) + { + /* Weird. This can only happen in a race condition */ + GETDNS_FREE(dnsreq->context->mf, continuous_query); + ret = GETDNS_RETURN_GENERIC_ERROR; + } + } + else + { + ret = GETDNS_RETURN_MEMORY_ERROR; + } + } + /* To do: insert netreq into query list */ + /* to do: queue message request to socket */ + + return ret; +} + /* TODO: actualy delete what is required.. */ static void mdns_cleanup(getdns_network_req *netreq) diff --git a/src/mdns.h b/src/mdns.h index 30ae3135..4a85602c 100644 --- a/src/mdns.h +++ b/src/mdns.h @@ -30,6 +30,33 @@ _getdns_submit_mdns_request(getdns_network_req *netreq); getdns_return_t _getdns_mdns_namespace_check(getdns_dns_req *dnsreq); + +/* + * data structure for continuous queries + */ + +typedef struct getdns_mdns_known_record +{ + uint32_t ttl; /* todo: should this be an expiration date? */ + uint8_t * record_data; + int record_length; +} getdns_mdns_known_record; + +typedef struct getdns_mdns_continuous_query +{ + uint8_t name[256]; /* binary representation of name being queried */ + int name_len; + uint16_t request_class; + uint16_t request_type; + /* list of known records */ + _getdns_rbtree_t known_records_by_value; + /* list of user queries */ + _getdns_rbtree_t netreq_by_query_id; + /* todo: do we need an expiration date, or a timer? */ + /* todo: do we need an update mark for showing last results? */ +} getdns_mdns_continuous_query; + + #endif /* HAVE_MDNS_SUPPORT */ #endif /* MDNS_H */ From 4ccfa2a7819385da682ab465d1ba5fc17c1fe9a2 Mon Sep 17 00:00:00 2001 From: Christian Huitema Date: Sat, 21 Jan 2017 14:46:38 -0800 Subject: [PATCH 03/25] Preparing fix for 64 bit warning in net_req_query_id_cmp --- src/context.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/context.c b/src/context.c index d9f2e13b..987d5ea2 100644 --- a/src/context.c +++ b/src/context.c @@ -818,6 +818,10 @@ tls_only_is_in_transports_list(getdns_context *context) { static int net_req_query_id_cmp(const void *id1, const void *id2) +{ + return (intptr_t)id1 - (intptr_t)id2; +} +/* { int ret = 0; @@ -828,6 +832,7 @@ net_req_query_id_cmp(const void *id1, const void *id2) return ret; } +*/ static getdns_tsig_info const tsig_info[] = { { GETDNS_NO_TSIG, NULL, 0, NULL, 0, 0, 0 } From 4c71d6239f9ca9ab734d2ba40fb329ca5ccbfe97 Mon Sep 17 00:00:00 2001 From: Christian Huitema Date: Sat, 21 Jan 2017 14:49:58 -0800 Subject: [PATCH 04/25] Fixing potential bug for comparision function net_req_query_id_cmp on 64 bits architectures. --- src/context.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/context.c b/src/context.c index 987d5ea2..fd93c68b 100644 --- a/src/context.c +++ b/src/context.c @@ -819,10 +819,11 @@ tls_only_is_in_transports_list(getdns_context *context) { static int net_req_query_id_cmp(const void *id1, const void *id2) { - return (intptr_t)id1 - (intptr_t)id2; -} -/* -{ + /* + * old code was: + * return (intptr_t)id1 - (intptr_t)id2; + *but this is incorrect on 64 bit architectures. + */ int ret = 0; if (id1 != id2) @@ -832,7 +833,7 @@ net_req_query_id_cmp(const void *id1, const void *id2) return ret; } -*/ + static getdns_tsig_info const tsig_info[] = { { GETDNS_NO_TSIG, NULL, 0, NULL, 0, 0, 0 } From 93d6f2b18fcbd5b1e63dd4707eacc24197977529 Mon Sep 17 00:00:00 2001 From: Christian Huitema Date: Mon, 6 Feb 2017 18:23:35 -1000 Subject: [PATCH 05/25] Intermediate commit, after definition of the MDNS context --- src/context.c | 23 ++++ src/context.h | 10 +- src/debug.h | 1 + src/mdns.c | 260 ++++++++++++++++++++++++++++++++++++++----- src/mdns.h | 20 +++- src/server.c | 22 +++- src/types-internal.h | 10 +- 7 files changed, 307 insertions(+), 39 deletions(-) diff --git a/src/context.c b/src/context.c index fd93c68b..16e0da92 100644 --- a/src/context.c +++ b/src/context.c @@ -98,6 +98,16 @@ static pthread_mutex_t ssl_init_lock = PTHREAD_MUTEX_INITIALIZER; #endif static bool ssl_init=false; +#ifdef HAVE_MDNS_SUPPORT +/* + * Forward declaration of MDNS context init and destroy function. + * We do this here instead of including mdns.h, in order to + * minimize dependencies. + */ +void _getdns_mdns_context_init(struct getdns_context *context); +void _getdns_mdns_context_destroy(struct getdns_context *context); +#endif + void *plain_mem_funcs_user_arg = MF_PLAIN; typedef struct host_name_addrs { @@ -1471,8 +1481,14 @@ getdns_context_create_with_extended_memory_functions( goto error; #endif + +#ifdef HAVE_MDNS_SUPPORT + _getdns_mdns_context_init(result); +#endif + create_local_hosts(result); + *context = result; return GETDNS_RETURN_GOOD; error: @@ -1552,6 +1568,13 @@ getdns_context_destroy(struct getdns_context *context) ub_ctx_delete(context->unbound_ctx); #endif +#ifdef HAVE_MDNS_SUPPORT + /* + * Release all ressource allocated for MDNS. + */ + _getdns_mdns_context_destroy(context); +#endif + if (context->namespaces) GETDNS_FREE(context->my_mf, context->namespaces); diff --git a/src/context.h b/src/context.h index 94ab8c45..b5d2148e 100644 --- a/src/context.h +++ b/src/context.h @@ -344,12 +344,14 @@ struct getdns_context { * or in full mode. If working in extended mode, two multicast sockets are * left open, for IPv4 and IPv6. Data can be received on either socket. * The context also keeps a list of open queries, characterized by a - * name and an RR type. + * name and an RR type, and a list of received answers, characterized + * by name, RR type and data value. */ - int mdns_extended_support; - int fd_mdns_v4; - int fd_mdns_v6; + int mdns_extended_support; /* 0 = no support, 1 = supported, 2 = initialization needed */ + int mdns_fdv4; + int mdns_fdv6; _getdns_rbtree_t mdns_continuous_queries_by_name_rrtype; + _getdns_rbtree_t mdns_known_records_by_value; #endif /* HAVE_MDNS_SUPPORT */ }; /* getdns_context */ diff --git a/src/debug.h b/src/debug.h index 5087efb4..c9cbe248 100644 --- a/src/debug.h +++ b/src/debug.h @@ -128,6 +128,7 @@ #define MDNS_DEBUG_ENTRY "-> MDNS ENTRY: " #define MDNS_DEBUG_READ "-- MDNS READ: " +#define MDNS_DEBUG_MREAD "-- MDNS MREAD: " #define MDNS_DEBUG_WRITE "-- MDNS WRITE: " #define MDNS_DEBUG_CLEANUP "-- MDNS CLEANUP:" diff --git a/src/mdns.c b/src/mdns.c index 44027d58..52310db0 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -48,12 +48,10 @@ uint64_t _getdns_get_time_as_uintt64(); #define MDNS_MCAST_IPV4_LONG 0xE00000FB /* 224.0.0.251 */ #define MDNS_MCAST_PORT 5353 -/* - * TODO: When we start supporting IPv6 with MDNS, need to define this: - * static uint8_t mdns_mcast_ipv6[] = { - * 0xFF, 0x02, 0, 0, 0, 0, 0, 0, - * 0, 0, 0, 0, 0, 0, 0, 0xFB }; - */ +static uint8_t mdns_mcast_ipv6[] = { + 0xFF, 0x02, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0xFB +}; static uint8_t mdns_suffix_dot_local[] = { 5, 'l', 'o', 'c', 'a', 'l', 0 }; static uint8_t mdns_suffix_254_169_in_addr_arpa[] = { @@ -63,19 +61,19 @@ static uint8_t mdns_suffix_254_169_in_addr_arpa[] = { 4, 'a', 'r', 'p', 'a', 0 }; static uint8_t mdns_suffix_8_e_f_ip6_arpa[] = { 1, '8', 1, 'e', 1, 'f', - 7, 'i', 'p', 'v', '6', + 3, 'i', 'p', '6', 4, 'a', 'r', 'p', 'a', 0 }; static uint8_t mdns_suffix_9_e_f_ip6_arpa[] = { 1, '9', 1, 'e', 1, 'f', - 7, 'i', 'p', 'v', '6', + 3, 'i', 'p', '6', 4, 'a', 'r', 'p', 'a', 0 }; static uint8_t mdns_suffix_a_e_f_ip6_arpa[] = { 1, 'a', 1, 'e', 1, 'f', - 7, 'i', 'p', 'v', '6', + 3, 'i', 'p', '6', 4, 'a', 'r', 'p', 'a', 0 }; static uint8_t mdns_suffix_b_e_f_ip6_arpa[] = { 1, 'b', 1, 'e', 1, 'f', - 7, 'i', 'p', 'v', '6', + 3, 'i', 'p', '6', 4, 'a', 'r', 'p', 'a', 0 }; @@ -105,13 +103,25 @@ static int mdns_cmp_known_records(const void * nkr1, const void * nkr2) getdns_mdns_known_record * kr1 = (getdns_mdns_known_record *)nkr1; getdns_mdns_known_record * kr2 = (getdns_mdns_known_record *)nkr2; - if (kr1->record_length != kr2->record_length) + if (kr1->request_class != kr2->request_class) { - ret = (kr1->record_length < kr2->record_length) ? -1 : 1; + ret = (kr1->request_class < kr2->request_class) ? -1 : 1; } - else + else if (kr1->request_type != kr2->request_type) { - ret = memcmp((const void*)kr1->record_data, (const void*)kr2->record_data, kr1->record_length); + ret = (kr1->request_type < kr2->request_type) ? -1 : 1; + } + else if (kr1->name_len != kr2->name_len) + { + ret = (kr1->name_len < kr2->name_len) ? -1 : 1; + } + else if (kr1->record_len != kr2->record_len) + { + ret = (kr1->record_len < kr2->record_len) ? -1 : 1; + } + else if ((ret = memcmp((void*)kr1->name, (void*)kr2->name, kr2->name_len)) == 0) + { + ret = memcmp((const void*)kr1->record_data, (const void*)kr2->record_data, kr1->record_len); } return ret; @@ -146,14 +156,181 @@ static int mdns_cmp_continuous_queries_by_name_rrtype(const void * nqnr1, const return ret; } + +/* +* Create the two required multicast sockets +*/ +static int mdns_open_ipv4_multicast() +{ + getdns_return_t ret = 0; + SOCKET fd4 = -1; + SOCKADDR_IN ipv4_dest; + SOCKADDR_IN ipv4_port; + uint8_t so_reuse_bool = 1; + uint8_t ttl = 255; + IP_MREQ mreq4; + + memset(&ipv4_dest, 0, sizeof(ipv4_dest)); + memset(&ipv4_port, 0, sizeof(ipv4_dest)); + ipv4_dest.sin_family = AF_INET; + ipv4_dest.sin_port = htons(MDNS_MCAST_PORT); + ipv4_dest.sin_addr.S_un.S_addr = htonl(MDNS_MCAST_IPV4_LONG); + ipv4_port.sin_family = AF_INET; + ipv4_port.sin_port = htons(MDNS_MCAST_PORT); + + + fd4 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + + if (fd4 != -1) + { + /* + * No need to test the output of the so_reuse call, + * since the only result that matters is that of bind. + */ + (void)setsockopt(fd4, SOL_SOCKET, SO_REUSEADDR + , (const char*)&so_reuse_bool, (int) sizeof(BOOL)); + + if (bind(fd4, (SOCKADDR*)&ipv4_port, sizeof(ipv4_port)) != 0) + { + ret = -1; + } + else + { + mreq4.imr_multiaddr = ipv4_dest.sin_addr; + mreq4.imr_interface = ipv4_port.sin_addr; + + if (setsockopt(fd4, IPPROTO_IP, IP_ADD_MEMBERSHIP + , (const char*)&mreq4, (int) sizeof(mreq4)) != 0) + { + ret = -1; + } + else if (setsockopt(fd4, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) != 0) + { + ret = -1; + } + } + } + + if (ret != 0 && fd4 != -1) + { +#ifdef USE_WINSOCK + closesocket(fd4); +#else + close(fd4); +#endif + fd4 = -1; + } + + return fd4; +} + +static int mdns_open_ipv6_multicast() +{ + getdns_return_t ret = 0; + SOCKET fd6 = -1; + SOCKADDR_IN6 ipv6_dest; + SOCKADDR_IN6 ipv6_port; + uint8_t so_reuse_bool = 1; + uint8_t ttl = 255; + IPV6_MREQ mreq6; + + memset(&ipv6_dest, 0, sizeof(ipv6_dest)); + memset(&ipv6_port, 0, sizeof(ipv6_dest)); + ipv6_dest.sin6_family = AF_INET6; + ipv6_dest.sin6_port = htons(MDNS_MCAST_PORT); + ipv6_port.sin6_family = AF_INET6; + ipv6_port.sin6_port = htons(MDNS_MCAST_PORT); + memcpy(&ipv6_dest.sin6_addr + , mdns_mcast_ipv6, sizeof(mdns_mcast_ipv6)); + + + fd6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); + + if (fd6 != -1) + { + /* + * No need to test the output of the so_reuse call, + * since the only result that matters is that of bind. + */ + (void)setsockopt(fd6, SOL_SOCKET, SO_REUSEADDR + , (const char*)&so_reuse_bool, (int) sizeof(BOOL)); + + if (bind(fd6, (SOCKADDR*)&ipv6_port, sizeof(ipv6_port)) != 0) + { + ret = -1; + } + else + { + memcpy(&mreq6.ipv6mr_multiaddr + , &ipv6_dest.sin6_addr, sizeof(mreq6.ipv6mr_multiaddr)); + memcpy(&mreq6.ipv6mr_interface + , &ipv6_port.sin6_addr, sizeof(mreq6.ipv6mr_interface)); + + if (setsockopt(fd6, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP + , (const char*)&mreq6, (int) sizeof(mreq6)) != 0) + { + ret = -1; + } + else if (setsockopt(fd6, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) != 0) + { + ret = -1; + } + } + } + + if (ret != 0 && fd6 != -1) + { +#ifdef USE_WINSOCK + closesocket(fd6); +#else + close(fd6); +#endif + fd6 = -1; + } + + return fd6; +} + +/* + * Delayed opening of the MDNS sockets, and launch of the MDNS listeners + */ +static getdns_return_t mdns_delayed_network_init(struct getdns_context *context) +{ + getdns_return_t ret = 0; + + if (context->mdns_extended_support == 2) + { + context->mdns_fdv4 = mdns_open_ipv4_multicast(); + context->mdns_fdv6 = mdns_open_ipv6_multicast(); + + if (context->mdns_fdv4 == -1 || context->mdns_fdv6 == -1) + { + if (context->mdns_fdv4 != -1) +#ifdef USE_WINSOCK + closesocket(context->mdns_fdv4); +#else + close(context->mdns_fdv4); +#endif + ret = GETDNS_RETURN_GENERIC_ERROR; + } + else + { + /* TODO: launch the receive loops */ + } + } + + return ret; +} + /* * Initialize a continuous query from netreq */ -static int mdns_initialize_continuous_request(getdns_network_req *netreq) +static getdns_return_t mdns_initialize_continuous_request(getdns_network_req *netreq) { int ret = 0; getdns_mdns_continuous_query temp_query, *continuous_query, *inserted_query; getdns_dns_req *dnsreq = netreq->owner; + struct getdns_context *context = dnsreq->context; /* * Fill the target request, but only initialize name and request_type */ @@ -168,27 +345,30 @@ static int mdns_initialize_continuous_request(getdns_network_req *netreq) * TODO: should lock the context object when doing that. */ continuous_query = (getdns_mdns_continuous_query *) - _getdns_rbtree_search(&dnsreq->context->mdns_continuous_queries_by_name_rrtype, &temp_query); + _getdns_rbtree_search(&context->mdns_continuous_queries_by_name_rrtype, &temp_query); if (continuous_query == NULL) { continuous_query = (getdns_mdns_continuous_query *) - GETDNS_MALLOC(dnsreq->context->mf, getdns_mdns_continuous_query); + GETDNS_MALLOC(context->mf, getdns_mdns_continuous_query); if (continuous_query != NULL) { + continuous_query->node.parent = NULL; + continuous_query->node.left = NULL; + continuous_query->node.right = NULL; + continuous_query->node.key = (void*)continuous_query; continuous_query->request_class = temp_query.request_class; continuous_query->request_type = temp_query.request_type; continuous_query->name_len = temp_query.name_len; memcpy(continuous_query->name, temp_query.name, temp_query.name_len); - _getdns_rbtree_init(&continuous_query->known_records_by_value, mdns_cmp_known_records); - /* Tracking of network requests on this socket */ - _getdns_rbtree_init(&continuous_query->netreq_by_query_id, mdns_cmp_netreq_by_query_id); + continuous_query->netreq_first = NULL; /* Add the new continuous query to the context */ - inserted_query = _getdns_rbtree_insert(&dnsreq->context->mdns_continuous_queries_by_name_rrtype, - continuous_query); + inserted_query = (getdns_mdns_continuous_query *) + _getdns_rbtree_insert(&context->mdns_continuous_queries_by_name_rrtype, + &continuous_query->node); if (inserted_query == NULL) { /* Weird. This can only happen in a race condition */ - GETDNS_FREE(dnsreq->context->mf, continuous_query); + GETDNS_FREE(context->mf, &continuous_query); ret = GETDNS_RETURN_GENERIC_ERROR; } } @@ -197,12 +377,42 @@ static int mdns_initialize_continuous_request(getdns_network_req *netreq) ret = GETDNS_RETURN_MEMORY_ERROR; } } - /* To do: insert netreq into query list */ + /* insert netreq into query list */ + netreq->mdns_netreq_next = continuous_query->netreq_first; + continuous_query->netreq_first = netreq; + /* to do: queue message request to socket */ return ret; } +/* + * Initialize the MDNS part of the context structure. + */ +void _getdns_mdns_context_init(struct getdns_context *context) +{ + + context->mdns_extended_support = 2; /* 0 = no support, 1 = supported, 2 = initialization needed */ + context->mdns_fdv4 = -1; /* invalid socket, i.e. not initialized */ + context->mdns_fdv6 = -1; /* invalid socket, i.e. not initialized */ + _getdns_rbtree_init(&context->mdns_continuous_queries_by_name_rrtype + , mdns_cmp_continuous_queries_by_name_rrtype); + _getdns_rbtree_init(&context->mdns_known_records_by_value + , mdns_cmp_known_records); +} + +/* + * Delete all the data allocated for MDNS in a context + */ +void _getdns_mdns_context_destroy(struct getdns_context *context) +{ + /* Close the sockets */ + + /* Clear all the continuous queries */ + + /* Clear all the cached records */ +} + /* TODO: actualy delete what is required.. */ static void mdns_cleanup(getdns_network_req *netreq) @@ -212,8 +422,6 @@ mdns_cleanup(getdns_network_req *netreq) getdns_dns_req *dnsreq = netreq->owner; GETDNS_CLEAR_EVENT(dnsreq->loop, &netreq->event); - - GETDNS_NULL_FREE(dnsreq->context->mf, netreq->tcp.read_buf); } void diff --git a/src/mdns.h b/src/mdns.h index 4a85602c..b2ac131b 100644 --- a/src/mdns.h +++ b/src/mdns.h @@ -37,26 +37,36 @@ _getdns_mdns_namespace_check(getdns_dns_req *dnsreq); typedef struct getdns_mdns_known_record { - uint32_t ttl; /* todo: should this be an expiration date? */ + /* For storage in context->mdns_known_records_by_value */ + _getdns_rbnode_t node; + uint64_t insertion_microsec; + uint16_t request_type; + uint16_t request_class; + uint32_t ttl; + int name_len; + int record_len; + uint8_t* name; uint8_t * record_data; - int record_length; } getdns_mdns_known_record; typedef struct getdns_mdns_continuous_query { + /* For storage in context->mdns_continuous_queries_by_name_rrtype */ + _getdns_rbnode_t node; uint8_t name[256]; /* binary representation of name being queried */ int name_len; uint16_t request_class; uint16_t request_type; - /* list of known records */ - _getdns_rbtree_t known_records_by_value; /* list of user queries */ - _getdns_rbtree_t netreq_by_query_id; + getdns_network_req *netreq_first; /* todo: do we need an expiration date, or a timer? */ /* todo: do we need an update mark for showing last results? */ } getdns_mdns_continuous_query; +void _getdns_mdns_context_init(struct getdns_context *context); +void _getdns_mdns_context_destroy(struct getdns_context *context); + #endif /* HAVE_MDNS_SUPPORT */ #endif /* MDNS_H */ diff --git a/src/server.c b/src/server.c index 968564e8..91553725 100644 --- a/src/server.c +++ b/src/server.c @@ -135,7 +135,11 @@ static void tcp_connection_destroy(tcp_connection *conn) loop->vmt->clear(loop, &conn->event); if (conn->fd >= 0) +#ifdef USE_WINSOCK + (void) closesocket(conn->fd); +#else (void) close(conn->fd); +#endif GETDNS_FREE(*mf, conn->read_buf); for (cur = conn->to_write; cur; cur = next) { @@ -185,8 +189,8 @@ static void tcp_write_cb(void *userarg) } to_write = conn->to_write; if (conn->fd == -1 || - (written = write(conn->fd, &to_write->write_buf[to_write->written], - to_write->write_buf_len - to_write->written)) == -1) { + (written = send(conn->fd, &to_write->write_buf[to_write->written], + to_write->write_buf_len - to_write->written, 0)) == -1) { /* IO error, close connection */ conn->event.read_cb = conn->event.write_cb = @@ -280,7 +284,11 @@ getdns_reply( (struct sockaddr *)&conn->remote_in, conn->addrlen) == -1) { /* IO error, cleanup this listener */ loop->vmt->clear(loop, &conn->l->event); +#ifdef USE_WINSOCK + closesocket(conn->l->fd); +#else close(conn->l->fd); +#endif conn->l->fd = -1; } /* Unlink this connection */ @@ -359,7 +367,7 @@ static void tcp_read_cb(void *userarg) (void) loop->vmt->schedule(loop, conn->fd, DOWNSTREAM_IDLE_TIMEOUT, &conn->event); - if ((bytes_read = read(conn->fd, conn->read_pos, conn->to_read)) < 0) { + if ((bytes_read = recv(conn->fd, conn->read_pos, conn->to_read, 0)) < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) return; /* Come back to do the read later */ @@ -473,7 +481,11 @@ static void tcp_accept_cb(void *userarg) &conn->super.remote_in, &conn->super.addrlen)) == -1) { /* IO error, cleanup this listener */ loop->vmt->clear(loop, &l->event); +#ifdef USE_WINSOCK + closesocket(l->fd); +#else close(l->fd); +#endif l->fd = -1; GETDNS_FREE(*mf, conn); return; @@ -543,7 +555,11 @@ static void udp_read_cb(void *userarg) (struct sockaddr *)&conn->remote_in, &conn->addrlen)) == -1) { /* IO error, cleanup this listener. */ loop->vmt->clear(loop, &l->event); +#ifdef USE_WINSOCK + closesocket(l->fd); +#else close(l->fd); +#endif l->fd = -1; #if 0 && defined(SERVER_DEBUG) && SERVER_DEBUG diff --git a/src/types-internal.h b/src/types-internal.h index 2ac85581..162762e4 100644 --- a/src/types-internal.h +++ b/src/types-internal.h @@ -183,7 +183,6 @@ typedef struct getdns_tcp_state { } getdns_tcp_state; - /** * Request data **/ @@ -191,6 +190,15 @@ typedef struct getdns_network_req { /* For storage in upstream->netreq_by_query_id */ _getdns_rbnode_t node; +#ifdef HAVE_MDNS_SUPPORT + /* + * for storage in continuous query context. We never + * expect much more than one query per msdn context, + * so no need for RB Tree. + */ + struct getdns_network_req * mdns_netreq_next; + struct getdns_mdns_continuous_query * mdns_continuous_query; +#endif /* HAVE_MDNS_SUPPORT */ /* the async_id from unbound */ int unbound_id; /* state var */ From 1587e2f8f5ffc9613b41b14afff336370e61ad4f Mon Sep 17 00:00:00 2001 From: Christian Huitema Date: Mon, 13 Feb 2017 18:28:46 -1000 Subject: [PATCH 06/25] Code to manage the MDNS cache using LRUHASH --- src/context.h | 6 +- src/debug.h | 4 + src/mdns.c | 1187 +++++++++++++++++++++++++++++++++++++++++++++++-- src/mdns.h | 41 +- src/server.c | 4 + 5 files changed, 1198 insertions(+), 44 deletions(-) diff --git a/src/context.h b/src/context.h index b5d2148e..2267c06e 100644 --- a/src/context.h +++ b/src/context.h @@ -348,10 +348,10 @@ struct getdns_context { * by name, RR type and data value. */ int mdns_extended_support; /* 0 = no support, 1 = supported, 2 = initialization needed */ - int mdns_fdv4; - int mdns_fdv6; + int mdns_connection_nb; /* typically 0 or 2 for IPv4 and IPv6 */ + struct mdns_network_connection * mdns_connection; + struct lruhash * mdns_cache; _getdns_rbtree_t mdns_continuous_queries_by_name_rrtype; - _getdns_rbtree_t mdns_known_records_by_value; #endif /* HAVE_MDNS_SUPPORT */ }; /* getdns_context */ diff --git a/src/debug.h b/src/debug.h index c9cbe248..5299f351 100644 --- a/src/debug.h +++ b/src/debug.h @@ -139,5 +139,9 @@ #define DEBUG_MDNS(...) DEBUG_OFF(__VA_ARGS__) #endif +#ifndef log_info +#define log_info(...) fprintf(stderr, __VA_ARGS__) +#endif + #endif /* debug.h */ diff --git a/src/mdns.c b/src/mdns.c index 52310db0..f21bea38 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -41,6 +41,14 @@ typedef u_short sa_family_t; uint64_t _getdns_get_time_as_uintt64(); +#include "util/storage/lruhash.h" +#include "util/storage/lookup3.h" + +#ifndef HAVE_LIBUNBOUND +#include "util/storage/lruhash.c" +#include "util/storage/lookup3.c" +#endif + /* * Constants defined in RFC 6762 */ @@ -76,22 +84,615 @@ static uint8_t mdns_suffix_b_e_f_ip6_arpa[] = { 3, 'i', 'p', '6', 4, 'a', 'r', 'p', 'a', 0 }; +/* + * MDNS cache management using LRU Hash. + * + * Each record contains a DNS query + response, formatted as received from + * the network. By convention, there will be exactly one query, and + * a variable number of answers. Auth and AD sections will not be cached. + * For maintenance purpose, each recontains a last accessed time stamp. + * + * This structure works very well for classic DNS caches, but for MDNS we + * have to consider processing a new record for an existing cache entry. If + * the record is present, its TTL should be updated. If the record is not + * present, it should be added to the existing data. + * + * After an update, the TTL of all the records should be updated. Some + * records will end up with a TTL value of zero. These records should be + * deleted, using a "compression" procedure. + */ /* -* Compare function for the netreq_by_query_id, -* used in the red-black tree of all netreq by continuous query. + * Missing LRU hash function: create only if not currently in cache, + * and return the created or found entry. + * + * TODO: move this to lruhash.c, once source control issues are fixed. + */ +static struct lruhash_entry* +lruhash_insert_or_retrieve(struct lruhash* table, hashvalue_type hash, +struct lruhash_entry* entry, void* data, void* cb_arg) +{ + struct lruhash_bin* bin; + struct lruhash_entry* found, *reclaimlist = NULL; + size_t need_size; + fptr_ok(fptr_whitelist_hash_sizefunc(table->sizefunc)); + fptr_ok(fptr_whitelist_hash_delkeyfunc(table->delkeyfunc)); + fptr_ok(fptr_whitelist_hash_deldatafunc(table->deldatafunc)); + fptr_ok(fptr_whitelist_hash_compfunc(table->compfunc)); + fptr_ok(fptr_whitelist_hash_markdelfunc(table->markdelfunc)); + need_size = table->sizefunc(entry->key, data); + if (cb_arg == NULL) cb_arg = table->cb_arg; + + /* find bin */ + lock_quick_lock(&table->lock); + bin = &table->array[hash & table->size_mask]; + lock_quick_lock(&bin->lock); + + /* see if entry exists already */ + if ((found = bin_find_entry(table, bin, hash, entry->key)) != NULL) { + /* if so: keep the existing data - acquire a writelock */ + lock_rw_wrlock(&found->lock); + } + else + { + /* if not: add to bin */ + entry->overflow_next = bin->overflow_list; + bin->overflow_list = entry; + lru_front(table, entry); + table->num++; + table->space_used += need_size; + /* return the entry that was presented, and lock it */ + found = entry; + lock_rw_wrlock(&found->lock); + } + lock_quick_unlock(&bin->lock); + if (table->space_used > table->space_max) + reclaim_space(table, &reclaimlist); + if (table->num >= table->size) + table_grow(table); + lock_quick_unlock(&table->lock); + + /* finish reclaim if any (outside of critical region) */ + while (reclaimlist) { + struct lruhash_entry* n = reclaimlist->overflow_next; + void* d = reclaimlist->data; + (*table->delkeyfunc)(reclaimlist->key, cb_arg); + (*table->deldatafunc)(d, cb_arg); + reclaimlist = n; + } + + /* return the entry that was selected */ + return found; +} + +/* + * For the data part, we want to allocate in rounded increments, so as to reduce the + * number of calls to XMALLOC + */ + +static uint32_t +mdns_util_suggest_size(uint32_t required_size) +{ + return (required_size <= 512) ? ((required_size <= 256) ? 256 : 512) : + ((required_size + 1023) & 0xFFFFFC00); +} + +/* + * Cache management utilities + */ +static int +mdns_util_skip_name(uint8_t *p) +{ + int x = 0; + int l; + + for (;;) { + l = p[x]; + if (l == 0) + { + x++; + break; + } + else if (l >= 0xC0) + { + x += 2; + break; + } + else + { + x += l + 1; + } + } + return x; +} + +static int +mdns_util_skip_query(uint8_t *p) +{ + return mdns_util_skip_name(p) + 4; +} + +/* + * Comparison and other functions required for cache management + */ + + /** + * Calculates the size of an entry. + * + * size = mdns_cache_size (key, data). + */ +static size_t mdns_cache_entry_size(void* vkey, void* vdata) +{ + size_t sz = 0; + + if (vkey != NULL) + { + sz += sizeof(getdns_mdns_cached_key_header) + ((getdns_mdns_cached_key_header*)vkey)->name_len; + } + + if (vdata != NULL) + { + sz += ((getdns_mdns_cached_record_header*)vdata)->allocated_length; + } + + return sz; +} + +/** type of function that compares two keys. return 0 if equal. */ +static int mdns_cache_key_comp(void* vkey1, void* vkey2) +{ + getdns_mdns_cached_key_header *header1 = (getdns_mdns_cached_key_header*)vkey1; + getdns_mdns_cached_key_header *header2 = (getdns_mdns_cached_key_header*)vkey2; + + return (header1->record_type == header2->record_type && + header1->record_class == header2->record_class && + header1->name_len == header2->name_len) + ? memcmp(((uint8_t*)vkey1) + sizeof(getdns_mdns_cached_key_header), + ((uint8_t*)vkey2) + sizeof(getdns_mdns_cached_key_header), + header1->name_len) + : -1; +} + +/** old keys are deleted. +* markdel() is used first. +* This function is called: func(key, userarg) +* the userarg is set to the context in which the LRU hash table was created */ -static int mdns_cmp_netreq_by_query_id(const void * id1, const void * id2) +static void msdn_cache_delkey(void* vkey, void* vcontext) +{ + GETDNS_FREE(((struct getdns_context *) vcontext)->mf, vkey); +} + +/** old data is deleted. This function is called: func(data, userarg). */ +static void msdn_cache_deldata(void* vdata, void* vcontext) +{ + GETDNS_FREE(((struct getdns_context *) vcontext)->mf, vdata); +} + +/* + * Create a key in preallocated buffer + * the allocated size of key should be >= sizeof(getdns_mdns_cached_key_header) + name_len + */ +static void msdn_cache_create_key_in_buffer( + uint8_t* key, + uint8_t * name, int name_len, + int record_type, int record_class) +{ + getdns_mdns_cached_key_header * header = (getdns_mdns_cached_key_header*)key; + header->record_type = record_type; + header->record_class = record_class; + header->name_len = name_len; + (void) memcpy(key + sizeof(getdns_mdns_cached_key_header), name, name_len); +} + +static uint8_t * mdns_cache_create_key( + uint8_t * name, int name_len, + int record_type, int record_class, + struct getdns_context * context) +{ + uint8_t* key = GETDNS_XMALLOC(context->mf, uint8_t, sizeof(getdns_mdns_cached_key_header) + name_len); + + if (key != NULL) + { + msdn_cache_create_key_in_buffer(key, name, name_len, record_type, record_class); + } + + return key; +} + +static uint8_t * mdns_cache_create_data( + uint8_t * name, int name_len, + int record_type, int record_class, + int record_data_len, + uint64_t current_time, + struct getdns_context * context) +{ + getdns_mdns_cached_record_header * header; + int current_index; + size_t data_size = sizeof(getdns_mdns_cached_record_header) + name_len + 4; + size_t alloc_size = mdns_util_suggest_size(data_size + record_data_len + 2 + 2 + 2 + 4 + 2); + + uint8_t* data = GETDNS_XMALLOC(context->mf, uint8_t, alloc_size); + + if (data != NULL) + { + header = (getdns_mdns_cached_record_header *)data; + header->insertion_microsec = current_time; + header->content_len = data_size; + header->allocated_length = alloc_size; + current_index = sizeof(getdns_mdns_cached_record_header); + memcpy(data + current_index, name, name_len); + current_index += name_len; + data[current_index++] = (uint8_t)(record_type >> 8); + data[current_index++] = (uint8_t)(record_type); + data[current_index++] = (uint8_t)(record_class >> 8); + data[current_index++] = (uint8_t)(record_class); + } + + return data; +} + + +/* + * Add a record. + */ +static int +mdns_add_record_to_cache_entry(struct getdns_context *context, + uint8_t * old_record, uint8_t ** new_record, + int record_type, int record_class, int ttl, + uint8_t * record_data, int record_data_len) +{ + int ret = 0; + getdns_mdns_cached_record_header *header = (getdns_mdns_cached_record_header*)old_record; + /* Compute the record length */ + uint32_t record_length = 2 + 2 + 2 + 4 + 2 + record_data_len; + uint32_t current_length = header->content_len; + /* update the number of records */ + uint8_t *start_answer_code = old_record + sizeof(getdns_mdns_cached_record_header) + 2 + 2; + uint16_t nb_answers = (start_answer_code[0] << 8) + start_answer_code[1]; + nb_answers++; + start_answer_code[0] = (uint8_t)(nb_answers >> 8); + start_answer_code[1] = (uint8_t)(nb_answers&0xFF); + + /* Update the content length */ + header->content_len += record_length; + if (header->content_len > header->allocated_length) + { + /* realloc to a new length, */ + do { + header->allocated_length = mdns_util_suggest_size(header->content_len); + } while (header->content_len > header->allocated_length); + + *new_record = GETDNS_XREALLOC(context->mf, old_record, uint8_t, header->allocated_length); + } + else + { + *new_record = old_record; + } + + if (*new_record == NULL) + { + ret = GETDNS_RETURN_MEMORY_ERROR; + } + else + { + /* copy the record */ + /* First, point name relative to beginning of DNS message */ + (*new_record)[current_length++] = 0xC0; + (*new_record)[current_length++] = 0x12; + /* encode the components of the per record header */ + (*new_record)[current_length++] = (uint8_t)((record_type >> 8) & 0xFF); + (*new_record)[current_length++] = (uint8_t)((record_type)& 0xFF); + (*new_record)[current_length++] = (uint8_t)((record_class >> 8) & 0xFF); + (*new_record)[current_length++] = (uint8_t)((record_class)& 0xFF); + (*new_record)[current_length++] = (uint8_t)((ttl >> 24) & 0xFF); + (*new_record)[current_length++] = (uint8_t)((ttl >> 16) & 0xFF); + (*new_record)[current_length++] = (uint8_t)((ttl >> 8) & 0xFF); + (*new_record)[current_length++] = (uint8_t)((ttl)& 0xFF); + (*new_record)[current_length++] = (uint8_t)((record_data_len >> 8) & 0xFF); + (*new_record)[current_length++] = (uint8_t)((record_data_len) & 0xFF); + memcpy(*new_record + current_length, record_data, record_data_len); + } + + return ret; +} + +static int +mdns_update_cache_ttl_and_prune(struct getdns_context *context, + uint8_t * old_record, uint8_t ** new_record, + int record_type, int record_class, int ttl, + uint8_t * record_data, int record_data_len, + uint64_t current_time) +{ + /* + * Compute the TTL delta + */ + int ret = 0; + getdns_mdns_cached_record_header *header = (getdns_mdns_cached_record_header*)old_record; + uint32_t delta_t_sec = (uint32_t)((current_time - header->insertion_microsec) / 1000000ll); + header->insertion_microsec += delta_t_sec * 1000000; + int message_index; + int answer_index; + int nb_answers; + int nb_answers_left; + int current_record_length; + int current_record_data_len; + uint32_t current_record_ttl; + int not_matched_yet = (record_data == NULL) ? 0 : 1; + int current_record_match; + int last_copied_index; + int current_hole_index; + + /* + * Skip the query + */ + message_index = sizeof(getdns_mdns_cached_record_header); + nb_answers = (old_record[message_index + 4] << 8) | old_record[message_index + 5]; + nb_answers_left = nb_answers; + answer_index = mdns_util_skip_query(old_record + message_index + 12); + last_copied_index = answer_index; + + /* + * Examine each record + */ + for (int i = 0; i < nb_answers; i++) + { + current_record_ttl = (old_record[answer_index + 2 + 2 + 2] << 24) + | (old_record[answer_index + 2 + 2 + 2 + 1] << 16) + | (old_record[answer_index + 2 + 2 + 2 + 2] << 8) + | (old_record[answer_index + 2 + 2 + 2 + 3]); + + current_record_data_len = (old_record[answer_index + 2 + 2 + 2 + 4] << 8) + | (old_record[answer_index + 2 + 2 + 2 + 4 + 1]); + + current_record_length = 2 + 2 + 2 + 4 + 2 + current_record_data_len; + + if (not_matched_yet && + current_record_data_len == record_data_len && + memcmp(old_record + answer_index + 2 + 2 + 2 + 4 + 2, record_data, record_data_len) == 0) + { + current_record_match = 1; + not_matched_yet = 0; + current_record_ttl = ttl; + } + else + { + /* Not a match */ + current_record_match = 0; + + if (current_record_ttl > delta_t_sec) + { + current_record_ttl -= delta_t_sec; + } + else + { + current_record_ttl = 0; + } + } + + if (current_record_ttl != 0) + { + nb_answers_left--; + + /* this record should be compacted away */ + if (current_hole_index == 0) + { + /* encountering the first hole in the message, + * no need to copy anything yet. + */ + last_copied_index = answer_index; + } + else if (current_hole_index != answer_index) + { + /* copy the data from hole to answer */ + memmove(old_record + last_copied_index, old_record + current_hole_index, + answer_index - current_hole_index); + last_copied_index += answer_index - current_hole_index; + } + + /* in all cases, the current hole begins after the message's encoding */ + current_hole_index = answer_index + current_record_length; + } + else + { + /* keeping this record, but updating the TTL */ + old_record[answer_index + 2 + 2 + 2] = (uint8_t)(current_record_ttl >> 24); + old_record[answer_index + 2 + 2 + 2 + 1] = (uint8_t)(current_record_ttl >> 16); + old_record[answer_index + 2 + 2 + 2 + 2] = (uint8_t)(current_record_ttl >> 8); + old_record[answer_index + 2 + 2 + 2 + 3] = (uint8_t)(current_record_ttl); + } + /* progress to the next record */ + answer_index += current_record_length; + } + + /* if necessary, copy the pending data */ + if (current_hole_index != answer_index) + { + /* copy the data from hole to last answer */ + memmove(old_record + last_copied_index, old_record + current_hole_index, + answer_index - current_hole_index); + last_copied_index += answer_index - current_hole_index; + } + + /* if some records were deleted, update the record headers */ + if (nb_answers != nb_answers_left) + { + header->content_len = last_copied_index; + old_record[message_index + 4] = (uint8_t)(nb_answers >> 8); + old_record[message_index + 5] = (uint8_t)(nb_answers); + } + + /* + * if the update was never seen, ask for an addition + */ + if (ttl == 0 && not_matched_yet) + { + mdns_add_record_to_cache_entry(context, old_record, new_record, + record_type, record_class, ttl, record_data, record_data_len); + nb_answers_left++; + } + else + { + *new_record = old_record; + } + + /* + * TODO: if there are no record left standing, return a signal that the cache should be pruned. + */ + + return ret; +} + + +/* +* Add entry function for the MDNS record cache. +*/ +static int +mdns_propose_entry_to_cache( + struct getdns_context *context, + uint8_t * name, int name_len, + int record_type, int record_class, int ttl, + uint8_t * record_data, int record_data_len, + uint64_t current_time) +{ + int ret = 0; + size_t required_memory = 0; + uint8_t temp_key[256 + sizeof(getdns_mdns_cached_key_header)]; + hashvalue_type hash; + struct lruhash_entry *entry, *new_entry; + uint8_t *key, *data; + + msdn_cache_create_key_in_buffer(temp_key, name, name_len, record_type, record_class); + + + /* TODO: make hash init value a random number in the context, for defense against DOS */ + hash = hashlittle(temp_key, name_len + sizeof(getdns_mdns_cached_key_header), 0xCAC8E); + + entry = lruhash_lookup(context->mdns_cache, hash, temp_key, 1); + + if (entry == NULL && ttl != 0) + { + /* + * Create an empty entry. + */ + key = mdns_cache_create_key(name, name_len, record_type, record_class, context); + data = mdns_cache_create_data(name, name_len, record_type, record_class, + record_data_len, current_time, context); + new_entry = GETDNS_XMALLOC(context->mf, struct lruhash_entry, 1); + + if (key == NULL || data == NULL || new_entry == NULL) + { + if (key != NULL) + { + GETDNS_FREE(context->mf, key); + key = NULL; + } + + if (data != NULL) + { + GETDNS_FREE(context->mf, data); + data = NULL; + } + + if (new_entry != NULL) + { + GETDNS_FREE(context->mf, new_entry); + new_entry = NULL; + } + + } + else + { + memset(new_entry, 0, sizeof(struct lruhash_entry)); + lock_rw_init(new_entry->lock); + new_entry->hash = hash; + new_entry->key = key; + new_entry->data = data; + + entry = lruhash_insert_or_retrieve(context->mdns_cache, hash, new_entry, data, NULL); + + if (entry != new_entry) + { + lock_rw_destroy(new_entry->lock); + GETDNS_FREE(context->mf, new_entry); + } + } + } + + if (entry != NULL) + { + ret = mdns_update_cache_ttl_and_prune(context, + (uint8_t*)entry->data, &data, + record_type, record_class, ttl, record_data, record_data_len, + current_time); + + /* then, unlock the entry */ + lock_rw_unlock(entry->lock); + + /* TODO: if the entry was marked for deletion, move it to the bottom of the LRU */ + } + + return ret; +} + +#if 0 +/* + * Add record function for the MDNS record cache. + */ +static int +mdns_add_record_to_cache(struct getdns_context *context, + uint8_t * name, int name_len, + int record_type, int record_class, int ttl, + uint8_t * record_data, int record_data_len) { int ret = 0; - if (id1 != id2) - { - ret = (((intptr_t)id1) < ((intptr_t)id2)) ? -1 : 1; - } + /* First, format a key */ + + /* Next, get the record from the LRU cache */ + + /* If there is no record, need to create one */ + + /* Else, just do simple update */ + return ret; } + +/* +* MDNS cache management. +* +* This is provisional, until we can get a proper cache by reusing the outbound code. +* +* The cache is a collection of getdns_mdns_known_record structures, each holding +* one record: name, type, class, ttl, rdata length and rdata, plus a 64 bit time stamp. +* The collection is organized as an RB tree. The comparison function in mdns_cmp_known_records +* is somewhat arbitrary: compare by numeric fields class, type, name length, and +* record length first, then by name value, and then by record value. +* +* When a message is received, it will be parsed, and each valid record in the +* ANSWER, AUTH or additional section will be added to the cache. If there is already +* a matching record, only the TTL and time stamp will be updated. If the new TTL is zero, +* the matching record will be removed from the cache. +* +* When a request is first presented, we will try to solve it from the cache. If there +* is response present, we will format a reply containing all the matching records, with +* an updated TTL. If the updated TTL is negative, the record will be deleted from the +* cache and will not be added to the query. +* +* For continuing requests, we may need to send queries with the list of known answers. +* These will be extracted from the cache. +* +* We may want to periodically check all the records in the cache and remove all those +* that have expired. This could be treated as a garbage collection task. +* +* The cache is emptied and deleted upon context deletion. +* +* In a multithreaded environment, we will assume that whoever accesses the cache holds a +* on the context. This is not ideal, but fine grain locks can lead to all kinds of +* synchronization issues. +*/ + /* * Compare function for the getdns_mdns_known_record type, * used in the red-black tree of known records per query. @@ -103,30 +704,413 @@ static int mdns_cmp_known_records(const void * nkr1, const void * nkr2) getdns_mdns_known_record * kr1 = (getdns_mdns_known_record *)nkr1; getdns_mdns_known_record * kr2 = (getdns_mdns_known_record *)nkr2; - if (kr1->request_class != kr2->request_class) + if (kr1->record_class != kr2->record_class) { - ret = (kr1->request_class < kr2->request_class) ? -1 : 1; + ret = (kr1->record_class < kr2->record_class) ? -1 : 1; } - else if (kr1->request_type != kr2->request_type) + else if (kr1->record_type != kr2->record_type) { - ret = (kr1->request_type < kr2->request_type) ? -1 : 1; + ret = (kr1->record_type < kr2->record_type) ? -1 : 1; } else if (kr1->name_len != kr2->name_len) { ret = (kr1->name_len < kr2->name_len) ? -1 : 1; } - else if (kr1->record_len != kr2->record_len) + else if (kr1->record_data_len != kr2->record_data_len) { - ret = (kr1->record_len < kr2->record_len) ? -1 : 1; + ret = (kr1->record_data_len < kr2->record_data_len) ? -1 : 1; } else if ((ret = memcmp((void*)kr1->name, (void*)kr2->name, kr2->name_len)) == 0) { - ret = memcmp((const void*)kr1->record_data, (const void*)kr2->record_data, kr1->record_len); + ret = memcmp((const void*)kr1->record_data, (const void*)kr2->record_data, kr1->record_data_len); } return ret; } +/* + * Add record function for the MDNS record cache. + */ +static int +mdns_add_known_record_to_cache( +struct getdns_context *context, + uint8_t * name, int name_len, + int record_type, int record_class, int ttl, + uint8_t * record_data, int record_data_len) +{ + int ret = 0; + getdns_mdns_known_record temp, *record, *inserted_record; + size_t required_memory = 0; + _getdns_rbnode_t * node_found, *to_delete; + + temp.name = name; + temp.name_len = name_len; + temp.record_class = record_class; + temp.record_type = record_type; + temp.record_data = record_data; + temp.record_data_len = record_data_len; + + + if (ttl == 0) + { + to_delete = _getdns_rbtree_delete( + &context->mdns_known_records_by_value, &temp); + + if (to_delete != NULL) + { + GETDNS_FREE(context->mf, to_delete->key); + } + } + else + { + node_found = _getdns_rbtree_search(&context->mdns_known_records_by_value, &temp); + + if (node_found != NULL) + { + record = (getdns_mdns_known_record *)node_found->key; + record->ttl = ttl; + record->insertion_microsec = _getdns_get_time_as_uintt64(); + } + else + { + required_memory = sizeof(getdns_mdns_known_record) + name_len + record_data_len; + + record = (getdns_mdns_known_record *) + GETDNS_XMALLOC(context->mf, uint8_t, required_memory); + + if (record == NULL) + { + ret = GETDNS_RETURN_MEMORY_ERROR; + } + else + { + record->node.parent = NULL; + record->node.left = NULL; + record->node.right = NULL; + record->node.key = (void*)record; + record->record_class = temp.record_class; + record->name = ((uint8_t*)record) + sizeof(getdns_mdns_known_record); + record->name_len = name_len; + record->record_class = record_class; + record->record_type = record_type; + record->record_data = record->name + name_len; + record->record_data_len = record_data_len; + record->insertion_microsec = _getdns_get_time_as_uintt64(); + + memcpy(record->name, name, name_len); + memcpy(record->record_data, record_data, record_data_len); + + inserted_record = (getdns_mdns_known_record *) + _getdns_rbtree_insert(&context->mdns_known_records_by_value, + &record->node); + if (inserted_record == NULL) + { + /* Weird. This can only happen in a race condition */ + GETDNS_FREE(context->mf, record); + ret = GETDNS_RETURN_GENERIC_ERROR; + } + } + } + } + + return ret; +} + +/* + * Get the position of the first matching record in the cache + */ +static _getdns_rbnode_t * +mdns_get_first_record_from_cache( +struct getdns_context *context, + uint8_t * name, int name_len, + int record_type, int record_class, + getdns_mdns_known_record* next_key) +{ + /* First, get a search key */ + getdns_mdns_known_record temp; + _getdns_rbnode_t *next_node; + + if (next_key == NULL) + { + temp.name = name; + temp.name_len = name_len; + temp.record_class = record_class; + temp.record_type = record_type; + temp.record_data = name; + temp.record_data_len = 0; + + next_key = &temp; + } + + /* + * Find the starting point + */ + if (!_getdns_rbtree_find_less_equal( + &context->mdns_known_records_by_value, + next_key, + &next_node)) + { + /* + * The key was not an exact match. Need to find the first node larger + * than the key. + */ + if (next_node == NULL) + { + /* + * The key was smallest than the smallest key in the tree, or the tree is empty + */ + next_node = _getdns_rbtree_first(&context->mdns_known_records_by_value); + } + else + { + /* + * Search retrurned a key smaller than target, so we pick the next one. + */ + next_node = _getdns_rbtree_next(next_node); + } + } + + /* + * At this point, we do not check that this is the right node + */ + + return next_node; +} + +/* + * Count the number of records for this name and type, purging those that have expired. + * Return the first valid node in the set, or NULL if there are no such nodes. + */ +static _getdns_rbnode_t * +mdns_count_and_purge_record_from_cache( + struct getdns_context *context, + uint8_t * name, int name_len, + int record_type, int record_class, + _getdns_rbnode_t* next_node, + uint64_t current_time_microsec, + int *nb_records, int *total_content) +{ + int current_record = 0; + int record_length_sum = 0; + int valid_ttl = 0; + getdns_mdns_known_record *next_key; + _getdns_rbnode_t *first_valid_node = NULL, *old_node = NULL; + + while (next_node) + { + next_key = (getdns_mdns_known_record *)next_node->key; + if (next_key->name_len != name_len || + next_key->record_class != record_class || + next_key->record_type != record_type || + memcmp(next_key->name, name, name_len) != 0) + { + next_node = NULL; + next_key = NULL; + break; + } + + old_node = next_node; + next_node = _getdns_rbtree_next(next_node); + + if (next_key->insertion_microsec + (((uint64_t)next_key->ttl) << 20) >= + current_time_microsec) + { + current_record++; + record_length_sum += next_key->record_data_len; + + if (first_valid_node == NULL) + { + first_valid_node = old_node; + } + } + else + { + _getdns_rbnode_t * deleted = _getdns_rbtree_delete( + &context->mdns_known_records_by_value, next_key); + + if (deleted != NULL) + { + GETDNS_FREE(context->mf, next_key); + } + } + } + + *nb_records = current_record; + *total_content = record_length_sum; + + return first_valid_node; +} + +/* + * Fill a response buffer with the records present in the set, + * up to a limit + */ +_getdns_rbnode_t * +mdns_fill_response_buffer_from_cache( +struct getdns_context *context, + uint8_t * name, int name_len, + int record_type, int record_class, + uint8_t * response, int* response_len, int response_len_max, + int * nb_records, + _getdns_rbnode_t* next_node, + uint64_t current_time_microsec, + int name_offset + ) +{ + int current_length = 0; + getdns_mdns_known_record * next_key; + int64_t ttl_64; + int32_t current_ttl; + int current_record = 0; + int coding_length; + int name_coding_length = (name_offset < 0) ? name_len : 2; + + while (next_node) + { + next_key = (getdns_mdns_known_record *)next_node->key; + if (next_key->name_len != name_len || + next_key->record_class != record_class || + next_key->record_type != record_type || + memcmp(next_key->name, name, name_len) != 0) + { + next_node = NULL; + next_key = NULL; + break; + } + + /* + * Compute the required size. + */ + coding_length = name_len + name_coding_length + 2 + 4 + 2 + next_key->record_data_len; + + if (current_length + coding_length > response_len_max) + { + break; + } + + /* + * encode the record with the updated TTL + */ + + ttl_64 = current_time_microsec - next_key->insertion_microsec; + ttl_64 += (((uint64_t)next_key->ttl) << 20); + + if (ttl_64 > 0) + { + current_ttl = max(ttl_64 >> 20, 1); + } + else + { + current_ttl = 1; + } + + if (name_offset >= 0) + { + /* Perform name compression, per DNS spec */ + response[current_length++] = (uint8_t)((0xC0 | (name_offset >> 8)) & 0xFF); + response[current_length++] = (uint8_t)(name_offset & 0xFF); + } + else + { + memcpy(response + current_length, name, name_len); + current_length += name_len; + } + response[current_length++] = (uint8_t)((record_type >> 8) & 0xFF); + response[current_length++] = (uint8_t)((record_type)& 0xFF); + response[current_length++] = (uint8_t)((record_class >> 8) & 0xFF); + response[current_length++] = (uint8_t)((record_class)& 0xFF); + response[current_length++] = (uint8_t)((current_ttl >> 24) & 0xFF); + response[current_length++] = (uint8_t)((current_ttl >> 16) & 0xFF); + response[current_length++] = (uint8_t)((current_ttl >> 8) & 0xFF); + response[current_length++] = (uint8_t)((current_ttl)& 0xFF); + response[current_length++] = (uint8_t)((next_key->record_data_len >> 8) & 0xFF); + response[current_length++] = (uint8_t)((next_key->record_data_len)& 0xFF); + memcpy(response + current_length, next_key->record_data, next_key->record_data_len); + current_length += next_key->record_data_len; + + /* + * continue with the next node + */ + current_record++; + next_node = _getdns_rbtree_next(next_node); + } + + *response_len = current_length; + *nb_records = current_record; + + return next_node; +} + +/* + * Compose a response from the MDNS record cache. + */ +static int +mdns_compose_response_from_cache( + struct getdns_context *context, + uint8_t * name, int name_len, + int record_type, int record_class, + uint8_t ** response, int* response_len, int response_len_max, + int query_id, + getdns_mdns_known_record** next_key) +{ + int ret = 0; + /* First, get a search key */ + int nb_records = 0; + int total_content = 0; + int total_length = 0; + uint64_t current_time = _getdns_get_time_as_uintt64(); + + _getdns_rbnode_t * first_node = mdns_get_first_record_from_cache( + context, name, name_len, record_type, record_class, *next_key); + /* Purge the expired records and compute the desired length */ + first_node = mdns_count_and_purge_record_from_cache( + context, name, name_len, record_type, record_class, first_node, current_time, + &nb_records, &total_content); + + /* todo: check whether encoding an empty message is OK */ + /* todo: check whether something special is needed for continuation records */ + + /* Allocate the required memory */ + total_length = 12 /* DNS header */ + + name_len + 4 /* Query */ + + total_content + (2 + 2 + 2 + 4 + 2)*nb_records /* answers */; + /* TODO: do we need EDNS encoding? */ + + if (response_len_max == 0) + { + /* setting this parameter to zero indicates that a full buffer allocation is desired */ + { + if (*response == NULL) + { + *response = GETDNS_XMALLOC(context->mf, uint8_t, total_length); + } + else + { + *response = GETDNS_XREALLOC(context->mf, *response, uint8_t, total_length); + } + + if (*response == NULL) + { + ret = GETDNS_RETURN_MEMORY_ERROR; + } + else + { + response_len_max = total_length; + } + } + } + if (ret == 0) + { + + /* + * Now, proceed with the encoding + */ + } +} +#endif /* if 0, remove the RB based cache code */ + + + /* * Compare function for the mdns_continuous_query_by_name_rrtype, * used in the red-black tree of all ongoing queries. @@ -156,11 +1140,56 @@ static int mdns_cmp_continuous_queries_by_name_rrtype(const void * nqnr1, const return ret; } +/* + * Multicast receive event callback + */ +static void +mdns_udp_multicast_read_cb(void *userarg) +{ + mdns_network_connection * cnx = (mdns_network_connection *)userarg; + ssize_t read; + DEBUG_MDNS("%s %-35s: CTX: %p, NET=%d \n", MDNS_DEBUG_MREAD, + __FUNCTION__, cnx->context, cnx->addr_mcast.ss_family); + + GETDNS_CLEAR_EVENT( + cnx->context->extension, &cnx->event); + + read = recvfrom(cnx->fd, (void *)cnx->response, + sizeof(cnx->response), 0, NULL, NULL); + + + if (read == -1 && _getdns_EWOULDBLOCK) + return; /* TODO: this will stop the receive loop! */ + + if (read >= GLDNS_HEADER_SIZE) + { + /* parse the response, find the relevant queries, submit the records to the cache */ + + /* + netreq->response_len = read; + netreq->debug_end_time = _getdns_get_time_as_uintt64(); + netreq->state = NET_REQ_FINISHED; + _getdns_check_dns_req_complete(dnsreq); + */ + } + else + { + /* bogus packet.. Should log. */ + } + + /* + * Relaunch the event, so we can go read the next packet. + */ + GETDNS_SCHEDULE_EVENT( + cnx->context->extension, cnx->fd, 0, + getdns_eventloop_event_init(&cnx->event, cnx, + mdns_udp_multicast_read_cb, NULL, NULL)); +} /* -* Create the two required multicast sockets -*/ -static int mdns_open_ipv4_multicast() + * Create the two required multicast sockets + */ +static int mdns_open_ipv4_multicast(SOCKADDR_STORAGE* mcast_dest, int* mcast_dest_len) { getdns_return_t ret = 0; SOCKET fd4 = -1; @@ -170,6 +1199,8 @@ static int mdns_open_ipv4_multicast() uint8_t ttl = 255; IP_MREQ mreq4; + memset(&mcast_dest, 0, sizeof(SOCKADDR_STORAGE)); + *mcast_dest_len = 0; memset(&ipv4_dest, 0, sizeof(ipv4_dest)); memset(&ipv4_port, 0, sizeof(ipv4_dest)); ipv4_dest.sin_family = AF_INET; @@ -221,10 +1252,16 @@ static int mdns_open_ipv4_multicast() fd4 = -1; } + if (ret == 0) + { + memcpy(&mcast_dest, &ipv4_dest, sizeof(ipv4_dest)); + *mcast_dest_len = sizeof(ipv4_dest); + } + return fd4; } -static int mdns_open_ipv6_multicast() +static int mdns_open_ipv6_multicast(SOCKADDR_STORAGE* mcast_dest, int* mcast_dest_len) { getdns_return_t ret = 0; SOCKET fd6 = -1; @@ -234,6 +1271,8 @@ static int mdns_open_ipv6_multicast() uint8_t ttl = 255; IPV6_MREQ mreq6; + memset(&mcast_dest, 0, sizeof(SOCKADDR_STORAGE)); + *mcast_dest_len = 0; memset(&ipv6_dest, 0, sizeof(ipv6_dest)); memset(&ipv6_port, 0, sizeof(ipv6_dest)); ipv6_dest.sin6_family = AF_INET6; @@ -288,6 +1327,11 @@ static int mdns_open_ipv6_multicast() fd6 = -1; } + if (ret == 0) + { + memcpy(&mcast_dest, &ipv6_dest, sizeof(ipv6_dest)); + *mcast_dest_len = sizeof(ipv6_dest); + } return fd6; } @@ -300,23 +1344,86 @@ static getdns_return_t mdns_delayed_network_init(struct getdns_context *context) if (context->mdns_extended_support == 2) { - context->mdns_fdv4 = mdns_open_ipv4_multicast(); - context->mdns_fdv6 = mdns_open_ipv6_multicast(); + context->mdns_cache = lruhash_create(100, 1000, + mdns_cache_entry_size, mdns_cache_key_comp, + msdn_cache_delkey, msdn_cache_deldata, + context); - if (context->mdns_fdv4 == -1 || context->mdns_fdv6 == -1) + if (context->mdns_cache == NULL) { - if (context->mdns_fdv4 != -1) -#ifdef USE_WINSOCK - closesocket(context->mdns_fdv4); -#else - close(context->mdns_fdv4); -#endif - ret = GETDNS_RETURN_GENERIC_ERROR; + ret = GETDNS_RETURN_MEMORY_ERROR; } else { - /* TODO: launch the receive loops */ - } + context->mdns_connection = (mdns_network_connection *) + GETDNS_XMALLOC(context->my_mf, mdns_network_connection, 2); + + if (context->mdns_connection == NULL) + { + ret = GETDNS_RETURN_MEMORY_ERROR; + } + else + { + context->mdns_connection_nb = 2; + + context->mdns_connection[0].fd = mdns_open_ipv4_multicast( + &context->mdns_connection[0].addr_mcast + , &context->mdns_connection[0].addr_mcast_len); + context->mdns_connection[1].fd = mdns_open_ipv6_multicast( + &context->mdns_connection[0].addr_mcast + , &context->mdns_connection[0].addr_mcast_len); + + if (context->mdns_connection[0].fd == -1 || + context->mdns_connection[1].fd == -1) + { + ret = GETDNS_RETURN_GENERIC_ERROR; + } + else + { + /* TODO: launch the receive loops */ + for (int i = 0; i < 2; i++) + { + GETDNS_CLEAR_EVENT(context->extension, &context->mdns_connection[i].event); + GETDNS_SCHEDULE_EVENT( + context->extension, context->mdns_connection[i].fd, 0, + getdns_eventloop_event_init(&context->mdns_connection[i].event, + &context->mdns_connection[i], + mdns_udp_multicast_read_cb, NULL, NULL)); + } + } + + if (ret != 0) + { + for (int i = 0; i < 2; i++) + { + if (context->mdns_connection[i].fd != -1) + { + + GETDNS_CLEAR_EVENT(context->extension + , &context->mdns_connection[i].event); +#ifdef USE_WINSOCK + closesocket(context->mdns_connection[i].fd); +#else + close(context->mdns_connection[i].fd); +#endif + } + } + + GETDNS_FREE(context->my_mf, context->mdns_connection); + context->mdns_connection = NULL; + context->mdns_connection_nb = 0; + } + } /* mdns-connection != NULL */ + + if (ret != 0) + { + /* delete the cache that was just created, since the network connection failed */ + lruhash_delete(context->mdns_cache); + context->mdns_cache = NULL; + } + } /* cache != NULL */ + + context->mdns_extended_support = (ret == 0) ? 1 : 0; } return ret; @@ -331,6 +1438,8 @@ static getdns_return_t mdns_initialize_continuous_request(getdns_network_req *ne getdns_mdns_continuous_query temp_query, *continuous_query, *inserted_query; getdns_dns_req *dnsreq = netreq->owner; struct getdns_context *context = dnsreq->context; + _getdns_rbnode_t * node_found; + /* * Fill the target request, but only initialize name and request_type */ @@ -344,9 +1453,13 @@ static getdns_return_t mdns_initialize_continuous_request(getdns_network_req *ne * if there is not, create one. * TODO: should lock the context object when doing that. */ - continuous_query = (getdns_mdns_continuous_query *) - _getdns_rbtree_search(&context->mdns_continuous_queries_by_name_rrtype, &temp_query); - if (continuous_query == NULL) + node_found = _getdns_rbtree_search(&context->mdns_continuous_queries_by_name_rrtype, &temp_query); + + if (node_found != NULL) + { + continuous_query = (getdns_mdns_continuous_query *)node_found->key; + } + else { continuous_query = (getdns_mdns_continuous_query *) GETDNS_MALLOC(context->mf, getdns_mdns_continuous_query); @@ -391,14 +1504,12 @@ static getdns_return_t mdns_initialize_continuous_request(getdns_network_req *ne */ void _getdns_mdns_context_init(struct getdns_context *context) { - context->mdns_extended_support = 2; /* 0 = no support, 1 = supported, 2 = initialization needed */ - context->mdns_fdv4 = -1; /* invalid socket, i.e. not initialized */ - context->mdns_fdv6 = -1; /* invalid socket, i.e. not initialized */ + context->mdns_connection = NULL; + context->mdns_connection_nb = 0; + context->mdns_cache = NULL; _getdns_rbtree_init(&context->mdns_continuous_queries_by_name_rrtype , mdns_cmp_continuous_queries_by_name_rrtype); - _getdns_rbtree_init(&context->mdns_known_records_by_value - , mdns_cmp_known_records); } /* diff --git a/src/mdns.h b/src/mdns.h index b2ac131b..d5cc9f79 100644 --- a/src/mdns.h +++ b/src/mdns.h @@ -40,15 +40,40 @@ typedef struct getdns_mdns_known_record /* For storage in context->mdns_known_records_by_value */ _getdns_rbnode_t node; uint64_t insertion_microsec; - uint16_t request_type; - uint16_t request_class; + uint16_t record_type; + uint16_t record_class; uint32_t ttl; int name_len; - int record_len; + int record_data_len; uint8_t* name; uint8_t * record_data; } getdns_mdns_known_record; +/* + * Each entry in the hash table is keyed by type, class and name. + * The data part contains: + * - 64 bit time stamp + * - 32 bit word describing the record size + * - 32 bit word describing teh allocated memory size + * - valid DNS response, including 1 query and N answers, 0 AUTH, 0 AD. + * For economy, all answers are encoded using header compression, pointing + * to the name in the query, i.e. offset 12 from beginning of message + */ + +typedef struct getdns_mdns_cached_key_header +{ + uint16_t record_type; + uint16_t record_class; + int name_len; +} getdns_mdns_cached_key_header; + +typedef struct getdns_mdns_cached_record_header +{ + uint64_t insertion_microsec; + uint32_t content_len; + uint32_t allocated_length; +} getdns_mdns_cached_record_header; + typedef struct getdns_mdns_continuous_query { /* For storage in context->mdns_continuous_queries_by_name_rrtype */ @@ -63,6 +88,16 @@ typedef struct getdns_mdns_continuous_query /* todo: do we need an update mark for showing last results? */ } getdns_mdns_continuous_query; +typedef struct mdns_network_connection +{ + struct getdns_context* context; + int fd; + int addr_mcast_len; + SOCKADDR_STORAGE addr_mcast; + getdns_eventloop_event event; + uint8_t response[1500]; +} mdns_network_connection; + void _getdns_mdns_context_init(struct getdns_context *context); void _getdns_mdns_context_destroy(struct getdns_context *context); diff --git a/src/server.c b/src/server.c index 91553725..ef167179 100644 --- a/src/server.c +++ b/src/server.c @@ -708,7 +708,11 @@ static void remove_listeners(listen_set *set) continue; loop->vmt->clear(loop, &l->event); +#ifdef USE_WINSOCK + closesocket(l->fd); +#else close(l->fd); +#endif l->fd = -1; if (l->transport != GETDNS_TRANSPORT_TCP) From 6d3e0c7ca215b867a6d6662aa9d720e819c3953d Mon Sep 17 00:00:00 2001 From: Christian Huitema Date: Tue, 14 Feb 2017 11:30:29 -1000 Subject: [PATCH 07/25] Rewrote the continuous query organization to use the LRU cache instead of an RB tree. --- src/context.h | 1 - src/mdns.c | 615 +++++-------------------------------------- src/mdns.h | 2 + src/types-internal.h | 5 +- 4 files changed, 74 insertions(+), 549 deletions(-) diff --git a/src/context.h b/src/context.h index 2267c06e..69eb4d21 100644 --- a/src/context.h +++ b/src/context.h @@ -351,7 +351,6 @@ struct getdns_context { int mdns_connection_nb; /* typically 0 or 2 for IPv4 and IPv6 */ struct mdns_network_connection * mdns_connection; struct lruhash * mdns_cache; - _getdns_rbtree_t mdns_continuous_queries_by_name_rrtype; #endif /* HAVE_MDNS_SUPPORT */ }; /* getdns_context */ diff --git a/src/mdns.c b/src/mdns.c index f21bea38..eec9c740 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -263,9 +263,32 @@ static void msdn_cache_delkey(void* vkey, void* vcontext) GETDNS_FREE(((struct getdns_context *) vcontext)->mf, vkey); } -/** old data is deleted. This function is called: func(data, userarg). */ +/** old data is deleted. This function is called: func(data, userarg). + * Since we use the hash table for both data and requests, need to + * terminate whatever request was ongoing. TODO: we should have some smarts + * in cache management and never drop cached entries with active requests. + */ static void msdn_cache_deldata(void* vdata, void* vcontext) { + /* need to terminate the pending queries? */ + getdns_mdns_cached_record_header* header = ((getdns_mdns_cached_record_header*)vdata); + + while (header->netreq_first) + { + /* Need to unchain the request from that entry */ + getdns_network_req* netreq = header->netreq_first; + header->netreq_first = netreq->mdns_netreq_next; + netreq->mdns_netreq_next = NULL; + + /* TODO: treating as a timeout for now, may consider treating as error */ + netreq->debug_end_time = _getdns_get_time_as_uintt64(); + netreq->state = NET_REQ_TIMED_OUT; + if (netreq->owner->user_callback) { + (void)_getdns_context_request_timed_out(netreq->owner); + } + _getdns_check_dns_req_complete(netreq->owner); + + } GETDNS_FREE(((struct getdns_context *) vcontext)->mf, vdata); } @@ -309,7 +332,7 @@ static uint8_t * mdns_cache_create_data( { getdns_mdns_cached_record_header * header; int current_index; - size_t data_size = sizeof(getdns_mdns_cached_record_header) + name_len + 4; + size_t data_size = sizeof(getdns_mdns_cached_record_header) + 12 + name_len + 4; size_t alloc_size = mdns_util_suggest_size(data_size + record_data_len + 2 + 2 + 2 + 4 + 2); uint8_t* data = GETDNS_XMALLOC(context->mf, uint8_t, alloc_size); @@ -320,7 +343,10 @@ static uint8_t * mdns_cache_create_data( header->insertion_microsec = current_time; header->content_len = data_size; header->allocated_length = alloc_size; + header->netreq_first = NULL; current_index = sizeof(getdns_mdns_cached_record_header); + memset(data + current_index, 0, 12); + current_index += 12; memcpy(data + current_index, name, name_len); current_index += name_len; data[current_index++] = (uint8_t)(record_type >> 8); @@ -553,6 +579,7 @@ mdns_propose_entry_to_cache( uint8_t * name, int name_len, int record_type, int record_class, int ttl, uint8_t * record_data, int record_data_len, + getdns_network_req * netreq, uint64_t current_time) { int ret = 0; @@ -561,6 +588,7 @@ mdns_propose_entry_to_cache( hashvalue_type hash; struct lruhash_entry *entry, *new_entry; uint8_t *key, *data; + getdns_mdns_cached_record_header * header; msdn_cache_create_key_in_buffer(temp_key, name, name_len, record_type, record_class); @@ -621,10 +649,19 @@ mdns_propose_entry_to_cache( if (entry != NULL) { - ret = mdns_update_cache_ttl_and_prune(context, - (uint8_t*)entry->data, &data, - record_type, record_class, ttl, record_data, record_data_len, - current_time); + if (record_data != NULL && record_data_len > 0) + ret = mdns_update_cache_ttl_and_prune(context, + (uint8_t*)entry->data, &data, + record_type, record_class, ttl, record_data, record_data_len, + current_time); + + if (netreq != NULL) + { + /* chain the continuous request to the cache line */ + header = (getdns_mdns_cached_record_header *) entry->data; + netreq->mdns_netreq_next = header->netreq_first; + header->netreq_first = netreq; + } /* then, unlock the entry */ lock_rw_unlock(entry->lock); @@ -635,482 +672,6 @@ mdns_propose_entry_to_cache( return ret; } -#if 0 -/* - * Add record function for the MDNS record cache. - */ -static int -mdns_add_record_to_cache(struct getdns_context *context, - uint8_t * name, int name_len, - int record_type, int record_class, int ttl, - uint8_t * record_data, int record_data_len) -{ - int ret = 0; - - /* First, format a key */ - - /* Next, get the record from the LRU cache */ - - /* If there is no record, need to create one */ - - /* Else, just do simple update */ - - return ret; -} - - -/* -* MDNS cache management. -* -* This is provisional, until we can get a proper cache by reusing the outbound code. -* -* The cache is a collection of getdns_mdns_known_record structures, each holding -* one record: name, type, class, ttl, rdata length and rdata, plus a 64 bit time stamp. -* The collection is organized as an RB tree. The comparison function in mdns_cmp_known_records -* is somewhat arbitrary: compare by numeric fields class, type, name length, and -* record length first, then by name value, and then by record value. -* -* When a message is received, it will be parsed, and each valid record in the -* ANSWER, AUTH or additional section will be added to the cache. If there is already -* a matching record, only the TTL and time stamp will be updated. If the new TTL is zero, -* the matching record will be removed from the cache. -* -* When a request is first presented, we will try to solve it from the cache. If there -* is response present, we will format a reply containing all the matching records, with -* an updated TTL. If the updated TTL is negative, the record will be deleted from the -* cache and will not be added to the query. -* -* For continuing requests, we may need to send queries with the list of known answers. -* These will be extracted from the cache. -* -* We may want to periodically check all the records in the cache and remove all those -* that have expired. This could be treated as a garbage collection task. -* -* The cache is emptied and deleted upon context deletion. -* -* In a multithreaded environment, we will assume that whoever accesses the cache holds a -* on the context. This is not ideal, but fine grain locks can lead to all kinds of -* synchronization issues. -*/ - -/* - * Compare function for the getdns_mdns_known_record type, - * used in the red-black tree of known records per query. - */ - -static int mdns_cmp_known_records(const void * nkr1, const void * nkr2) -{ - int ret = 0; - getdns_mdns_known_record * kr1 = (getdns_mdns_known_record *)nkr1; - getdns_mdns_known_record * kr2 = (getdns_mdns_known_record *)nkr2; - - if (kr1->record_class != kr2->record_class) - { - ret = (kr1->record_class < kr2->record_class) ? -1 : 1; - } - else if (kr1->record_type != kr2->record_type) - { - ret = (kr1->record_type < kr2->record_type) ? -1 : 1; - } - else if (kr1->name_len != kr2->name_len) - { - ret = (kr1->name_len < kr2->name_len) ? -1 : 1; - } - else if (kr1->record_data_len != kr2->record_data_len) - { - ret = (kr1->record_data_len < kr2->record_data_len) ? -1 : 1; - } - else if ((ret = memcmp((void*)kr1->name, (void*)kr2->name, kr2->name_len)) == 0) - { - ret = memcmp((const void*)kr1->record_data, (const void*)kr2->record_data, kr1->record_data_len); - } - - return ret; -} - -/* - * Add record function for the MDNS record cache. - */ -static int -mdns_add_known_record_to_cache( -struct getdns_context *context, - uint8_t * name, int name_len, - int record_type, int record_class, int ttl, - uint8_t * record_data, int record_data_len) -{ - int ret = 0; - getdns_mdns_known_record temp, *record, *inserted_record; - size_t required_memory = 0; - _getdns_rbnode_t * node_found, *to_delete; - - temp.name = name; - temp.name_len = name_len; - temp.record_class = record_class; - temp.record_type = record_type; - temp.record_data = record_data; - temp.record_data_len = record_data_len; - - - if (ttl == 0) - { - to_delete = _getdns_rbtree_delete( - &context->mdns_known_records_by_value, &temp); - - if (to_delete != NULL) - { - GETDNS_FREE(context->mf, to_delete->key); - } - } - else - { - node_found = _getdns_rbtree_search(&context->mdns_known_records_by_value, &temp); - - if (node_found != NULL) - { - record = (getdns_mdns_known_record *)node_found->key; - record->ttl = ttl; - record->insertion_microsec = _getdns_get_time_as_uintt64(); - } - else - { - required_memory = sizeof(getdns_mdns_known_record) + name_len + record_data_len; - - record = (getdns_mdns_known_record *) - GETDNS_XMALLOC(context->mf, uint8_t, required_memory); - - if (record == NULL) - { - ret = GETDNS_RETURN_MEMORY_ERROR; - } - else - { - record->node.parent = NULL; - record->node.left = NULL; - record->node.right = NULL; - record->node.key = (void*)record; - record->record_class = temp.record_class; - record->name = ((uint8_t*)record) + sizeof(getdns_mdns_known_record); - record->name_len = name_len; - record->record_class = record_class; - record->record_type = record_type; - record->record_data = record->name + name_len; - record->record_data_len = record_data_len; - record->insertion_microsec = _getdns_get_time_as_uintt64(); - - memcpy(record->name, name, name_len); - memcpy(record->record_data, record_data, record_data_len); - - inserted_record = (getdns_mdns_known_record *) - _getdns_rbtree_insert(&context->mdns_known_records_by_value, - &record->node); - if (inserted_record == NULL) - { - /* Weird. This can only happen in a race condition */ - GETDNS_FREE(context->mf, record); - ret = GETDNS_RETURN_GENERIC_ERROR; - } - } - } - } - - return ret; -} - -/* - * Get the position of the first matching record in the cache - */ -static _getdns_rbnode_t * -mdns_get_first_record_from_cache( -struct getdns_context *context, - uint8_t * name, int name_len, - int record_type, int record_class, - getdns_mdns_known_record* next_key) -{ - /* First, get a search key */ - getdns_mdns_known_record temp; - _getdns_rbnode_t *next_node; - - if (next_key == NULL) - { - temp.name = name; - temp.name_len = name_len; - temp.record_class = record_class; - temp.record_type = record_type; - temp.record_data = name; - temp.record_data_len = 0; - - next_key = &temp; - } - - /* - * Find the starting point - */ - if (!_getdns_rbtree_find_less_equal( - &context->mdns_known_records_by_value, - next_key, - &next_node)) - { - /* - * The key was not an exact match. Need to find the first node larger - * than the key. - */ - if (next_node == NULL) - { - /* - * The key was smallest than the smallest key in the tree, or the tree is empty - */ - next_node = _getdns_rbtree_first(&context->mdns_known_records_by_value); - } - else - { - /* - * Search retrurned a key smaller than target, so we pick the next one. - */ - next_node = _getdns_rbtree_next(next_node); - } - } - - /* - * At this point, we do not check that this is the right node - */ - - return next_node; -} - -/* - * Count the number of records for this name and type, purging those that have expired. - * Return the first valid node in the set, or NULL if there are no such nodes. - */ -static _getdns_rbnode_t * -mdns_count_and_purge_record_from_cache( - struct getdns_context *context, - uint8_t * name, int name_len, - int record_type, int record_class, - _getdns_rbnode_t* next_node, - uint64_t current_time_microsec, - int *nb_records, int *total_content) -{ - int current_record = 0; - int record_length_sum = 0; - int valid_ttl = 0; - getdns_mdns_known_record *next_key; - _getdns_rbnode_t *first_valid_node = NULL, *old_node = NULL; - - while (next_node) - { - next_key = (getdns_mdns_known_record *)next_node->key; - if (next_key->name_len != name_len || - next_key->record_class != record_class || - next_key->record_type != record_type || - memcmp(next_key->name, name, name_len) != 0) - { - next_node = NULL; - next_key = NULL; - break; - } - - old_node = next_node; - next_node = _getdns_rbtree_next(next_node); - - if (next_key->insertion_microsec + (((uint64_t)next_key->ttl) << 20) >= - current_time_microsec) - { - current_record++; - record_length_sum += next_key->record_data_len; - - if (first_valid_node == NULL) - { - first_valid_node = old_node; - } - } - else - { - _getdns_rbnode_t * deleted = _getdns_rbtree_delete( - &context->mdns_known_records_by_value, next_key); - - if (deleted != NULL) - { - GETDNS_FREE(context->mf, next_key); - } - } - } - - *nb_records = current_record; - *total_content = record_length_sum; - - return first_valid_node; -} - -/* - * Fill a response buffer with the records present in the set, - * up to a limit - */ -_getdns_rbnode_t * -mdns_fill_response_buffer_from_cache( -struct getdns_context *context, - uint8_t * name, int name_len, - int record_type, int record_class, - uint8_t * response, int* response_len, int response_len_max, - int * nb_records, - _getdns_rbnode_t* next_node, - uint64_t current_time_microsec, - int name_offset - ) -{ - int current_length = 0; - getdns_mdns_known_record * next_key; - int64_t ttl_64; - int32_t current_ttl; - int current_record = 0; - int coding_length; - int name_coding_length = (name_offset < 0) ? name_len : 2; - - while (next_node) - { - next_key = (getdns_mdns_known_record *)next_node->key; - if (next_key->name_len != name_len || - next_key->record_class != record_class || - next_key->record_type != record_type || - memcmp(next_key->name, name, name_len) != 0) - { - next_node = NULL; - next_key = NULL; - break; - } - - /* - * Compute the required size. - */ - coding_length = name_len + name_coding_length + 2 + 4 + 2 + next_key->record_data_len; - - if (current_length + coding_length > response_len_max) - { - break; - } - - /* - * encode the record with the updated TTL - */ - - ttl_64 = current_time_microsec - next_key->insertion_microsec; - ttl_64 += (((uint64_t)next_key->ttl) << 20); - - if (ttl_64 > 0) - { - current_ttl = max(ttl_64 >> 20, 1); - } - else - { - current_ttl = 1; - } - - if (name_offset >= 0) - { - /* Perform name compression, per DNS spec */ - response[current_length++] = (uint8_t)((0xC0 | (name_offset >> 8)) & 0xFF); - response[current_length++] = (uint8_t)(name_offset & 0xFF); - } - else - { - memcpy(response + current_length, name, name_len); - current_length += name_len; - } - response[current_length++] = (uint8_t)((record_type >> 8) & 0xFF); - response[current_length++] = (uint8_t)((record_type)& 0xFF); - response[current_length++] = (uint8_t)((record_class >> 8) & 0xFF); - response[current_length++] = (uint8_t)((record_class)& 0xFF); - response[current_length++] = (uint8_t)((current_ttl >> 24) & 0xFF); - response[current_length++] = (uint8_t)((current_ttl >> 16) & 0xFF); - response[current_length++] = (uint8_t)((current_ttl >> 8) & 0xFF); - response[current_length++] = (uint8_t)((current_ttl)& 0xFF); - response[current_length++] = (uint8_t)((next_key->record_data_len >> 8) & 0xFF); - response[current_length++] = (uint8_t)((next_key->record_data_len)& 0xFF); - memcpy(response + current_length, next_key->record_data, next_key->record_data_len); - current_length += next_key->record_data_len; - - /* - * continue with the next node - */ - current_record++; - next_node = _getdns_rbtree_next(next_node); - } - - *response_len = current_length; - *nb_records = current_record; - - return next_node; -} - -/* - * Compose a response from the MDNS record cache. - */ -static int -mdns_compose_response_from_cache( - struct getdns_context *context, - uint8_t * name, int name_len, - int record_type, int record_class, - uint8_t ** response, int* response_len, int response_len_max, - int query_id, - getdns_mdns_known_record** next_key) -{ - int ret = 0; - /* First, get a search key */ - int nb_records = 0; - int total_content = 0; - int total_length = 0; - uint64_t current_time = _getdns_get_time_as_uintt64(); - - _getdns_rbnode_t * first_node = mdns_get_first_record_from_cache( - context, name, name_len, record_type, record_class, *next_key); - /* Purge the expired records and compute the desired length */ - first_node = mdns_count_and_purge_record_from_cache( - context, name, name_len, record_type, record_class, first_node, current_time, - &nb_records, &total_content); - - /* todo: check whether encoding an empty message is OK */ - /* todo: check whether something special is needed for continuation records */ - - /* Allocate the required memory */ - total_length = 12 /* DNS header */ - + name_len + 4 /* Query */ - + total_content + (2 + 2 + 2 + 4 + 2)*nb_records /* answers */; - /* TODO: do we need EDNS encoding? */ - - if (response_len_max == 0) - { - /* setting this parameter to zero indicates that a full buffer allocation is desired */ - { - if (*response == NULL) - { - *response = GETDNS_XMALLOC(context->mf, uint8_t, total_length); - } - else - { - *response = GETDNS_XREALLOC(context->mf, *response, uint8_t, total_length); - } - - if (*response == NULL) - { - ret = GETDNS_RETURN_MEMORY_ERROR; - } - else - { - response_len_max = total_length; - } - } - } - if (ret == 0) - { - - /* - * Now, proceed with the encoding - */ - } -} -#endif /* if 0, remove the RB based cache code */ - - - /* * Compare function for the mdns_continuous_query_by_name_rrtype, * used in the red-black tree of all ongoing queries. @@ -1344,7 +905,7 @@ static getdns_return_t mdns_delayed_network_init(struct getdns_context *context) if (context->mdns_extended_support == 2) { - context->mdns_cache = lruhash_create(100, 1000, + context->mdns_cache = lruhash_create(128, 10000000, mdns_cache_entry_size, mdns_cache_key_comp, msdn_cache_delkey, msdn_cache_deldata, context); @@ -1435,64 +996,12 @@ static getdns_return_t mdns_delayed_network_init(struct getdns_context *context) static getdns_return_t mdns_initialize_continuous_request(getdns_network_req *netreq) { int ret = 0; - getdns_mdns_continuous_query temp_query, *continuous_query, *inserted_query; getdns_dns_req *dnsreq = netreq->owner; struct getdns_context *context = dnsreq->context; - _getdns_rbnode_t * node_found; - /* - * Fill the target request, but only initialize name and request_type - */ - temp_query.request_class = dnsreq->request_class; - temp_query.request_type = netreq->request_type; - temp_query.name_len = dnsreq->name_len; - /* TODO: check that dnsreq is in canonical form */ - memcpy(temp_query.name, dnsreq->name, dnsreq->name_len); - /* - * Check whether the continuous query is already in the RB tree. - * if there is not, create one. - * TODO: should lock the context object when doing that. - */ - node_found = _getdns_rbtree_search(&context->mdns_continuous_queries_by_name_rrtype, &temp_query); - - if (node_found != NULL) - { - continuous_query = (getdns_mdns_continuous_query *)node_found->key; - } - else - { - continuous_query = (getdns_mdns_continuous_query *) - GETDNS_MALLOC(context->mf, getdns_mdns_continuous_query); - if (continuous_query != NULL) - { - continuous_query->node.parent = NULL; - continuous_query->node.left = NULL; - continuous_query->node.right = NULL; - continuous_query->node.key = (void*)continuous_query; - continuous_query->request_class = temp_query.request_class; - continuous_query->request_type = temp_query.request_type; - continuous_query->name_len = temp_query.name_len; - memcpy(continuous_query->name, temp_query.name, temp_query.name_len); - continuous_query->netreq_first = NULL; - /* Add the new continuous query to the context */ - inserted_query = (getdns_mdns_continuous_query *) - _getdns_rbtree_insert(&context->mdns_continuous_queries_by_name_rrtype, - &continuous_query->node); - if (inserted_query == NULL) - { - /* Weird. This can only happen in a race condition */ - GETDNS_FREE(context->mf, &continuous_query); - ret = GETDNS_RETURN_GENERIC_ERROR; - } - } - else - { - ret = GETDNS_RETURN_MEMORY_ERROR; - } - } - /* insert netreq into query list */ - netreq->mdns_netreq_next = continuous_query->netreq_first; - continuous_query->netreq_first = netreq; + ret = mdns_propose_entry_to_cache(context, dnsreq->name, dnsreq->name_len, + netreq->request_type, dnsreq->request_class, 1, NULL, 0, + netreq, _getdns_get_time_as_uintt64()); /* to do: queue message request to socket */ @@ -1508,8 +1017,6 @@ void _getdns_mdns_context_init(struct getdns_context *context) context->mdns_connection = NULL; context->mdns_connection_nb = 0; context->mdns_cache = NULL; - _getdns_rbtree_init(&context->mdns_continuous_queries_by_name_rrtype - , mdns_cmp_continuous_queries_by_name_rrtype); } /* @@ -1517,11 +1024,31 @@ void _getdns_mdns_context_init(struct getdns_context *context) */ void _getdns_mdns_context_destroy(struct getdns_context *context) { - /* Close the sockets */ + /* Clear all the cached records. This will terminate all pending network requests */ + if (context->mdns_cache != NULL) + { + lruhash_delete(context->mdns_cache); + context->mdns_cache = NULL; + } + /* Close the connections */ + if (context->mdns_connection != NULL) + { + for (int i = 0; i < context->mdns_connection_nb; i++) + { + /* suppress the receive event */ + GETDNS_CLEAR_EVENT(context->extension, &context->mdns_connection[i].event); + /* close the socket */ +#ifdef USE_WINSOCK + closesocket(context->mdns_connection[i].fd); +#else + close(context->mdns_connection[i].fd); +#endif + } - /* Clear all the continuous queries */ - - /* Clear all the cached records */ + GETDNS_FREE(context->mf, context->mdns_connection); + context->mdns_connection = NULL; + context->mdns_connection_nb = 0; + } } /* TODO: actualy delete what is required.. */ diff --git a/src/mdns.h b/src/mdns.h index d5cc9f79..9642bde1 100644 --- a/src/mdns.h +++ b/src/mdns.h @@ -72,6 +72,8 @@ typedef struct getdns_mdns_cached_record_header uint64_t insertion_microsec; uint32_t content_len; uint32_t allocated_length; + /* list of user queries */ + getdns_network_req *netreq_first; } getdns_mdns_cached_record_header; typedef struct getdns_mdns_continuous_query diff --git a/src/types-internal.h b/src/types-internal.h index 162762e4..8f3e7ce2 100644 --- a/src/types-internal.h +++ b/src/types-internal.h @@ -192,12 +192,9 @@ typedef struct getdns_network_req _getdns_rbnode_t node; #ifdef HAVE_MDNS_SUPPORT /* - * for storage in continuous query context. We never - * expect much more than one query per msdn context, - * so no need for RB Tree. + * for storage of continuous query context in hash table of cached results. */ struct getdns_network_req * mdns_netreq_next; - struct getdns_mdns_continuous_query * mdns_continuous_query; #endif /* HAVE_MDNS_SUPPORT */ /* the async_id from unbound */ int unbound_id; From 03307a7b717698bed69855afa83c82bbff6bfa6c Mon Sep 17 00:00:00 2001 From: Christian Huitema Date: Thu, 23 Feb 2017 17:55:31 -0800 Subject: [PATCH 08/25] Code almost complete for the MDNS multicast + cache. Of course, we still need a lot of testing. --- src/mdns.c | 700 ++++++++++++++++++++++++++++++++++++++++++++++++----- src/mdns.h | 11 +- 2 files changed, 644 insertions(+), 67 deletions(-) diff --git a/src/mdns.c b/src/mdns.c index eec9c740..7923585c 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -24,6 +24,7 @@ #include "context.h" #include "general.h" #include "gldns/pkthdr.h" +#include "gldns/rrdef.h" #include "util-internal.h" #include "mdns.h" @@ -165,6 +166,34 @@ struct lruhash_entry* entry, void* data, void* cb_arg) return found; } +/* + * Another missing LRU hash function: demote, move an entry to the bottom + * of the LRU pile. + */ + +static void +lru_demote(struct lruhash* table, struct lruhash_entry* entry) +{ + log_assert(table && entry); + if (entry == table->lru_end) + return; /* nothing to do */ + /* remove from current lru position */ + lru_remove(table, entry); + /* add at end */ + entry->lru_next = NULL; + entry->lru_prev = table->lru_end; + + if (table->lru_end == NULL) + { + table->lru_start = entry; + } + else + { + table->lru_end->lru_next = entry; + } + table->lru_end = entry; +} + /* * For the data part, we want to allocate in rounded increments, so as to reduce the * number of calls to XMALLOC @@ -206,12 +235,298 @@ mdns_util_skip_name(uint8_t *p) return x; } +static size_t +mdns_util_copy_name(uint8_t * message, size_t message_length, size_t current_index, + uint8_t *name, int name_len_max, int name_index, int * name_len) +{ + uint8_t l; + size_t recursive_index; + + *name_len = 0; + while (current_index < message_length && name_index < name_len_max) { + l = message[current_index++]; + if (l == 0) + { + name[name_index++] = 0; + *name_len = name_index; + break; + } + else if (l >= 0xC0) + { + if (current_index < message_length) + { + recursive_index = ((l & 63) << 8) | message[current_index++]; + + (void) mdns_util_copy_name(message, message_length, + recursive_index, name, name_len_max, name_index, name_len); + + if (*name_len == 0) + { + current_index = message_length; + } + } + break; + } + else if (current_index + l < message_length && + name_index + l + 1 < name_len_max) + { + name[name_index++] = l; + + memcpy(name + name_index, message + current_index, l); + name_index += l; + current_index += l; + } + else + { + current_index = message_length; + break; + } + } + + return current_index; +} + static int mdns_util_skip_query(uint8_t *p) { return mdns_util_skip_name(p) + 4; } +/* + * Single copy procedure for many record types + * copy N octets, then the canonical value of the name. + */ +static int +mdns_util_canonical_flags_and_name(uint8_t *message, int message_length, + int record_length, + int current_index, + int nb_octets_to_copy, + uint8_t *buffer, int buffer_max, + uint8_t **actual_record, int *actual_length) +{ + int ret = 0; + int buffer_index = 0; + int name_len = 0; + + if (buffer_max <= nb_octets_to_copy || record_length <= nb_octets_to_copy) + { + /* incorrect buffer */ + ret = GETDNS_RETURN_GENERIC_ERROR; + } + else + { + for (int i = 0; i < nb_octets_to_copy; i++) + { + buffer[buffer_index++] = message[current_index++]; + } + + current_index = mdns_util_copy_name(message, message_length, current_index, buffer, buffer_max, buffer_index, &name_len); + if (current_index == record_length) + { + buffer_index += name_len; + *actual_record = buffer; + *actual_length = buffer_index; + } + else + { + /* something went wrong. */ + ret = GETDNS_RETURN_BAD_DOMAIN_NAME; + } + } + + return ret; +} +/* + * Set record value to canonical form + */ +static int +mdns_util_canonical_record(uint8_t *message, int message_length, + int record_type, int record_class, int record_length, + int record_index, + uint8_t *buffer, int buffer_max, + uint8_t **actual_record, int *actual_length) +{ + int ret = 0; + int current_index = record_index; + int buffer_index = 0; + int name_len = 0; + /* Check whether the record needs canonization */ + *actual_record = message + record_index; + *actual_length = record_length; + + if (record_class != GLDNS_RR_CLASS_IN) + { + /* + * No attempt at canonization outside the IN class. + */ + return 0; + } + + switch (record_type) + { + + case GLDNS_RR_TYPE_NS: + case GLDNS_RR_TYPE_CNAME: + case GLDNS_RR_TYPE_PTR: + case GLDNS_RR_TYPE_MD: + case GLDNS_RR_TYPE_MB: + case GLDNS_RR_TYPE_MF: + case GLDNS_RR_TYPE_MG: + case GLDNS_RR_TYPE_MR: + case GLDNS_RR_TYPE_NSAP_PTR: + /* + * copy the name in canonical form + */ + ret = mdns_util_canonical_flags_and_name(message, message_length, + record_length, current_index, 0, + buffer, buffer_max, actual_record, actual_length); + break; + + case GLDNS_RR_TYPE_A: + case GLDNS_RR_TYPE_AAAA: + case GLDNS_RR_TYPE_TXT: + case GLDNS_RR_TYPE_HINFO: + case GLDNS_RR_TYPE_MINFO: + case GLDNS_RR_TYPE_NULL: + case GLDNS_RR_TYPE_WKS: + case GLDNS_RR_TYPE_X25: + case GLDNS_RR_TYPE_ISDN: + case GLDNS_RR_TYPE_NSAP: + case GLDNS_RR_TYPE_SIG: + case GLDNS_RR_TYPE_KEY: + case GLDNS_RR_TYPE_GPOS: + case GLDNS_RR_TYPE_LOC: + case GLDNS_RR_TYPE_EID: + case GLDNS_RR_TYPE_NIMLOC: + /* leave the content as is, no domain name in content */ + break; + + case GLDNS_RR_TYPE_SRV: + /* + * Copy 6 octets for weight(2), priority(2) and port(2), + * then copy the name. + */ + ret = mdns_util_canonical_flags_and_name(message, message_length, + record_length, current_index, 6, + buffer, buffer_max, actual_record, actual_length); + break; + + case GLDNS_RR_TYPE_MX: + case GLDNS_RR_TYPE_RT: + case GLDNS_RR_TYPE_AFSDB: + /* + * copy two bytes preference or subtype, then + * copy the name in canonical form + */ + ret = mdns_util_canonical_flags_and_name(message, message_length, + record_length, current_index, 2, + buffer, buffer_max, actual_record, actual_length); + break; + + case GLDNS_RR_TYPE_NAPTR: + case GLDNS_RR_TYPE_SOA: + case GLDNS_RR_TYPE_RP: + case GLDNS_RR_TYPE_NXT: + case GLDNS_RR_TYPE_PX: + case GLDNS_RR_TYPE_ATMA: + /* + * Group of record types that are complex, and also + * unexpected in MDNS/DNS-SD operation. Copying the + * record directly will work as long as the sender + * does not attempt name compression. + * TODO: log some kind of error. + */ + break; +#if 0 + + /** RFC2915 */ + GLDNS_RR_TYPE_NAPTR = 35, + /** RFC2230 */ + GLDNS_RR_TYPE_KX = 36, + /** RFC2538 */ + GLDNS_RR_TYPE_CERT = 37, + /** RFC2874 */ + GLDNS_RR_TYPE_A6 = 38, + /** RFC2672 */ + GLDNS_RR_TYPE_DNAME = 39, + /** dnsind-kitchen-sink-02.txt */ + GLDNS_RR_TYPE_SINK = 40, + /** Pseudo OPT record... */ + GLDNS_RR_TYPE_OPT = 41, + /** RFC3123 */ + GLDNS_RR_TYPE_APL = 42, + /** RFC4034, RFC3658 */ + GLDNS_RR_TYPE_DS = 43, + /** SSH Key Fingerprint */ + GLDNS_RR_TYPE_SSHFP = 44, /* RFC 4255 */ + /** IPsec Key */ + GLDNS_RR_TYPE_IPSECKEY = 45, /* RFC 4025 */ + /** DNSSEC */ + GLDNS_RR_TYPE_RRSIG = 46, /* RFC 4034 */ + GLDNS_RR_TYPE_NSEC = 47, /* RFC 4034 */ + GLDNS_RR_TYPE_DNSKEY = 48, /* RFC 4034 */ + + GLDNS_RR_TYPE_DHCID = 49, /* RFC 4701 */ + /* NSEC3 */ + GLDNS_RR_TYPE_NSEC3 = 50, /* RFC 5155 */ + GLDNS_RR_TYPE_NSEC3PARAM = 51, /* RFC 5155 */ + GLDNS_RR_TYPE_NSEC3PARAMS = 51, + GLDNS_RR_TYPE_TLSA = 52, /* RFC 6698 */ + GLDNS_RR_TYPE_SMIMEA = 53, /* draft-ietf-dane-smime, TLSA-like but may + be extended */ + + GLDNS_RR_TYPE_HIP = 55, /* RFC 5205 */ + + /** draft-reid-dnsext-zs */ + GLDNS_RR_TYPE_NINFO = 56, + /** draft-reid-dnsext-rkey */ + GLDNS_RR_TYPE_RKEY = 57, + /** draft-ietf-dnsop-trust-history */ + GLDNS_RR_TYPE_TALINK = 58, + GLDNS_RR_TYPE_CDS = 59, /** RFC 7344 */ + GLDNS_RR_TYPE_CDNSKEY = 60, /** RFC 7344 */ + GLDNS_RR_TYPE_OPENPGPKEY = 61, /* RFC 7929 */ + GLDNS_RR_TYPE_CSYNC = 62, /* RFC 7477 */ + + GLDNS_RR_TYPE_SPF = 99, /* RFC 4408 */ + + GLDNS_RR_TYPE_UINFO = 100, + GLDNS_RR_TYPE_UID = 101, + GLDNS_RR_TYPE_GID = 102, + GLDNS_RR_TYPE_UNSPEC = 103, + + GLDNS_RR_TYPE_NID = 104, /* RFC 6742 */ + GLDNS_RR_TYPE_L32 = 105, /* RFC 6742 */ + GLDNS_RR_TYPE_L64 = 106, /* RFC 6742 */ + GLDNS_RR_TYPE_LP = 107, /* RFC 6742 */ + + /** draft-jabley-dnsext-eui48-eui64-rrtypes */ + GLDNS_RR_TYPE_EUI48 = 108, + GLDNS_RR_TYPE_EUI64 = 109, + + GLDNS_RR_TYPE_TKEY = 249, /* RFC 2930 */ + GLDNS_RR_TYPE_TSIG = 250, + GLDNS_RR_TYPE_IXFR = 251, + GLDNS_RR_TYPE_AXFR = 252, + /** A request for mailbox-related records (MB, MG or MR) */ + GLDNS_RR_TYPE_MAILB = 253, + /** A request for mail agent RRs (Obsolete - see MX) */ + GLDNS_RR_TYPE_MAILA = 254, + /** any type (wildcard) */ + GLDNS_RR_TYPE_ANY = 255, + GLDNS_RR_TYPE_URI = 256, /* RFC 7553 */ + GLDNS_RR_TYPE_CAA = 257, /* RFC 6844 */ +#endif + default: + /* + * Unknown record type. Not expected in MDNS/DNS-SD. Just keep the current value. + * TODO: log some kind of error. + */ + break; + } + + return ret; +} /* * Comparison and other functions required for cache management */ @@ -256,7 +571,8 @@ static int mdns_cache_key_comp(void* vkey1, void* vkey2) /** old keys are deleted. * markdel() is used first. * This function is called: func(key, userarg) -* the userarg is set to the context in which the LRU hash table was created +* the userarg is set to the context in which the LRU hash table was created. +* TODO: is there a need to free the lock in the embedded hash entry structure? */ static void msdn_cache_delkey(void* vkey, void* vcontext) { @@ -270,7 +586,6 @@ static void msdn_cache_delkey(void* vkey, void* vcontext) */ static void msdn_cache_deldata(void* vdata, void* vcontext) { - /* need to terminate the pending queries? */ getdns_mdns_cached_record_header* header = ((getdns_mdns_cached_record_header*)vdata); while (header->netreq_first) @@ -302,6 +617,8 @@ static void msdn_cache_create_key_in_buffer( int record_type, int record_class) { getdns_mdns_cached_key_header * header = (getdns_mdns_cached_key_header*)key; + + memset(key, 0, sizeof(getdns_mdns_cached_key_header)); header->record_type = record_type; header->record_class = record_class; header->name_len = name_len; @@ -447,7 +764,7 @@ mdns_update_cache_ttl_and_prune(struct getdns_context *context, int not_matched_yet = (record_data == NULL) ? 0 : 1; int current_record_match; int last_copied_index; - int current_hole_index; + int current_hole_index = 0; /* * Skip the query @@ -562,13 +879,17 @@ mdns_update_cache_ttl_and_prune(struct getdns_context *context, *new_record = old_record; } - /* - * TODO: if there are no record left standing, return a signal that the cache should be pruned. - */ - return ret; } +static int +mdns_cache_nb_records_in_entry(uint8_t * cached_data) +{ + int message_index = sizeof(getdns_mdns_cached_record_header); + int nb_answers = (cached_data[message_index + 4] << 8) | cached_data[message_index + 5]; + + return nb_answers; +} /* * Add entry function for the MDNS record cache. @@ -606,9 +927,8 @@ mdns_propose_entry_to_cache( key = mdns_cache_create_key(name, name_len, record_type, record_class, context); data = mdns_cache_create_data(name, name_len, record_type, record_class, record_data_len, current_time, context); - new_entry = GETDNS_XMALLOC(context->mf, struct lruhash_entry, 1); - if (key == NULL || data == NULL || new_entry == NULL) + if (key == NULL || data == NULL) { if (key != NULL) { @@ -621,16 +941,11 @@ mdns_propose_entry_to_cache( GETDNS_FREE(context->mf, data); data = NULL; } - - if (new_entry != NULL) - { - GETDNS_FREE(context->mf, new_entry); - new_entry = NULL; - } - } else { + new_entry = &((getdns_mdns_cached_key_header*)key)->entry; + memset(new_entry, 0, sizeof(struct lruhash_entry)); lock_rw_init(new_entry->lock); new_entry->hash = hash; @@ -641,8 +956,13 @@ mdns_propose_entry_to_cache( if (entry != new_entry) { - lock_rw_destroy(new_entry->lock); - GETDNS_FREE(context->mf, new_entry); + /* There was already an entry for this name, which is really weird. + * But it can in theory happen in a race condition. + */ + GETDNS_FREE(context->mf, key); + key = NULL; + GETDNS_FREE(context->mf, data); + data = NULL; } } } @@ -663,41 +983,87 @@ mdns_propose_entry_to_cache( header->netreq_first = netreq; } + /* if the entry is empty, move it to the bottom of the LRU */ + if (mdns_cache_nb_records_in_entry((uint8_t*)(entry->data)) == 0) + { + lru_demote(context->mdns_cache, entry); + } + /* then, unlock the entry */ lock_rw_unlock(entry->lock); - - /* TODO: if the entry was marked for deletion, move it to the bottom of the LRU */ } return ret; } /* - * Compare function for the mdns_continuous_query_by_name_rrtype, - * used in the red-black tree of all ongoing queries. + * Processing of requests after cache update. + * This is coded as synchronous processing, under lock. This is probably wrong. + * It would be better to just collate the responses for now, and + * process the queries out of the loop. */ -static int mdns_cmp_continuous_queries_by_name_rrtype(const void * nqnr1, const void * nqnr2) +static int +mdns_cache_complete_queries( + struct getdns_context *context, + uint8_t * name, int name_len, + int record_type, int record_class) { int ret = 0; - getdns_mdns_continuous_query * qnr1 = (getdns_mdns_continuous_query *)nqnr1; - getdns_mdns_continuous_query * qnr2 = (getdns_mdns_continuous_query *)nqnr2; + size_t required_memory = 0; + uint8_t temp_key[256 + sizeof(getdns_mdns_cached_key_header)]; + hashvalue_type hash; + struct lruhash_entry *entry; + uint8_t *packet; + int packet_length; + getdns_mdns_cached_record_header * header; + getdns_network_req * netreq; - if (qnr1->request_class != qnr2->request_class) + msdn_cache_create_key_in_buffer(temp_key, name, name_len, record_type, record_class); + + + /* TODO: make hash init value a random number in the context, for defense against DOS */ + hash = hashlittle(temp_key, name_len + sizeof(getdns_mdns_cached_key_header), 0xCAC8E); + + entry = lruhash_lookup(context->mdns_cache, hash, temp_key, 1); + + if (entry != NULL && entry->data != NULL) { - ret = (qnr1->request_class < qnr2->request_class) ? -1 : 1; - } - else if (qnr1->request_type != qnr2->request_type) - { - ret = (qnr1->request_type < qnr2->request_type) ? -1 : 1; - } - else if (qnr1->name_len != qnr2->name_len) - { - ret = (qnr1->name_len < qnr2->name_len) ? -1 : 1; - } - else - { - ret = memcmp((void*)qnr1->name, (void*)qnr2->name, qnr1->name_len); + header = (getdns_mdns_cached_record_header *)entry->data; + + packet = ((uint8_t *)entry->data) + sizeof(getdns_mdns_cached_record_header); + packet_length = header->content_len; /* TODO: check that */ + + while ((netreq = header->netreq_first) != NULL) + { + header->netreq_first = netreq->mdns_netreq_next; + netreq->mdns_netreq_next = NULL; + /* TODO: copy the returned value in the response field */ + if (packet_length > netreq->wire_data_sz) + { + /* TODO: allocation. */ + } + + if (netreq->response != NULL) + { + memcpy(netreq->response, packet, packet_length); + /* TODO: process the query */ + netreq->response_len = packet_length; + netreq->debug_end_time = _getdns_get_time_as_uintt64(); + netreq->state = NET_REQ_FINISHED; + _getdns_check_dns_req_complete(netreq->owner); + } + else + { + /* Fail the query? */ + netreq->response_len = 0; + netreq->debug_end_time = _getdns_get_time_as_uintt64(); + netreq->state = NET_REQ_ERRORED; + _getdns_check_dns_req_complete(netreq->owner); + } + } + lock_rw_unlock(entry->lock); } + return ret; } @@ -708,10 +1074,13 @@ static void mdns_udp_multicast_read_cb(void *userarg) { mdns_network_connection * cnx = (mdns_network_connection *)userarg; + uint64_t current_time; ssize_t read; DEBUG_MDNS("%s %-35s: CTX: %p, NET=%d \n", MDNS_DEBUG_MREAD, __FUNCTION__, cnx->context, cnx->addr_mcast.ss_family); + current_time = _getdns_get_time_as_uintt64(); + GETDNS_CLEAR_EVENT( cnx->context->extension, &cnx->event); @@ -725,13 +1094,128 @@ mdns_udp_multicast_read_cb(void *userarg) if (read >= GLDNS_HEADER_SIZE) { /* parse the response, find the relevant queries, submit the records to the cache */ + int opcodeAndflags = cnx->response[2]; + int nb_queries = (cnx->response[4] << 8) | cnx->response[5]; + int nb_responses = (cnx->response[6] << 8) | cnx->response[7]; - /* - netreq->response_len = read; - netreq->debug_end_time = _getdns_get_time_as_uintt64(); - netreq->state = NET_REQ_FINISHED; - _getdns_check_dns_req_complete(dnsreq); - */ + if (opcodeAndflags != 0x84) + { + /* this is not an MDNS answer packet. */ + } + else + { + size_t current_index = 12; + uint8_t name[256]; + int name_len; + int record_type; + int record_class; + int record_ttl; + int record_data_len; + int nb_records = 0; + int nb_queries_skipped = 0; + int start_of_records; + int signalled_records = 0; + + /* + * In normal mDNS operation, there should not be any query here. + * But just in case, we can skip the queries... + */ + while (current_index < read && nb_queries_skipped < nb_queries) + { + current_index += mdns_util_skip_query(&cnx->response[current_index]); + nb_queries_skipped++; + } + start_of_records = current_index; + /* + * Parse the answers and propose them to the cache + */ + + while (current_index < read && nb_records < nb_responses) + { + /* Copy and skip the name */ + current_index = mdns_util_copy_name(cnx->response, read, current_index, + name, sizeof(name), 0, &name_len); + if (current_index + 12 >= read) + { + /* bogus packet.. Should log. */ + current_index = read; + } + else + { + /* Parse the record header */ + record_type = (cnx->response[current_index++] << 8); + record_type |= (cnx->response[current_index++]); + record_class = (cnx->response[current_index++] << 8); + record_class |= (cnx->response[current_index++]); + record_ttl = (cnx->response[current_index++] << 24); + record_ttl |= (cnx->response[current_index++] << 16); + record_ttl |= (cnx->response[current_index++] << 8); + record_ttl |= (cnx->response[current_index++]); + record_data_len = (cnx->response[current_index++] << 8); + record_data_len |= (cnx->response[current_index++]); + + if (current_index + record_data_len < read) + { + /* + * Set the record to canonical form. This is required, since + * MDNS software commonly uses name compression for PTR or SRV records. + */ + int actual_length; + uint8_t *actual_record; + uint8_t buffer[1024]; + + /* TODO: do something in case of canonization failures */ + (void) mdns_util_canonical_record(cnx->response, read, + record_type, record_class, record_data_len, current_index, + buffer, sizeof(buffer), &actual_record, &actual_length); + + + /* Submit to the cache. As a side effect, may signal that a continuous request is done. */ + (void) mdns_propose_entry_to_cache(cnx->context, name, name_len, + record_type, record_class, record_ttl, + actual_record, actual_length, NULL, + current_time); + + current_index += record_data_len; + nb_records++; + } + else + { + /* bogus packet.. Should log. */ + current_index = read; + } + } + } + + /* Go over the queries that were mentioned in the update, and prepare returns. */ + current_index = start_of_records; + while (current_index < read && signalled_records < nb_responses) + { + /* copy the name */ + current_index = mdns_util_copy_name(cnx->response, read, current_index, + name, sizeof(name), 0, &name_len); + if (current_index + 12 >= read) + { + /* bogus packet.. Should log. */ + current_index = read; + } + else + { + /* Parse the record header */ + record_type = (cnx->response[current_index++] << 8); + record_type |= (cnx->response[current_index++]); + record_class = (cnx->response[current_index++] << 8); + record_class |= (cnx->response[current_index++]); + current_index += 4; + record_data_len = (cnx->response[current_index++] << 8); + record_data_len |= (cnx->response[current_index++]); + current_index += record_data_len; + + /* process the pending requests */ + (void)mdns_cache_complete_queries(cnx->context, name, name_len, record_type, record_class); + } + } + } } else { @@ -999,11 +1483,68 @@ static getdns_return_t mdns_initialize_continuous_request(getdns_network_req *ne getdns_dns_req *dnsreq = netreq->owner; struct getdns_context *context = dnsreq->context; - ret = mdns_propose_entry_to_cache(context, dnsreq->name, dnsreq->name_len, - netreq->request_type, dnsreq->request_class, 1, NULL, 0, - netreq, _getdns_get_time_as_uintt64()); + size_t required_memory = 0; + uint8_t temp_key[256 + sizeof(getdns_mdns_cached_key_header)]; + hashvalue_type hash; + struct lruhash_entry *entry; + size_t pkt_len = netreq->response - netreq->query; - /* to do: queue message request to socket */ + msdn_cache_create_key_in_buffer(temp_key, dnsreq->name, dnsreq->name_len, + netreq->request_type, dnsreq->request_class); + + /* TODO: make hash init value a random number in the context, for defense against DOS */ + hash = hashlittle(temp_key, dnsreq->name_len + sizeof(getdns_mdns_cached_key_header), 0xCAC8E); + + entry = lruhash_lookup(context->mdns_cache, hash, temp_key, 1); + + if (entry == NULL) + { + /* + * First, create an entry for the query + */ + + ret = mdns_propose_entry_to_cache(context, dnsreq->name, dnsreq->name_len, + netreq->request_type, dnsreq->request_class, 1, NULL, 0, + netreq, _getdns_get_time_as_uintt64()); + } + else + { + /* + * Check whether the cache entry is recent. + * If yes, just return it, but first update the entry tracking in the cache entry. + */ + + /* + * and unlock the entry! + */ + } + + if (ret == 0) + { + /* If the entry was created less than 1 sec ago, send a query */ + + if (context->mdns_connection_nb <= 0) + { + /* oops, no network! */ + ret = GETDNS_RETURN_GENERIC_ERROR; + } + else + { + /* TODO? Set TTL=255 for compliance with RFC 6762 */ + int fd_index = context->mdns_connection_nb - 1; + + if ((ssize_t)pkt_len != sendto( + context->mdns_connection[fd_index].fd + , (const void *)netreq->query, pkt_len, 0 + , (SOCKADDR*)&context->mdns_connection[fd_index].addr_mcast + , context->mdns_connection[fd_index].addr_mcast_len)) + { + ret = GETDNS_RETURN_GENERIC_ERROR; + } + + /* TODO: update the send query time */ + } + } return ret; } @@ -1103,9 +1644,9 @@ mdns_timeout_cb(void *userarg) -/**************************/ -/* UDP callback functions */ -/**************************/ +/*****************************************/ +/* UDP callback functions for basic MDNS */ +/*****************************************/ static void mdns_udp_read_cb(void *userarg) @@ -1220,21 +1761,50 @@ _getdns_submit_mdns_request(getdns_network_req *netreq) netreq, netreq->request_type); int fd = -1; getdns_dns_req *dnsreq = netreq->owner; + struct getdns_context * context = dnsreq->context; + getdns_return_t ret = 0; - /* Open the UDP socket required for the request */ - if ((fd = socket( - AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) - return -1; - /* TODO: do we need getdns_sock_nonblock(fd); */ + /* + * TO DO: depending on context type, perform basic processing versus full MDNS + */ - /* Schedule the MDNS request */ - netreq->fd = fd; - GETDNS_CLEAR_EVENT(dnsreq->loop, &netreq->event); - GETDNS_SCHEDULE_EVENT( - dnsreq->loop, netreq->fd, dnsreq->context->timeout, - getdns_eventloop_event_init(&netreq->event, netreq, - NULL, mdns_udp_write_cb, mdns_timeout_cb)); - return GETDNS_RETURN_GOOD; + if (context->mdns_extended_support == 2) + { + /* Not initialize yet. Do it know before processing the query */ + ret = mdns_delayed_network_init(context); + + if (ret != 0) + { + return ret; + } + } + + if (context->mdns_extended_support == 1) + { + /* extended DNS support */ + ret = mdns_initialize_continuous_request(netreq); + } + else + { + /* basic MDNS request */ + + /* Open the UDP socket required for the request */ + if ((fd = socket( + AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) + return -1; + /* TODO: do we need getdns_sock_nonblock(fd); */ + + /* Schedule the MDNS request */ + netreq->fd = fd; + GETDNS_CLEAR_EVENT(dnsreq->loop, &netreq->event); + GETDNS_SCHEDULE_EVENT( + dnsreq->loop, netreq->fd, dnsreq->context->timeout, + getdns_eventloop_event_init(&netreq->event, netreq, + NULL, mdns_udp_write_cb, mdns_timeout_cb)); + ret = GETDNS_RETURN_GOOD; + } + + return ret; } /* diff --git a/src/mdns.h b/src/mdns.h index 9642bde1..91f3c2a3 100644 --- a/src/mdns.h +++ b/src/mdns.h @@ -24,6 +24,7 @@ #ifdef HAVE_MDNS_SUPPORT #include "getdns/getdns.h" #include "types-internal.h" +#include "util/storage/lruhash.h" getdns_return_t _getdns_submit_mdns_request(getdns_network_req *netreq); @@ -51,20 +52,26 @@ typedef struct getdns_mdns_known_record /* * Each entry in the hash table is keyed by type, class and name. + * The key structure also contains the LRU hash entry structure. * The data part contains: * - 64 bit time stamp * - 32 bit word describing the record size * - 32 bit word describing teh allocated memory size * - valid DNS response, including 1 query and N answers, 0 AUTH, 0 AD. - * For economy, all answers are encoded using header compression, pointing - * to the name in the query, i.e. offset 12 from beginning of message + * For economy, the names of all answers are encoded using header compression, pointing + * to the name in the query, i.e. offset 12 from beginning of message. + * For stability, the names included in the data part of records are not compressed. */ typedef struct getdns_mdns_cached_key_header { + /* embedded entry, for LRU hash */ + struct lruhash_entry entry; + /* identification */ uint16_t record_type; uint16_t record_class; int name_len; + /* the octets following this structure contain the name */ } getdns_mdns_cached_key_header; typedef struct getdns_mdns_cached_record_header From 40585290811b6eb848121e1a86d9470ffff42a36 Mon Sep 17 00:00:00 2001 From: Christian Huitema Date: Fri, 3 Mar 2017 16:52:02 -0800 Subject: [PATCH 09/25] First version of the MDNS multicast client that actually works. --- src/mdns.c | 513 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 429 insertions(+), 84 deletions(-) diff --git a/src/mdns.c b/src/mdns.c index 7923585c..9efef1ff 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -85,6 +85,10 @@ static uint8_t mdns_suffix_b_e_f_ip6_arpa[] = { 3, 'i', 'p', '6', 4, 'a', 'r', 'p', 'a', 0 }; +#define MDNS_PACKET_INDEX_QCODE 2 +#define MDNS_PACKET_INDEX_QUERY 4 +#define MDNS_PACKET_INDEX_ANSWER 6 + /* * MDNS cache management using LRU Hash. * @@ -607,6 +611,19 @@ static void msdn_cache_deldata(void* vdata, void* vcontext) GETDNS_FREE(((struct getdns_context *) vcontext)->mf, vdata); } +/* + * Read the number of answers in a cached record + */ +static int +mdns_cache_nb_records_in_entry(uint8_t * cached_data) +{ + int message_index = sizeof(getdns_mdns_cached_record_header); + int nb_answers = (cached_data[message_index + MDNS_PACKET_INDEX_ANSWER] << 8) | + cached_data[message_index + MDNS_PACKET_INDEX_ANSWER + 1]; + + return nb_answers; +} + /* * Create a key in preallocated buffer * the allocated size of key should be >= sizeof(getdns_mdns_cached_key_header) + name_len @@ -663,6 +680,7 @@ static uint8_t * mdns_cache_create_data( header->netreq_first = NULL; current_index = sizeof(getdns_mdns_cached_record_header); memset(data + current_index, 0, 12); + data[current_index + MDNS_PACKET_INDEX_QUERY + 1] = 1; /* 1 query present by default */ current_index += 12; memcpy(data + current_index, name, name_len); current_index += name_len; @@ -691,13 +709,13 @@ mdns_add_record_to_cache_entry(struct getdns_context *context, uint32_t record_length = 2 + 2 + 2 + 4 + 2 + record_data_len; uint32_t current_length = header->content_len; /* update the number of records */ - uint8_t *start_answer_code = old_record + sizeof(getdns_mdns_cached_record_header) + 2 + 2; + uint8_t *start_answer_code = old_record + sizeof(getdns_mdns_cached_record_header) + MDNS_PACKET_INDEX_ANSWER; uint16_t nb_answers = (start_answer_code[0] << 8) + start_answer_code[1]; nb_answers++; start_answer_code[0] = (uint8_t)(nb_answers >> 8); start_answer_code[1] = (uint8_t)(nb_answers&0xFF); - /* Update the content length */ + /* Update the content length and reallocate memory if needed */ header->content_len += record_length; if (header->content_len > header->allocated_length) { @@ -722,7 +740,7 @@ mdns_add_record_to_cache_entry(struct getdns_context *context, /* copy the record */ /* First, point name relative to beginning of DNS message */ (*new_record)[current_length++] = 0xC0; - (*new_record)[current_length++] = 0x12; + (*new_record)[current_length++] = 12; /* encode the components of the per record header */ (*new_record)[current_length++] = (uint8_t)((record_type >> 8) & 0xFF); (*new_record)[current_length++] = (uint8_t)((record_type)& 0xFF); @@ -735,6 +753,7 @@ mdns_add_record_to_cache_entry(struct getdns_context *context, (*new_record)[current_length++] = (uint8_t)((record_data_len >> 8) & 0xFF); (*new_record)[current_length++] = (uint8_t)((record_data_len) & 0xFF); memcpy(*new_record + current_length, record_data, record_data_len); + } return ret; @@ -765,14 +784,17 @@ mdns_update_cache_ttl_and_prune(struct getdns_context *context, int current_record_match; int last_copied_index; int current_hole_index = 0; + int record_name_length = 0; + int record_ttl_index = 0; /* * Skip the query */ message_index = sizeof(getdns_mdns_cached_record_header); - nb_answers = (old_record[message_index + 4] << 8) | old_record[message_index + 5]; + nb_answers = (old_record[message_index + MDNS_PACKET_INDEX_ANSWER] << 8) | + old_record[message_index + MDNS_PACKET_INDEX_ANSWER + 1]; nb_answers_left = nb_answers; - answer_index = mdns_util_skip_query(old_record + message_index + 12); + answer_index = message_index + 12 + mdns_util_skip_query(old_record + message_index + 12); last_copied_index = answer_index; /* @@ -780,19 +802,22 @@ mdns_update_cache_ttl_and_prune(struct getdns_context *context, */ for (int i = 0; i < nb_answers; i++) { - current_record_ttl = (old_record[answer_index + 2 + 2 + 2] << 24) - | (old_record[answer_index + 2 + 2 + 2 + 1] << 16) - | (old_record[answer_index + 2 + 2 + 2 + 2] << 8) - | (old_record[answer_index + 2 + 2 + 2 + 3]); + record_name_length = mdns_util_skip_name(old_record + answer_index); + record_ttl_index = answer_index + record_name_length + 2 + 2; - current_record_data_len = (old_record[answer_index + 2 + 2 + 2 + 4] << 8) - | (old_record[answer_index + 2 + 2 + 2 + 4 + 1]); + current_record_ttl = (old_record[record_ttl_index] << 24) + | (old_record[record_ttl_index + 1] << 16) + | (old_record[record_ttl_index + 2] << 8) + | (old_record[record_ttl_index + 3]); - current_record_length = 2 + 2 + 2 + 4 + 2 + current_record_data_len; + current_record_data_len = (old_record[record_ttl_index + 4] << 8) + | (old_record[record_ttl_index + 5]); + + current_record_length = record_name_length + 2 + 2 + 4 + 2 + current_record_data_len; if (not_matched_yet && current_record_data_len == record_data_len && - memcmp(old_record + answer_index + 2 + 2 + 2 + 4 + 2, record_data, record_data_len) == 0) + memcmp(old_record + record_ttl_index + 4 + 2, record_data, record_data_len) == 0) { current_record_match = 1; not_matched_yet = 0; @@ -813,7 +838,7 @@ mdns_update_cache_ttl_and_prune(struct getdns_context *context, } } - if (current_record_ttl != 0) + if (current_record_ttl == 0) { nb_answers_left--; @@ -833,42 +858,43 @@ mdns_update_cache_ttl_and_prune(struct getdns_context *context, last_copied_index += answer_index - current_hole_index; } - /* in all cases, the current hole begins after the message's encoding */ + /* extend the current hole */ current_hole_index = answer_index + current_record_length; } else { /* keeping this record, but updating the TTL */ - old_record[answer_index + 2 + 2 + 2] = (uint8_t)(current_record_ttl >> 24); - old_record[answer_index + 2 + 2 + 2 + 1] = (uint8_t)(current_record_ttl >> 16); - old_record[answer_index + 2 + 2 + 2 + 2] = (uint8_t)(current_record_ttl >> 8); - old_record[answer_index + 2 + 2 + 2 + 3] = (uint8_t)(current_record_ttl); + old_record[record_ttl_index] = (uint8_t)(current_record_ttl >> 24); + old_record[record_ttl_index + 1] = (uint8_t)(current_record_ttl >> 16); + old_record[record_ttl_index + 2] = (uint8_t)(current_record_ttl >> 8); + old_record[record_ttl_index + 3] = (uint8_t)(current_record_ttl); } /* progress to the next record */ answer_index += current_record_length; } /* if necessary, copy the pending data */ - if (current_hole_index != answer_index) + if (current_hole_index != answer_index && current_hole_index != 0) { /* copy the data from hole to last answer */ memmove(old_record + last_copied_index, old_record + current_hole_index, answer_index - current_hole_index); last_copied_index += answer_index - current_hole_index; + answer_index = last_copied_index; } /* if some records were deleted, update the record headers */ if (nb_answers != nb_answers_left) { header->content_len = last_copied_index; - old_record[message_index + 4] = (uint8_t)(nb_answers >> 8); - old_record[message_index + 5] = (uint8_t)(nb_answers); + old_record[message_index + MDNS_PACKET_INDEX_ANSWER] = (uint8_t)(nb_answers_left >> 8); + old_record[message_index + MDNS_PACKET_INDEX_ANSWER + 1] = (uint8_t)(nb_answers_left); } /* * if the update was never seen, ask for an addition */ - if (ttl == 0 && not_matched_yet) + if (ttl > 0 && not_matched_yet) { mdns_add_record_to_cache_entry(context, old_record, new_record, record_type, record_class, ttl, record_data, record_data_len); @@ -882,15 +908,30 @@ mdns_update_cache_ttl_and_prune(struct getdns_context *context, return ret; } -static int -mdns_cache_nb_records_in_entry(uint8_t * cached_data) +/* + * Get a cached entry by name and record type . + */ +static struct lruhash_entry * +mdns_access_cached_entry_by_name( +struct getdns_context *context, + uint8_t * name, int name_len, + int record_type, int record_class) { - int message_index = sizeof(getdns_mdns_cached_record_header); - int nb_answers = (cached_data[message_index + 4] << 8) | cached_data[message_index + 5]; + uint8_t temp_key[256 + sizeof(getdns_mdns_cached_key_header)]; + hashvalue_type hash; + struct lruhash_entry *entry; - return nb_answers; + msdn_cache_create_key_in_buffer(temp_key, name, name_len, record_type, record_class); + + /* TODO: make hash init value a random number in the context, for defense against DOS */ + hash = hashlittle(temp_key, name_len + sizeof(getdns_mdns_cached_key_header), 0xCAC8E); + + entry = lruhash_lookup(context->mdns_cache, hash, temp_key, 1); + + return entry; } + /* * Add entry function for the MDNS record cache. */ @@ -982,11 +1023,16 @@ mdns_propose_entry_to_cache( netreq->mdns_netreq_next = header->netreq_first; header->netreq_first = netreq; } - - /* if the entry is empty, move it to the bottom of the LRU */ - if (mdns_cache_nb_records_in_entry((uint8_t*)(entry->data)) == 0) + else { - lru_demote(context->mdns_cache, entry); + header = (getdns_mdns_cached_record_header *)entry->data; + + /* if the entry is empty, move it to the bottom of the LRU */ + if (mdns_cache_nb_records_in_entry((uint8_t*)(entry->data)) == 0 && + header->netreq_first == NULL) + { + lru_demote(context->mdns_cache, entry); + } } /* then, unlock the entry */ @@ -996,6 +1042,90 @@ mdns_propose_entry_to_cache( return ret; } + +/* + * Serve a request from the cached value + */ +static int +mdns_complete_query_from_cache_entry( + getdns_network_req *netreq, + struct lruhash_entry *entry) +{ + int ret = 0; + uint8_t *packet = ((uint8_t *)entry->data) + sizeof(getdns_mdns_cached_record_header); + getdns_mdns_cached_record_header * header = (getdns_mdns_cached_record_header*)entry->data; + size_t packet_length = header->content_len - sizeof(getdns_mdns_cached_record_header); + getdns_network_req **prev_netreq; + int found = 0; + int nb_answers = mdns_cache_nb_records_in_entry((uint8_t *)entry->data); + + /* Clear the event associated to the query */ + GETDNS_CLEAR_EVENT(netreq->owner->loop, &netreq->event); + + /* remove the completed query from the waiting list */ + prev_netreq = &header->netreq_first; + while (*prev_netreq != NULL) + { + if (*prev_netreq == netreq) + { + *prev_netreq = netreq->mdns_netreq_next; + netreq->mdns_netreq_next = NULL; + found = 1; + break; + } + else + { + prev_netreq = &((*prev_netreq)->mdns_netreq_next); + } + } + + if (found) + { + if (nb_answers == 0) + { + } + else + { + /* copy the returned value in the response field */ + if (packet_length > netreq->wire_data_sz) + { + netreq->response = GETDNS_XREALLOC( + netreq->owner->context->mf, netreq->response, uint8_t, packet_length); + } + + if (netreq->response != NULL) + { + memcpy(netreq->response, packet, packet_length); + + netreq->response[MDNS_PACKET_INDEX_QCODE] = 0x84; + + netreq->response_len = packet_length; + netreq->debug_end_time = _getdns_get_time_as_uintt64(); + netreq->state = NET_REQ_FINISHED; + _getdns_check_dns_req_complete(netreq->owner); + } + else + { + /* Fail the query? */ + netreq->response_len = 0; + netreq->debug_end_time = _getdns_get_time_as_uintt64(); + netreq->state = NET_REQ_ERRORED; + _getdns_check_dns_req_complete(netreq->owner); + } + } + } + else + { + /* Failure */ + netreq->response_len = 0; + netreq->debug_end_time = _getdns_get_time_as_uintt64(); + netreq->state = NET_REQ_ERRORED; + _getdns_check_dns_req_complete(netreq->owner); + } + + return ret; +} + /* * Processing of requests after cache update. * This is coded as synchronous processing, under lock. This is probably wrong. @@ -1010,55 +1140,21 @@ mdns_cache_complete_queries( { int ret = 0; size_t required_memory = 0; - uint8_t temp_key[256 + sizeof(getdns_mdns_cached_key_header)]; - hashvalue_type hash; struct lruhash_entry *entry; - uint8_t *packet; - int packet_length; getdns_mdns_cached_record_header * header; getdns_network_req * netreq; - msdn_cache_create_key_in_buffer(temp_key, name, name_len, record_type, record_class); + entry = mdns_access_cached_entry_by_name(context, name, name_len, record_type, record_class); - - /* TODO: make hash init value a random number in the context, for defense against DOS */ - hash = hashlittle(temp_key, name_len + sizeof(getdns_mdns_cached_key_header), 0xCAC8E); - - entry = lruhash_lookup(context->mdns_cache, hash, temp_key, 1); - - if (entry != NULL && entry->data != NULL) + if (entry != NULL) { - header = (getdns_mdns_cached_record_header *)entry->data; - - packet = ((uint8_t *)entry->data) + sizeof(getdns_mdns_cached_record_header); - packet_length = header->content_len; /* TODO: check that */ - - while ((netreq = header->netreq_first) != NULL) + if (entry->data != NULL) { - header->netreq_first = netreq->mdns_netreq_next; - netreq->mdns_netreq_next = NULL; - /* TODO: copy the returned value in the response field */ - if (packet_length > netreq->wire_data_sz) - { - /* TODO: allocation. */ - } + header = (getdns_mdns_cached_record_header *)entry->data; - if (netreq->response != NULL) + while ((netreq = header->netreq_first) != NULL) { - memcpy(netreq->response, packet, packet_length); - /* TODO: process the query */ - netreq->response_len = packet_length; - netreq->debug_end_time = _getdns_get_time_as_uintt64(); - netreq->state = NET_REQ_FINISHED; - _getdns_check_dns_req_complete(netreq->owner); - } - else - { - /* Fail the query? */ - netreq->response_len = 0; - netreq->debug_end_time = _getdns_get_time_as_uintt64(); - netreq->state = NET_REQ_ERRORED; - _getdns_check_dns_req_complete(netreq->owner); + mdns_complete_query_from_cache_entry(netreq, entry); } } lock_rw_unlock(entry->lock); @@ -1067,6 +1163,57 @@ mdns_cache_complete_queries( return ret; } +/* +* Timeout of multicast MDNS query +*/ +static void +mdns_mcast_timeout_cb(void *userarg) +{ + getdns_network_req *netreq = (getdns_network_req *)userarg; + getdns_dns_req *dnsreq = netreq->owner; + getdns_context *context = dnsreq->context; + + int ret = 0; + size_t required_memory = 0; + uint8_t temp_key[256 + sizeof(getdns_mdns_cached_key_header)]; + hashvalue_type hash; + struct lruhash_entry *entry; + int found = 0; + + DEBUG_MDNS("%s %-35s: MSG: %p\n", + MDNS_DEBUG_CLEANUP, __FUNCTION__, netreq); + + msdn_cache_create_key_in_buffer(temp_key, dnsreq->name, dnsreq->name_len, + netreq->request_type, dnsreq->request_class); + + + /* TODO: make hash init value a random number in the context, for defense against DOS */ + hash = hashlittle(temp_key, dnsreq->name_len + sizeof(getdns_mdns_cached_key_header), 0xCAC8E); + + /* Open the corresponding cache entry */ + entry = lruhash_lookup(context->mdns_cache, hash, temp_key, 1); + + if (entry != NULL) + { + if (entry->data != NULL) + { + /* Remove entry from chain and serve the query */ + found = 1; + mdns_complete_query_from_cache_entry(netreq, entry); + } + lock_rw_unlock(entry->lock); + } + + if (!found) + { + /* Fail the request on timeout */ + netreq->response_len = 0; + netreq->debug_end_time = _getdns_get_time_as_uintt64(); + netreq->state = NET_REQ_ERRORED; + _getdns_check_dns_req_complete(netreq->owner); + } +} + /* * Multicast receive event callback */ @@ -1145,7 +1292,8 @@ mdns_udp_multicast_read_cb(void *userarg) /* Parse the record header */ record_type = (cnx->response[current_index++] << 8); record_type |= (cnx->response[current_index++]); - record_class = (cnx->response[current_index++] << 8); + /* TODO: handle the cache flush bit! */ + record_class = (cnx->response[current_index++] << 8)&0x7F; record_class |= (cnx->response[current_index++]); record_ttl = (cnx->response[current_index++] << 24); record_ttl |= (cnx->response[current_index++] << 16); @@ -1154,7 +1302,7 @@ mdns_udp_multicast_read_cb(void *userarg) record_data_len = (cnx->response[current_index++] << 8); record_data_len |= (cnx->response[current_index++]); - if (current_index + record_data_len < read) + if (current_index + record_data_len <= read) { /* * Set the record to canonical form. This is required, since @@ -1244,7 +1392,7 @@ static int mdns_open_ipv4_multicast(SOCKADDR_STORAGE* mcast_dest, int* mcast_des uint8_t ttl = 255; IP_MREQ mreq4; - memset(&mcast_dest, 0, sizeof(SOCKADDR_STORAGE)); + memset(mcast_dest, 0, sizeof(SOCKADDR_STORAGE)); *mcast_dest_len = 0; memset(&ipv4_dest, 0, sizeof(ipv4_dest)); memset(&ipv4_port, 0, sizeof(ipv4_dest)); @@ -1299,7 +1447,7 @@ static int mdns_open_ipv4_multicast(SOCKADDR_STORAGE* mcast_dest, int* mcast_des if (ret == 0) { - memcpy(&mcast_dest, &ipv4_dest, sizeof(ipv4_dest)); + memcpy(mcast_dest, &ipv4_dest, sizeof(ipv4_dest)); *mcast_dest_len = sizeof(ipv4_dest); } @@ -1316,7 +1464,7 @@ static int mdns_open_ipv6_multicast(SOCKADDR_STORAGE* mcast_dest, int* mcast_des uint8_t ttl = 255; IPV6_MREQ mreq6; - memset(&mcast_dest, 0, sizeof(SOCKADDR_STORAGE)); + memset(mcast_dest, 0, sizeof(SOCKADDR_STORAGE)); *mcast_dest_len = 0; memset(&ipv6_dest, 0, sizeof(ipv6_dest)); memset(&ipv6_port, 0, sizeof(ipv6_dest)); @@ -1374,7 +1522,7 @@ static int mdns_open_ipv6_multicast(SOCKADDR_STORAGE* mcast_dest, int* mcast_des if (ret == 0) { - memcpy(&mcast_dest, &ipv6_dest, sizeof(ipv6_dest)); + memcpy(mcast_dest, &ipv6_dest, sizeof(ipv6_dest)); *mcast_dest_len = sizeof(ipv6_dest); } return fd6; @@ -1414,9 +1562,11 @@ static getdns_return_t mdns_delayed_network_init(struct getdns_context *context) context->mdns_connection[0].fd = mdns_open_ipv4_multicast( &context->mdns_connection[0].addr_mcast , &context->mdns_connection[0].addr_mcast_len); + context->mdns_connection[0].context = context; context->mdns_connection[1].fd = mdns_open_ipv6_multicast( - &context->mdns_connection[0].addr_mcast - , &context->mdns_connection[0].addr_mcast_len); + &context->mdns_connection[1].addr_mcast + , &context->mdns_connection[1].addr_mcast_len); + context->mdns_connection[1].context = context; if (context->mdns_connection[0].fd == -1 || context->mdns_connection[1].fd == -1) @@ -1479,7 +1629,7 @@ static getdns_return_t mdns_delayed_network_init(struct getdns_context *context) */ static getdns_return_t mdns_initialize_continuous_request(getdns_network_req *netreq) { - int ret = 0; + getdns_return_t ret = 0; getdns_dns_req *dnsreq = netreq->owner; struct getdns_context *context = dnsreq->context; @@ -1521,6 +1671,15 @@ static getdns_return_t mdns_initialize_continuous_request(getdns_network_req *ne if (ret == 0) { + /* If the query is not actually complete, are a per query timer. */ + if (netreq->state < NET_REQ_FINISHED) + { + GETDNS_CLEAR_EVENT(dnsreq->loop, &netreq->event); + GETDNS_SCHEDULE_EVENT( + dnsreq->loop, -1, dnsreq->context->timeout, + getdns_eventloop_event_init(&netreq->event, netreq, + NULL, NULL, mdns_mcast_timeout_cb)); + } /* If the entry was created less than 1 sec ago, send a query */ if (context->mdns_connection_nb <= 0) @@ -1532,12 +1691,13 @@ static getdns_return_t mdns_initialize_continuous_request(getdns_network_req *ne { /* TODO? Set TTL=255 for compliance with RFC 6762 */ int fd_index = context->mdns_connection_nb - 1; - - if ((ssize_t)pkt_len != sendto( + int sent = sendto( context->mdns_connection[fd_index].fd , (const void *)netreq->query, pkt_len, 0 , (SOCKADDR*)&context->mdns_connection[fd_index].addr_mcast - , context->mdns_connection[fd_index].addr_mcast_len)) + , context->mdns_connection[fd_index].addr_mcast_len); + + if (pkt_len != sent) { ret = GETDNS_RETURN_GENERIC_ERROR; } @@ -1900,3 +2060,188 @@ _getdns_mdns_namespace_check( } #endif /* HAVE_MDNS_SUPPORT */ + +#ifdef MDNS_UNIT_TEST + +/* + * Test adding data to the LRU Cache + */ + +static BYTE mdns_exampleRRAM[] = { + 0, 0, /* Transaction ID = 0 */ + 0x84, 0, /* Answer: QR=1 (80), AA=1 (04) */ + 0, 0, /* QD Count = 0 */ + 0, 1, /* AN Count = 1 */ + 0, 0, /* NS Count = 0 */ + 0, 0, /* AD Count = 0 */ + 7, /* length of "example" name part */ + 'e', 'x', 'a', 'm', 'p', 'l', 'e', + 5, /* length of "local" name part */ + 'l', 'o', 'c', 'a', 'l', + 0, /* length of the root name part */ + 0, 1, /* QTYPE = 1, A record */ + 0, 1, /* QCLASS = 1, IN */ + 0, 0, 0, 255, /* TTL: 255 sec */ + 0, 4, /* length of RDATA */ + 10, 0, 0, 1 /* Value of RDATA (some IPv4 address) */ +}; + + +uint8_t mdns_test_name[] = { + 7, /* length of "example" name part */ + 't', 'e', 's', 't', 'i', 'n', 'g', + 5, /* length of "local" name part */ + 'l', 'o', 'c', 'a', 'l', + 0, /* length of the root name part */ +}; + +uint8_t mdns_test_first_address[4] = { 10, 0, 0, 1 }; +uint8_t mdns_test_second_address[4] = { 10, 0, 0, 2 }; +uint8_t mdns_test_third_address[4] = { 10, 0, 0, 3 }; + +int mdns_finalize_lru_test(struct getdns_context* context, + uint8_t * name, int name_len, int record_type, int record_class, + int expected_nb_records, + uint8_t * buffer, size_t buffer_max, size_t* entry_length) +{ + int ret = 0; + /* verify that the entry is there */ + struct lruhash_entry * entry = + mdns_access_cached_entry_by_name(context, name, name_len, record_type, record_class); + + + *entry_length = 0; + + if (entry == NULL) + { + if (expected_nb_records != 0) + ret = -1; + } + else + { + int nbanswers = mdns_cache_nb_records_in_entry((uint8_t*)entry->data); + if (nbanswers != expected_nb_records) + { + ret = -2; + } + + if (buffer != NULL) + { + getdns_mdns_cached_record_header * header = + (getdns_mdns_cached_record_header*)entry->data; + size_t record_length = header->content_len - sizeof(getdns_mdns_cached_record_header); + + if (record_length > buffer_max) + { + ret = -3; + } + else + { + memcpy(buffer, ((uint8_t *)entry->data) + sizeof(getdns_mdns_cached_record_header), + record_length); + *entry_length = record_length; + } + } + + lock_rw_unlock(entry->lock); + } + + return ret; +} + +int mdns_addition_test(struct getdns_context* context, + uint8_t * buffer, size_t buffer_max, size_t* entry_length) +{ + int ret = + mdns_propose_entry_to_cache(context, mdns_test_name, sizeof(mdns_test_name), 1, 1, 255, + mdns_test_first_address, 4, NULL, _getdns_get_time_as_uintt64()); + + if (ret == 0) + { + ret = mdns_finalize_lru_test(context, &mdns_exampleRRAM[12], 15, 1, 1, + 1, buffer, buffer_max, entry_length); + } + + return ret; +} + +int mdns_addition_test2(struct getdns_context* context, + uint8_t * buffer, size_t buffer_max, size_t* entry_length) +{ + int ret = + mdns_propose_entry_to_cache(context, mdns_test_name, sizeof(mdns_test_name), 1, 1, 255, + mdns_test_first_address, 4, NULL, _getdns_get_time_as_uintt64()); + + if (ret == 0) + { + /* add a second entry, with a different value */ + ret = + mdns_propose_entry_to_cache(context, mdns_test_name, sizeof(mdns_test_name), 1, 1, 255, + mdns_test_second_address, 4, NULL, _getdns_get_time_as_uintt64()); + } + + if (ret == 0) + { + /* add a third entry, with a different value */ + ret = + mdns_propose_entry_to_cache(context, mdns_test_name, sizeof(mdns_test_name), 1, 1, 255, + mdns_test_third_address, 4, NULL, _getdns_get_time_as_uintt64()); + } + + if (ret == 0) + { + ret = mdns_finalize_lru_test(context, mdns_test_name, sizeof(mdns_test_name), 1, 1, + 3, buffer, buffer_max, entry_length); + } + + return ret; +} + +int mdns_deletion_test(struct getdns_context* context, + uint8_t * buffer, size_t buffer_max, size_t* entry_length) +{ + /* insert data with TTL = 0 to trigger suppression */ + int ret = + mdns_propose_entry_to_cache(context, mdns_test_name, sizeof(mdns_test_name), 1, 1, 0, + mdns_test_second_address, 4, NULL, _getdns_get_time_as_uintt64()); + + if (ret == 0) + { + ret = mdns_finalize_lru_test(context, mdns_test_name, sizeof(mdns_test_name), 1, 1, + 2, buffer, buffer_max, entry_length); + } + + return ret; +} + +int mdns_deletion_test2(struct getdns_context* context, + uint8_t * buffer, size_t buffer_max, size_t* entry_length) +{ + /* insert data with TTL = 0 to trigger suppression */ + int ret = + mdns_propose_entry_to_cache(context, mdns_test_name, sizeof(mdns_test_name), 1, 1, 0, + mdns_test_first_address, 4, NULL, _getdns_get_time_as_uintt64()); + + if (ret == 0) + { + ret = + mdns_propose_entry_to_cache(context, mdns_test_name, sizeof(mdns_test_name), 1, 1, 0, + mdns_test_third_address, 4, NULL, _getdns_get_time_as_uintt64()); + } + + if (ret == 0) + { + ret = mdns_finalize_lru_test(context, mdns_test_name, sizeof(mdns_test_name), 1, 1, + 0, buffer, buffer_max, entry_length); + } + + return ret; +} + + +int mdns_test_prepare(struct getdns_context* context) +{ + return mdns_delayed_network_init(context); +} + +#endif \ No newline at end of file From 028dd0bf3c8be0824fb49d798e8a4b9acfe9c94f Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Wed, 8 Mar 2017 21:25:39 +0100 Subject: [PATCH 10/25] Configure option to enable draft mdns support --- configure.ac | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 555b7792..7e8273e9 100644 --- a/configure.ac +++ b/configure.ac @@ -481,9 +481,10 @@ case "$enable_dsa" in ;; esac -AC_ARG_ENABLE(all-drafts, AC_HELP_STRING([--enable-all-drafts], [No drafts in this release])) +AC_ARG_ENABLE(all-drafts, AC_HELP_STRING([--enable-all-drafts], [Enables the draft mdns client support])) case "$enable_all_drafts" in yes) + AC_DEFINE_UNQUOTED([HAVE_MDNS_SUPPORT], [1], [Define this to enable the draft mdns client support.]) ;; no|*) ;; @@ -514,6 +515,15 @@ AC_DEFINE_UNQUOTED([EDNS_COOKIE_ROLLOVER_TIME], [(24 * 60 * 60)], [How often the AC_DEFINE_UNQUOTED([MAXIMUM_UPSTREAM_OPTION_SPACE], [3000], [limit for dynamically-generated DNS options]) AC_DEFINE_UNQUOTED([EDNS_PADDING_OPCODE], [12], [The edns padding option code.]) +AC_ARG_ENABLE(draft-mdns-support, AC_HELP_STRING([--enable-draft-mdns-support], [Enable draft mdns client support])) +case "$enable_draft_mdns_support" in + yes) + AC_DEFINE_UNQUOTED([HAVE_MDNS_SUPPORT], [1], [Define this to enable the draft mdns client support.]) + ;; + no|*) + ;; +esac + my_with_libunbound=1 AC_ARG_ENABLE(stub-only, AC_HELP_STRING([--enable-stub-only], [Restricts resolution modes to STUB (which will be the default mode). Removes the libunbound dependency.])) case "$enable_stub_only" in From ec685e900d5151dd805e1510e4ee60b9cb785b72 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Wed, 8 Mar 2017 22:10:22 +0100 Subject: [PATCH 11/25] Map rbtree symbols --- src/util/rbtree.c | 152 ++++++++++++++-------------- src/util/rbtree.h | 232 +++++++++---------------------------------- src/util/ub/rbtree.h | 192 +++++++++++++++++++++++++++++++++++ 3 files changed, 316 insertions(+), 260 deletions(-) create mode 100644 src/util/ub/rbtree.h diff --git a/src/util/rbtree.c b/src/util/rbtree.c index c52be727..f031c9a1 100644 --- a/src/util/rbtree.c +++ b/src/util/rbtree.c @@ -40,8 +40,8 @@ */ #include "config.h" -#include "util/log.h" -#include "util/fptr_wlist.h" +#include "log.h" +#include "fptr_wlist.h" #include "util/rbtree.h" /** Node colour black */ @@ -50,7 +50,7 @@ #define RED 1 /** the NULL node, global alloc */ -_getdns_rbnode_t _getdns_rbtree_null_node = { +rbnode_type rbtree_null_node = { RBTREE_NULL, /* Parent. */ RBTREE_NULL, /* Left. */ RBTREE_NULL, /* Right. */ @@ -59,13 +59,14 @@ _getdns_rbnode_t _getdns_rbtree_null_node = { }; /** rotate subtree left (to preserve redblack property) */ -static void _getdns_rbtree_rotate_left(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node); +static void rbtree_rotate_left(rbtree_type *rbtree, rbnode_type *node); /** rotate subtree right (to preserve redblack property) */ -static void _getdns_rbtree_rotate_right(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node); +static void rbtree_rotate_right(rbtree_type *rbtree, rbnode_type *node); /** Fixup node colours when insert happened */ -static void _getdns_rbtree_insert_fixup(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node); +static void rbtree_insert_fixup(rbtree_type *rbtree, rbnode_type *node); /** Fixup node colours when delete happened */ -static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode_t* child, _getdns_rbnode_t* child_parent); +static void rbtree_delete_fixup(rbtree_type* rbtree, rbnode_type* child, + rbnode_type* child_parent); /* * Creates a new red black tree, initializes and returns a pointer to it. @@ -73,25 +74,25 @@ static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode * Return NULL on failure. * */ -_getdns_rbtree_t * -_getdns_rbtree_create (int (*cmpf)(const void *, const void *)) +rbtree_type * +rbtree_create (int (*cmpf)(const void *, const void *)) { - _getdns_rbtree_t *rbtree; + rbtree_type *rbtree; /* Allocate memory for it */ - rbtree = (_getdns_rbtree_t *) malloc(sizeof(_getdns_rbtree_t)); + rbtree = (rbtree_type *) malloc(sizeof(rbtree_type)); if (!rbtree) { return NULL; } /* Initialize it */ - _getdns_rbtree_init(rbtree, cmpf); + rbtree_init(rbtree, cmpf); return rbtree; } void -_getdns_rbtree_init(_getdns_rbtree_t *rbtree, int (*cmpf)(const void *, const void *)) +rbtree_init(rbtree_type *rbtree, int (*cmpf)(const void *, const void *)) { /* Initialize it */ rbtree->root = RBTREE_NULL; @@ -104,9 +105,9 @@ _getdns_rbtree_init(_getdns_rbtree_t *rbtree, int (*cmpf)(const void *, const vo * */ static void -_getdns_rbtree_rotate_left(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node) +rbtree_rotate_left(rbtree_type *rbtree, rbnode_type *node) { - _getdns_rbnode_t *right = node->right; + rbnode_type *right = node->right; node->right = right->left; if (right->left != RBTREE_NULL) right->left->parent = node; @@ -131,9 +132,9 @@ _getdns_rbtree_rotate_left(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node) * */ static void -_getdns_rbtree_rotate_right(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node) +rbtree_rotate_right(rbtree_type *rbtree, rbnode_type *node) { - _getdns_rbnode_t *left = node->left; + rbnode_type *left = node->left; node->left = left->right; if (left->right != RBTREE_NULL) left->right->parent = node; @@ -154,9 +155,9 @@ _getdns_rbtree_rotate_right(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node) } static void -_getdns_rbtree_insert_fixup(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node) +rbtree_insert_fixup(rbtree_type *rbtree, rbnode_type *node) { - _getdns_rbnode_t *uncle; + rbnode_type *uncle; /* While not at the root and need fixing... */ while (node != rbtree->root && node->parent->color == RED) { @@ -179,12 +180,12 @@ _getdns_rbtree_insert_fixup(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node) /* Are we the right child? */ if (node == node->parent->right) { node = node->parent; - _getdns_rbtree_rotate_left(rbtree, node); + rbtree_rotate_left(rbtree, node); } /* Now we're the left child, repaint and rotate... */ node->parent->color = BLACK; node->parent->parent->color = RED; - _getdns_rbtree_rotate_right(rbtree, node->parent->parent); + rbtree_rotate_right(rbtree, node->parent->parent); } } else { uncle = node->parent->parent->left; @@ -204,12 +205,12 @@ _getdns_rbtree_insert_fixup(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node) /* Are we the right child? */ if (node == node->parent->left) { node = node->parent; - _getdns_rbtree_rotate_right(rbtree, node); + rbtree_rotate_right(rbtree, node); } /* Now we're the right child, repaint and rotate... */ node->parent->color = BLACK; node->parent->parent->color = RED; - _getdns_rbtree_rotate_left(rbtree, node->parent->parent); + rbtree_rotate_left(rbtree, node->parent->parent); } } } @@ -223,17 +224,17 @@ _getdns_rbtree_insert_fixup(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *node) * Returns NULL on failure or the pointer to the newly added node * otherwise. */ -_getdns_rbnode_t * -_getdns_rbtree_insert (_getdns_rbtree_t *rbtree, _getdns_rbnode_t *data) +rbnode_type * +rbtree_insert (rbtree_type *rbtree, rbnode_type *data) { /* XXX Not necessary, but keeps compiler quiet... */ int r = 0; /* We start at the root of the tree */ - _getdns_rbnode_t *node = rbtree->root; - _getdns_rbnode_t *parent = RBTREE_NULL; + rbnode_type *node = rbtree->root; + rbnode_type *parent = RBTREE_NULL; - fptr_ok(fptr_whitelist__getdns_rbtree_cmp(rbtree->cmp)); + fptr_ok(fptr_whitelist_rbtree_cmp(rbtree->cmp)); /* Lets find the new parent... */ while (node != RBTREE_NULL) { /* Compare two keys, do we have a duplicate? */ @@ -267,7 +268,7 @@ _getdns_rbtree_insert (_getdns_rbtree_t *rbtree, _getdns_rbnode_t *data) } /* Fix up the red-black properties... */ - _getdns_rbtree_insert_fixup(rbtree, data); + rbtree_insert_fixup(rbtree, data); return data; } @@ -276,12 +277,12 @@ _getdns_rbtree_insert (_getdns_rbtree_t *rbtree, _getdns_rbnode_t *data) * Searches the red black tree, returns the data if key is found or NULL otherwise. * */ -_getdns_rbnode_t * -_getdns_rbtree_search (_getdns_rbtree_t *rbtree, const void *key) +rbnode_type * +rbtree_search (rbtree_type *rbtree, const void *key) { - _getdns_rbnode_t *node; + rbnode_type *node; - if (_getdns_rbtree_find_less_equal(rbtree, key, &node)) { + if (rbtree_find_less_equal(rbtree, key, &node)) { return node; } else { return NULL; @@ -295,13 +296,14 @@ static void swap_int8(uint8_t* x, uint8_t* y) } /** helpers for delete: swap node pointers */ -static void swap_np(_getdns_rbnode_t** x, _getdns_rbnode_t** y) +static void swap_np(rbnode_type** x, rbnode_type** y) { - _getdns_rbnode_t* t = *x; *x = *y; *y = t; + rbnode_type* t = *x; *x = *y; *y = t; } /** Update parent pointers of child trees of 'parent' */ -static void change_parent_ptr(_getdns_rbtree_t* rbtree, _getdns_rbnode_t* parent, _getdns_rbnode_t* old, _getdns_rbnode_t* new) +static void change_parent_ptr(rbtree_type* rbtree, rbnode_type* parent, + rbnode_type* old, rbnode_type* new) { if(parent == RBTREE_NULL) { @@ -315,30 +317,31 @@ static void change_parent_ptr(_getdns_rbtree_t* rbtree, _getdns_rbnode_t* parent if(parent->right == old) parent->right = new; } /** Update parent pointer of a node 'child' */ -static void change_child_ptr(_getdns_rbnode_t* child, _getdns_rbnode_t* old, _getdns_rbnode_t* new) +static void change_child_ptr(rbnode_type* child, rbnode_type* old, + rbnode_type* new) { if(child == RBTREE_NULL) return; log_assert(child->parent == old || child->parent == new); if(child->parent == old) child->parent = new; } -_getdns_rbnode_t* -_getdns_rbtree_delete(_getdns_rbtree_t *rbtree, const void *key) +rbnode_type* +rbtree_delete(rbtree_type *rbtree, const void *key) { - _getdns_rbnode_t *to_delete; - _getdns_rbnode_t *child; - if((to_delete = _getdns_rbtree_search(rbtree, key)) == 0) return 0; + rbnode_type *to_delete; + rbnode_type *child; + if((to_delete = rbtree_search(rbtree, key)) == 0) return 0; rbtree->count--; /* make sure we have at most one non-leaf child */ if(to_delete->left != RBTREE_NULL && to_delete->right != RBTREE_NULL) { /* swap with smallest from right subtree (or largest from left) */ - _getdns_rbnode_t *smright = to_delete->right; + rbnode_type *smright = to_delete->right; while(smright->left != RBTREE_NULL) smright = smright->left; /* swap the smright and to_delete elements in the tree, - * but the _getdns_rbnode_t is first part of user data struct + * but the rbnode_type is first part of user data struct * so cannot just swap the keys and data pointers. Instead * readjust the pointers left,right,parent */ @@ -390,7 +393,7 @@ _getdns_rbtree_delete(_getdns_rbtree_t *rbtree, const void *key) /* change child to BLACK, removing a RED node is no problem */ if(child!=RBTREE_NULL) child->color = BLACK; } - else _getdns_rbtree_delete_fixup(rbtree, child, to_delete->parent); + else rbtree_delete_fixup(rbtree, child, to_delete->parent); /* unlink completely */ to_delete->parent = RBTREE_NULL; @@ -400,9 +403,10 @@ _getdns_rbtree_delete(_getdns_rbtree_t *rbtree, const void *key) return to_delete; } -static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode_t* child, _getdns_rbnode_t* child_parent) +static void rbtree_delete_fixup(rbtree_type* rbtree, rbnode_type* child, + rbnode_type* child_parent) { - _getdns_rbnode_t* sibling; + rbnode_type* sibling; int go_up = 1; /* determine sibling to the node that is one-black short */ @@ -422,8 +426,8 @@ static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode child_parent->color = RED; sibling->color = BLACK; if(child_parent->right == child) - _getdns_rbtree_rotate_right(rbtree, child_parent); - else _getdns_rbtree_rotate_left(rbtree, child_parent); + rbtree_rotate_right(rbtree, child_parent); + else rbtree_rotate_left(rbtree, child_parent); /* new sibling after rotation */ if(child_parent->right == child) sibling = child_parent->left; else sibling = child_parent->right; @@ -468,7 +472,7 @@ static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode { sibling->color = RED; sibling->right->color = BLACK; - _getdns_rbtree_rotate_left(rbtree, sibling); + rbtree_rotate_left(rbtree, sibling); /* new sibling after rotation */ if(child_parent->right == child) sibling = child_parent->left; else sibling = child_parent->right; @@ -480,7 +484,7 @@ static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode { sibling->color = RED; sibling->left->color = BLACK; - _getdns_rbtree_rotate_right(rbtree, sibling); + rbtree_rotate_right(rbtree, sibling); /* new sibling after rotation */ if(child_parent->right == child) sibling = child_parent->left; else sibling = child_parent->right; @@ -493,21 +497,22 @@ static void _getdns_rbtree_delete_fixup(_getdns_rbtree_t* rbtree, _getdns_rbnode { log_assert(sibling->left->color == RED); sibling->left->color = BLACK; - _getdns_rbtree_rotate_right(rbtree, child_parent); + rbtree_rotate_right(rbtree, child_parent); } else { log_assert(sibling->right->color == RED); sibling->right->color = BLACK; - _getdns_rbtree_rotate_left(rbtree, child_parent); + rbtree_rotate_left(rbtree, child_parent); } } int -_getdns_rbtree_find_less_equal(_getdns_rbtree_t *rbtree, const void *key, _getdns_rbnode_t **result) +rbtree_find_less_equal(rbtree_type *rbtree, const void *key, + rbnode_type **result) { int r; - _getdns_rbnode_t *node; + rbnode_type *node; log_assert(result); @@ -515,7 +520,7 @@ _getdns_rbtree_find_less_equal(_getdns_rbtree_t *rbtree, const void *key, _getdn node = rbtree->root; *result = NULL; - fptr_ok(fptr_whitelist__getdns_rbtree_cmp(rbtree->cmp)); + fptr_ok(fptr_whitelist_rbtree_cmp(rbtree->cmp)); /* While there are children... */ while (node != RBTREE_NULL) { @@ -540,19 +545,19 @@ _getdns_rbtree_find_less_equal(_getdns_rbtree_t *rbtree, const void *key, _getdn * Finds the first element in the red black tree * */ -_getdns_rbnode_t * -_getdns_rbtree_first (_getdns_rbtree_t *rbtree) +rbnode_type * +rbtree_first (rbtree_type *rbtree) { - _getdns_rbnode_t *node; + rbnode_type *node; for (node = rbtree->root; node->left != RBTREE_NULL; node = node->left); return node; } -_getdns_rbnode_t * -_getdns_rbtree_last (_getdns_rbtree_t *rbtree) +rbnode_type * +rbtree_last (rbtree_type *rbtree) { - _getdns_rbnode_t *node; + rbnode_type *node; for (node = rbtree->root; node->right != RBTREE_NULL; node = node->right); return node; @@ -562,10 +567,10 @@ _getdns_rbtree_last (_getdns_rbtree_t *rbtree) * Returns the next node... * */ -_getdns_rbnode_t * -_getdns_rbtree_next (_getdns_rbnode_t *node) +rbnode_type * +rbtree_next (rbnode_type *node) { - _getdns_rbnode_t *parent; + rbnode_type *parent; if (node->right != RBTREE_NULL) { /* One right, then keep on going left... */ @@ -581,10 +586,10 @@ _getdns_rbtree_next (_getdns_rbnode_t *node) return node; } -_getdns_rbnode_t * -_getdns_rbtree_previous(_getdns_rbnode_t *node) +rbnode_type * +rbtree_previous(rbnode_type *node) { - _getdns_rbnode_t *parent; + rbnode_type *parent; if (node->left != RBTREE_NULL) { /* One left, then keep on going right... */ @@ -602,19 +607,20 @@ _getdns_rbtree_previous(_getdns_rbnode_t *node) /** recursive descent traverse */ static void -_getdns_traverse_post(void (*func)(_getdns_rbnode_t*, void*), void* arg, _getdns_rbnode_t* node) +traverse_post(void (*func)(rbnode_type*, void*), void* arg, rbnode_type* node) { if(!node || node == RBTREE_NULL) return; /* recurse */ - _getdns_traverse_post(func, arg, node->left); - _getdns_traverse_post(func, arg, node->right); + traverse_post(func, arg, node->left); + traverse_post(func, arg, node->right); /* call user func */ (*func)(node, arg); } void -_getdns_traverse_postorder(_getdns_rbtree_t* tree, void (*func)(_getdns_rbnode_t*, void*), void* arg) +traverse_postorder(rbtree_type* tree, void (*func)(rbnode_type*, void*), + void* arg) { - _getdns_traverse_post(func, arg, tree->root); + traverse_post(func, arg, tree->root); } diff --git a/src/util/rbtree.h b/src/util/rbtree.h index aa620e1c..7772ffda 100644 --- a/src/util/rbtree.h +++ b/src/util/rbtree.h @@ -1,192 +1,50 @@ +/** + * + * \file rbtree.h + * /brief Alternative symbol names for unbound's rbtree.h + * + */ /* - * rbtree.h -- generic red-black tree + * Copyright (c) 2017, NLnet Labs, the getdns team + * All rights reserved. * - * Copyright (c) 2001-2007, NLnet Labs. All rights reserved. - * - * This software is open source. - * * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * Neither the name of the NLNET LABS nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the names of the copyright holders nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - -/** - * \file - * Red black tree. Implementation taken from NSD 3.0.5, adjusted for use - * in unbound (memory allocation, logging and so on). - */ - -#ifndef UTIL_RBTREE_H_ -#define UTIL_RBTREE_H_ - -/** - * This structure must be the first member of the data structure in - * the rbtree. This allows easy casting between an _getdns_rbnode_t and the - * user data (poor man's inheritance). - */ -typedef struct _getdns_rbnode_t _getdns_rbnode_t; -/** - * The _getdns_rbnode_t struct definition. - */ -struct _getdns_rbnode_t { - /** parent in rbtree, RBTREE_NULL for root */ - _getdns_rbnode_t *parent; - /** left node (smaller items) */ - _getdns_rbnode_t *left; - /** right node (larger items) */ - _getdns_rbnode_t *right; - /** pointer to sorting key */ - const void *key; - /** colour of this node */ - uint8_t color; -}; - -/** The nullpointer, points to empty node */ -#define RBTREE_NULL &_getdns_rbtree_null_node -/** the global empty node */ -extern _getdns_rbnode_t _getdns_rbtree_null_node; - -/** An entire red black tree */ -typedef struct _getdns_rbtree_t _getdns_rbtree_t; -/** definition for tree struct */ -struct _getdns_rbtree_t { - /** The root of the red-black tree */ - _getdns_rbnode_t *root; - - /** The number of the nodes in the tree */ - size_t count; - - /** - * Key compare function. <0,0,>0 like strcmp. - * Return 0 on two NULL ptrs. - */ - int (*cmp) (const void *, const void *); -}; - -/** - * Create new tree (malloced) with given key compare function. - * @param cmpf: compare function (like strcmp) takes pointers to two keys. - * @return: new tree, empty. - */ -_getdns_rbtree_t *_getdns_rbtree_create(int (*cmpf)(const void *, const void *)); - -/** - * Init a new tree (malloced by caller) with given key compare function. - * @param rbtree: uninitialised memory for new tree, returned empty. - * @param cmpf: compare function (like strcmp) takes pointers to two keys. - */ -void _getdns_rbtree_init(_getdns_rbtree_t *rbtree, int (*cmpf)(const void *, const void *)); - -/** - * Insert data into the tree. - * @param rbtree: tree to insert to. - * @param data: element to insert. - * @return: data ptr or NULL if key already present. - */ -_getdns_rbnode_t *_getdns_rbtree_insert(_getdns_rbtree_t *rbtree, _getdns_rbnode_t *data); - -/** - * Delete element from tree. - * @param rbtree: tree to delete from. - * @param key: key of item to delete. - * @return: node that is now unlinked from the tree. User to delete it. - * returns 0 if node not present - */ -_getdns_rbnode_t *_getdns_rbtree_delete(_getdns_rbtree_t *rbtree, const void *key); - -/** - * Find key in tree. Returns NULL if not found. - * @param rbtree: tree to find in. - * @param key: key that must match. - * @return: node that fits or NULL. - */ -_getdns_rbnode_t *_getdns_rbtree_search(_getdns_rbtree_t *rbtree, const void *key); - -/** - * Find, but match does not have to be exact. - * @param rbtree: tree to find in. - * @param key: key to find position of. - * @param result: set to the exact node if present, otherwise to element that - * precedes the position of key in the tree. NULL if no smaller element. - * @return: true if exact match in result. Else result points to <= element, - * or NULL if key is smaller than the smallest key. - */ -int _getdns_rbtree_find_less_equal(_getdns_rbtree_t *rbtree, const void *key, - _getdns_rbnode_t **result); - -/** - * Returns first (smallest) node in the tree - * @param rbtree: tree - * @return: smallest element or NULL if tree empty. - */ -_getdns_rbnode_t *_getdns_rbtree_first(_getdns_rbtree_t *rbtree); - -/** - * Returns last (largest) node in the tree - * @param rbtree: tree - * @return: largest element or NULL if tree empty. - */ -_getdns_rbnode_t *_getdns_rbtree_last(_getdns_rbtree_t *rbtree); - -/** - * Returns next larger node in the tree - * @param rbtree: tree - * @return: next larger element or NULL if no larger in tree. - */ -_getdns_rbnode_t *_getdns_rbtree_next(_getdns_rbnode_t *rbtree); - -/** - * Returns previous smaller node in the tree - * @param rbtree: tree - * @return: previous smaller element or NULL if no previous in tree. - */ -_getdns_rbnode_t *_getdns_rbtree_previous(_getdns_rbnode_t *rbtree); - -/** - * Call with node=variable of struct* with _getdns_rbnode_t as first element. - * with type is the type of a pointer to that struct. - */ -#define RBTREE_FOR(node, type, rbtree) \ - for(node=(type)_getdns_rbtree_first(rbtree); \ - (_getdns_rbnode_t*)node != RBTREE_NULL; \ - node = (type)_getdns_rbtree_next((_getdns_rbnode_t*)node)) - -/** - * Call function for all elements in the redblack tree, such that - * leaf elements are called before parent elements. So that all - * elements can be safely free()d. - * Note that your function must not remove the nodes from the tree. - * Since that may trigger rebalances of the rbtree. - * @param tree: the tree - * @param func: function called with element and user arg. - * The function must not alter the rbtree. - * @param arg: user argument. - */ -void _getdns_traverse_postorder(_getdns_rbtree_t* tree, void (*func)(_getdns_rbnode_t*, void*), - void* arg); - -#endif /* UTIL_RBTREE_H_ */ +#ifndef RBTREE_H_SYMBOLS +#define RBTREE_H_SYMBOLS +#define rbnode_type _getdns_rbnode_t +#define rbtree_null_node _getdns_rbtree_null_node +#define rbtree_type _getdns_rbtree_t +#define rbtree_create _getdns_rbtree_create +#define rbtree_init _getdns_rbtree_init +#define rbtree_insert _getdns_rbtree_insert +#define rbtree_delete _getdns_rbtree_delete +#define rbtree_search _getdns_rbtree_search +#define rbtree_find_less_equal _getdns_rbtree_find_less_equal +#define rbtree_first _getdns_rbtree_first +#define rbtree_last _getdns_rbtree_last +#define rbtree_next _getdns_rbtree_next +#define rbtree_previous _getdns_rbtree_previous +#define traverse_postorder _getdns_traverse_postorder +#include "util/ub/rbtree.h" +#endif diff --git a/src/util/ub/rbtree.h b/src/util/ub/rbtree.h new file mode 100644 index 00000000..dfcf09ac --- /dev/null +++ b/src/util/ub/rbtree.h @@ -0,0 +1,192 @@ +/* + * rbtree.h -- generic red-black tree + * + * Copyright (c) 2001-2007, NLnet Labs. All rights reserved. + * + * This software is open source. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the NLNET LABS nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +/** + * \file + * Red black tree. Implementation taken from NSD 3.0.5, adjusted for use + * in unbound (memory allocation, logging and so on). + */ + +#ifndef UTIL_RBTREE_H_ +#define UTIL_RBTREE_H_ + +/** + * This structure must be the first member of the data structure in + * the rbtree. This allows easy casting between an rbnode_type and the + * user data (poor man's inheritance). + */ +typedef struct rbnode_type rbnode_type; +/** + * The rbnode_type struct definition. + */ +struct rbnode_type { + /** parent in rbtree, RBTREE_NULL for root */ + rbnode_type *parent; + /** left node (smaller items) */ + rbnode_type *left; + /** right node (larger items) */ + rbnode_type *right; + /** pointer to sorting key */ + const void *key; + /** colour of this node */ + uint8_t color; +}; + +/** The nullpointer, points to empty node */ +#define RBTREE_NULL &rbtree_null_node +/** the global empty node */ +extern rbnode_type rbtree_null_node; + +/** An entire red black tree */ +typedef struct rbtree_type rbtree_type; +/** definition for tree struct */ +struct rbtree_type { + /** The root of the red-black tree */ + rbnode_type *root; + + /** The number of the nodes in the tree */ + size_t count; + + /** + * Key compare function. <0,0,>0 like strcmp. + * Return 0 on two NULL ptrs. + */ + int (*cmp) (const void *, const void *); +}; + +/** + * Create new tree (malloced) with given key compare function. + * @param cmpf: compare function (like strcmp) takes pointers to two keys. + * @return: new tree, empty. + */ +rbtree_type *rbtree_create(int (*cmpf)(const void *, const void *)); + +/** + * Init a new tree (malloced by caller) with given key compare function. + * @param rbtree: uninitialised memory for new tree, returned empty. + * @param cmpf: compare function (like strcmp) takes pointers to two keys. + */ +void rbtree_init(rbtree_type *rbtree, int (*cmpf)(const void *, const void *)); + +/** + * Insert data into the tree. + * @param rbtree: tree to insert to. + * @param data: element to insert. + * @return: data ptr or NULL if key already present. + */ +rbnode_type *rbtree_insert(rbtree_type *rbtree, rbnode_type *data); + +/** + * Delete element from tree. + * @param rbtree: tree to delete from. + * @param key: key of item to delete. + * @return: node that is now unlinked from the tree. User to delete it. + * returns 0 if node not present + */ +rbnode_type *rbtree_delete(rbtree_type *rbtree, const void *key); + +/** + * Find key in tree. Returns NULL if not found. + * @param rbtree: tree to find in. + * @param key: key that must match. + * @return: node that fits or NULL. + */ +rbnode_type *rbtree_search(rbtree_type *rbtree, const void *key); + +/** + * Find, but match does not have to be exact. + * @param rbtree: tree to find in. + * @param key: key to find position of. + * @param result: set to the exact node if present, otherwise to element that + * precedes the position of key in the tree. NULL if no smaller element. + * @return: true if exact match in result. Else result points to <= element, + * or NULL if key is smaller than the smallest key. + */ +int rbtree_find_less_equal(rbtree_type *rbtree, const void *key, + rbnode_type **result); + +/** + * Returns first (smallest) node in the tree + * @param rbtree: tree + * @return: smallest element or NULL if tree empty. + */ +rbnode_type *rbtree_first(rbtree_type *rbtree); + +/** + * Returns last (largest) node in the tree + * @param rbtree: tree + * @return: largest element or NULL if tree empty. + */ +rbnode_type *rbtree_last(rbtree_type *rbtree); + +/** + * Returns next larger node in the tree + * @param rbtree: tree + * @return: next larger element or NULL if no larger in tree. + */ +rbnode_type *rbtree_next(rbnode_type *rbtree); + +/** + * Returns previous smaller node in the tree + * @param rbtree: tree + * @return: previous smaller element or NULL if no previous in tree. + */ +rbnode_type *rbtree_previous(rbnode_type *rbtree); + +/** + * Call with node=variable of struct* with rbnode_type as first element. + * with type is the type of a pointer to that struct. + */ +#define RBTREE_FOR(node, type, rbtree) \ + for(node=(type)rbtree_first(rbtree); \ + (rbnode_type*)node != RBTREE_NULL; \ + node = (type)rbtree_next((rbnode_type*)node)) + +/** + * Call function for all elements in the redblack tree, such that + * leaf elements are called before parent elements. So that all + * elements can be safely free()d. + * Note that your function must not remove the nodes from the tree. + * Since that may trigger rebalances of the rbtree. + * @param tree: the tree + * @param func: function called with element and user arg. + * The function must not alter the rbtree. + * @param arg: user argument. + */ +void traverse_postorder(rbtree_type* tree, void (*func)(rbnode_type*, void*), + void* arg); + +#endif /* UTIL_RBTREE_H_ */ From e02442eb9891343adea28cf4544b92ea81a8e8f5 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Wed, 8 Mar 2017 23:04:52 +0100 Subject: [PATCH 12/25] Original val_secalgo files + symbol mapping --- src/Makefile.in | 2 +- src/util/ub/val_secalgo.h | 107 +++++ src/util/ub_reroute/sldns/keyraw.h | 1 + src/util/ub_reroute/sldns/rrdef.h | 1 + src/util/ub_reroute/sldns/sbuffer.h | 1 + src/util/ub_reroute/util/data/packed_rrset.h | 1 + src/util/ub_reroute/validator/val_nsec3.h | 1 + src/util/ub_reroute/validator/val_secalgo.h | 1 + src/util/val_secalgo.c | 415 ++++++++++--------- src/util/val_secalgo.h | 171 ++++---- 10 files changed, 399 insertions(+), 302 deletions(-) create mode 100644 src/util/ub/val_secalgo.h create mode 100644 src/util/ub_reroute/sldns/keyraw.h create mode 100644 src/util/ub_reroute/sldns/rrdef.h create mode 100644 src/util/ub_reroute/sldns/sbuffer.h create mode 100644 src/util/ub_reroute/util/data/packed_rrset.h create mode 100644 src/util/ub_reroute/validator/val_nsec3.h create mode 100644 src/util/ub_reroute/validator/val_secalgo.h diff --git a/src/Makefile.in b/src/Makefile.in index 6f06858a..2b0502f4 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -48,7 +48,7 @@ srcdir = @srcdir@ LIBTOOL = ../libtool CC=@CC@ -CFLAGS=-I$(srcdir) -I. @CFLAGS@ @CPPFLAGS@ $(XTRA_CFLAGS) +CFLAGS=-I$(srcdir) -I. -I$(srcdir)/util/ub_reroute @CFLAGS@ @CPPFLAGS@ $(XTRA_CFLAGS) WPEDANTICFLAG=@WPEDANTICFLAG@ WNOERRORFLAG=@WNOERRORFLAG@ LDFLAGS=@LDFLAGS@ @LIBS@ diff --git a/src/util/ub/val_secalgo.h b/src/util/ub/val_secalgo.h new file mode 100644 index 00000000..52aaeb9f --- /dev/null +++ b/src/util/ub/val_secalgo.h @@ -0,0 +1,107 @@ +/* + * validator/val_secalgo.h - validator security algorithm functions. + * + * Copyright (c) 2012, NLnet Labs. All rights reserved. + * + * This software is open source. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the NLNET LABS nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file + * + * This file contains helper functions for the validator module. + * The functions take buffers with raw data and convert to library calls. + */ + +#ifndef VALIDATOR_VAL_SECALGO_H +#define VALIDATOR_VAL_SECALGO_H +struct sldns_buffer; + +/** Return size of nsec3 hash algorithm, 0 if not supported */ +size_t nsec3_hash_algo_size_supported(int id); + +/** + * Hash a single hash call of an NSEC3 hash algorithm. + * Iterations and salt are done by the caller. + * @param algo: nsec3 hash algorithm. + * @param buf: the buffer to digest + * @param len: length of buffer to digest. + * @param res: result stored here (must have sufficient space). + * @return false on failure. +*/ +int secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, + unsigned char* res); + +/** + * Calculate the sha256 hash for the data buffer into the result. + * @param buf: buffer to digest. + * @param len: length of the buffer to digest. + * @param res: result is stored here (space 256/8 bytes). + */ +void secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res); + +/** + * Return size of DS digest according to its hash algorithm. + * @param algo: DS digest algo. + * @return size in bytes of digest, or 0 if not supported. + */ +size_t ds_digest_size_supported(int algo); + +/** + * @param algo: the DS digest algo + * @param buf: the buffer to digest + * @param len: length of buffer to digest. + * @param res: result stored here (must have sufficient space). + * @return false on failure. + */ +int secalgo_ds_digest(int algo, unsigned char* buf, size_t len, + unsigned char* res); + +/** return true if DNSKEY algorithm id is supported */ +int dnskey_algo_id_is_supported(int id); + +/** + * Check a canonical sig+rrset and signature against a dnskey + * @param buf: buffer with data to verify, the first rrsig part and the + * canonicalized rrset. + * @param algo: DNSKEY algorithm. + * @param sigblock: signature rdata field from RRSIG + * @param sigblock_len: length of sigblock data. + * @param key: public key data from DNSKEY RR. + * @param keylen: length of keydata. + * @param reason: bogus reason in more detail. + * @return secure if verification succeeded, bogus on crypto failure, + * unchecked on format errors and alloc failures. + */ +enum sec_status verify_canonrrset(struct sldns_buffer* buf, int algo, + unsigned char* sigblock, unsigned int sigblock_len, + unsigned char* key, unsigned int keylen, char** reason); + +#endif /* VALIDATOR_VAL_SECALGO_H */ diff --git a/src/util/ub_reroute/sldns/keyraw.h b/src/util/ub_reroute/sldns/keyraw.h new file mode 100644 index 00000000..27bc5c3e --- /dev/null +++ b/src/util/ub_reroute/sldns/keyraw.h @@ -0,0 +1 @@ +#include "gldns/keyraw.h" diff --git a/src/util/ub_reroute/sldns/rrdef.h b/src/util/ub_reroute/sldns/rrdef.h new file mode 100644 index 00000000..842c1ba6 --- /dev/null +++ b/src/util/ub_reroute/sldns/rrdef.h @@ -0,0 +1 @@ +#include "gldns/rrdef.h" diff --git a/src/util/ub_reroute/sldns/sbuffer.h b/src/util/ub_reroute/sldns/sbuffer.h new file mode 100644 index 00000000..963b8628 --- /dev/null +++ b/src/util/ub_reroute/sldns/sbuffer.h @@ -0,0 +1 @@ +#include "gldns/gbuffer.h" diff --git a/src/util/ub_reroute/util/data/packed_rrset.h b/src/util/ub_reroute/util/data/packed_rrset.h new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/src/util/ub_reroute/util/data/packed_rrset.h @@ -0,0 +1 @@ + diff --git a/src/util/ub_reroute/validator/val_nsec3.h b/src/util/ub_reroute/validator/val_nsec3.h new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/src/util/ub_reroute/validator/val_nsec3.h @@ -0,0 +1 @@ + diff --git a/src/util/ub_reroute/validator/val_secalgo.h b/src/util/ub_reroute/validator/val_secalgo.h new file mode 100644 index 00000000..1e187cba --- /dev/null +++ b/src/util/ub_reroute/validator/val_secalgo.h @@ -0,0 +1 @@ +#include "util/val_secalgo.h" diff --git a/src/util/val_secalgo.c b/src/util/val_secalgo.c index 77238b2c..302820fc 100644 --- a/src/util/val_secalgo.c +++ b/src/util/val_secalgo.c @@ -41,12 +41,14 @@ * and do the library calls (for the crypto library in use). */ #include "config.h" -#include "util/val_secalgo.h" -#define NSEC3_HASH_SHA1 0x01 +/* packed_rrset on top to define enum types (forced by c99 standard) */ +#include "util/data/packed_rrset.h" +#include "validator/val_secalgo.h" +#include "validator/val_nsec3.h" #include "util/log.h" -#include "gldns/rrdef.h" -#include "gldns/keyraw.h" -#include "gldns/gbuffer.h" +#include "sldns/rrdef.h" +#include "sldns/keyraw.h" +#include "sldns/sbuffer.h" #if !defined(HAVE_SSL) && !defined(HAVE_NSS) && !defined(HAVE_NETTLE) #error "Need crypto library to do digital signature cryptography" @@ -70,9 +72,12 @@ #include #endif +/** fake DSA support for unit tests */ +int fake_dsa = 0; + /* return size of digest if supported, or 0 otherwise */ size_t -_getdns_nsec3_hash_algo_size_supported(int id) +nsec3_hash_algo_size_supported(int id) { switch(id) { case NSEC3_HASH_SHA1: @@ -84,7 +89,7 @@ _getdns_nsec3_hash_algo_size_supported(int id) /* perform nsec3 hash. return false on failure */ int -_getdns_secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, +secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, unsigned char* res) { switch(algo) { @@ -97,7 +102,7 @@ _getdns_secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, } void -_getdns_secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res) +secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res) { (void)SHA256(buf, len, res); } @@ -108,27 +113,27 @@ _getdns_secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res) * @return size in bytes of digest, or 0 if not supported. */ size_t -_getdns_ds_digest_size_supported(int algo) +ds_digest_size_supported(int algo) { switch(algo) { #ifdef HAVE_EVP_SHA1 - case GLDNS_SHA1: + case LDNS_SHA1: return SHA_DIGEST_LENGTH; #endif #ifdef HAVE_EVP_SHA256 - case GLDNS_SHA256: + case LDNS_SHA256: return SHA256_DIGEST_LENGTH; #endif #ifdef USE_GOST - case GLDNS_HASH_GOST: + case LDNS_HASH_GOST: /* we support GOST if it can be loaded */ - (void)gldns_key_EVP_load_gost_id(); + (void)sldns_key_EVP_load_gost_id(); if(EVP_get_digestbyname("md_gost94")) return 32; else return 0; #endif #ifdef USE_ECDSA - case GLDNS_SHA384: + case LDNS_SHA384: return SHA384_DIGEST_LENGTH; #endif default: break; @@ -144,33 +149,33 @@ do_gost94(unsigned char* data, size_t len, unsigned char* dest) const EVP_MD* md = EVP_get_digestbyname("md_gost94"); if(!md) return 0; - return gldns_digest_evp(data, (unsigned int)len, dest, md); + return sldns_digest_evp(data, (unsigned int)len, dest, md); } #endif int -_getdns_secalgo_ds_digest(int algo, unsigned char* buf, size_t len, +secalgo_ds_digest(int algo, unsigned char* buf, size_t len, unsigned char* res) { switch(algo) { #ifdef HAVE_EVP_SHA1 - case GLDNS_SHA1: + case LDNS_SHA1: (void)SHA1(buf, len, res); return 1; #endif #ifdef HAVE_EVP_SHA256 - case GLDNS_SHA256: + case LDNS_SHA256: (void)SHA256(buf, len, res); return 1; #endif #ifdef USE_GOST - case GLDNS_HASH_GOST: + case LDNS_HASH_GOST: if(do_gost94(buf, len, res)) return 1; break; #endif #ifdef USE_ECDSA - case GLDNS_SHA384: + case LDNS_SHA384: (void)SHA384(buf, len, res); return 1; #endif @@ -184,33 +189,37 @@ _getdns_secalgo_ds_digest(int algo, unsigned char* buf, size_t len, /** return true if DNSKEY algorithm id is supported */ int -_getdns_dnskey_algo_id_is_supported(int id) +dnskey_algo_id_is_supported(int id) { switch(id) { - case GLDNS_RSAMD5: + case LDNS_RSAMD5: /* RFC 6725 deprecates RSAMD5 */ return 0; + case LDNS_DSA: + case LDNS_DSA_NSEC3: #ifdef USE_DSA - case GLDNS_DSA: - case GLDNS_DSA_NSEC3: + return 1; +#else + if(fake_dsa) return 1; + return 0; #endif - case GLDNS_RSASHA1: - case GLDNS_RSASHA1_NSEC3: + case LDNS_RSASHA1: + case LDNS_RSASHA1_NSEC3: #if defined(HAVE_EVP_SHA256) && defined(USE_SHA2) - case GLDNS_RSASHA256: + case LDNS_RSASHA256: #endif #if defined(HAVE_EVP_SHA512) && defined(USE_SHA2) - case GLDNS_RSASHA512: + case LDNS_RSASHA512: #endif #ifdef USE_ECDSA - case GLDNS_ECDSAP256SHA256: - case GLDNS_ECDSAP384SHA384: + case LDNS_ECDSAP256SHA256: + case LDNS_ECDSAP384SHA384: #endif return 1; #ifdef USE_GOST - case GLDNS_ECC_GOST: + case LDNS_ECC_GOST: /* we support GOST if it can be loaded */ - return gldns_key_EVP_load_gost_id(); + return sldns_key_EVP_load_gost_id(); #endif default: return 0; @@ -230,7 +239,6 @@ log_crypto_error(const char* str, unsigned long e) ERR_error_string_n(e, buf, sizeof(buf)); /* buf now contains */ /* error:[error code]:[library name]:[function name]:[reason string] */ - (void)str; log_err("%s crypto %s", str, buf); } @@ -356,7 +364,7 @@ i * the '44' is the total remaining length. #ifdef USE_ECDSA_EVP_WORKAROUND static EVP_MD ecdsa_evp_256_md; static EVP_MD ecdsa_evp_384_md; -void _getdns_ecdsa_evp_workaround_init(void) +void ecdsa_evp_workaround_init(void) { /* openssl before 1.0.0 fixes RSA with the SHA256 * hash in EVP. We create one for ecdsa_sha256 */ @@ -391,17 +399,17 @@ setup_key_digest(int algo, EVP_PKEY** evp_key, const EVP_MD** digest_type, switch(algo) { #ifdef USE_DSA - case GLDNS_DSA: - case GLDNS_DSA_NSEC3: + case LDNS_DSA: + case LDNS_DSA_NSEC3: *evp_key = EVP_PKEY_new(); if(!*evp_key) { log_err("verify: malloc failure in crypto"); return 0; } - dsa = gldns_key_buf2dsa_raw(key, keylen); + dsa = sldns_key_buf2dsa_raw(key, keylen); if(!dsa) { verbose(VERB_QUERY, "verify: " - "gldns_key_buf2dsa_raw failed"); + "sldns_key_buf2dsa_raw failed"); return 0; } if(EVP_PKEY_assign_DSA(*evp_key, dsa) == 0) { @@ -417,23 +425,23 @@ setup_key_digest(int algo, EVP_PKEY** evp_key, const EVP_MD** digest_type, break; #endif /* USE_DSA */ - case GLDNS_RSASHA1: - case GLDNS_RSASHA1_NSEC3: + case LDNS_RSASHA1: + case LDNS_RSASHA1_NSEC3: #if defined(HAVE_EVP_SHA256) && defined(USE_SHA2) - case GLDNS_RSASHA256: + case LDNS_RSASHA256: #endif #if defined(HAVE_EVP_SHA512) && defined(USE_SHA2) - case GLDNS_RSASHA512: + case LDNS_RSASHA512: #endif *evp_key = EVP_PKEY_new(); if(!*evp_key) { log_err("verify: malloc failure in crypto"); return 0; } - rsa = gldns_key_buf2rsa_raw(key, keylen); + rsa = sldns_key_buf2rsa_raw(key, keylen); if(!rsa) { verbose(VERB_QUERY, "verify: " - "gldns_key_buf2rsa_raw SHA failed"); + "sldns_key_buf2rsa_raw SHA failed"); return 0; } if(EVP_PKEY_assign_RSA(*evp_key, rsa) == 0) { @@ -444,28 +452,28 @@ setup_key_digest(int algo, EVP_PKEY** evp_key, const EVP_MD** digest_type, /* select SHA version */ #if defined(HAVE_EVP_SHA256) && defined(USE_SHA2) - if(algo == GLDNS_RSASHA256) + if(algo == LDNS_RSASHA256) *digest_type = EVP_sha256(); else #endif #if defined(HAVE_EVP_SHA512) && defined(USE_SHA2) - if(algo == GLDNS_RSASHA512) + if(algo == LDNS_RSASHA512) *digest_type = EVP_sha512(); else #endif *digest_type = EVP_sha1(); break; - case GLDNS_RSAMD5: + case LDNS_RSAMD5: *evp_key = EVP_PKEY_new(); if(!*evp_key) { log_err("verify: malloc failure in crypto"); return 0; } - rsa = gldns_key_buf2rsa_raw(key, keylen); + rsa = sldns_key_buf2rsa_raw(key, keylen); if(!rsa) { verbose(VERB_QUERY, "verify: " - "gldns_key_buf2rsa_raw MD5 failed"); + "sldns_key_buf2rsa_raw MD5 failed"); return 0; } if(EVP_PKEY_assign_RSA(*evp_key, rsa) == 0) { @@ -477,11 +485,11 @@ setup_key_digest(int algo, EVP_PKEY** evp_key, const EVP_MD** digest_type, break; #ifdef USE_GOST - case GLDNS_ECC_GOST: - *evp_key = gldns_gost2pkey_raw(key, keylen); + case LDNS_ECC_GOST: + *evp_key = sldns_gost2pkey_raw(key, keylen); if(!*evp_key) { verbose(VERB_QUERY, "verify: " - "gldns_gost2pkey_raw failed"); + "sldns_gost2pkey_raw failed"); return 0; } *digest_type = EVP_get_digestbyname("md_gost94"); @@ -493,12 +501,12 @@ setup_key_digest(int algo, EVP_PKEY** evp_key, const EVP_MD** digest_type, break; #endif #ifdef USE_ECDSA - case GLDNS_ECDSAP256SHA256: - *evp_key = gldns_ecdsa2pkey_raw(key, keylen, - GLDNS_ECDSAP256SHA256); + case LDNS_ECDSAP256SHA256: + *evp_key = sldns_ecdsa2pkey_raw(key, keylen, + LDNS_ECDSAP256SHA256); if(!*evp_key) { verbose(VERB_QUERY, "verify: " - "gldns_ecdsa2pkey_raw failed"); + "sldns_ecdsa2pkey_raw failed"); return 0; } #ifdef USE_ECDSA_EVP_WORKAROUND @@ -507,12 +515,12 @@ setup_key_digest(int algo, EVP_PKEY** evp_key, const EVP_MD** digest_type, *digest_type = EVP_sha256(); #endif break; - case GLDNS_ECDSAP384SHA384: - *evp_key = gldns_ecdsa2pkey_raw(key, keylen, - GLDNS_ECDSAP384SHA384); + case LDNS_ECDSAP384SHA384: + *evp_key = sldns_ecdsa2pkey_raw(key, keylen, + LDNS_ECDSAP384SHA384); if(!*evp_key) { verbose(VERB_QUERY, "verify: " - "gldns_ecdsa2pkey_raw failed"); + "sldns_ecdsa2pkey_raw failed"); return 0; } #ifdef USE_ECDSA_EVP_WORKAROUND @@ -543,8 +551,8 @@ setup_key_digest(int algo, EVP_PKEY** evp_key, const EVP_MD** digest_type, * @return secure if verification succeeded, bogus on crypto failure, * unchecked on format errors and alloc failures. */ -int -_getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, +enum sec_status +verify_canonrrset(sldns_buffer* buf, int algo, unsigned char* sigblock, unsigned int sigblock_len, unsigned char* key, unsigned int keylen, char** reason) { @@ -552,22 +560,27 @@ _getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, EVP_MD_CTX* ctx; int res, dofree = 0, docrypto_free = 0; EVP_PKEY *evp_key = NULL; + +#ifndef USE_DSA + if((algo == LDNS_DSA || algo == LDNS_DSA_NSEC3) && fake_dsa) + return sec_status_secure; +#endif if(!setup_key_digest(algo, &evp_key, &digest_type, key, keylen)) { verbose(VERB_QUERY, "verify: failed to setup key"); *reason = "use of key for crypto failed"; EVP_PKEY_free(evp_key); - return 0; + return sec_status_bogus; } #ifdef USE_DSA /* if it is a DSA signature in bind format, convert to DER format */ - if((algo == GLDNS_DSA || algo == GLDNS_DSA_NSEC3) && + if((algo == LDNS_DSA || algo == LDNS_DSA_NSEC3) && sigblock_len == 1+2*SHA_DIGEST_LENGTH) { if(!setup_dsa_sig(&sigblock, &sigblock_len)) { verbose(VERB_QUERY, "verify: failed to setup DSA sig"); *reason = "use of key for DSA crypto failed"; EVP_PKEY_free(evp_key); - return 0; + return sec_status_bogus; } docrypto_free = 1; } @@ -576,13 +589,13 @@ _getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, else #endif #ifdef USE_ECDSA - if(algo == GLDNS_ECDSAP256SHA256 || algo == GLDNS_ECDSAP384SHA384) { + if(algo == LDNS_ECDSAP256SHA256 || algo == LDNS_ECDSAP384SHA384) { /* EVP uses ASN prefix on sig, which is not in the wire data */ if(!setup_ecdsa_sig(&sigblock, &sigblock_len)) { verbose(VERB_QUERY, "verify: failed to setup ECDSA sig"); *reason = "use of signature for ECDSA crypto failed"; EVP_PKEY_free(evp_key); - return 0; + return sec_status_bogus; } dofree = 1; } @@ -600,7 +613,7 @@ _getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, EVP_PKEY_free(evp_key); if(dofree) free(sigblock); else if(docrypto_free) OPENSSL_free(sigblock); - return 0; + return sec_status_unchecked; } if(EVP_VerifyInit(ctx, digest_type) == 0) { verbose(VERB_QUERY, "verify: EVP_VerifyInit failed"); @@ -608,16 +621,16 @@ _getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, EVP_PKEY_free(evp_key); if(dofree) free(sigblock); else if(docrypto_free) OPENSSL_free(sigblock); - return 0; + return sec_status_unchecked; } - if(EVP_VerifyUpdate(ctx, (unsigned char*)gldns_buffer_begin(buf), - (unsigned int)gldns_buffer_limit(buf)) == 0) { + if(EVP_VerifyUpdate(ctx, (unsigned char*)sldns_buffer_begin(buf), + (unsigned int)sldns_buffer_limit(buf)) == 0) { verbose(VERB_QUERY, "verify: EVP_VerifyUpdate failed"); EVP_MD_CTX_destroy(ctx); EVP_PKEY_free(evp_key); if(dofree) free(sigblock); else if(docrypto_free) OPENSSL_free(sigblock); - return 0; + return sec_status_unchecked; } res = EVP_VerifyFinal(ctx, sigblock, sigblock_len, evp_key); @@ -633,15 +646,15 @@ _getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, else if(docrypto_free) OPENSSL_free(sigblock); if(res == 1) { - return 1; + return sec_status_secure; } else if(res == 0) { verbose(VERB_QUERY, "verify: signature mismatch"); *reason = "signature crypto failed"; - return 0; + return sec_status_bogus; } log_crypto_error("verify:", ERR_get_error()); - return 0; + return sec_status_unchecked; } /**************************************************/ @@ -658,7 +671,7 @@ _getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, /* return size of digest if supported, or 0 otherwise */ size_t -_getdns_nsec3_hash_algo_size_supported(int id) +nsec3_hash_algo_size_supported(int id) { switch(id) { case NSEC3_HASH_SHA1: @@ -670,7 +683,7 @@ _getdns_nsec3_hash_algo_size_supported(int id) /* perform nsec3 hash. return false on failure */ int -_getdns_secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, +secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, unsigned char* res) { switch(algo) { @@ -683,53 +696,53 @@ _getdns_secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, } void -_getdns_secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res) +secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res) { (void)HASH_HashBuf(HASH_AlgSHA256, res, buf, (unsigned long)len); } size_t -_getdns_ds_digest_size_supported(int algo) +ds_digest_size_supported(int algo) { /* uses libNSS */ switch(algo) { - case GLDNS_SHA1: + case LDNS_SHA1: return SHA1_LENGTH; #ifdef USE_SHA2 - case GLDNS_SHA256: + case LDNS_SHA256: return SHA256_LENGTH; #endif #ifdef USE_ECDSA - case GLDNS_SHA384: + case LDNS_SHA384: return SHA384_LENGTH; #endif /* GOST not supported in NSS */ - case GLDNS_HASH_GOST: + case LDNS_HASH_GOST: default: break; } return 0; } int -_getdns_secalgo_ds_digest(int algo, unsigned char* buf, size_t len, +secalgo_ds_digest(int algo, unsigned char* buf, size_t len, unsigned char* res) { /* uses libNSS */ switch(algo) { - case GLDNS_SHA1: + case LDNS_SHA1: return HASH_HashBuf(HASH_AlgSHA1, res, buf, len) == SECSuccess; #if defined(USE_SHA2) - case GLDNS_SHA256: + case LDNS_SHA256: return HASH_HashBuf(HASH_AlgSHA256, res, buf, len) == SECSuccess; #endif #ifdef USE_ECDSA - case GLDNS_SHA384: + case LDNS_SHA384: return HASH_HashBuf(HASH_AlgSHA384, res, buf, len) == SECSuccess; #endif - case GLDNS_HASH_GOST: + case LDNS_HASH_GOST: default: verbose(VERB_QUERY, "unknown DS digest algorithm %d", algo); @@ -739,32 +752,32 @@ _getdns_secalgo_ds_digest(int algo, unsigned char* buf, size_t len, } int -_getdns_dnskey_algo_id_is_supported(int id) +dnskey_algo_id_is_supported(int id) { /* uses libNSS */ switch(id) { - case GLDNS_RSAMD5: + case LDNS_RSAMD5: /* RFC 6725 deprecates RSAMD5 */ return 0; #ifdef USE_DSA - case GLDNS_DSA: - case GLDNS_DSA_NSEC3: + case LDNS_DSA: + case LDNS_DSA_NSEC3: #endif - case GLDNS_RSASHA1: - case GLDNS_RSASHA1_NSEC3: + case LDNS_RSASHA1: + case LDNS_RSASHA1_NSEC3: #ifdef USE_SHA2 - case GLDNS_RSASHA256: + case LDNS_RSASHA256: #endif #ifdef USE_SHA2 - case GLDNS_RSASHA512: + case LDNS_RSASHA512: #endif return 1; #ifdef USE_ECDSA - case GLDNS_ECDSAP256SHA256: - case GLDNS_ECDSAP384SHA384: + case LDNS_ECDSAP256SHA256: + case LDNS_ECDSAP384SHA384: return PK11_TokenExists(CKM_ECDSA); #endif - case GLDNS_ECC_GOST: + case LDNS_ECC_GOST: default: return 0; } @@ -810,10 +823,10 @@ static SECKEYPublicKey* nss_buf2ecdsa(unsigned char* key, size_t len, int algo) unsigned char buf[256+2]; /* sufficient for 2*384/8+1 */ /* check length, which uncompressed must be 2 bignums */ - if(algo == GLDNS_ECDSAP256SHA256) { + if(algo == LDNS_ECDSAP256SHA256) { if(len != 2*256/8) return NULL; /* ECCurve_X9_62_PRIME_256V1 */ - } else if(algo == GLDNS_ECDSAP384SHA384) { + } else if(algo == LDNS_ECDSAP384SHA384) { if(len != 2*384/8) return NULL; /* ECCurve_X9_62_PRIME_384R1 */ } else return NULL; @@ -822,7 +835,7 @@ static SECKEYPublicKey* nss_buf2ecdsa(unsigned char* key, size_t len, int algo) memmove(buf+1, key, len); pub.data = buf; pub.len = len+1; - if(algo == GLDNS_ECDSAP256SHA256) { + if(algo == LDNS_ECDSAP256SHA256) { params.data = param256; params.len = sizeof(param256); } else { @@ -991,8 +1004,8 @@ nss_setup_key_digest(int algo, SECKEYPublicKey** pubkey, HASH_HashType* htype, switch(algo) { #ifdef USE_DSA - case GLDNS_DSA: - case GLDNS_DSA_NSEC3: + case LDNS_DSA: + case LDNS_DSA_NSEC3: *pubkey = nss_buf2dsa(key, keylen); if(!*pubkey) { log_err("verify: malloc failure in crypto"); @@ -1002,13 +1015,13 @@ nss_setup_key_digest(int algo, SECKEYPublicKey** pubkey, HASH_HashType* htype, /* no prefix for DSA verification */ break; #endif - case GLDNS_RSASHA1: - case GLDNS_RSASHA1_NSEC3: + case LDNS_RSASHA1: + case LDNS_RSASHA1_NSEC3: #ifdef USE_SHA2 - case GLDNS_RSASHA256: + case LDNS_RSASHA256: #endif #ifdef USE_SHA2 - case GLDNS_RSASHA512: + case LDNS_RSASHA512: #endif *pubkey = nss_buf2rsa(key, keylen); if(!*pubkey) { @@ -1017,14 +1030,14 @@ nss_setup_key_digest(int algo, SECKEYPublicKey** pubkey, HASH_HashType* htype, } /* select SHA version */ #ifdef USE_SHA2 - if(algo == GLDNS_RSASHA256) { + if(algo == LDNS_RSASHA256) { *htype = HASH_AlgSHA256; *prefix = p_sha256; *prefixlen = sizeof(p_sha256); } else #endif #ifdef USE_SHA2 - if(algo == GLDNS_RSASHA512) { + if(algo == LDNS_RSASHA512) { *htype = HASH_AlgSHA512; *prefix = p_sha512; *prefixlen = sizeof(p_sha512); @@ -1037,7 +1050,7 @@ nss_setup_key_digest(int algo, SECKEYPublicKey** pubkey, HASH_HashType* htype, } break; - case GLDNS_RSAMD5: + case LDNS_RSAMD5: *pubkey = nss_buf2rsa(key, keylen); if(!*pubkey) { log_err("verify: malloc failure in crypto"); @@ -1049,9 +1062,9 @@ nss_setup_key_digest(int algo, SECKEYPublicKey** pubkey, HASH_HashType* htype, break; #ifdef USE_ECDSA - case GLDNS_ECDSAP256SHA256: + case LDNS_ECDSAP256SHA256: *pubkey = nss_buf2ecdsa(key, keylen, - GLDNS_ECDSAP256SHA256); + LDNS_ECDSAP256SHA256); if(!*pubkey) { log_err("verify: malloc failure in crypto"); return 0; @@ -1059,9 +1072,9 @@ nss_setup_key_digest(int algo, SECKEYPublicKey** pubkey, HASH_HashType* htype, *htype = HASH_AlgSHA256; /* no prefix for DSA verification */ break; - case GLDNS_ECDSAP384SHA384: + case LDNS_ECDSAP384SHA384: *pubkey = nss_buf2ecdsa(key, keylen, - GLDNS_ECDSAP384SHA384); + LDNS_ECDSAP384SHA384); if(!*pubkey) { log_err("verify: malloc failure in crypto"); return 0; @@ -1070,7 +1083,7 @@ nss_setup_key_digest(int algo, SECKEYPublicKey** pubkey, HASH_HashType* htype, /* no prefix for DSA verification */ break; #endif /* USE_ECDSA */ - case GLDNS_ECC_GOST: + case LDNS_ECC_GOST: default: verbose(VERB_QUERY, "verify: unknown algorithm %d", algo); @@ -1092,8 +1105,8 @@ nss_setup_key_digest(int algo, SECKEYPublicKey** pubkey, HASH_HashType* htype, * @return secure if verification succeeded, bogus on crypto failure, * unchecked on format errors and alloc failures. */ -int -_getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, +enum sec_status +verify_canonrrset(sldns_buffer* buf, int algo, unsigned char* sigblock, unsigned int sigblock_len, unsigned char* key, unsigned int keylen, char** reason) { @@ -1115,12 +1128,12 @@ _getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, verbose(VERB_QUERY, "verify: failed to setup key"); *reason = "use of key for crypto failed"; SECKEY_DestroyPublicKey(pubkey); - return 0; + return sec_status_bogus; } #ifdef USE_DSA /* need to convert DSA, ECDSA signatures? */ - if((algo == GLDNS_DSA || algo == GLDNS_DSA_NSEC3)) { + if((algo == LDNS_DSA || algo == LDNS_DSA_NSEC3)) { if(sigblock_len == 1+2*SHA1_LENGTH) { secsig.data ++; secsig.len --; @@ -1130,13 +1143,13 @@ _getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, verbose(VERB_QUERY, "verify: failed DER decode"); *reason = "signature DER decode failed"; SECKEY_DestroyPublicKey(pubkey); - return 0; + return sec_status_bogus; } if(SECITEM_CopyItem(pubkey->arena, &secsig, p)) { log_err("alloc failure in DER decode"); SECKEY_DestroyPublicKey(pubkey); SECITEM_FreeItem(p, PR_TRUE); - return 0; + return sec_status_unchecked; } SECITEM_FreeItem(p, PR_TRUE); } @@ -1149,20 +1162,20 @@ _getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, if(sechash.len > sizeof(hash)) { verbose(VERB_QUERY, "verify: hash too large for buffer"); SECKEY_DestroyPublicKey(pubkey); - return 0; + return sec_status_unchecked; } - if(HASH_HashBuf(htype, hash, (unsigned char*)gldns_buffer_begin(buf), - (unsigned int)gldns_buffer_limit(buf)) != SECSuccess) { + if(HASH_HashBuf(htype, hash, (unsigned char*)sldns_buffer_begin(buf), + (unsigned int)sldns_buffer_limit(buf)) != SECSuccess) { verbose(VERB_QUERY, "verify: HASH_HashBuf failed"); SECKEY_DestroyPublicKey(pubkey); - return 0; + return sec_status_unchecked; } if(prefix) { int hashlen = sechash.len; if(prefixlen+hashlen > sizeof(hash2)) { verbose(VERB_QUERY, "verify: hashprefix too large"); SECKEY_DestroyPublicKey(pubkey); - return 0; + return sec_status_unchecked; } sechash.data = hash2; sechash.len = prefixlen+hashlen; @@ -1175,7 +1188,7 @@ _getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, SECKEY_DestroyPublicKey(pubkey); if(res == SECSuccess) { - return 1; + return sec_status_secure; } err = PORT_GetError(); if(err != SEC_ERROR_BAD_SIGNATURE) { @@ -1185,17 +1198,17 @@ _getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, /* if it is not supported, like ECC is removed, we get, * SEC_ERROR_NO_MODULE */ if(err == SEC_ERROR_NO_MODULE) - return 0; + return sec_status_unchecked; /* but other errors are commonly returned * for a bad signature from NSS. Thus we return bogus, * not unchecked */ *reason = "signature crypto failed"; - return 0; + return sec_status_bogus; } verbose(VERB_QUERY, "verify: signature mismatch: %s", PORT_ErrorToString(err)); *reason = "signature crypto failed"; - return 0; + return sec_status_bogus; } #elif defined(HAVE_NETTLE) @@ -1259,7 +1272,7 @@ _digest_nettle(int algo, uint8_t* buf, size_t len, /* return size of digest if supported, or 0 otherwise */ size_t -_getdns_nsec3_hash_algo_size_supported(int id) +nsec3_hash_algo_size_supported(int id) { switch(id) { case NSEC3_HASH_SHA1: @@ -1271,7 +1284,7 @@ _getdns_nsec3_hash_algo_size_supported(int id) /* perform nsec3 hash. return false on failure */ int -_getdns_secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, +secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, unsigned char* res) { switch(algo) { @@ -1284,7 +1297,7 @@ _getdns_secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, } void -_getdns_secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res) +secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res) { _digest_nettle(SHA256_DIGEST_SIZE, (uint8_t*)buf, len, res); } @@ -1295,21 +1308,21 @@ _getdns_secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res) * @return size in bytes of digest, or 0 if not supported. */ size_t -_getdns_ds_digest_size_supported(int algo) +ds_digest_size_supported(int algo) { switch(algo) { - case GLDNS_SHA1: + case LDNS_SHA1: return SHA1_DIGEST_SIZE; #ifdef USE_SHA2 - case GLDNS_SHA256: + case LDNS_SHA256: return SHA256_DIGEST_SIZE; #endif #ifdef USE_ECDSA - case GLDNS_SHA384: + case LDNS_SHA384: return SHA384_DIGEST_SIZE; #endif /* GOST not supported */ - case GLDNS_HASH_GOST: + case LDNS_HASH_GOST: default: break; } @@ -1317,22 +1330,22 @@ _getdns_ds_digest_size_supported(int algo) } int -_getdns_secalgo_ds_digest(int algo, unsigned char* buf, size_t len, +secalgo_ds_digest(int algo, unsigned char* buf, size_t len, unsigned char* res) { switch(algo) { - case GLDNS_SHA1: + case LDNS_SHA1: return _digest_nettle(SHA1_DIGEST_SIZE, buf, len, res); #if defined(USE_SHA2) - case GLDNS_SHA256: + case LDNS_SHA256: return _digest_nettle(SHA256_DIGEST_SIZE, buf, len, res); #endif #ifdef USE_ECDSA - case GLDNS_SHA384: + case LDNS_SHA384: return _digest_nettle(SHA384_DIGEST_SIZE, buf, len, res); #endif - case GLDNS_HASH_GOST: + case LDNS_HASH_GOST: default: verbose(VERB_QUERY, "unknown DS digest algorithm %d", algo); @@ -1342,27 +1355,27 @@ _getdns_secalgo_ds_digest(int algo, unsigned char* buf, size_t len, } int -_getdns_dnskey_algo_id_is_supported(int id) +dnskey_algo_id_is_supported(int id) { /* uses libnettle */ switch(id) { #ifdef USE_DSA - case GLDNS_DSA: - case GLDNS_DSA_NSEC3: + case LDNS_DSA: + case LDNS_DSA_NSEC3: #endif - case GLDNS_RSASHA1: - case GLDNS_RSASHA1_NSEC3: + case LDNS_RSASHA1: + case LDNS_RSASHA1_NSEC3: #ifdef USE_SHA2 - case GLDNS_RSASHA256: - case GLDNS_RSASHA512: + case LDNS_RSASHA256: + case LDNS_RSASHA512: #endif #ifdef USE_ECDSA - case GLDNS_ECDSAP256SHA256: - case GLDNS_ECDSAP384SHA384: + case LDNS_ECDSAP256SHA256: + case LDNS_ECDSAP384SHA384: #endif return 1; - case GLDNS_RSAMD5: /* RFC 6725 deprecates RSAMD5 */ - case GLDNS_ECC_GOST: + case LDNS_RSAMD5: /* RFC 6725 deprecates RSAMD5 */ + case LDNS_ECC_GOST: default: return 0; } @@ -1370,11 +1383,11 @@ _getdns_dnskey_algo_id_is_supported(int id) #ifdef USE_DSA static char * -_verify_nettle_dsa(gldns_buffer* buf, unsigned char* sigblock, +_verify_nettle_dsa(sldns_buffer* buf, unsigned char* sigblock, unsigned int sigblock_len, unsigned char* key, unsigned int keylen) { uint8_t digest[SHA1_DIGEST_SIZE]; - uint8_t key_t; + uint8_t key_t_value; int res = 0; size_t offset; struct dsa_public_key pubkey; @@ -1413,8 +1426,8 @@ _verify_nettle_dsa(gldns_buffer* buf, unsigned char* sigblock, } /* Validate T values constraints - RFC 2536 sec. 2 & sec. 3 */ - key_t = key[0]; - if (key_t > 8) { + key_t_value = key[0]; + if (key_t_value > 8) { return "invalid T value in DSA pubkey"; } @@ -1425,9 +1438,9 @@ _verify_nettle_dsa(gldns_buffer* buf, unsigned char* sigblock, expected_len = 1 + /* T */ 20 + /* Q */ - (64 + key_t*8) + /* P */ - (64 + key_t*8) + /* G */ - (64 + key_t*8); /* Y */ + (64 + key_t_value*8) + /* P */ + (64 + key_t_value*8) + /* G */ + (64 + key_t_value*8); /* Y */ if (keylen != expected_len ) { return "invalid DSA pubkey length"; } @@ -1437,15 +1450,15 @@ _verify_nettle_dsa(gldns_buffer* buf, unsigned char* sigblock, offset = 1; nettle_mpz_set_str_256_u(pubkey.q, 20, key+offset); offset += 20; - nettle_mpz_set_str_256_u(pubkey.p, (64 + key_t*8), key+offset); - offset += (64 + key_t*8); - nettle_mpz_set_str_256_u(pubkey.g, (64 + key_t*8), key+offset); - offset += (64 + key_t*8); - nettle_mpz_set_str_256_u(pubkey.y, (64 + key_t*8), key+offset); + nettle_mpz_set_str_256_u(pubkey.p, (64 + key_t_value*8), key+offset); + offset += (64 + key_t_value*8); + nettle_mpz_set_str_256_u(pubkey.g, (64 + key_t_value*8), key+offset); + offset += (64 + key_t_value*8); + nettle_mpz_set_str_256_u(pubkey.y, (64 + key_t_value*8), key+offset); /* Digest content of "buf" and verify its DSA signature in "sigblock"*/ - res = _digest_nettle(SHA1_DIGEST_SIZE, (unsigned char*)gldns_buffer_begin(buf), - (unsigned int)gldns_buffer_limit(buf), (unsigned char*)digest); + res = _digest_nettle(SHA1_DIGEST_SIZE, (unsigned char*)sldns_buffer_begin(buf), + (unsigned int)sldns_buffer_limit(buf), (unsigned char*)digest); res &= dsa_sha1_verify_digest(&pubkey, digest, &signature); /* Clear and return */ @@ -1459,7 +1472,7 @@ _verify_nettle_dsa(gldns_buffer* buf, unsigned char* sigblock, #endif /* USE_DSA */ static char * -_verify_nettle_rsa(gldns_buffer* buf, unsigned int digest_size, char* sigblock, +_verify_nettle_rsa(sldns_buffer* buf, unsigned int digest_size, char* sigblock, unsigned int sigblock_len, uint8_t* key, unsigned int keylen) { uint16_t exp_len = 0; @@ -1502,24 +1515,24 @@ _verify_nettle_rsa(gldns_buffer* buf, unsigned int digest_size, char* sigblock, case SHA1_DIGEST_SIZE: { uint8_t digest[SHA1_DIGEST_SIZE]; - res = _digest_nettle(SHA1_DIGEST_SIZE, (unsigned char*)gldns_buffer_begin(buf), - (unsigned int)gldns_buffer_limit(buf), (unsigned char*)digest); + res = _digest_nettle(SHA1_DIGEST_SIZE, (unsigned char*)sldns_buffer_begin(buf), + (unsigned int)sldns_buffer_limit(buf), (unsigned char*)digest); res &= rsa_sha1_verify_digest(&pubkey, digest, signature); break; } case SHA256_DIGEST_SIZE: { uint8_t digest[SHA256_DIGEST_SIZE]; - res = _digest_nettle(SHA256_DIGEST_SIZE, (unsigned char*)gldns_buffer_begin(buf), - (unsigned int)gldns_buffer_limit(buf), (unsigned char*)digest); + res = _digest_nettle(SHA256_DIGEST_SIZE, (unsigned char*)sldns_buffer_begin(buf), + (unsigned int)sldns_buffer_limit(buf), (unsigned char*)digest); res &= rsa_sha256_verify_digest(&pubkey, digest, signature); break; } case SHA512_DIGEST_SIZE: { uint8_t digest[SHA512_DIGEST_SIZE]; - res = _digest_nettle(SHA512_DIGEST_SIZE, (unsigned char*)gldns_buffer_begin(buf), - (unsigned int)gldns_buffer_limit(buf), (unsigned char*)digest); + res = _digest_nettle(SHA512_DIGEST_SIZE, (unsigned char*)sldns_buffer_begin(buf), + (unsigned int)sldns_buffer_limit(buf), (unsigned char*)digest); res &= rsa_sha512_verify_digest(&pubkey, digest, signature); break; } @@ -1539,7 +1552,7 @@ _verify_nettle_rsa(gldns_buffer* buf, unsigned int digest_size, char* sigblock, #ifdef USE_ECDSA static char * -_verify_nettle_ecdsa(gldns_buffer* buf, unsigned int digest_size, unsigned char* sigblock, +_verify_nettle_ecdsa(sldns_buffer* buf, unsigned int digest_size, unsigned char* sigblock, unsigned int sigblock_len, unsigned char* key, unsigned int keylen) { int res = 0; @@ -1563,8 +1576,8 @@ _verify_nettle_ecdsa(gldns_buffer* buf, unsigned int digest_size, unsigned char* nettle_mpz_init_set_str_256_u(y, SHA256_DIGEST_SIZE, key+SHA256_DIGEST_SIZE); nettle_mpz_set_str_256_u(signature.r, SHA256_DIGEST_SIZE, sigblock); nettle_mpz_set_str_256_u(signature.s, SHA256_DIGEST_SIZE, sigblock+SHA256_DIGEST_SIZE); - res = _digest_nettle(SHA256_DIGEST_SIZE, (unsigned char*)gldns_buffer_begin(buf), - (unsigned int)gldns_buffer_limit(buf), (unsigned char*)digest); + res = _digest_nettle(SHA256_DIGEST_SIZE, (unsigned char*)sldns_buffer_begin(buf), + (unsigned int)sldns_buffer_limit(buf), (unsigned char*)digest); res &= nettle_ecc_point_set(&pubkey, x, y); res &= nettle_ecdsa_verify (&pubkey, SHA256_DIGEST_SIZE, digest, &signature); mpz_clear(x); @@ -1580,8 +1593,8 @@ _verify_nettle_ecdsa(gldns_buffer* buf, unsigned int digest_size, unsigned char* nettle_mpz_init_set_str_256_u(y, SHA384_DIGEST_SIZE, key+SHA384_DIGEST_SIZE); nettle_mpz_set_str_256_u(signature.r, SHA384_DIGEST_SIZE, sigblock); nettle_mpz_set_str_256_u(signature.s, SHA384_DIGEST_SIZE, sigblock+SHA384_DIGEST_SIZE); - res = _digest_nettle(SHA384_DIGEST_SIZE, (unsigned char*)gldns_buffer_begin(buf), - (unsigned int)gldns_buffer_limit(buf), (unsigned char*)digest); + res = _digest_nettle(SHA384_DIGEST_SIZE, (unsigned char*)sldns_buffer_begin(buf), + (unsigned int)sldns_buffer_limit(buf), (unsigned char*)digest); res &= nettle_ecc_point_set(&pubkey, x, y); res &= nettle_ecdsa_verify (&pubkey, SHA384_DIGEST_SIZE, digest, &signature); mpz_clear(x); @@ -1615,8 +1628,8 @@ _verify_nettle_ecdsa(gldns_buffer* buf, unsigned int digest_size, unsigned char* * @return secure if verification succeeded, bogus on crypto failure, * unchecked on format errors and alloc failures. */ -int -_getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, +enum sec_status +verify_canonrrset(sldns_buffer* buf, int algo, unsigned char* sigblock, unsigned int sigblock_len, unsigned char* key, unsigned int keylen, char** reason) { @@ -1624,54 +1637,54 @@ _getdns_verify_canonrrset(gldns_buffer* buf, int algo, unsigned char* sigblock, if (sigblock_len == 0 || keylen == 0) { *reason = "null signature"; - return 0; + return sec_status_bogus; } switch(algo) { #ifdef USE_DSA - case GLDNS_DSA: - case GLDNS_DSA_NSEC3: + case LDNS_DSA: + case LDNS_DSA_NSEC3: *reason = _verify_nettle_dsa(buf, sigblock, sigblock_len, key, keylen); if (*reason != NULL) - return 0; + return sec_status_bogus; else - return 1; + return sec_status_secure; #endif /* USE_DSA */ - case GLDNS_RSASHA1: - case GLDNS_RSASHA1_NSEC3: + case LDNS_RSASHA1: + case LDNS_RSASHA1_NSEC3: digest_size = (digest_size ? digest_size : SHA1_DIGEST_SIZE); #ifdef USE_SHA2 - case GLDNS_RSASHA256: + case LDNS_RSASHA256: digest_size = (digest_size ? digest_size : SHA256_DIGEST_SIZE); - case GLDNS_RSASHA512: + case LDNS_RSASHA512: digest_size = (digest_size ? digest_size : SHA512_DIGEST_SIZE); #endif *reason = _verify_nettle_rsa(buf, digest_size, (char*)sigblock, sigblock_len, key, keylen); if (*reason != NULL) - return 0; + return sec_status_bogus; else - return 1; + return sec_status_secure; #ifdef USE_ECDSA - case GLDNS_ECDSAP256SHA256: + case LDNS_ECDSAP256SHA256: digest_size = (digest_size ? digest_size : SHA256_DIGEST_SIZE); - case GLDNS_ECDSAP384SHA384: + case LDNS_ECDSAP384SHA384: digest_size = (digest_size ? digest_size : SHA384_DIGEST_SIZE); *reason = _verify_nettle_ecdsa(buf, digest_size, sigblock, sigblock_len, key, keylen); if (*reason != NULL) - return 0; + return sec_status_bogus; else - return 1; + return sec_status_secure; #endif - case GLDNS_RSAMD5: - case GLDNS_ECC_GOST: + case LDNS_RSAMD5: + case LDNS_ECC_GOST: default: *reason = "unable to verify signature, unknown algorithm"; - return 0; + return sec_status_bogus; } } diff --git a/src/util/val_secalgo.h b/src/util/val_secalgo.h index 704449ec..d9904f06 100644 --- a/src/util/val_secalgo.h +++ b/src/util/val_secalgo.h @@ -1,107 +1,78 @@ +/** + * + * \file rbtree.h + * /brief Alternative symbol names for unbound's rbtree.h + * + */ /* - * validator/val_secalgo.h - validator security algorithm functions. + * Copyright (c) 2017, NLnet Labs, the getdns team + * All rights reserved. * - * Copyright (c) 2012, NLnet Labs. All rights reserved. - * - * This software is open source. - * * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * Neither the name of the NLNET LABS nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the names of the copyright holders nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#ifndef VAL_SECALGO_H_SYMBOLS +#define VAL_SECALGO_H_SYMBOLS +#define sldns_buffer gldns_buffer +#define nsec3_hash_algo_size_supported _getdns_nsec3_hash_algo_size_supported +#define secalgo_nsec3_hash _getdns_secalgo_nsec3_hash +#define secalgo_hash_sha256 _getdns_secalgo_hash_sha256 +#define ds_digest_size_supported _getdns_ds_digest_size_supported +#define secalgo_ds_digest _getdns_secalgo_ds_digest +#define dnskey_algo_id_is_supported _getdns_dnskey_algo_id_is_supported +#define verify_canonrrset _getdns_verify_canonrrset +#define sec_status _getdns_sec_status +#define sec_status_secure _getdns_sec_status_secure +#define sec_status_insecure _getdns_sec_status_insecure +#define sec_status_unchecked _getdns_sec_status_unchecked +#define sec_status_bogus _getdns_sec_status_bogus -/** - * \file - * - * This file contains helper functions for the validator module. - * The functions take buffers with raw data and convert to library calls. - */ +enum sec_status { sec_status_bogus = 0 + , sec_status_unchecked = 0 + , sec_status_insecure = 0 + , sec_status_secure = 1 }; +#define NSEC3_HASH_SHA1 0x01 -#ifndef VALIDATOR_VAL_SECALGO_H -#define VALIDATOR_VAL_SECALGO_H -struct gldns_buffer; - -/** Return size of nsec3 hash algorithm, 0 if not supported */ -size_t _getdns_nsec3_hash_algo_size_supported(int id); - -/** - * Hash a single hash call of an NSEC3 hash algorithm. - * Iterations and salt are done by the caller. - * @param algo: nsec3 hash algorithm. - * @param buf: the buffer to digest - * @param len: length of buffer to digest. - * @param res: result stored here (must have sufficient space). - * @return false on failure. -*/ -int _getdns_secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, - unsigned char* res); - -/** - * Calculate the sha256 hash for the data buffer into the result. - * @param buf: buffer to digest. - * @param len: length of the buffer to digest. - * @param res: result is stored here (space 256/8 bytes). - */ -void _getdns_secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res); - -/** - * Return size of DS digest according to its hash algorithm. - * @param algo: DS digest algo. - * @return size in bytes of digest, or 0 if not supported. - */ -size_t _getdns_ds_digest_size_supported(int algo); - -/** - * @param algo: the DS digest algo - * @param buf: the buffer to digest - * @param len: length of buffer to digest. - * @param res: result stored here (must have sufficient space). - * @return false on failure. - */ -int _getdns_secalgo_ds_digest(int algo, unsigned char* buf, size_t len, - unsigned char* res); - -/** return true if DNSKEY algorithm id is supported */ -int _getdns_dnskey_algo_id_is_supported(int id); - -/** - * Check a canonical sig+rrset and signature against a dnskey - * @param buf: buffer with data to verify, the first rrsig part and the - * canonicalized rrset. - * @param algo: DNSKEY algorithm. - * @param sigblock: signature rdata field from RRSIG - * @param sigblock_len: length of sigblock data. - * @param key: public key data from DNSKEY RR. - * @param keylen: length of keydata. - * @param reason: bogus reason in more detail. - * @return secure if verification succeeded, bogus on crypto failure, - * unchecked on format errors and alloc failures. - */ -int _getdns_verify_canonrrset(struct gldns_buffer* buf, int algo, - unsigned char* sigblock, unsigned int sigblock_len, - unsigned char* key, unsigned int keylen, char** reason); - -#endif /* VALIDATOR_VAL_SECALGO_H */ +#define LDNS_SHA1 GLDNS_SHA1 +#define LDNS_SHA256 GLDNS_SHA256 +#define LDNS_SHA384 GLDNS_SHA384 +#define LDNS_HASH_GOST GLDNS_HASH_GOST +#define LDNS_RSAMD5 GLDNS_RSAMD5 +#define LDNS_DSA GLDNS_DSA +#define LDNS_DSA_NSEC3 GLDNS_DSA_NSEC3 +#define LDNS_RSASHA1 GLDNS_RSASHA1 +#define LDNS_RSASHA1_NSEC3 GLDNS_RSASHA1_NSEC3 +#define LDNS_RSASHA256 GLDNS_RSASHA256 +#define LDNS_RSASHA512 GLDNS_RSASHA512 +#define LDNS_ECDSAP256SHA256 GLDNS_ECDSAP256SHA256 +#define LDNS_ECDSAP384SHA384 GLDNS_ECDSAP384SHA384 +#define LDNS_ECC_GOST GLDNS_ECC_GOST +#define sldns_key_EVP_load_gost_id gldns_key_EVP_load_gost_id +#define sldns_digest_evp gldns_digest_evp +#define sldns_key_buf2dsa_raw gldns_key_buf2dsa_raw +#define sldns_key_buf2rsa_raw gldns_key_buf2rsa_raw +#define sldns_gost2pkey_raw gldns_gost2pkey_raw +#define sldns_ecdsa2pkey_raw gldns_ecdsa2pkey_raw +#define sldns_buffer_begin gldns_buffer_begin +#define sldns_buffer_limit gldns_buffer_limit +#include "util/ub/val_secalgo.h" +#endif From 0ecaf163d9ae7be742bb5dda8068ed774c773c05 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Wed, 8 Mar 2017 23:14:24 +0100 Subject: [PATCH 13/25] Update original source directly --- src/util/import.sh | 62 ++++------------------------------------------ 1 file changed, 5 insertions(+), 57 deletions(-) diff --git a/src/util/import.sh b/src/util/import.sh index 82f03921..43c9be7a 100755 --- a/src/util/import.sh +++ b/src/util/import.sh @@ -1,60 +1,8 @@ #!/bin/sh -# Meant to be run from this directory +REPO=http://unbound.net/svn/trunk -mkdir ub || true -cd ub -for f in rbtree.c rbtree.h -do - wget http://unbound.net/svn/trunk/util/$f || \ - ftp http://unbound.net/svn/trunk/util/$f || continue - sed -e 's/event_/_getdns_event_/g' \ - -e 's/signal_add/_getdns_signal_add/g' \ - -e 's/signal_del/_getdns_signal_del/g' \ - -e 's/signal_set/_getdns_signal_set/g' \ - -e 's/evtimer_/_getdns_evtimer_/g' \ - -e 's/struct event/struct _getdns_event/g' \ - -e 's/mini_ev_cmp/_getdns_mini_ev_cmp/g' \ - -e 's/static void handle_timeouts/void handle_timeouts/g' \ - -e 's/handle_timeouts/_getdns_handle_timeouts/g' \ - -e 's/static int handle_select/int handle_select/g' \ - -e 's/handle_select/_getdns_handle_select/g' \ - -e 's/#include "rbtree\.h"/#include "util\/rbtree.h"/g' \ - -e 's/rbnode_/_getdns_rbnode_/g' \ - -e 's/rbtree_/_getdns_rbtree_/g' \ - -e 's/traverse_post/_getdns_traverse_post/g' \ - -e 's/#include "fptr_wlist\.h"/#include "util\/fptr_wlist.h"/g' \ - -e 's/#include "log\.h"/#include "util\/log.h"/g' \ - -e '/^#define _getdns_.* mini_getdns_/d' \ - -e '/^\/\* redefine to use our own namespace so that on platforms where$/d' \ - -e '/^ \* linkers crosslink library-private symbols with other symbols, it works \*\//d' \ - $f > ../$f -done -for f in val_secalgo.h val_secalgo.c -do - wget http://unbound.net/svn/trunk/validator/$f || \ - ftp http://unbound.net/svn/trunk/validator/$f || continue - sed -e 's/sldns/gldns/g' \ - -e '/^\/\* packed_rrset on top to define enum types (forced by c99 standard) \*\/$/d' \ - -e '/^#include "util\/data\/packed_rrset.h"$/d' \ - -e 's/^#include "validator/#include "util/g' \ - -e 's/^#include "gldns\/sbuffer/#include "gldns\/gbuffer/g' \ - -e 's/^#include "util\/val_nsec3.h"/#define NSEC3_HASH_SHA1 0x01/g' \ - -e 's/ds_digest_size_supported/_getdns_ds_digest_size_supported/g' \ - -e 's/secalgo_ds_digest/_getdns_secalgo_ds_digest/g' \ - -e 's/dnskey_algo_id_is_supported/_getdns_dnskey_algo_id_is_supported/g' \ - -e 's/verify_canonrrset/_getdns_verify_canonrrset/g' \ - -e 's/nsec3_hash_algo_size_supported/_getdns_nsec3_hash_algo_size_supported/g' \ - -e 's/secalgo_nsec3_hash/_getdns_secalgo_nsec3_hash/g' \ - -e 's/secalgo_hash_sha256/_getdns_secalgo_hash_sha256/g' \ - -e 's/ecdsa_evp_workaround_init/_getdns_ecdsa_evp_workaround_init/g' \ - -e 's/LDNS_/GLDNS_/g' \ - -e 's/enum sec_status/int/g' \ - -e 's/sec_status_bogus/0/g' \ - -e 's/sec_status_unchecked/0/g' \ - -e 's/sec_status_secure/1/g' \ - $f > ../$f -done - -cd .. -rm -r ub +wget -O rbtree.c ${REPO}/util/rbtree.c +wget -O ub/rbtree.h ${REPO}/util/rbtree.h +wget -O val_secalgo.c ${REPO}/validator/val_secalgo.c +wget -O ub/val_secalgo.h ${REPO}/validator/val_secalgo.h From dd656b7421bfbf4a622535291dd64df89c1ca9f5 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Thu, 9 Mar 2017 10:44:38 +0100 Subject: [PATCH 14/25] More comprehensible auxiliary directory names (in src/util) --- spec/example/Makefile.in | 24 +- src/Makefile.in | 368 +++++++----------- src/test/Makefile.in | 71 ++-- src/tools/Makefile.in | 7 +- src/util/{ => auxiliary}/fptr_wlist.h | 0 src/util/auxiliary/log.h | 1 + .../{ub_reroute => auxiliary}/sldns/keyraw.h | 0 .../{ub_reroute => auxiliary}/sldns/rrdef.h | 0 .../{ub_reroute => auxiliary}/sldns/sbuffer.h | 0 .../util/data/packed_rrset.h | 0 src/util/{ => auxiliary/util}/log.h | 0 .../validator/val_nsec3.h | 0 .../validator/val_secalgo.h | 0 src/util/import.sh | 8 +- src/util/{ub => orig-headers}/rbtree.h | 0 src/util/{ub => orig-headers}/val_secalgo.h | 0 src/util/rbtree.h | 2 +- src/util/val_secalgo.h | 2 +- 18 files changed, 187 insertions(+), 296 deletions(-) rename src/util/{ => auxiliary}/fptr_wlist.h (100%) create mode 100644 src/util/auxiliary/log.h rename src/util/{ub_reroute => auxiliary}/sldns/keyraw.h (100%) rename src/util/{ub_reroute => auxiliary}/sldns/rrdef.h (100%) rename src/util/{ub_reroute => auxiliary}/sldns/sbuffer.h (100%) rename src/util/{ub_reroute => auxiliary}/util/data/packed_rrset.h (100%) rename src/util/{ => auxiliary/util}/log.h (100%) rename src/util/{ub_reroute => auxiliary}/validator/val_nsec3.h (100%) rename src/util/{ub_reroute => auxiliary}/validator/val_secalgo.h (100%) rename src/util/{ub => orig-headers}/rbtree.h (100%) rename src/util/{ub => orig-headers}/val_secalgo.h (100%) diff --git a/spec/example/Makefile.in b/spec/example/Makefile.in index 8ff7f2d1..7bf5e016 100644 --- a/spec/example/Makefile.in +++ b/spec/example/Makefile.in @@ -149,24 +149,16 @@ depend: # Dependencies for the examples example-all-functions.lo example-all-functions.o: $(srcdir)/example-all-functions.c $(srcdir)/getdns_libevent.h \ - ../../src/config.h \ - ../../src/getdns/getdns.h \ - $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ - ../../src/getdns/getdns_extra.h -example-reverse.lo example-reverse.o: $(srcdir)/example-reverse.c $(srcdir)/getdns_libevent.h \ - ../../src/config.h \ - ../../src/getdns/getdns.h \ - $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ + ../../src/config.h ../../src/getdns/getdns.h \ + $(srcdir)/../../src/getdns/getdns_ext_libevent.h ../../src/getdns/getdns_extra.h +example-reverse.lo example-reverse.o: $(srcdir)/example-reverse.c $(srcdir)/getdns_libevent.h ../../src/config.h \ + ../../src/getdns/getdns.h $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ ../../src/getdns/getdns_extra.h example-simple-answers.lo example-simple-answers.o: $(srcdir)/example-simple-answers.c $(srcdir)/getdns_libevent.h \ - ../../src/config.h \ - ../../src/getdns/getdns.h \ - $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ - ../../src/getdns/getdns_extra.h + ../../src/config.h ../../src/getdns/getdns.h \ + $(srcdir)/../../src/getdns/getdns_ext_libevent.h ../../src/getdns/getdns_extra.h example-synchronous.lo example-synchronous.o: $(srcdir)/example-synchronous.c $(srcdir)/getdns_core_only.h \ ../../src/getdns/getdns.h -example-tree.lo example-tree.o: $(srcdir)/example-tree.c $(srcdir)/getdns_libevent.h \ - ../../src/config.h \ - ../../src/getdns/getdns.h \ - $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ +example-tree.lo example-tree.o: $(srcdir)/example-tree.c $(srcdir)/getdns_libevent.h ../../src/config.h \ + ../../src/getdns/getdns.h $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ ../../src/getdns/getdns_extra.h diff --git a/src/Makefile.in b/src/Makefile.in index 2b0502f4..4997677e 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -48,7 +48,7 @@ srcdir = @srcdir@ LIBTOOL = ../libtool CC=@CC@ -CFLAGS=-I$(srcdir) -I. -I$(srcdir)/util/ub_reroute @CFLAGS@ @CPPFLAGS@ $(XTRA_CFLAGS) +CFLAGS=-I$(srcdir) -I. -I$(srcdir)/util/auxiliary @CFLAGS@ @CPPFLAGS@ $(XTRA_CFLAGS) WPEDANTICFLAG=@WPEDANTICFLAG@ WNOERRORFLAG=@WNOERRORFLAG@ LDFLAGS=@LDFLAGS@ @LIBS@ @@ -191,7 +191,7 @@ Makefile: $(srcdir)/Makefile.in ../config.status depend: (cd $(srcdir) ; awk 'BEGIN{P=1}{if(P)print}/^# Dependencies/{P=0}' Makefile.in > Makefile.in.new ) - (blddir=`pwd`; cd $(srcdir) ; gcc -MM -I. -I"$$blddir" *.c gldns/*.c compat/*.c util/*.c jsmn/*.c extension/*.c| \ + (blddir=`pwd`; cd $(srcdir) ; gcc -MM -I. -I"$$blddir" -Iutil/auxiliary *.c gldns/*.c compat/*.c util/*.c jsmn/*.c extension/*.c| \ sed -e "s? $$blddir/? ?g" \ -e 's?gldns/?$$(srcdir)/gldns/?g' \ -e 's?compat/?$$(srcdir)/compat/?g' \ @@ -215,232 +215,160 @@ depend: FORCE: # Dependencies for gldns, utils, the extensions and compat functions -const-info.lo const-info.o: $(srcdir)/const-info.c \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/const-info.h -context.lo context.o: $(srcdir)/context.c \ - config.h \ - $(srcdir)/debug.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/wire2str.h $(srcdir)/context.h \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/extension/default_eventloop.h \ - $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/server.h \ - $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h \ - $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/stub.h $(srcdir)/list.h $(srcdir)/dict.h $(srcdir)/pubkey-pinning.h -convert.lo convert.o: $(srcdir)/convert.c \ - config.h \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/util-internal.h $(srcdir)/context.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ - $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ - $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ - $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/str2wire.h \ - $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/parseutil.h $(srcdir)/const-info.h $(srcdir)/dict.h $(srcdir)/list.h $(srcdir)/jsmn/jsmn.h \ - $(srcdir)/convert.h -dict.lo dict.o: $(srcdir)/dict.c \ - config.h \ - $(srcdir)/types-internal.h \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/util/rbtree.h $(srcdir)/util-internal.h $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h \ - $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ +const-info.lo const-info.o: $(srcdir)/const-info.c getdns/getdns.h getdns/getdns_extra.h \ + getdns/getdns.h $(srcdir)/const-info.h +context.lo context.o: $(srcdir)/context.c config.h $(srcdir)/debug.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h \ + $(srcdir)/gldns/wire2str.h $(srcdir)/context.h getdns/getdns.h getdns/getdns_extra.h \ + getdns/getdns.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h config.h \ + $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ + $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ + $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/stub.h $(srcdir)/list.h $(srcdir)/dict.h \ + $(srcdir)/pubkey-pinning.h +convert.lo convert.o: $(srcdir)/convert.c config.h getdns/getdns.h getdns/getdns_extra.h \ + getdns/getdns.h $(srcdir)/util-internal.h $(srcdir)/context.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h config.h \ + $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ + $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ + $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h \ + $(srcdir)/gldns/parseutil.h $(srcdir)/const-info.h $(srcdir)/dict.h $(srcdir)/list.h $(srcdir)/jsmn/jsmn.h $(srcdir)/convert.h +dict.lo dict.o: $(srcdir)/dict.c config.h $(srcdir)/types-internal.h getdns/getdns.h \ + getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/util-internal.h $(srcdir)/context.h \ + $(srcdir)/extension/default_eventloop.h config.h $(srcdir)/extension/poll_eventloop.h \ + getdns/getdns_extra.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dict.h $(srcdir)/list.h \ - $(srcdir)/const-info.h $(srcdir)/gldns/wire2str.h -dnssec.lo dnssec.o: $(srcdir)/dnssec.c \ - config.h \ - $(srcdir)/debug.h \ - getdns/getdns.h \ - $(srcdir)/context.h \ - getdns/getdns_extra.h \ - $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/extension/default_eventloop.h \ - $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/server.h \ - $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h \ - $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/wire2str.h \ - $(srcdir)/gldns/keyraw.h $(srcdir)/gldns/parseutil.h $(srcdir)/general.h $(srcdir)/dict.h $(srcdir)/list.h \ - $(srcdir)/util/val_secalgo.h -general.lo general.o: $(srcdir)/general.c \ - config.h \ - $(srcdir)/general.h \ - getdns/getdns.h \ - $(srcdir)/types-internal.h \ - getdns/getdns_extra.h \ - $(srcdir)/util/rbtree.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/gldns/wire2str.h $(srcdir)/context.h \ - $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ - $(srcdir)/types-internal.h $(srcdir)/server.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ - $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/stub.h $(srcdir)/dict.h \ - $(srcdir)/mdns.h -list.lo list.o: $(srcdir)/list.c $(srcdir)/types-internal.h \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/util/rbtree.h $(srcdir)/util-internal.h \ - config.h \ - $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ - $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ - $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/list.h $(srcdir)/dict.h -mdns.lo mdns.o: $(srcdir)/mdns.c \ - config.h \ - $(srcdir)/debug.h $(srcdir)/context.h \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/extension/default_eventloop.h \ - $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/general.h \ - $(srcdir)/gldns/pkthdr.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ - $(srcdir)/mdns.h -pubkey-pinning.lo pubkey-pinning.o: $(srcdir)/pubkey-pinning.c \ - config.h \ - $(srcdir)/debug.h \ - getdns/getdns.h \ - $(srcdir)/context.h \ - getdns/getdns_extra.h \ - $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/extension/default_eventloop.h \ - $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/server.h \ - $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h -request-internal.lo request-internal.o: $(srcdir)/request-internal.c \ - config.h \ - $(srcdir)/types-internal.h \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/util/rbtree.h $(srcdir)/util-internal.h $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h \ - $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ + $(srcdir)/const-info.h $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/parseutil.h +dnssec.lo dnssec.o: $(srcdir)/dnssec.c config.h $(srcdir)/debug.h getdns/getdns.h $(srcdir)/context.h \ + getdns/getdns_extra.h getdns/getdns.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h config.h \ + $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ + $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ + $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h \ + $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/keyraw.h $(srcdir)/gldns/parseutil.h $(srcdir)/general.h $(srcdir)/dict.h \ + $(srcdir)/list.h $(srcdir)/util/val_secalgo.h $(srcdir)/util/orig-headers/val_secalgo.h +general.lo general.o: $(srcdir)/general.c config.h $(srcdir)/general.h getdns/getdns.h $(srcdir)/types-internal.h \ + getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/gldns/wire2str.h $(srcdir)/context.h \ + $(srcdir)/extension/default_eventloop.h config.h $(srcdir)/extension/poll_eventloop.h \ + getdns/getdns_extra.h $(srcdir)/types-internal.h $(srcdir)/server.h $(srcdir)/util-internal.h \ + $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h \ + $(srcdir)/gldns/rrdef.h $(srcdir)/stub.h $(srcdir)/dict.h $(srcdir)/mdns.h +list.lo list.o: $(srcdir)/list.c $(srcdir)/types-internal.h getdns/getdns.h getdns/getdns_extra.h \ + getdns/getdns.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/util-internal.h \ + config.h $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h config.h \ + $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ + $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ + $(srcdir)/gldns/pkthdr.h $(srcdir)/list.h $(srcdir)/dict.h +mdns.lo mdns.o: $(srcdir)/mdns.c config.h $(srcdir)/debug.h $(srcdir)/context.h getdns/getdns.h \ + getdns/getdns_extra.h getdns/getdns.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h config.h \ + $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ + $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/general.h $(srcdir)/gldns/pkthdr.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h \ + $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/mdns.h +pubkey-pinning.lo pubkey-pinning.o: $(srcdir)/pubkey-pinning.c config.h $(srcdir)/debug.h getdns/getdns.h \ + $(srcdir)/context.h getdns/getdns.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h \ + config.h $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h \ + $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ + $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h +request-internal.lo request-internal.o: $(srcdir)/request-internal.c config.h $(srcdir)/types-internal.h \ + getdns/getdns.h getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/util-internal.h $(srcdir)/context.h \ + $(srcdir)/extension/default_eventloop.h config.h $(srcdir)/extension/poll_eventloop.h \ + getdns/getdns_extra.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/rrdef.h \ $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/dict.h $(srcdir)/convert.h -rr-dict.lo rr-dict.o: $(srcdir)/rr-dict.c $(srcdir)/rr-dict.h \ - config.h \ - getdns/getdns.h \ - $(srcdir)/gldns/gbuffer.h $(srcdir)/util-internal.h $(srcdir)/context.h \ - getdns/getdns_extra.h \ - $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/extension/default_eventloop.h \ - $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ +rr-dict.lo rr-dict.o: $(srcdir)/rr-dict.c $(srcdir)/rr-dict.h config.h getdns/getdns.h $(srcdir)/gldns/gbuffer.h \ + $(srcdir)/util-internal.h $(srcdir)/context.h getdns/getdns_extra.h getdns/getdns.h \ + $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h \ + $(srcdir)/extension/default_eventloop.h config.h $(srcdir)/extension/poll_eventloop.h \ + getdns/getdns_extra.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ $(srcdir)/rr-iter.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dict.h -rr-iter.lo rr-iter.o: $(srcdir)/rr-iter.c $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ - config.h \ - getdns/getdns.h \ +rr-iter.lo rr-iter.o: $(srcdir)/rr-iter.c $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h config.h getdns/getdns.h \ $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/rrdef.h -server.lo server.o: $(srcdir)/server.c \ - config.h \ - getdns/getdns_extra.h \ - getdns/getdns.h \ - $(srcdir)/context.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/extension/default_eventloop.h \ - $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h -stub.lo stub.o: $(srcdir)/stub.c \ - config.h \ - $(srcdir)/debug.h $(srcdir)/stub.h \ - getdns/getdns.h \ - $(srcdir)/types-internal.h \ - getdns/getdns_extra.h \ - $(srcdir)/util/rbtree.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/rrdef.h \ +server.lo server.o: $(srcdir)/server.c config.h getdns/getdns_extra.h getdns/getdns.h \ + $(srcdir)/context.h getdns/getdns.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h config.h \ + $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ + $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h +stub.lo stub.o: $(srcdir)/stub.c config.h $(srcdir)/debug.h $(srcdir)/stub.h getdns/getdns.h $(srcdir)/types-internal.h \ + getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/rrdef.h \ $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/wire2str.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ - $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ - $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util-internal.h $(srcdir)/general.h \ - $(srcdir)/pubkey-pinning.h -sync.lo sync.o: $(srcdir)/sync.c \ - getdns/getdns.h \ - config.h \ - $(srcdir)/context.h \ - getdns/getdns_extra.h \ - $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/extension/default_eventloop.h \ - $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ - $(srcdir)/general.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ - $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/stub.h $(srcdir)/gldns/wire2str.h -ub_loop.lo ub_loop.o: $(srcdir)/ub_loop.c $(srcdir)/ub_loop.h \ - config.h \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/debug.h -util-internal.lo util-internal.o: $(srcdir)/util-internal.c \ - config.h \ - getdns/getdns.h \ - $(srcdir)/dict.h $(srcdir)/util/rbtree.h $(srcdir)/types-internal.h \ - getdns/getdns_extra.h \ - $(srcdir)/list.h $(srcdir)/util-internal.h $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h \ - $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ + $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h config.h \ + $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ + $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util-internal.h $(srcdir)/general.h $(srcdir)/pubkey-pinning.h +sync.lo sync.o: $(srcdir)/sync.c getdns/getdns.h config.h $(srcdir)/context.h getdns/getdns_extra.h \ + getdns/getdns.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h config.h \ + $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ + $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/general.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ + $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/stub.h \ + $(srcdir)/gldns/wire2str.h +ub_loop.lo ub_loop.o: $(srcdir)/ub_loop.c $(srcdir)/ub_loop.h config.h getdns/getdns.h \ + getdns/getdns_extra.h getdns/getdns.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/debug.h +util-internal.lo util-internal.o: $(srcdir)/util-internal.c config.h getdns/getdns.h $(srcdir)/dict.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/types-internal.h \ + getdns/getdns_extra.h getdns/getdns.h $(srcdir)/list.h $(srcdir)/util-internal.h $(srcdir)/context.h \ + $(srcdir)/extension/default_eventloop.h config.h $(srcdir)/extension/poll_eventloop.h \ + getdns/getdns_extra.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/str2wire.h \ $(srcdir)/gldns/rrdef.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h -gbuffer.lo gbuffer.o: $(srcdir)/gldns/gbuffer.c \ - config.h \ +version.lo version.o: version.c +gbuffer.lo gbuffer.o: $(srcdir)/gldns/gbuffer.c config.h $(srcdir)/gldns/gbuffer.h +keyraw.lo keyraw.o: $(srcdir)/gldns/keyraw.c config.h $(srcdir)/gldns/keyraw.h $(srcdir)/gldns/rrdef.h +parse.lo parse.o: $(srcdir)/gldns/parse.c config.h $(srcdir)/gldns/parse.h $(srcdir)/gldns/parseutil.h \ $(srcdir)/gldns/gbuffer.h -keyraw.lo keyraw.o: $(srcdir)/gldns/keyraw.c \ - config.h \ - $(srcdir)/gldns/keyraw.h $(srcdir)/gldns/rrdef.h -parse.lo parse.o: $(srcdir)/gldns/parse.c \ - config.h \ - $(srcdir)/gldns/parse.h $(srcdir)/gldns/parseutil.h $(srcdir)/gldns/gbuffer.h -parseutil.lo parseutil.o: $(srcdir)/gldns/parseutil.c \ - config.h \ - $(srcdir)/gldns/parseutil.h -rrdef.lo rrdef.o: $(srcdir)/gldns/rrdef.c \ - config.h \ - $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/parseutil.h -str2wire.lo str2wire.o: $(srcdir)/gldns/str2wire.c \ - config.h \ - $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/gbuffer.h \ - $(srcdir)/gldns/parse.h $(srcdir)/gldns/parseutil.h -wire2str.lo wire2str.o: $(srcdir)/gldns/wire2str.c \ - config.h \ - $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/pkthdr.h \ - $(srcdir)/gldns/parseutil.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/keyraw.h -arc4_lock.lo arc4_lock.o: $(srcdir)/compat/arc4_lock.c \ - config.h -arc4random.lo arc4random.o: $(srcdir)/compat/arc4random.c \ - config.h \ - $(srcdir)/compat/chacha_private.h -arc4random_uniform.lo arc4random_uniform.o: $(srcdir)/compat/arc4random_uniform.c \ - config.h -explicit_bzero.lo explicit_bzero.o: $(srcdir)/compat/explicit_bzero.c \ - config.h -getentropy_linux.lo getentropy_linux.o: $(srcdir)/compat/getentropy_linux.c \ - config.h -getentropy_osx.lo getentropy_osx.o: $(srcdir)/compat/getentropy_osx.c \ - config.h -getentropy_solaris.lo getentropy_solaris.o: $(srcdir)/compat/getentropy_solaris.c \ - config.h +parseutil.lo parseutil.o: $(srcdir)/gldns/parseutil.c config.h $(srcdir)/gldns/parseutil.h +rrdef.lo rrdef.o: $(srcdir)/gldns/rrdef.c config.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/parseutil.h +str2wire.lo str2wire.o: $(srcdir)/gldns/str2wire.c config.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h \ + $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/parse.h $(srcdir)/gldns/parseutil.h +wire2str.lo wire2str.o: $(srcdir)/gldns/wire2str.c config.h $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/str2wire.h \ + $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/parseutil.h $(srcdir)/gldns/gbuffer.h \ + $(srcdir)/gldns/keyraw.h +arc4_lock.lo arc4_lock.o: $(srcdir)/compat/arc4_lock.c config.h +arc4random.lo arc4random.o: $(srcdir)/compat/arc4random.c config.h $(srcdir)/compat/chacha_private.h +arc4random_uniform.lo arc4random_uniform.o: $(srcdir)/compat/arc4random_uniform.c config.h +explicit_bzero.lo explicit_bzero.o: $(srcdir)/compat/explicit_bzero.c config.h +getentropy_linux.lo getentropy_linux.o: $(srcdir)/compat/getentropy_linux.c config.h +getentropy_osx.lo getentropy_osx.o: $(srcdir)/compat/getentropy_osx.c config.h +getentropy_solaris.lo getentropy_solaris.o: $(srcdir)/compat/getentropy_solaris.c config.h getentropy_win.lo getentropy_win.o: $(srcdir)/compat/getentropy_win.c -gettimeofday.lo gettimeofday.o: $(srcdir)/compat/gettimeofday.c \ - config.h -inet_ntop.lo inet_ntop.o: $(srcdir)/compat/inet_ntop.c \ - config.h -inet_pton.lo inet_pton.o: $(srcdir)/compat/inet_pton.c \ - config.h -sha512.lo sha512.o: $(srcdir)/compat/sha512.c \ - config.h -strlcpy.lo strlcpy.o: $(srcdir)/compat/strlcpy.c \ - config.h -rbtree.lo rbtree.o: $(srcdir)/util/rbtree.c \ - config.h \ - $(srcdir)/util/log.h $(srcdir)/debug.h $(srcdir)/util/fptr_wlist.h $(srcdir)/util/rbtree.h -val_secalgo.lo val_secalgo.o: $(srcdir)/util/val_secalgo.c \ - config.h \ - $(srcdir)/util/val_secalgo.h $(srcdir)/util/log.h $(srcdir)/debug.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/keyraw.h \ - $(srcdir)/gldns/gbuffer.h +gettimeofday.lo gettimeofday.o: $(srcdir)/compat/gettimeofday.c config.h +inet_ntop.lo inet_ntop.o: $(srcdir)/compat/inet_ntop.c config.h +inet_pton.lo inet_pton.o: $(srcdir)/compat/inet_pton.c config.h +sha512.lo sha512.o: $(srcdir)/compat/sha512.c config.h +strlcpy.lo strlcpy.o: $(srcdir)/compat/strlcpy.c config.h +rbtree.lo rbtree.o: $(srcdir)/util/rbtree.c config.h $(srcdir)/util/auxiliary/log.h \ + $(srcdir)/util/auxiliary/$(srcdir)/util/log.h $(srcdir)/debug.h config.h $(srcdir)/util/auxiliary/fptr_wlist.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h +val_secalgo.lo val_secalgo.o: $(srcdir)/util/val_secalgo.c config.h \ + $(srcdir)/util/auxiliary/$(srcdir)/util/data/packed_rrset.h \ + $(srcdir)/util/auxiliary/validator/val_secalgo.h $(srcdir)/util/val_secalgo.h \ + $(srcdir)/util/orig-headers/val_secalgo.h $(srcdir)/util/auxiliary/validator/val_nsec3.h \ + $(srcdir)/util/auxiliary/$(srcdir)/util/log.h $(srcdir)/debug.h config.h $(srcdir)/util/auxiliary/sldns/rrdef.h \ + $(srcdir)/gldns/rrdef.h $(srcdir)/util/auxiliary/sldns/keyraw.h $(srcdir)/gldns/keyraw.h \ + $(srcdir)/util/auxiliary/sldns/sbuffer.h $(srcdir)/gldns/gbuffer.h jsmn.lo jsmn.o: $(srcdir)/jsmn/jsmn.c $(srcdir)/jsmn/jsmn.h -libev.lo libev.o: $(srcdir)/extension/libev.c \ - config.h \ - $(srcdir)/types-internal.h \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/util/rbtree.h $(srcdir)/getdns/getdns_ext_libev.h -libevent.lo libevent.o: $(srcdir)/extension/libevent.c \ - config.h \ - $(srcdir)/types-internal.h \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/util/rbtree.h $(srcdir)/getdns/getdns_ext_libevent.h -libuv.lo libuv.o: $(srcdir)/extension/libuv.c \ - config.h \ - $(srcdir)/debug.h $(srcdir)/types-internal.h \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/util/rbtree.h $(srcdir)/getdns/getdns_ext_libuv.h -poll_eventloop.lo poll_eventloop.o: $(srcdir)/extension/poll_eventloop.c \ - config.h \ - $(srcdir)/extension/poll_eventloop.h \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/debug.h -select_eventloop.lo select_eventloop.o: $(srcdir)/extension/select_eventloop.c \ - config.h \ - $(srcdir)/extension/select_eventloop.h \ - getdns/getdns.h \ - getdns/getdns_extra.h \ - $(srcdir)/debug.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h +libev.lo libev.o: $(srcdir)/extension/libev.c config.h $(srcdir)/types-internal.h getdns/getdns.h \ + getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/getdns/getdns_ext_libev.h \ + getdns/getdns_extra.h +libevent.lo libevent.o: $(srcdir)/extension/libevent.c config.h $(srcdir)/types-internal.h \ + getdns/getdns.h getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/getdns/getdns_ext_libevent.h \ + getdns/getdns_extra.h +libuv.lo libuv.o: $(srcdir)/extension/libuv.c config.h $(srcdir)/debug.h config.h $(srcdir)/types-internal.h \ + getdns/getdns.h getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/getdns/getdns_ext_libuv.h \ + getdns/getdns_extra.h +poll_eventloop.lo poll_eventloop.o: $(srcdir)/extension/poll_eventloop.c config.h \ + $(srcdir)/extension/poll_eventloop.h getdns/getdns.h getdns/getdns_extra.h \ + $(srcdir)/types-internal.h getdns/getdns.h getdns/getdns_extra.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/debug.h config.h +select_eventloop.lo select_eventloop.o: $(srcdir)/extension/select_eventloop.c config.h \ + $(srcdir)/extension/select_eventloop.h getdns/getdns.h getdns/getdns_extra.h \ + $(srcdir)/debug.h config.h $(srcdir)/types-internal.h getdns/getdns.h getdns/getdns_extra.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h diff --git a/src/test/Makefile.in b/src/test/Makefile.in index 758435c1..c481fdab 100644 --- a/src/test/Makefile.in +++ b/src/test/Makefile.in @@ -216,13 +216,10 @@ depend: .PHONY: clean test # Dependencies for the unit tests -check_getdns.lo check_getdns.o: $(srcdir)/check_getdns.c \ - ../getdns/getdns.h \ - $(srcdir)/check_getdns_common.h \ - ../getdns/getdns_extra.h \ - $(srcdir)/check_getdns_address.h $(srcdir)/check_getdns_address_sync.h \ - $(srcdir)/check_getdns_cancel_callback.h $(srcdir)/check_getdns_context_create.h \ - $(srcdir)/check_getdns_context_destroy.h \ +check_getdns.lo check_getdns.o: $(srcdir)/check_getdns.c ../getdns/getdns.h $(srcdir)/check_getdns_common.h \ + ../getdns/getdns_extra.h $(srcdir)/check_getdns_address.h \ + $(srcdir)/check_getdns_address_sync.h $(srcdir)/check_getdns_cancel_callback.h \ + $(srcdir)/check_getdns_context_create.h $(srcdir)/check_getdns_context_destroy.h \ $(srcdir)/check_getdns_context_set_context_update_callback.h \ $(srcdir)/check_getdns_context_set_dns_transport.h \ $(srcdir)/check_getdns_context_set_timeout.h \ @@ -242,58 +239,34 @@ check_getdns.lo check_getdns.o: $(srcdir)/check_getdns.c \ $(srcdir)/check_getdns_list_get_list.h $(srcdir)/check_getdns_pretty_print_dict.h \ $(srcdir)/check_getdns_service.h $(srcdir)/check_getdns_service_sync.h \ $(srcdir)/check_getdns_transport.h -check_getdns_common.lo check_getdns_common.o: $(srcdir)/check_getdns_common.c \ - ../getdns/getdns.h \ - ../config.h \ - $(srcdir)/check_getdns_common.h \ - ../getdns/getdns_extra.h \ +check_getdns_common.lo check_getdns_common.o: $(srcdir)/check_getdns_common.c ../getdns/getdns.h \ + ../config.h $(srcdir)/check_getdns_common.h ../getdns/getdns_extra.h \ $(srcdir)/check_getdns_eventloop.h check_getdns_context_set_timeout.lo check_getdns_context_set_timeout.o: $(srcdir)/check_getdns_context_set_timeout.c \ $(srcdir)/check_getdns_context_set_timeout.h $(srcdir)/check_getdns_common.h \ - ../getdns/getdns.h \ - ../getdns/getdns_extra.h + ../getdns/getdns.h ../getdns/getdns_extra.h check_getdns_libev.lo check_getdns_libev.o: $(srcdir)/check_getdns_libev.c $(srcdir)/check_getdns_eventloop.h \ - ../config.h \ - ../getdns/getdns.h \ - $(srcdir)/../getdns/getdns_ext_libev.h \ - ../getdns/getdns_extra.h \ - $(srcdir)/check_getdns_common.h + ../config.h ../getdns/getdns.h $(srcdir)/../getdns/getdns_ext_libev.h \ + ../getdns/getdns_extra.h $(srcdir)/check_getdns_common.h check_getdns_libevent.lo check_getdns_libevent.o: $(srcdir)/check_getdns_libevent.c $(srcdir)/check_getdns_eventloop.h \ - ../config.h \ - ../getdns/getdns.h \ - $(srcdir)/../getdns/getdns_ext_libevent.h \ - ../getdns/getdns_extra.h \ - $(srcdir)/check_getdns_libevent.h $(srcdir)/check_getdns_common.h + ../config.h ../getdns/getdns.h $(srcdir)/../getdns/getdns_ext_libevent.h \ + ../getdns/getdns_extra.h $(srcdir)/check_getdns_libevent.h $(srcdir)/check_getdns_common.h check_getdns_libuv.lo check_getdns_libuv.o: $(srcdir)/check_getdns_libuv.c $(srcdir)/check_getdns_eventloop.h \ - ../config.h \ - ../getdns/getdns.h \ - $(srcdir)/../getdns/getdns_ext_libuv.h \ - ../getdns/getdns_extra.h \ - $(srcdir)/check_getdns_common.h + ../config.h ../getdns/getdns.h $(srcdir)/../getdns/getdns_ext_libuv.h \ + ../getdns/getdns_extra.h $(srcdir)/check_getdns_common.h check_getdns_selectloop.lo check_getdns_selectloop.o: $(srcdir)/check_getdns_selectloop.c \ - $(srcdir)/check_getdns_eventloop.h \ - ../config.h \ - ../getdns/getdns.h \ + $(srcdir)/check_getdns_eventloop.h ../config.h ../getdns/getdns.h \ ../getdns/getdns_extra.h check_getdns_transport.lo check_getdns_transport.o: $(srcdir)/check_getdns_transport.c \ - $(srcdir)/check_getdns_transport.h $(srcdir)/check_getdns_common.h \ - ../getdns/getdns.h \ + $(srcdir)/check_getdns_transport.h $(srcdir)/check_getdns_common.h ../getdns/getdns.h \ ../getdns/getdns_extra.h -scratchpad.template.lo scratchpad.template.o: scratchpad.template.c \ - ../getdns/getdns.h \ +scratchpad.template.lo scratchpad.template.o: scratchpad.template.c ../getdns/getdns.h \ ../getdns/getdns_extra.h testmessages.lo testmessages.o: $(srcdir)/testmessages.c $(srcdir)/testmessages.h -tests_dict.lo tests_dict.o: $(srcdir)/tests_dict.c $(srcdir)/testmessages.h \ - ../getdns/getdns.h -tests_list.lo tests_list.o: $(srcdir)/tests_list.c $(srcdir)/testmessages.h \ - ../getdns/getdns.h -tests_namespaces.lo tests_namespaces.o: $(srcdir)/tests_namespaces.c $(srcdir)/testmessages.h \ - ../getdns/getdns.h -tests_stub_async.lo tests_stub_async.o: $(srcdir)/tests_stub_async.c \ - ../config.h \ - $(srcdir)/testmessages.h \ - ../getdns/getdns.h \ - ../getdns/getdns_extra.h -tests_stub_sync.lo tests_stub_sync.o: $(srcdir)/tests_stub_sync.c $(srcdir)/testmessages.h \ - ../getdns/getdns.h \ +tests_dict.lo tests_dict.o: $(srcdir)/tests_dict.c $(srcdir)/testmessages.h ../getdns/getdns.h +tests_list.lo tests_list.o: $(srcdir)/tests_list.c $(srcdir)/testmessages.h ../getdns/getdns.h +tests_namespaces.lo tests_namespaces.o: $(srcdir)/tests_namespaces.c $(srcdir)/testmessages.h ../getdns/getdns.h +tests_stub_async.lo tests_stub_async.o: $(srcdir)/tests_stub_async.c ../config.h $(srcdir)/testmessages.h \ + ../getdns/getdns.h ../getdns/getdns_extra.h +tests_stub_sync.lo tests_stub_sync.o: $(srcdir)/tests_stub_sync.c $(srcdir)/testmessages.h ../getdns/getdns.h \ ../getdns/getdns_extra.h diff --git a/src/tools/Makefile.in b/src/tools/Makefile.in index d066e824..d98cf437 100644 --- a/src/tools/Makefile.in +++ b/src/tools/Makefile.in @@ -113,8 +113,5 @@ depend: .PHONY: clean test # Dependencies for getdns_query -getdns_query.lo getdns_query.o: $(srcdir)/getdns_query.c \ - ../config.h \ - $(srcdir)/../debug.h \ - ../getdns/getdns.h \ - ../getdns/getdns_extra.h +getdns_query.lo getdns_query.o: $(srcdir)/getdns_query.c ../config.h $(srcdir)/../debug.h ../config.h \ + ../getdns/getdns.h ../getdns/getdns_extra.h diff --git a/src/util/fptr_wlist.h b/src/util/auxiliary/fptr_wlist.h similarity index 100% rename from src/util/fptr_wlist.h rename to src/util/auxiliary/fptr_wlist.h diff --git a/src/util/auxiliary/log.h b/src/util/auxiliary/log.h new file mode 100644 index 00000000..bc885bf4 --- /dev/null +++ b/src/util/auxiliary/log.h @@ -0,0 +1 @@ +#include "util/log.h" diff --git a/src/util/ub_reroute/sldns/keyraw.h b/src/util/auxiliary/sldns/keyraw.h similarity index 100% rename from src/util/ub_reroute/sldns/keyraw.h rename to src/util/auxiliary/sldns/keyraw.h diff --git a/src/util/ub_reroute/sldns/rrdef.h b/src/util/auxiliary/sldns/rrdef.h similarity index 100% rename from src/util/ub_reroute/sldns/rrdef.h rename to src/util/auxiliary/sldns/rrdef.h diff --git a/src/util/ub_reroute/sldns/sbuffer.h b/src/util/auxiliary/sldns/sbuffer.h similarity index 100% rename from src/util/ub_reroute/sldns/sbuffer.h rename to src/util/auxiliary/sldns/sbuffer.h diff --git a/src/util/ub_reroute/util/data/packed_rrset.h b/src/util/auxiliary/util/data/packed_rrset.h similarity index 100% rename from src/util/ub_reroute/util/data/packed_rrset.h rename to src/util/auxiliary/util/data/packed_rrset.h diff --git a/src/util/log.h b/src/util/auxiliary/util/log.h similarity index 100% rename from src/util/log.h rename to src/util/auxiliary/util/log.h diff --git a/src/util/ub_reroute/validator/val_nsec3.h b/src/util/auxiliary/validator/val_nsec3.h similarity index 100% rename from src/util/ub_reroute/validator/val_nsec3.h rename to src/util/auxiliary/validator/val_nsec3.h diff --git a/src/util/ub_reroute/validator/val_secalgo.h b/src/util/auxiliary/validator/val_secalgo.h similarity index 100% rename from src/util/ub_reroute/validator/val_secalgo.h rename to src/util/auxiliary/validator/val_secalgo.h diff --git a/src/util/import.sh b/src/util/import.sh index 43c9be7a..e9626988 100755 --- a/src/util/import.sh +++ b/src/util/import.sh @@ -2,7 +2,7 @@ REPO=http://unbound.net/svn/trunk -wget -O rbtree.c ${REPO}/util/rbtree.c -wget -O ub/rbtree.h ${REPO}/util/rbtree.h -wget -O val_secalgo.c ${REPO}/validator/val_secalgo.c -wget -O ub/val_secalgo.h ${REPO}/validator/val_secalgo.h +wget -O rbtree.c ${REPO}/util/rbtree.c +wget -O orig-headers/rbtree.h ${REPO}/util/rbtree.h +wget -O val_secalgo.c ${REPO}/validator/val_secalgo.c +wget -O orig-headers/val_secalgo.h ${REPO}/validator/val_secalgo.h diff --git a/src/util/ub/rbtree.h b/src/util/orig-headers/rbtree.h similarity index 100% rename from src/util/ub/rbtree.h rename to src/util/orig-headers/rbtree.h diff --git a/src/util/ub/val_secalgo.h b/src/util/orig-headers/val_secalgo.h similarity index 100% rename from src/util/ub/val_secalgo.h rename to src/util/orig-headers/val_secalgo.h diff --git a/src/util/rbtree.h b/src/util/rbtree.h index 7772ffda..c3ae4128 100644 --- a/src/util/rbtree.h +++ b/src/util/rbtree.h @@ -46,5 +46,5 @@ #define rbtree_next _getdns_rbtree_next #define rbtree_previous _getdns_rbtree_previous #define traverse_postorder _getdns_traverse_postorder -#include "util/ub/rbtree.h" +#include "util/orig-headers/rbtree.h" #endif diff --git a/src/util/val_secalgo.h b/src/util/val_secalgo.h index d9904f06..55bf423e 100644 --- a/src/util/val_secalgo.h +++ b/src/util/val_secalgo.h @@ -74,5 +74,5 @@ enum sec_status { sec_status_bogus = 0 #define sldns_ecdsa2pkey_raw gldns_ecdsa2pkey_raw #define sldns_buffer_begin gldns_buffer_begin #define sldns_buffer_limit gldns_buffer_limit -#include "util/ub/val_secalgo.h" +#include "util/orig-headers/val_secalgo.h" #endif From 5a2ee50de356db7adc51a9688bff8b2ba2293349 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Thu, 9 Mar 2017 11:40:39 +0100 Subject: [PATCH 15/25] Have a define for any debugging --- src/debug.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/debug.h b/src/debug.h index 5087efb4..26ca50e4 100644 --- a/src/debug.h +++ b/src/debug.h @@ -138,5 +138,14 @@ #define DEBUG_MDNS(...) DEBUG_OFF(__VA_ARGS__) #endif +#if (defined(SCHED_DEBUG) && SCHED_DEBUG) || \ + (defined(STUB_DEBUG) && STUB_DEBUG) || \ + (defined(DAEMON_DEBUG) && DAEMON_DEBUG) || \ + (defined(SEC_DEBUG) && SEC_DEBUG) || \ + (defined(SERVER_DEBUG) && SERVER_DEBUG) || \ + (defined(MDNS_DEBUG) && MDNS_DEBUG) +#define DEBUGGING 1 +#endif + #endif /* debug.h */ From 5b5123a79dcab6f7fda3b98f9924492c2a0f643d Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Thu, 9 Mar 2017 11:46:15 +0100 Subject: [PATCH 16/25] HAVE_PTHREAD instead of HAVE_PTHREADS like unbound --- configure.ac | 4 ++-- src/compat/arc4_lock.c | 2 +- src/context.c | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/configure.ac b/configure.ac index 555b7792..2bd99c6f 100644 --- a/configure.ac +++ b/configure.ac @@ -992,7 +992,7 @@ fi #---- check for pthreads library -AC_SEARCH_LIBS([pthread_mutex_init],[pthread],[AC_DEFINE([HAVE_PTHREADS], [1], [Have pthreads library])], [AC_MSG_WARN([pthreads not available])]) +AC_SEARCH_LIBS([pthread_mutex_init],[pthread],[AC_DEFINE([HAVE_PTHREAD], [1], [Have pthreads library])], [AC_MSG_WARN([pthreads not available])]) AC_MSG_CHECKING([whether the C compiler (${CC-cc}) supports the __func__ variable]) AC_LANG_PUSH(C) @@ -1008,7 +1008,7 @@ dnl ----- Start of "Things needed for gldns" section dnl ----- dnl --------------------------------------------------------------------------- -AC_CHECK_HEADERS([stdarg.h stdint.h netinet/in.h arpa/inet.h netdb.h sys/socket.h time.h sys/time.h sys/select.h],,, [AC_INCLUDES_DEFAULT]) +AC_CHECK_HEADERS([stdarg.h stdint.h netinet/in.h arpa/inet.h netdb.h sys/socket.h time.h sys/time.h sys/select.h endian.h],,, [AC_INCLUDES_DEFAULT]) dnl Check the printf-format attribute (if any) dnl result in HAVE_ATTR_FORMAT. diff --git a/src/compat/arc4_lock.c b/src/compat/arc4_lock.c index 34b85974..cb9b5056 100644 --- a/src/compat/arc4_lock.c +++ b/src/compat/arc4_lock.c @@ -34,7 +34,7 @@ #include "config.h" #define LOCKRET(func) func -#ifdef HAVE_PTHREADS +#ifdef HAVE_PTHREAD #include "pthread.h" static pthread_mutex_t arc_lock = PTHREAD_MUTEX_INITIALIZER; diff --git a/src/context.c b/src/context.c index 5421fd81..1237ae7f 100644 --- a/src/context.c +++ b/src/context.c @@ -62,7 +62,7 @@ typedef unsigned short in_port_t; #include #include -#ifdef HAVE_PTHREADS +#ifdef HAVE_PTHREAD #include #endif #include @@ -93,7 +93,7 @@ typedef unsigned short in_port_t; upstream. Using 1 hour for all transports - based on RFC7858 value for for TLS.*/ #define BACKOFF_RETRY 3600 -#ifdef HAVE_PTHREADS +#ifdef HAVE_PTHREAD static pthread_mutex_t ssl_init_lock = PTHREAD_MUTEX_INITIALIZER; #endif static bool ssl_init=false; @@ -1434,7 +1434,7 @@ getdns_context_create_with_extended_memory_functions( /* Unbound needs SSL to be init'ed this early when TLS is used. However we * don't know that till later so we will have to do this every time. */ -#ifdef HAVE_PTHREADS +#ifdef HAVE_PTHREAD pthread_mutex_lock(&ssl_init_lock); #else /* XXX implement Windows-style lock here */ @@ -1444,7 +1444,7 @@ getdns_context_create_with_extended_memory_functions( SSL_library_init(); ssl_init = true; } -#ifdef HAVE_PTHREADS +#ifdef HAVE_PTHREAD pthread_mutex_unlock(&ssl_init_lock); #else /* XXX implement Windows-style unlock here */ From f751de696a5e8569b1339deedb2354ee79c7bca2 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Thu, 9 Mar 2017 12:08:53 +0100 Subject: [PATCH 17/25] Import lruhash and lookup3 from unbound --- src/Makefile.in | 13 +- src/util/auxiliary/fptr_wlist.h | 43 +- src/util/auxiliary/util/fptr_wlist.h | 42 + src/util/auxiliary/util/log.h | 17 +- src/util/auxiliary/util/storage/lookup3.h | 1 + src/util/auxiliary/util/storage/lruhash.h | 1 + src/util/import.sh | 6 + src/util/locks.c | 264 ++++++ src/util/locks.h | 64 ++ src/util/lookup3.c | 1032 +++++++++++++++++++++ src/util/lookup3.h | 41 + src/util/lruhash.c | 545 +++++++++++ src/util/lruhash.h | 68 ++ src/util/orig-headers/locks.h | 313 +++++++ src/util/orig-headers/lookup3.h | 71 ++ src/util/orig-headers/lruhash.h | 414 +++++++++ 16 files changed, 2886 insertions(+), 49 deletions(-) create mode 100644 src/util/auxiliary/util/fptr_wlist.h create mode 100644 src/util/auxiliary/util/storage/lookup3.h create mode 100644 src/util/auxiliary/util/storage/lruhash.h create mode 100644 src/util/locks.c create mode 100644 src/util/locks.h create mode 100644 src/util/lookup3.c create mode 100644 src/util/lookup3.h create mode 100644 src/util/lruhash.c create mode 100644 src/util/lruhash.h create mode 100644 src/util/orig-headers/locks.h create mode 100644 src/util/orig-headers/lookup3.h create mode 100644 src/util/orig-headers/lruhash.h diff --git a/src/Makefile.in b/src/Makefile.in index 4997677e..abbb076e 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -79,7 +79,7 @@ LIBOBJDIR= LIBOBJS=@LIBOBJS@ COMPAT_OBJ=$(LIBOBJS:.o=.lo) -UTIL_OBJ=rbtree.lo val_secalgo.lo +UTIL_OBJ=rbtree.lo val_secalgo.lo lruhash.lo lookup3.lo locks.lo JSMN_OBJ=jsmn.lo @@ -341,9 +341,18 @@ inet_ntop.lo inet_ntop.o: $(srcdir)/compat/inet_ntop.c config.h inet_pton.lo inet_pton.o: $(srcdir)/compat/inet_pton.c config.h sha512.lo sha512.o: $(srcdir)/compat/sha512.c config.h strlcpy.lo strlcpy.o: $(srcdir)/compat/strlcpy.c config.h +locks.lo locks.o: $(srcdir)/util/locks.c config.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/$(srcdir)/util/log.h $(srcdir)/debug.h config.h +lookup3.lo lookup3.o: $(srcdir)/util/lookup3.c config.h $(srcdir)/util/auxiliary/$(srcdir)/util/storage/lookup3.h \ + $(srcdir)/util/lookup3.h $(srcdir)/util/orig-headers/lookup3.h +lruhash.lo lruhash.o: $(srcdir)/util/lruhash.c config.h $(srcdir)/util/auxiliary/$(srcdir)/util/storage/lruhash.h \ + $(srcdir)/util/lruhash.h $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h \ + $(srcdir)/util/orig-headers/locks.h $(srcdir)/util/auxiliary/$(srcdir)/util/log.h $(srcdir)/debug.h config.h \ + $(srcdir)/util/auxiliary/$(srcdir)/util/fptr_wlist.h rbtree.lo rbtree.o: $(srcdir)/util/rbtree.c config.h $(srcdir)/util/auxiliary/log.h \ $(srcdir)/util/auxiliary/$(srcdir)/util/log.h $(srcdir)/debug.h config.h $(srcdir)/util/auxiliary/fptr_wlist.h \ - $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h + $(srcdir)/util/auxiliary/$(srcdir)/util/fptr_wlist.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h val_secalgo.lo val_secalgo.o: $(srcdir)/util/val_secalgo.c config.h \ $(srcdir)/util/auxiliary/$(srcdir)/util/data/packed_rrset.h \ $(srcdir)/util/auxiliary/validator/val_secalgo.h $(srcdir)/util/val_secalgo.h \ diff --git a/src/util/auxiliary/fptr_wlist.h b/src/util/auxiliary/fptr_wlist.h index d98741df..1eb5a74d 100644 --- a/src/util/auxiliary/fptr_wlist.h +++ b/src/util/auxiliary/fptr_wlist.h @@ -1,42 +1 @@ -/** - * - * /brief dummy prototypes for function pointer whitelisting - * - */ - -/* - * Copyright (c) 2013, NLnet Labs, Verisign, Inc. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither the names of the copyright holders nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef UTIL_FPTR_WLIST_H -#define UTIL_FPTR_WLIST_H - -#define fptr_ok(x) -#define fptr_whitelist_event(x) -#define fptr_whitelist_rbtree_cmp(x) - -#endif /* UTIL_FPTR_WLIST_H */ - +#include "util/fptr_wlist.h" diff --git a/src/util/auxiliary/util/fptr_wlist.h b/src/util/auxiliary/util/fptr_wlist.h new file mode 100644 index 00000000..d98741df --- /dev/null +++ b/src/util/auxiliary/util/fptr_wlist.h @@ -0,0 +1,42 @@ +/** + * + * /brief dummy prototypes for function pointer whitelisting + * + */ + +/* + * Copyright (c) 2013, NLnet Labs, Verisign, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the names of the copyright holders nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef UTIL_FPTR_WLIST_H +#define UTIL_FPTR_WLIST_H + +#define fptr_ok(x) +#define fptr_whitelist_event(x) +#define fptr_whitelist_rbtree_cmp(x) + +#endif /* UTIL_FPTR_WLIST_H */ + diff --git a/src/util/auxiliary/util/log.h b/src/util/auxiliary/util/log.h index 30c9ef8f..cdd949b2 100644 --- a/src/util/auxiliary/util/log.h +++ b/src/util/auxiliary/util/log.h @@ -37,15 +37,22 @@ #include "config.h" #include "debug.h" -#if defined(SEC_DEBUG) && SEC_DEBUG +#ifdef DEBUGGING #define verbose(x, ...) DEBUG_NL(__VA_ARGS__) -#define log_err(...) DEBUG_NL(__VA_ARGS__) +#define log_err(...) DEBUG_NL(__VA_ARGS__) +#define log_info(...) DEBUG_NL(__VA_ARGS__) +#define fatal_exit(...) do { DEBUG_NL(__VA_ARGS__); exit(EXIT_FAILURE); } while(0) +#define log_assert(x) do { if(!(x)) fatal_exit("%s:%d: %s: assertion %s failed", \ + __FILE__, __LINE__, __FUNC__, #x); \ + } while(0) #else -#define verbose(...) -#define log_err(...) +#define verbose(...) ((void)0) +#define log_err(...) ((void)0) +#define log_info(...) ((void)0) +#define fatal_exit(...) ((void)0) +#define log_assert(x) ((void)0) #endif -#define log_assert(x) #endif /* UTIL_LOG_H */ diff --git a/src/util/auxiliary/util/storage/lookup3.h b/src/util/auxiliary/util/storage/lookup3.h new file mode 100644 index 00000000..d00a0b5c --- /dev/null +++ b/src/util/auxiliary/util/storage/lookup3.h @@ -0,0 +1 @@ +#include "util/lookup3.h" diff --git a/src/util/auxiliary/util/storage/lruhash.h b/src/util/auxiliary/util/storage/lruhash.h new file mode 100644 index 00000000..cb2d18a1 --- /dev/null +++ b/src/util/auxiliary/util/storage/lruhash.h @@ -0,0 +1 @@ +#include "util/lruhash.h" diff --git a/src/util/import.sh b/src/util/import.sh index e9626988..49050268 100755 --- a/src/util/import.sh +++ b/src/util/import.sh @@ -6,3 +6,9 @@ wget -O rbtree.c ${REPO}/util/rbtree.c wget -O orig-headers/rbtree.h ${REPO}/util/rbtree.h wget -O val_secalgo.c ${REPO}/validator/val_secalgo.c wget -O orig-headers/val_secalgo.h ${REPO}/validator/val_secalgo.h +wget -O lruhash.c ${REPO}/util/storage/lruhash.c +wget -O orig-headers/lruhash.h ${REPO}/util/storage/lruhash.h +wget -O lookup3.c ${REPO}/util/storage/lookup3.c +wget -O orig-headers/lookup3.h ${REPO}/util/storage/lookup3.h +wget -O locks.c ${REPO}/util/locks.c +wget -O orig-headers/locks.h ${REPO}/util/locks.h diff --git a/src/util/locks.c b/src/util/locks.c new file mode 100644 index 00000000..b65a02bd --- /dev/null +++ b/src/util/locks.c @@ -0,0 +1,264 @@ +/** + * util/locks.c - unbound locking primitives + * + * Copyright (c) 2007, NLnet Labs. All rights reserved. + * + * This software is open source. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the NLNET LABS nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file + * Implementation of locking and threading support. + * A place for locking debug code since most locking functions are macros. + */ + +#include "config.h" +#include "util/locks.h" +#include +#ifdef HAVE_SYS_WAIT_H +#include +#endif + +/** block all signals, masks them away. */ +void +ub_thread_blocksigs(void) +{ +#if defined(HAVE_PTHREAD) || defined(HAVE_SOLARIS_THREADS) || defined(HAVE_SIGPROCMASK) +# if defined(HAVE_PTHREAD) || defined(HAVE_SOLARIS_THREADS) + int err; +# endif + sigset_t sigset; + sigfillset(&sigset); +#ifdef HAVE_PTHREAD + if((err=pthread_sigmask(SIG_SETMASK, &sigset, NULL))) + fatal_exit("pthread_sigmask: %s", strerror(err)); +#else +# ifdef HAVE_SOLARIS_THREADS + if((err=thr_sigsetmask(SIG_SETMASK, &sigset, NULL))) + fatal_exit("thr_sigsetmask: %s", strerror(err)); +# else + /* have nothing, do single process signal mask */ + if(sigprocmask(SIG_SETMASK, &sigset, NULL)) + fatal_exit("sigprocmask: %s", strerror(errno)); +# endif /* HAVE_SOLARIS_THREADS */ +#endif /* HAVE_PTHREAD */ +#endif /* have signal stuff */ +} + +/** unblock one signal, so we can catch it */ +void ub_thread_sig_unblock(int sig) +{ +#if defined(HAVE_PTHREAD) || defined(HAVE_SOLARIS_THREADS) || defined(HAVE_SIGPROCMASK) +# if defined(HAVE_PTHREAD) || defined(HAVE_SOLARIS_THREADS) + int err; +# endif + sigset_t sigset; + sigemptyset(&sigset); + sigaddset(&sigset, sig); +#ifdef HAVE_PTHREAD + if((err=pthread_sigmask(SIG_UNBLOCK, &sigset, NULL))) + fatal_exit("pthread_sigmask: %s", strerror(err)); +#else +# ifdef HAVE_SOLARIS_THREADS + if((err=thr_sigsetmask(SIG_UNBLOCK, &sigset, NULL))) + fatal_exit("thr_sigsetmask: %s", strerror(err)); +# else + /* have nothing, do single thread case */ + if(sigprocmask(SIG_UNBLOCK, &sigset, NULL)) + fatal_exit("sigprocmask: %s", strerror(errno)); +# endif /* HAVE_SOLARIS_THREADS */ +#endif /* HAVE_PTHREAD */ +#else + (void)sig; +#endif /* have signal stuff */ +} + +#if !defined(HAVE_PTHREAD) && !defined(HAVE_SOLARIS_THREADS) && !defined(HAVE_WINDOWS_THREADS) +/** + * No threading available: fork a new process. + * This means no shared data structure, and no locking. + * Only the main thread ever returns. Exits on errors. + * @param thr: the location where to store the thread-id. + * @param func: function body of the thread. Return value of func is lost. + * @param arg: user argument to func. + */ +void +ub_thr_fork_create(ub_thread_type* thr, void* (*func)(void*), void* arg) +{ + pid_t pid = fork(); + switch(pid) { + default: /* main */ + *thr = (ub_thread_type)pid; + return; + case 0: /* child */ + *thr = (ub_thread_type)getpid(); + (void)(*func)(arg); + exit(0); + case -1: /* error */ + fatal_exit("could not fork: %s", strerror(errno)); + } +} + +/** + * There is no threading. Wait for a process to terminate. + * Note that ub_thread_type is defined as pid_t. + * @param thread: the process id to wait for. + */ +void ub_thr_fork_wait(ub_thread_type thread) +{ + int status = 0; + if(waitpid((pid_t)thread, &status, 0) == -1) + log_err("waitpid(%d): %s", (int)thread, strerror(errno)); + if(status != 0) + log_warn("process %d abnormal exit with status %d", + (int)thread, status); +} +#endif /* !defined(HAVE_PTHREAD) && !defined(HAVE_SOLARIS_THREADS) && !defined(HAVE_WINDOWS_THREADS) */ + +#ifdef HAVE_SOLARIS_THREADS +void* ub_thread_key_get(ub_thread_key_type key) +{ + void* ret=NULL; + LOCKRET(thr_getspecific(key, &ret)); + return ret; +} +#endif + +#ifdef HAVE_WINDOWS_THREADS +/** log a windows GetLastError message */ +static void log_win_err(const char* str, DWORD err) +{ + LPTSTR buf; + if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER, + NULL, err, 0, (LPTSTR)&buf, 0, NULL) == 0) { + /* could not format error message */ + log_err("%s, GetLastError=%d", str, (int)err); + return; + } + log_err("%s, (err=%d): %s", str, (int)err, buf); + LocalFree(buf); +} + +void lock_basic_init(lock_basic_type* lock) +{ + /* implement own lock, because windows HANDLE as Mutex usage + * uses too many handles and would bog down the whole system. */ + (void)InterlockedExchange(lock, 0); +} + +void lock_basic_destroy(lock_basic_type* lock) +{ + (void)InterlockedExchange(lock, 0); +} + +void lock_basic_lock(lock_basic_type* lock) +{ + LONG wait = 1; /* wait 1 msec at first */ + + while(InterlockedExchange(lock, 1)) { + /* if the old value was 1 then if was already locked */ + Sleep(wait); /* wait with sleep */ + wait *= 2; /* exponential backoff for waiting */ + } + /* the old value was 0, but we inserted 1, we locked it! */ +} + +void lock_basic_unlock(lock_basic_type* lock) +{ + /* unlock it by inserting the value of 0. xchg for cache coherency. */ + (void)InterlockedExchange(lock, 0); +} + +void ub_thread_key_create(ub_thread_key_type* key, void* f) +{ + *key = TlsAlloc(); + if(*key == TLS_OUT_OF_INDEXES) { + *key = 0; + log_win_err("TlsAlloc Failed(OUT_OF_INDEXES)", GetLastError()); + } + else ub_thread_key_set(*key, f); +} + +void ub_thread_key_set(ub_thread_key_type key, void* v) +{ + if(!TlsSetValue(key, v)) { + log_win_err("TlsSetValue failed", GetLastError()); + } +} + +void* ub_thread_key_get(ub_thread_key_type key) +{ + void* ret = (void*)TlsGetValue(key); + if(ret == NULL && GetLastError() != ERROR_SUCCESS) { + log_win_err("TlsGetValue failed", GetLastError()); + } + return ret; +} + +void ub_thread_create(ub_thread_type* thr, void* (*func)(void*), void* arg) +{ +#ifndef HAVE__BEGINTHREADEX + *thr = CreateThread(NULL, /* default security (no inherit handle) */ + 0, /* default stack size */ + (LPTHREAD_START_ROUTINE)func, arg, + 0, /* default flags, run immediately */ + NULL); /* do not store thread identifier anywhere */ +#else + /* the beginthreadex routine setups for the C lib; aligns stack */ + *thr=(ub_thread_type)_beginthreadex(NULL, 0, (void*)func, arg, 0, NULL); +#endif + if(*thr == NULL) { + log_win_err("CreateThread failed", GetLastError()); + fatal_exit("thread create failed"); + } +} + +ub_thread_type ub_thread_self(void) +{ + return GetCurrentThread(); +} + +void ub_thread_join(ub_thread_type thr) +{ + DWORD ret = WaitForSingleObject(thr, INFINITE); + if(ret == WAIT_FAILED) { + log_win_err("WaitForSingleObject(Thread):WAIT_FAILED", + GetLastError()); + } else if(ret == WAIT_TIMEOUT) { + log_win_err("WaitForSingleObject(Thread):WAIT_TIMEOUT", + GetLastError()); + } + /* and close the handle to the thread */ + if(!CloseHandle(thr)) { + log_win_err("CloseHandle(Thread) failed", GetLastError()); + } +} +#endif /* HAVE_WINDOWS_THREADS */ diff --git a/src/util/locks.h b/src/util/locks.h new file mode 100644 index 00000000..fea65e0a --- /dev/null +++ b/src/util/locks.h @@ -0,0 +1,64 @@ +/** + * + * \file locks.h + * /brief Alternative symbol names for unbound's locks.h + * + */ +/* + * Copyright (c) 2017, NLnet Labs, the getdns team + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the names of the copyright holders nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef LOCKS_H_SYMBOLS +#define LOCKS_H_SYMBOLS + +#include "config.h" + +#define ub_thread_blocksigs _getdns_ub_thread_blocksigs +#define ub_thread_sig_unblock _getdns_ub_thread_sig_unblock + +#define ub_thread_type _getdns_ub_thread_type +#define ub_thr_fork_create _getdns_ub_thr_fork_create +#define ub_thr_fork_wait _getdns_ub_thr_fork_wait + +#if defined(HAVE_SOLARIS_THREADS) || defined(HAVE_WINDOWS_THREADS) +#define ub_thread_key_type _getdns_ub_thread_key_type +#define ub_thread_key_create _getdns_ub_thread_key_create +#define ub_thread_key_set _getdns_ub_thread_key_set +#define ub_thread_key_get _getdns_ub_thread_key_get +#endif + +#ifdef HAVE_WINDOWS_THREADS +#define lock_basic_type _getdns_lock_basic_type +#define lock_basic_init _getdns_lock_basic_init +#define lock_basic_destroy _getdns_lock_basic_destroy +#define lock_basic_lock _getdns_lock_basic_lock_ +#define lock_basic_unlock _getdns_lock_basic_unlock + +#define ub_thread_create _getdns_ub_thread_create +#define ub_thread_self _getdns_ub_thread_self +#endif + +#include "util/orig-headers/locks.h" +#endif diff --git a/src/util/lookup3.c b/src/util/lookup3.c new file mode 100644 index 00000000..e9b05af3 --- /dev/null +++ b/src/util/lookup3.c @@ -0,0 +1,1032 @@ +/* + February 2013(Wouter) patch defines for BSD endianness, from Brad Smith. + January 2012(Wouter) added randomised initial value, fallout from 28c3. + March 2007(Wouter) adapted from lookup3.c original, add config.h include. + added #ifdef VALGRIND to remove 298,384,660 'unused variable k8' warnings. + added include of lookup3.h to check definitions match declarations. + removed include of stdint - config.h takes care of platform independence. + url http://burtleburtle.net/bob/hash/index.html. +*/ +/* +------------------------------------------------------------------------------- +lookup3.c, by Bob Jenkins, May 2006, Public Domain. + +These are functions for producing 32-bit hashes for hash table lookup. +hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final() +are externally useful functions. Routines to test the hash are included +if SELF_TEST is defined. You can use this free for any purpose. It's in +the public domain. It has no warranty. + +You probably want to use hashlittle(). hashlittle() and hashbig() +hash byte arrays. hashlittle() is is faster than hashbig() on +little-endian machines. Intel and AMD are little-endian machines. +On second thought, you probably want hashlittle2(), which is identical to +hashlittle() except it returns two 32-bit hashes for the price of one. +You could implement hashbig2() if you wanted but I haven't bothered here. + +If you want to find a hash of, say, exactly 7 integers, do + a = i1; b = i2; c = i3; + mix(a,b,c); + a += i4; b += i5; c += i6; + mix(a,b,c); + a += i7; + final(a,b,c); +then use c as the hash value. If you have a variable length array of +4-byte integers to hash, use hashword(). If you have a byte array (like +a character string), use hashlittle(). If you have several byte arrays, or +a mix of things, see the comments above hashlittle(). + +Why is this so big? I read 12 bytes at a time into 3 4-byte integers, +then mix those integers. This is fast (you can do a lot more thorough +mixing with 12*3 instructions on 3 integers than you can with 3 instructions +on 1 byte), but shoehorning those bytes into integers efficiently is messy. +------------------------------------------------------------------------------- +*/ +/*#define SELF_TEST 1*/ + +#include "config.h" +#include "util/storage/lookup3.h" +#include /* defines printf for tests */ +#include /* defines time_t for timings in the test */ +/*#include defines uint32_t etc (from config.h) */ +#include /* attempt to define endianness */ +#ifdef HAVE_SYS_TYPES_H +# include /* attempt to define endianness (solaris) */ +#endif +#if defined(linux) || defined(__OpenBSD__) +# ifdef HAVE_ENDIAN_H +# include /* attempt to define endianness */ +# else +# include /* on older OpenBSD */ +# endif +#endif +#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) +#include /* attempt to define endianness */ +#endif + +/* random initial value */ +static uint32_t raninit = (uint32_t)0xdeadbeef; + +void +hash_set_raninit(uint32_t v) +{ + raninit = v; +} + +/* + * My best guess at if you are big-endian or little-endian. This may + * need adjustment. + */ +#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \ + __BYTE_ORDER == __LITTLE_ENDIAN) || \ + (defined(i386) || defined(__i386__) || defined(__i486__) || \ + defined(__i586__) || defined(__i686__) || defined(vax) || defined(MIPSEL) || defined(__x86)) +# define HASH_LITTLE_ENDIAN 1 +# define HASH_BIG_ENDIAN 0 +#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \ + __BYTE_ORDER == __BIG_ENDIAN) || \ + (defined(sparc) || defined(__sparc) || defined(__sparc__) || defined(POWERPC) || defined(mc68000) || defined(sel)) +# define HASH_LITTLE_ENDIAN 0 +# define HASH_BIG_ENDIAN 1 +#elif defined(_MACHINE_ENDIAN_H_) +/* test for machine_endian_h protects failure if some are empty strings */ +# if defined(_BYTE_ORDER) && defined(_BIG_ENDIAN) && _BYTE_ORDER == _BIG_ENDIAN +# define HASH_LITTLE_ENDIAN 0 +# define HASH_BIG_ENDIAN 1 +# endif +# if defined(_BYTE_ORDER) && defined(_LITTLE_ENDIAN) && _BYTE_ORDER == _LITTLE_ENDIAN +# define HASH_LITTLE_ENDIAN 1 +# define HASH_BIG_ENDIAN 0 +# endif /* _MACHINE_ENDIAN_H_ */ +#else +# define HASH_LITTLE_ENDIAN 0 +# define HASH_BIG_ENDIAN 0 +#endif + +#define hashsize(n) ((uint32_t)1<<(n)) +#define hashmask(n) (hashsize(n)-1) +#define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k)))) + +/* +------------------------------------------------------------------------------- +mix -- mix 3 32-bit values reversibly. + +This is reversible, so any information in (a,b,c) before mix() is +still in (a,b,c) after mix(). + +If four pairs of (a,b,c) inputs are run through mix(), or through +mix() in reverse, there are at least 32 bits of the output that +are sometimes the same for one pair and different for another pair. +This was tested for: +* pairs that differed by one bit, by two bits, in any combination + of top bits of (a,b,c), or in any combination of bottom bits of + (a,b,c). +* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed + the output delta to a Gray code (a^(a>>1)) so a string of 1's (as + is commonly produced by subtraction) look like a single 1-bit + difference. +* the base values were pseudorandom, all zero but one bit set, or + all zero plus a counter that starts at zero. + +Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that +satisfy this are + 4 6 8 16 19 4 + 9 15 3 18 27 15 + 14 9 3 7 17 3 +Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing +for "differ" defined as + with a one-bit base and a two-bit delta. I +used http://burtleburtle.net/bob/hash/avalanche.html to choose +the operations, constants, and arrangements of the variables. + +This does not achieve avalanche. There are input bits of (a,b,c) +that fail to affect some output bits of (a,b,c), especially of a. The +most thoroughly mixed value is c, but it doesn't really even achieve +avalanche in c. + +This allows some parallelism. Read-after-writes are good at doubling +the number of bits affected, so the goal of mixing pulls in the opposite +direction as the goal of parallelism. I did what I could. Rotates +seem to cost as much as shifts on every machine I could lay my hands +on, and rotates are much kinder to the top and bottom bits, so I used +rotates. +------------------------------------------------------------------------------- +*/ +#define mix(a,b,c) \ +{ \ + a -= c; a ^= rot(c, 4); c += b; \ + b -= a; b ^= rot(a, 6); a += c; \ + c -= b; c ^= rot(b, 8); b += a; \ + a -= c; a ^= rot(c,16); c += b; \ + b -= a; b ^= rot(a,19); a += c; \ + c -= b; c ^= rot(b, 4); b += a; \ +} + +/* +------------------------------------------------------------------------------- +final -- final mixing of 3 32-bit values (a,b,c) into c + +Pairs of (a,b,c) values differing in only a few bits will usually +produce values of c that look totally different. This was tested for +* pairs that differed by one bit, by two bits, in any combination + of top bits of (a,b,c), or in any combination of bottom bits of + (a,b,c). +* "differ" is defined as +, -, ^, or ~^. For + and -, I transformed + the output delta to a Gray code (a^(a>>1)) so a string of 1's (as + is commonly produced by subtraction) look like a single 1-bit + difference. +* the base values were pseudorandom, all zero but one bit set, or + all zero plus a counter that starts at zero. + +These constants passed: + 14 11 25 16 4 14 24 + 12 14 25 16 4 14 24 +and these came close: + 4 8 15 26 3 22 24 + 10 8 15 26 3 22 24 + 11 8 15 26 3 22 24 +------------------------------------------------------------------------------- +*/ +#define final(a,b,c) \ +{ \ + c ^= b; c -= rot(b,14); \ + a ^= c; a -= rot(c,11); \ + b ^= a; b -= rot(a,25); \ + c ^= b; c -= rot(b,16); \ + a ^= c; a -= rot(c,4); \ + b ^= a; b -= rot(a,14); \ + c ^= b; c -= rot(b,24); \ +} + +/* +-------------------------------------------------------------------- + This works on all machines. To be useful, it requires + -- that the key be an array of uint32_t's, and + -- that the length be the number of uint32_t's in the key + + The function hashword() is identical to hashlittle() on little-endian + machines, and identical to hashbig() on big-endian machines, + except that the length has to be measured in uint32_ts rather than in + bytes. hashlittle() is more complicated than hashword() only because + hashlittle() has to dance around fitting the key bytes into registers. +-------------------------------------------------------------------- +*/ +uint32_t hashword( +const uint32_t *k, /* the key, an array of uint32_t values */ +size_t length, /* the length of the key, in uint32_ts */ +uint32_t initval) /* the previous hash, or an arbitrary value */ +{ + uint32_t a,b,c; + + /* Set up the internal state */ + a = b = c = raninit + (((uint32_t)length)<<2) + initval; + + /*------------------------------------------------- handle most of the key */ + while (length > 3) + { + a += k[0]; + b += k[1]; + c += k[2]; + mix(a,b,c); + length -= 3; + k += 3; + } + + /*------------------------------------------- handle the last 3 uint32_t's */ + switch(length) /* all the case statements fall through */ + { + case 3 : c+=k[2]; + case 2 : b+=k[1]; + case 1 : a+=k[0]; + final(a,b,c); + case 0: /* case 0: nothing left to add */ + break; + } + /*------------------------------------------------------ report the result */ + return c; +} + + +#ifdef SELF_TEST + +/* +-------------------------------------------------------------------- +hashword2() -- same as hashword(), but take two seeds and return two +32-bit values. pc and pb must both be nonnull, and *pc and *pb must +both be initialized with seeds. If you pass in (*pb)==0, the output +(*pc) will be the same as the return value from hashword(). +-------------------------------------------------------------------- +*/ +void hashword2 ( +const uint32_t *k, /* the key, an array of uint32_t values */ +size_t length, /* the length of the key, in uint32_ts */ +uint32_t *pc, /* IN: seed OUT: primary hash value */ +uint32_t *pb) /* IN: more seed OUT: secondary hash value */ +{ + uint32_t a,b,c; + + /* Set up the internal state */ + a = b = c = raninit + ((uint32_t)(length<<2)) + *pc; + c += *pb; + + /*------------------------------------------------- handle most of the key */ + while (length > 3) + { + a += k[0]; + b += k[1]; + c += k[2]; + mix(a,b,c); + length -= 3; + k += 3; + } + + /*------------------------------------------- handle the last 3 uint32_t's */ + switch(length) /* all the case statements fall through */ + { + case 3 : c+=k[2]; + case 2 : b+=k[1]; + case 1 : a+=k[0]; + final(a,b,c); + case 0: /* case 0: nothing left to add */ + break; + } + /*------------------------------------------------------ report the result */ + *pc=c; *pb=b; +} + +#endif /* SELF_TEST */ + +/* +------------------------------------------------------------------------------- +hashlittle() -- hash a variable-length key into a 32-bit value + k : the key (the unaligned variable-length array of bytes) + length : the length of the key, counting by bytes + initval : can be any 4-byte value +Returns a 32-bit value. Every bit of the key affects every bit of +the return value. Two keys differing by one or two bits will have +totally different hash values. + +The best hash table sizes are powers of 2. There is no need to do +mod a prime (mod is sooo slow!). If you need less than 32 bits, +use a bitmask. For example, if you need only 10 bits, do + h = (h & hashmask(10)); +In which case, the hash table should have hashsize(10) elements. + +If you are hashing n strings (uint8_t **)k, do it like this: + for (i=0, h=0; i 12) + { + a += k[0]; + b += k[1]; + c += k[2]; + mix(a,b,c); + length -= 12; + k += 3; + } + + /*----------------------------- handle the last (probably partial) block */ + /* + * "k[2]&0xffffff" actually reads beyond the end of the string, but + * then masks off the part it's not allowed to read. Because the + * string is aligned, the masked-off tail is in the same word as the + * rest of the string. Every machine with memory protection I've seen + * does it on word boundaries, so is OK with this. But VALGRIND will + * still catch it and complain. The masking trick does make the hash + * noticeably faster for short strings (like English words). + */ +#ifndef VALGRIND + + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; + case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; + case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=k[1]&0xffffff; a+=k[0]; break; + case 6 : b+=k[1]&0xffff; a+=k[0]; break; + case 5 : b+=k[1]&0xff; a+=k[0]; break; + case 4 : a+=k[0]; break; + case 3 : a+=k[0]&0xffffff; break; + case 2 : a+=k[0]&0xffff; break; + case 1 : a+=k[0]&0xff; break; + case 0 : return c; /* zero length strings require no mixing */ + } + +#else /* make valgrind happy */ + + k8 = (const uint8_t *)k; + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ + case 9 : c+=k8[8]; /* fall through */ + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ + case 5 : b+=k8[4]; /* fall through */ + case 4 : a+=k[0]; break; + case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ + case 1 : a+=k8[0]; break; + case 0 : return c; + } + +#endif /* !valgrind */ + + } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { + const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ + const uint8_t *k8; + + /*--------------- all but last block: aligned reads and different mixing */ + while (length > 12) + { + a += k[0] + (((uint32_t)k[1])<<16); + b += k[2] + (((uint32_t)k[3])<<16); + c += k[4] + (((uint32_t)k[5])<<16); + mix(a,b,c); + length -= 12; + k += 6; + } + + /*----------------------------- handle the last (probably partial) block */ + k8 = (const uint8_t *)k; + switch(length) + { + case 12: c+=k[4]+(((uint32_t)k[5])<<16); + b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 10: c+=k[4]; + b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 9 : c+=k8[8]; /* fall through */ + case 8 : b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 6 : b+=k[2]; + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 5 : b+=k8[4]; /* fall through */ + case 4 : a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 2 : a+=k[0]; + break; + case 1 : a+=k8[0]; + break; + case 0 : return c; /* zero length requires no mixing */ + } + + } else { /* need to read the key one byte at a time */ + const uint8_t *k = (const uint8_t *)key; + + /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ + while (length > 12) + { + a += k[0]; + a += ((uint32_t)k[1])<<8; + a += ((uint32_t)k[2])<<16; + a += ((uint32_t)k[3])<<24; + b += k[4]; + b += ((uint32_t)k[5])<<8; + b += ((uint32_t)k[6])<<16; + b += ((uint32_t)k[7])<<24; + c += k[8]; + c += ((uint32_t)k[9])<<8; + c += ((uint32_t)k[10])<<16; + c += ((uint32_t)k[11])<<24; + mix(a,b,c); + length -= 12; + k += 12; + } + + /*-------------------------------- last block: affect all 32 bits of (c) */ + switch(length) /* all the case statements fall through */ + { + case 12: c+=((uint32_t)k[11])<<24; + case 11: c+=((uint32_t)k[10])<<16; + case 10: c+=((uint32_t)k[9])<<8; + case 9 : c+=k[8]; + case 8 : b+=((uint32_t)k[7])<<24; + case 7 : b+=((uint32_t)k[6])<<16; + case 6 : b+=((uint32_t)k[5])<<8; + case 5 : b+=k[4]; + case 4 : a+=((uint32_t)k[3])<<24; + case 3 : a+=((uint32_t)k[2])<<16; + case 2 : a+=((uint32_t)k[1])<<8; + case 1 : a+=k[0]; + break; + case 0 : return c; + } + } + + final(a,b,c); + return c; +} + +#ifdef SELF_TEST + +/* + * hashlittle2: return 2 32-bit hash values + * + * This is identical to hashlittle(), except it returns two 32-bit hash + * values instead of just one. This is good enough for hash table + * lookup with 2^^64 buckets, or if you want a second hash if you're not + * happy with the first, or if you want a probably-unique 64-bit ID for + * the key. *pc is better mixed than *pb, so use *pc first. If you want + * a 64-bit value do something like "*pc + (((uint64_t)*pb)<<32)". + */ +void hashlittle2( + const void *key, /* the key to hash */ + size_t length, /* length of the key */ + uint32_t *pc, /* IN: primary initval, OUT: primary hash */ + uint32_t *pb) /* IN: secondary initval, OUT: secondary hash */ +{ + uint32_t a,b,c; /* internal state */ + union { const void *ptr; size_t i; } u; /* needed for Mac Powerbook G4 */ + + /* Set up the internal state */ + a = b = c = raninit + ((uint32_t)length) + *pc; + c += *pb; + + u.ptr = key; + if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) { + const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ +#ifdef VALGRIND + const uint8_t *k8; +#endif + + /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ + while (length > 12) + { + a += k[0]; + b += k[1]; + c += k[2]; + mix(a,b,c); + length -= 12; + k += 3; + } + + /*----------------------------- handle the last (probably partial) block */ + /* + * "k[2]&0xffffff" actually reads beyond the end of the string, but + * then masks off the part it's not allowed to read. Because the + * string is aligned, the masked-off tail is in the same word as the + * rest of the string. Every machine with memory protection I've seen + * does it on word boundaries, so is OK with this. But VALGRIND will + * still catch it and complain. The masking trick does make the hash + * noticeably faster for short strings (like English words). + */ +#ifndef VALGRIND + + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break; + case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break; + case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break; + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=k[1]&0xffffff; a+=k[0]; break; + case 6 : b+=k[1]&0xffff; a+=k[0]; break; + case 5 : b+=k[1]&0xff; a+=k[0]; break; + case 4 : a+=k[0]; break; + case 3 : a+=k[0]&0xffffff; break; + case 2 : a+=k[0]&0xffff; break; + case 1 : a+=k[0]&0xff; break; + case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ + } + +#else /* make valgrind happy */ + + k8 = (const uint8_t *)k; + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 10: c+=((uint32_t)k8[9])<<8; /* fall through */ + case 9 : c+=k8[8]; /* fall through */ + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 6 : b+=((uint32_t)k8[5])<<8; /* fall through */ + case 5 : b+=k8[4]; /* fall through */ + case 4 : a+=k[0]; break; + case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 2 : a+=((uint32_t)k8[1])<<8; /* fall through */ + case 1 : a+=k8[0]; break; + case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ + } + +#endif /* !valgrind */ + + } else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0)) { + const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */ + const uint8_t *k8; + + /*--------------- all but last block: aligned reads and different mixing */ + while (length > 12) + { + a += k[0] + (((uint32_t)k[1])<<16); + b += k[2] + (((uint32_t)k[3])<<16); + c += k[4] + (((uint32_t)k[5])<<16); + mix(a,b,c); + length -= 12; + k += 6; + } + + /*----------------------------- handle the last (probably partial) block */ + k8 = (const uint8_t *)k; + switch(length) + { + case 12: c+=k[4]+(((uint32_t)k[5])<<16); + b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 11: c+=((uint32_t)k8[10])<<16; /* fall through */ + case 10: c+=k[4]; + b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 9 : c+=k8[8]; /* fall through */ + case 8 : b+=k[2]+(((uint32_t)k[3])<<16); + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 7 : b+=((uint32_t)k8[6])<<16; /* fall through */ + case 6 : b+=k[2]; + a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 5 : b+=k8[4]; /* fall through */ + case 4 : a+=k[0]+(((uint32_t)k[1])<<16); + break; + case 3 : a+=((uint32_t)k8[2])<<16; /* fall through */ + case 2 : a+=k[0]; + break; + case 1 : a+=k8[0]; + break; + case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ + } + + } else { /* need to read the key one byte at a time */ + const uint8_t *k = (const uint8_t *)key; + + /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ + while (length > 12) + { + a += k[0]; + a += ((uint32_t)k[1])<<8; + a += ((uint32_t)k[2])<<16; + a += ((uint32_t)k[3])<<24; + b += k[4]; + b += ((uint32_t)k[5])<<8; + b += ((uint32_t)k[6])<<16; + b += ((uint32_t)k[7])<<24; + c += k[8]; + c += ((uint32_t)k[9])<<8; + c += ((uint32_t)k[10])<<16; + c += ((uint32_t)k[11])<<24; + mix(a,b,c); + length -= 12; + k += 12; + } + + /*-------------------------------- last block: affect all 32 bits of (c) */ + switch(length) /* all the case statements fall through */ + { + case 12: c+=((uint32_t)k[11])<<24; + case 11: c+=((uint32_t)k[10])<<16; + case 10: c+=((uint32_t)k[9])<<8; + case 9 : c+=k[8]; + case 8 : b+=((uint32_t)k[7])<<24; + case 7 : b+=((uint32_t)k[6])<<16; + case 6 : b+=((uint32_t)k[5])<<8; + case 5 : b+=k[4]; + case 4 : a+=((uint32_t)k[3])<<24; + case 3 : a+=((uint32_t)k[2])<<16; + case 2 : a+=((uint32_t)k[1])<<8; + case 1 : a+=k[0]; + break; + case 0 : *pc=c; *pb=b; return; /* zero length strings require no mixing */ + } + } + + final(a,b,c); + *pc=c; *pb=b; +} + +#endif /* SELF_TEST */ + +#if 0 /* currently not used */ + +/* + * hashbig(): + * This is the same as hashword() on big-endian machines. It is different + * from hashlittle() on all machines. hashbig() takes advantage of + * big-endian byte ordering. + */ +uint32_t hashbig( const void *key, size_t length, uint32_t initval) +{ + uint32_t a,b,c; + union { const void *ptr; size_t i; } u; /* to cast key to (size_t) happily */ + + /* Set up the internal state */ + a = b = c = raninit + ((uint32_t)length) + initval; + + u.ptr = key; + if (HASH_BIG_ENDIAN && ((u.i & 0x3) == 0)) { + const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */ +#ifdef VALGRIND + const uint8_t *k8; +#endif + + /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */ + while (length > 12) + { + a += k[0]; + b += k[1]; + c += k[2]; + mix(a,b,c); + length -= 12; + k += 3; + } + + /*----------------------------- handle the last (probably partial) block */ + /* + * "k[2]<<8" actually reads beyond the end of the string, but + * then shifts out the part it's not allowed to read. Because the + * string is aligned, the illegal read is in the same word as the + * rest of the string. Every machine with memory protection I've seen + * does it on word boundaries, so is OK with this. But VALGRIND will + * still catch it and complain. The masking trick does make the hash + * noticeably faster for short strings (like English words). + */ +#ifndef VALGRIND + + switch(length) + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=k[2]&0xffffff00; b+=k[1]; a+=k[0]; break; + case 10: c+=k[2]&0xffff0000; b+=k[1]; a+=k[0]; break; + case 9 : c+=k[2]&0xff000000; b+=k[1]; a+=k[0]; break; + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=k[1]&0xffffff00; a+=k[0]; break; + case 6 : b+=k[1]&0xffff0000; a+=k[0]; break; + case 5 : b+=k[1]&0xff000000; a+=k[0]; break; + case 4 : a+=k[0]; break; + case 3 : a+=k[0]&0xffffff00; break; + case 2 : a+=k[0]&0xffff0000; break; + case 1 : a+=k[0]&0xff000000; break; + case 0 : return c; /* zero length strings require no mixing */ + } + +#else /* make valgrind happy */ + + k8 = (const uint8_t *)k; + switch(length) /* all the case statements fall through */ + { + case 12: c+=k[2]; b+=k[1]; a+=k[0]; break; + case 11: c+=((uint32_t)k8[10])<<8; /* fall through */ + case 10: c+=((uint32_t)k8[9])<<16; /* fall through */ + case 9 : c+=((uint32_t)k8[8])<<24; /* fall through */ + case 8 : b+=k[1]; a+=k[0]; break; + case 7 : b+=((uint32_t)k8[6])<<8; /* fall through */ + case 6 : b+=((uint32_t)k8[5])<<16; /* fall through */ + case 5 : b+=((uint32_t)k8[4])<<24; /* fall through */ + case 4 : a+=k[0]; break; + case 3 : a+=((uint32_t)k8[2])<<8; /* fall through */ + case 2 : a+=((uint32_t)k8[1])<<16; /* fall through */ + case 1 : a+=((uint32_t)k8[0])<<24; break; + case 0 : return c; + } + +#endif /* !VALGRIND */ + + } else { /* need to read the key one byte at a time */ + const uint8_t *k = (const uint8_t *)key; + + /*--------------- all but the last block: affect some 32 bits of (a,b,c) */ + while (length > 12) + { + a += ((uint32_t)k[0])<<24; + a += ((uint32_t)k[1])<<16; + a += ((uint32_t)k[2])<<8; + a += ((uint32_t)k[3]); + b += ((uint32_t)k[4])<<24; + b += ((uint32_t)k[5])<<16; + b += ((uint32_t)k[6])<<8; + b += ((uint32_t)k[7]); + c += ((uint32_t)k[8])<<24; + c += ((uint32_t)k[9])<<16; + c += ((uint32_t)k[10])<<8; + c += ((uint32_t)k[11]); + mix(a,b,c); + length -= 12; + k += 12; + } + + /*-------------------------------- last block: affect all 32 bits of (c) */ + switch(length) /* all the case statements fall through */ + { + case 12: c+=k[11]; + case 11: c+=((uint32_t)k[10])<<8; + case 10: c+=((uint32_t)k[9])<<16; + case 9 : c+=((uint32_t)k[8])<<24; + case 8 : b+=k[7]; + case 7 : b+=((uint32_t)k[6])<<8; + case 6 : b+=((uint32_t)k[5])<<16; + case 5 : b+=((uint32_t)k[4])<<24; + case 4 : a+=k[3]; + case 3 : a+=((uint32_t)k[2])<<8; + case 2 : a+=((uint32_t)k[1])<<16; + case 1 : a+=((uint32_t)k[0])<<24; + break; + case 0 : return c; + } + } + + final(a,b,c); + return c; +} + +#endif /* 0 == currently not used */ + +#ifdef SELF_TEST + +/* used for timings */ +void driver1(void) +{ + uint8_t buf[256]; + uint32_t i; + uint32_t h=0; + time_t a,z; + + time(&a); + for (i=0; i<256; ++i) buf[i] = 'x'; + for (i=0; i<1; ++i) + { + h = hashlittle(&buf[0],1,h); + } + time(&z); + if (z-a > 0) printf("time %d %.8x\n", z-a, h); +} + +/* check that every input bit changes every output bit half the time */ +#define HASHSTATE 1 +#define HASHLEN 1 +#define MAXPAIR 60 +#define MAXLEN 70 +void driver2(void) +{ + uint8_t qa[MAXLEN+1], qb[MAXLEN+2], *a = &qa[0], *b = &qb[1]; + uint32_t c[HASHSTATE], d[HASHSTATE], i=0, j=0, k, l, m=0, z; + uint32_t e[HASHSTATE],f[HASHSTATE],g[HASHSTATE],h[HASHSTATE]; + uint32_t x[HASHSTATE],y[HASHSTATE]; + uint32_t hlen; + + printf("No more than %d trials should ever be needed \n",MAXPAIR/2); + for (hlen=0; hlen < MAXLEN; ++hlen) + { + z=0; + for (i=0; i>(8-j)); + c[0] = hashlittle(a, hlen, m); + b[i] ^= ((k+1)<>(8-j)); + d[0] = hashlittle(b, hlen, m); + /* check every bit is 1, 0, set, and not set at least once */ + for (l=0; lz) z=k; + if (k==MAXPAIR) + { + printf("Some bit didn't change: "); + printf("%.8x %.8x %.8x %.8x %.8x %.8x ", + e[0],f[0],g[0],h[0],x[0],y[0]); + printf("i %d j %d m %d len %d\n", i, j, m, hlen); + } + if (z==MAXPAIR) goto done; + } + } + } + done: + if (z < MAXPAIR) + { + printf("Mix success %2d bytes %2d initvals ",i,m); + printf("required %d trials\n", z/2); + } + } + printf("\n"); +} + +/* Check for reading beyond the end of the buffer and alignment problems */ +void driver3(void) +{ + uint8_t buf[MAXLEN+20], *b; + uint32_t len; + uint8_t q[] = "This is the time for all good men to come to the aid of their country..."; + uint32_t h; + uint8_t qq[] = "xThis is the time for all good men to come to the aid of their country..."; + uint32_t i; + uint8_t qqq[] = "xxThis is the time for all good men to come to the aid of their country..."; + uint32_t j; + uint8_t qqqq[] = "xxxThis is the time for all good men to come to the aid of their country..."; + uint32_t ref,x,y; + uint8_t *p; + + printf("Endianness. These lines should all be the same (for values filled in):\n"); + printf("%.8x %.8x %.8x\n", + hashword((const uint32_t *)q, (sizeof(q)-1)/4, 13), + hashword((const uint32_t *)q, (sizeof(q)-5)/4, 13), + hashword((const uint32_t *)q, (sizeof(q)-9)/4, 13)); + p = q; + printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", + hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), + hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), + hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), + hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), + hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), + hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); + p = &qq[1]; + printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", + hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), + hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), + hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), + hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), + hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), + hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); + p = &qqq[2]; + printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", + hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), + hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), + hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), + hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), + hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), + hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); + p = &qqqq[3]; + printf("%.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x %.8x\n", + hashlittle(p, sizeof(q)-1, 13), hashlittle(p, sizeof(q)-2, 13), + hashlittle(p, sizeof(q)-3, 13), hashlittle(p, sizeof(q)-4, 13), + hashlittle(p, sizeof(q)-5, 13), hashlittle(p, sizeof(q)-6, 13), + hashlittle(p, sizeof(q)-7, 13), hashlittle(p, sizeof(q)-8, 13), + hashlittle(p, sizeof(q)-9, 13), hashlittle(p, sizeof(q)-10, 13), + hashlittle(p, sizeof(q)-11, 13), hashlittle(p, sizeof(q)-12, 13)); + printf("\n"); + + /* check that hashlittle2 and hashlittle produce the same results */ + i=47; j=0; + hashlittle2(q, sizeof(q), &i, &j); + if (hashlittle(q, sizeof(q), 47) != i) + printf("hashlittle2 and hashlittle mismatch\n"); + + /* check that hashword2 and hashword produce the same results */ + len = raninit; + i=47, j=0; + hashword2(&len, 1, &i, &j); + if (hashword(&len, 1, 47) != i) + printf("hashword2 and hashword mismatch %x %x\n", + i, hashword(&len, 1, 47)); + + /* check hashlittle doesn't read before or after the ends of the string */ + for (h=0, b=buf+1; h<8; ++h, ++b) + { + for (i=0; ilock); + table->sizefunc = sizefunc; + table->compfunc = compfunc; + table->delkeyfunc = delkeyfunc; + table->deldatafunc = deldatafunc; + table->cb_arg = arg; + table->size = start_size; + table->size_mask = (int)(start_size-1); + table->lru_start = NULL; + table->lru_end = NULL; + table->num = 0; + table->space_used = 0; + table->space_max = maxmem; + table->array = calloc(table->size, sizeof(struct lruhash_bin)); + if(!table->array) { + lock_quick_destroy(&table->lock); + free(table); + return NULL; + } + bin_init(table->array, table->size); + lock_protect(&table->lock, table, sizeof(*table)); + lock_protect(&table->lock, table->array, + table->size*sizeof(struct lruhash_bin)); + return table; +} + +void +bin_delete(struct lruhash* table, struct lruhash_bin* bin) +{ + struct lruhash_entry* p, *np; + void *d; + if(!bin) + return; + lock_quick_destroy(&bin->lock); + p = bin->overflow_list; + bin->overflow_list = NULL; + while(p) { + np = p->overflow_next; + d = p->data; + (*table->delkeyfunc)(p->key, table->cb_arg); + (*table->deldatafunc)(d, table->cb_arg); + p = np; + } +} + +void +bin_split(struct lruhash* table, struct lruhash_bin* newa, + int newmask) +{ + size_t i; + struct lruhash_entry *p, *np; + struct lruhash_bin* newbin; + /* move entries to new table. Notice that since hash x is mapped to + * bin x & mask, and new mask uses one more bit, so all entries in + * one bin will go into the old bin or bin | newbit */ +#ifndef THREADS_DISABLED + int newbit = newmask - table->size_mask; +#endif + /* so, really, this task could also be threaded, per bin. */ + /* LRU list is not changed */ + for(i=0; isize; i++) + { + lock_quick_lock(&table->array[i].lock); + p = table->array[i].overflow_list; + /* lock both destination bins */ + lock_quick_lock(&newa[i].lock); + lock_quick_lock(&newa[newbit|i].lock); + while(p) { + np = p->overflow_next; + /* link into correct new bin */ + newbin = &newa[p->hash & newmask]; + p->overflow_next = newbin->overflow_list; + newbin->overflow_list = p; + p=np; + } + lock_quick_unlock(&newa[i].lock); + lock_quick_unlock(&newa[newbit|i].lock); + lock_quick_unlock(&table->array[i].lock); + } +} + +void +lruhash_delete(struct lruhash* table) +{ + size_t i; + if(!table) + return; + /* delete lock on hashtable to force check its OK */ + lock_quick_destroy(&table->lock); + for(i=0; isize; i++) + bin_delete(table, &table->array[i]); + free(table->array); + free(table); +} + +void +bin_overflow_remove(struct lruhash_bin* bin, struct lruhash_entry* entry) +{ + struct lruhash_entry* p = bin->overflow_list; + struct lruhash_entry** prevp = &bin->overflow_list; + while(p) { + if(p == entry) { + *prevp = p->overflow_next; + return; + } + prevp = &p->overflow_next; + p = p->overflow_next; + } +} + +void +reclaim_space(struct lruhash* table, struct lruhash_entry** list) +{ + struct lruhash_entry* d; + struct lruhash_bin* bin; + log_assert(table); + /* does not delete MRU entry, so table will not be empty. */ + while(table->num > 1 && table->space_used > table->space_max) { + /* notice that since we hold the hashtable lock, nobody + can change the lru chain. So it cannot be deleted underneath + us. We still need the hashbin and entry write lock to make + sure we flush all users away from the entry. + which is unlikely, since it is LRU, if someone got a rdlock + it would be moved to front, but to be sure. */ + d = table->lru_end; + /* specialised, delete from end of double linked list, + and we know num>1, so there is a previous lru entry. */ + log_assert(d && d->lru_prev); + table->lru_end = d->lru_prev; + d->lru_prev->lru_next = NULL; + /* schedule entry for deletion */ + bin = &table->array[d->hash & table->size_mask]; + table->num --; + lock_quick_lock(&bin->lock); + bin_overflow_remove(bin, d); + d->overflow_next = *list; + *list = d; + lock_rw_wrlock(&d->lock); + table->space_used -= table->sizefunc(d->key, d->data); + if(table->markdelfunc) + (*table->markdelfunc)(d->key); + lock_rw_unlock(&d->lock); + lock_quick_unlock(&bin->lock); + } +} + +struct lruhash_entry* +bin_find_entry(struct lruhash* table, + struct lruhash_bin* bin, hashvalue_type hash, void* key) +{ + struct lruhash_entry* p = bin->overflow_list; + while(p) { + if(p->hash == hash && table->compfunc(p->key, key) == 0) + return p; + p = p->overflow_next; + } + return NULL; +} + +void +table_grow(struct lruhash* table) +{ + struct lruhash_bin* newa; + int newmask; + size_t i; + if(table->size_mask == (int)(((size_t)-1)>>1)) { + log_err("hash array malloc: size_t too small"); + return; + } + /* try to allocate new array, if not fail */ + newa = calloc(table->size*2, sizeof(struct lruhash_bin)); + if(!newa) { + log_err("hash grow: malloc failed"); + /* continue with smaller array. Though its slower. */ + return; + } + bin_init(newa, table->size*2); + newmask = (table->size_mask << 1) | 1; + bin_split(table, newa, newmask); + /* delete the old bins */ + lock_unprotect(&table->lock, table->array); + for(i=0; isize; i++) { + lock_quick_destroy(&table->array[i].lock); + } + free(table->array); + + table->size *= 2; + table->size_mask = newmask; + table->array = newa; + lock_protect(&table->lock, table->array, + table->size*sizeof(struct lruhash_bin)); + return; +} + +void +lru_front(struct lruhash* table, struct lruhash_entry* entry) +{ + entry->lru_prev = NULL; + entry->lru_next = table->lru_start; + if(!table->lru_start) + table->lru_end = entry; + else table->lru_start->lru_prev = entry; + table->lru_start = entry; +} + +void +lru_remove(struct lruhash* table, struct lruhash_entry* entry) +{ + if(entry->lru_prev) + entry->lru_prev->lru_next = entry->lru_next; + else table->lru_start = entry->lru_next; + if(entry->lru_next) + entry->lru_next->lru_prev = entry->lru_prev; + else table->lru_end = entry->lru_prev; +} + +void +lru_touch(struct lruhash* table, struct lruhash_entry* entry) +{ + log_assert(table && entry); + if(entry == table->lru_start) + return; /* nothing to do */ + /* remove from current lru position */ + lru_remove(table, entry); + /* add at front */ + lru_front(table, entry); +} + +void +lruhash_insert(struct lruhash* table, hashvalue_type hash, + struct lruhash_entry* entry, void* data, void* cb_arg) +{ + struct lruhash_bin* bin; + struct lruhash_entry* found, *reclaimlist=NULL; + size_t need_size; + fptr_ok(fptr_whitelist_hash_sizefunc(table->sizefunc)); + fptr_ok(fptr_whitelist_hash_delkeyfunc(table->delkeyfunc)); + fptr_ok(fptr_whitelist_hash_deldatafunc(table->deldatafunc)); + fptr_ok(fptr_whitelist_hash_compfunc(table->compfunc)); + fptr_ok(fptr_whitelist_hash_markdelfunc(table->markdelfunc)); + need_size = table->sizefunc(entry->key, data); + if(cb_arg == NULL) cb_arg = table->cb_arg; + + /* find bin */ + lock_quick_lock(&table->lock); + bin = &table->array[hash & table->size_mask]; + lock_quick_lock(&bin->lock); + + /* see if entry exists already */ + if(!(found=bin_find_entry(table, bin, hash, entry->key))) { + /* if not: add to bin */ + entry->overflow_next = bin->overflow_list; + bin->overflow_list = entry; + lru_front(table, entry); + table->num++; + table->space_used += need_size; + } else { + /* if so: update data - needs a writelock */ + table->space_used += need_size - + (*table->sizefunc)(found->key, found->data); + (*table->delkeyfunc)(entry->key, cb_arg); + lru_touch(table, found); + lock_rw_wrlock(&found->lock); + (*table->deldatafunc)(found->data, cb_arg); + found->data = data; + lock_rw_unlock(&found->lock); + } + lock_quick_unlock(&bin->lock); + if(table->space_used > table->space_max) + reclaim_space(table, &reclaimlist); + if(table->num >= table->size) + table_grow(table); + lock_quick_unlock(&table->lock); + + /* finish reclaim if any (outside of critical region) */ + while(reclaimlist) { + struct lruhash_entry* n = reclaimlist->overflow_next; + void* d = reclaimlist->data; + (*table->delkeyfunc)(reclaimlist->key, cb_arg); + (*table->deldatafunc)(d, cb_arg); + reclaimlist = n; + } +} + +struct lruhash_entry* +lruhash_lookup(struct lruhash* table, hashvalue_type hash, void* key, int wr) +{ + struct lruhash_entry* entry; + struct lruhash_bin* bin; + fptr_ok(fptr_whitelist_hash_compfunc(table->compfunc)); + + lock_quick_lock(&table->lock); + bin = &table->array[hash & table->size_mask]; + lock_quick_lock(&bin->lock); + if((entry=bin_find_entry(table, bin, hash, key))) + lru_touch(table, entry); + lock_quick_unlock(&table->lock); + + if(entry) { + if(wr) { lock_rw_wrlock(&entry->lock); } + else { lock_rw_rdlock(&entry->lock); } + } + lock_quick_unlock(&bin->lock); + return entry; +} + +void +lruhash_remove(struct lruhash* table, hashvalue_type hash, void* key) +{ + struct lruhash_entry* entry; + struct lruhash_bin* bin; + void *d; + fptr_ok(fptr_whitelist_hash_sizefunc(table->sizefunc)); + fptr_ok(fptr_whitelist_hash_delkeyfunc(table->delkeyfunc)); + fptr_ok(fptr_whitelist_hash_deldatafunc(table->deldatafunc)); + fptr_ok(fptr_whitelist_hash_compfunc(table->compfunc)); + fptr_ok(fptr_whitelist_hash_markdelfunc(table->markdelfunc)); + + lock_quick_lock(&table->lock); + bin = &table->array[hash & table->size_mask]; + lock_quick_lock(&bin->lock); + if((entry=bin_find_entry(table, bin, hash, key))) { + bin_overflow_remove(bin, entry); + lru_remove(table, entry); + } else { + lock_quick_unlock(&table->lock); + lock_quick_unlock(&bin->lock); + return; + } + table->num--; + table->space_used -= (*table->sizefunc)(entry->key, entry->data); + lock_quick_unlock(&table->lock); + lock_rw_wrlock(&entry->lock); + if(table->markdelfunc) + (*table->markdelfunc)(entry->key); + lock_rw_unlock(&entry->lock); + lock_quick_unlock(&bin->lock); + /* finish removal */ + d = entry->data; + (*table->delkeyfunc)(entry->key, table->cb_arg); + (*table->deldatafunc)(d, table->cb_arg); +} + +/** clear bin, respecting locks, does not do space, LRU */ +static void +bin_clear(struct lruhash* table, struct lruhash_bin* bin) +{ + struct lruhash_entry* p, *np; + void *d; + lock_quick_lock(&bin->lock); + p = bin->overflow_list; + while(p) { + lock_rw_wrlock(&p->lock); + np = p->overflow_next; + d = p->data; + if(table->markdelfunc) + (*table->markdelfunc)(p->key); + lock_rw_unlock(&p->lock); + (*table->delkeyfunc)(p->key, table->cb_arg); + (*table->deldatafunc)(d, table->cb_arg); + p = np; + } + bin->overflow_list = NULL; + lock_quick_unlock(&bin->lock); +} + +void +lruhash_clear(struct lruhash* table) +{ + size_t i; + if(!table) + return; + fptr_ok(fptr_whitelist_hash_delkeyfunc(table->delkeyfunc)); + fptr_ok(fptr_whitelist_hash_deldatafunc(table->deldatafunc)); + fptr_ok(fptr_whitelist_hash_markdelfunc(table->markdelfunc)); + + lock_quick_lock(&table->lock); + for(i=0; isize; i++) { + bin_clear(table, &table->array[i]); + } + table->lru_start = NULL; + table->lru_end = NULL; + table->num = 0; + table->space_used = 0; + lock_quick_unlock(&table->lock); +} + +void +lruhash_status(struct lruhash* table, const char* id, int extended) +{ + lock_quick_lock(&table->lock); + log_info("%s: %u entries, memory %u / %u", + id, (unsigned)table->num, (unsigned)table->space_used, + (unsigned)table->space_max); + log_info(" itemsize %u, array %u, mask %d", + (unsigned)(table->num? table->space_used/table->num : 0), + (unsigned)table->size, table->size_mask); + if(extended) { + size_t i; + int min=(int)table->size*2, max=-2; + for(i=0; isize; i++) { + int here = 0; + struct lruhash_entry *en; + lock_quick_lock(&table->array[i].lock); + en = table->array[i].overflow_list; + while(en) { + here ++; + en = en->overflow_next; + } + lock_quick_unlock(&table->array[i].lock); + if(extended >= 2) + log_info("bin[%d] %d", (int)i, here); + if(here > max) max = here; + if(here < min) min = here; + } + log_info(" bin min %d, avg %.2lf, max %d", min, + (double)table->num/(double)table->size, max); + } + lock_quick_unlock(&table->lock); +} + +size_t +lruhash_get_mem(struct lruhash* table) +{ + size_t s; + lock_quick_lock(&table->lock); + s = sizeof(struct lruhash) + table->space_used; +#ifdef USE_THREAD_DEBUG + if(table->size != 0) { + size_t i; + for(i=0; isize; i++) + s += sizeof(struct lruhash_bin) + + lock_get_mem(&table->array[i].lock); + } +#else /* no THREAD_DEBUG */ + if(table->size != 0) + s += (table->size)*(sizeof(struct lruhash_bin) + + lock_get_mem(&table->array[0].lock)); +#endif + lock_quick_unlock(&table->lock); + s += lock_get_mem(&table->lock); + return s; +} + +void +lruhash_setmarkdel(struct lruhash* table, lruhash_markdelfunc_type md) +{ + lock_quick_lock(&table->lock); + table->markdelfunc = md; + lock_quick_unlock(&table->lock); +} + +void +lruhash_traverse(struct lruhash* h, int wr, + void (*func)(struct lruhash_entry*, void*), void* arg) +{ + size_t i; + struct lruhash_entry* e; + + lock_quick_lock(&h->lock); + for(i=0; isize; i++) { + lock_quick_lock(&h->array[i].lock); + for(e = h->array[i].overflow_list; e; e = e->overflow_next) { + if(wr) { + lock_rw_wrlock(&e->lock); + } else { + lock_rw_rdlock(&e->lock); + } + (*func)(e, arg); + lock_rw_unlock(&e->lock); + } + lock_quick_unlock(&h->array[i].lock); + } + lock_quick_unlock(&h->lock); +} diff --git a/src/util/lruhash.h b/src/util/lruhash.h new file mode 100644 index 00000000..17c8b551 --- /dev/null +++ b/src/util/lruhash.h @@ -0,0 +1,68 @@ +/** + * + * \file lruhash.h + * /brief Alternative symbol names for unbound's lruhash.h + * + */ +/* + * Copyright (c) 2017, NLnet Labs, the getdns team + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the names of the copyright holders nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef LRUHASH_H_SYMBOLS +#define LRUHASH_H_SYMBOLS + +#define lruhash _getdns_lruhash +#define lruhash_bin _getdns_lruhash_bin +#define lruhash_entry _getdns_lruhash_entry +#define hashvalue_type _getdns_hashvalue_type +#define lruhash_sizefunc_type _getdns_lruhash_sizefunc_type +#define lruhash_compfunc_type _getdns_lruhash_compfunc_type +#define lruhash_delkeyfunc_type _getdns_lruhash_delkeyfunc_type +#define lruhash_deldatafunc_type _getdns_lruhash_deldatafunc_type +#define lruhash_markdelfunc_type _getdns_lruhash_markdelfunc_type +#define lruhash_create _getdns_lruhash_create +#define lruhash_delete _getdns_lruhash_delete +#define lruhash_clear _getdns_lruhash_clear +#define lruhash_insert _getdns_lruhash_insert +#define lruhash_lookup _getdns_lruhash_lookup +#define lru_touch _getdns_lru_touch +#define lruhash_setmarkdel _getdns_lruhash_setmarkdel + +#define lruhash_remove _getdns_lruhash_remove +#define bin_init _getdns_bin_init +#define bin_delete _getdns_bin_delete +#define bin_find_entry _getdns_bin_find_entry +#define bin_overflow_remove _getdns_bin_overflow_remove +#define bin_split _getdns_bin_split +#define reclaim_space _getdns_reclaim_space +#define table_grow _getdns_table_grow +#define lru_front _getdns_lru_front +#define lru_remove _getdns_lru_remove +#define lruhash_status _getdns_lruhash_status +#define lruhash_get_mem _getdns_lruhash_get_mem +#define lruhash_traverse _getdns_lruhash_traverse + +#include "util/orig-headers/lruhash.h" +#endif diff --git a/src/util/orig-headers/locks.h b/src/util/orig-headers/locks.h new file mode 100644 index 00000000..d86ee492 --- /dev/null +++ b/src/util/orig-headers/locks.h @@ -0,0 +1,313 @@ +/** + * util/locks.h - unbound locking primitives + * + * Copyright (c) 2007, NLnet Labs. All rights reserved. + * + * This software is open source. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the NLNET LABS nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef UTIL_LOCKS_H +#define UTIL_LOCKS_H + +/** + * \file + * Locking primitives. + * If pthreads is available, these are used. + * If no locking exists, they do nothing. + * + * The idea is to have different sorts of locks for different tasks. + * This allows the locking code to be ported more easily. + * + * Types of locks that are supported. + * o lock_rw: lock that has many readers and one writer (to a data entry). + * o lock_basic: simple mutex. Blocking, one person has access only. + * This lock is meant for non performance sensitive uses. + * o lock_quick: speed lock. For performance sensitive locking of critical + * sections. Could be implemented by a mutex or a spinlock. + * + * Also thread creation and deletion functions are defined here. + */ + +/* if you define your own LOCKRET before including locks.h, you can get most + * locking functions without the dependency on log_err. */ +#ifndef LOCKRET +#include "util/log.h" +/** + * The following macro is used to check the return value of the + * pthread calls. They return 0 on success and an errno on error. + * The errno is logged to the logfile with a descriptive comment. + */ +#define LOCKRET(func) do {\ + int lockret_err; \ + if( (lockret_err=(func)) != 0) \ + log_err("%s at %d could not " #func ": %s", \ + __FILE__, __LINE__, strerror(lockret_err)); \ + } while(0) +#endif + +/** DEBUG: use thread debug whenever possible */ +#if defined(HAVE_PTHREAD) && defined(HAVE_PTHREAD_SPINLOCK_T) && defined(ENABLE_LOCK_CHECKS) +# define USE_THREAD_DEBUG +#endif + +#ifdef USE_THREAD_DEBUG +/******************* THREAD DEBUG ************************/ +/* (some) checking; to detect races and deadlocks. */ +#include "testcode/checklocks.h" + +#else /* USE_THREAD_DEBUG */ +#define lock_protect(lock, area, size) /* nop */ +#define lock_unprotect(lock, area) /* nop */ +#define lock_get_mem(lock) (0) /* nothing */ +#define checklock_start() /* nop */ +#define checklock_stop() /* nop */ + +#ifdef HAVE_PTHREAD +#include + +/******************* PTHREAD ************************/ + +/** use pthread mutex for basic lock */ +typedef pthread_mutex_t lock_basic_type; +/** small front for pthread init func, NULL is default attrs. */ +#define lock_basic_init(lock) LOCKRET(pthread_mutex_init(lock, NULL)) +#define lock_basic_destroy(lock) LOCKRET(pthread_mutex_destroy(lock)) +#define lock_basic_lock(lock) LOCKRET(pthread_mutex_lock(lock)) +#define lock_basic_unlock(lock) LOCKRET(pthread_mutex_unlock(lock)) + +#ifndef HAVE_PTHREAD_RWLOCK_T +/** in case rwlocks are not supported, use a mutex. */ +typedef pthread_mutex_t lock_rw_type; +#define lock_rw_init(lock) LOCKRET(pthread_mutex_init(lock, NULL)) +#define lock_rw_destroy(lock) LOCKRET(pthread_mutex_destroy(lock)) +#define lock_rw_rdlock(lock) LOCKRET(pthread_mutex_lock(lock)) +#define lock_rw_wrlock(lock) LOCKRET(pthread_mutex_lock(lock)) +#define lock_rw_unlock(lock) LOCKRET(pthread_mutex_unlock(lock)) +#else /* HAVE_PTHREAD_RWLOCK_T */ +/** we use the pthread rwlock */ +typedef pthread_rwlock_t lock_rw_type; +/** small front for pthread init func, NULL is default attrs. */ +#define lock_rw_init(lock) LOCKRET(pthread_rwlock_init(lock, NULL)) +#define lock_rw_destroy(lock) LOCKRET(pthread_rwlock_destroy(lock)) +#define lock_rw_rdlock(lock) LOCKRET(pthread_rwlock_rdlock(lock)) +#define lock_rw_wrlock(lock) LOCKRET(pthread_rwlock_wrlock(lock)) +#define lock_rw_unlock(lock) LOCKRET(pthread_rwlock_unlock(lock)) +#endif /* HAVE_PTHREAD_RWLOCK_T */ + +#ifndef HAVE_PTHREAD_SPINLOCK_T +/** in case spinlocks are not supported, use a mutex. */ +typedef pthread_mutex_t lock_quick_type; +/** small front for pthread init func, NULL is default attrs. */ +#define lock_quick_init(lock) LOCKRET(pthread_mutex_init(lock, NULL)) +#define lock_quick_destroy(lock) LOCKRET(pthread_mutex_destroy(lock)) +#define lock_quick_lock(lock) LOCKRET(pthread_mutex_lock(lock)) +#define lock_quick_unlock(lock) LOCKRET(pthread_mutex_unlock(lock)) + +#else /* HAVE_PTHREAD_SPINLOCK_T */ +/** use pthread spinlock for the quick lock */ +typedef pthread_spinlock_t lock_quick_type; +/** + * allocate process private since this is available whether + * Thread Process-Shared Synchronization is supported or not. + * This means only threads inside this process may access the lock. + * (not threads from another process that shares memory). + * spinlocks are not supported on all pthread platforms. + */ +#define lock_quick_init(lock) LOCKRET(pthread_spin_init(lock, PTHREAD_PROCESS_PRIVATE)) +#define lock_quick_destroy(lock) LOCKRET(pthread_spin_destroy(lock)) +#define lock_quick_lock(lock) LOCKRET(pthread_spin_lock(lock)) +#define lock_quick_unlock(lock) LOCKRET(pthread_spin_unlock(lock)) + +#endif /* HAVE SPINLOCK */ + +/** Thread creation */ +typedef pthread_t ub_thread_type; +/** On alpine linux default thread stack size is 80 Kb. See +http://wiki.musl-libc.org/wiki/Functional_differences_from_glibc#Thread_stack_size +This is not enough and cause segfault. Other linux distros have 2 Mb at least. +Wrapper for set up thread stack size */ +#define PTHREADSTACKSIZE 2*1024*1024 +#define PTHREADCREATE(thr, stackrequired, func, arg) do {\ + pthread_attr_t attr; \ + size_t stacksize; \ + LOCKRET(pthread_attr_init(&attr)); \ + LOCKRET(pthread_attr_getstacksize(&attr, &stacksize)); \ + if (stacksize < stackrequired) { \ + LOCKRET(pthread_attr_setstacksize(&attr, stackrequired)); \ + LOCKRET(pthread_create(thr, &attr, func, arg)); \ + LOCKRET(pthread_attr_getstacksize(&attr, &stacksize)); \ + verbose(VERB_ALGO, "Thread stack size set to %u", (unsigned)stacksize); \ + } else {LOCKRET(pthread_create(thr, NULL, func, arg));} \ + } while(0) +/** Use wrapper for set thread stack size on attributes. */ +#define ub_thread_create(thr, func, arg) PTHREADCREATE(thr, PTHREADSTACKSIZE, func, arg) +/** get self id. */ +#define ub_thread_self() pthread_self() +/** wait for another thread to terminate */ +#define ub_thread_join(thread) LOCKRET(pthread_join(thread, NULL)) +typedef pthread_key_t ub_thread_key_type; +#define ub_thread_key_create(key, f) LOCKRET(pthread_key_create(key, f)) +#define ub_thread_key_set(key, v) LOCKRET(pthread_setspecific(key, v)) +#define ub_thread_key_get(key) pthread_getspecific(key) + +#else /* we do not HAVE_PTHREAD */ +#ifdef HAVE_SOLARIS_THREADS + +/******************* SOLARIS THREADS ************************/ +#include +#include + +typedef rwlock_t lock_rw_type; +#define lock_rw_init(lock) LOCKRET(rwlock_init(lock, USYNC_THREAD, NULL)) +#define lock_rw_destroy(lock) LOCKRET(rwlock_destroy(lock)) +#define lock_rw_rdlock(lock) LOCKRET(rw_rdlock(lock)) +#define lock_rw_wrlock(lock) LOCKRET(rw_wrlock(lock)) +#define lock_rw_unlock(lock) LOCKRET(rw_unlock(lock)) + +/** use basic mutex */ +typedef mutex_t lock_basic_type; +#define lock_basic_init(lock) LOCKRET(mutex_init(lock, USYNC_THREAD, NULL)) +#define lock_basic_destroy(lock) LOCKRET(mutex_destroy(lock)) +#define lock_basic_lock(lock) LOCKRET(mutex_lock(lock)) +#define lock_basic_unlock(lock) LOCKRET(mutex_unlock(lock)) + +/** No spinlocks in solaris threads API. Use a mutex. */ +typedef mutex_t lock_quick_type; +#define lock_quick_init(lock) LOCKRET(mutex_init(lock, USYNC_THREAD, NULL)) +#define lock_quick_destroy(lock) LOCKRET(mutex_destroy(lock)) +#define lock_quick_lock(lock) LOCKRET(mutex_lock(lock)) +#define lock_quick_unlock(lock) LOCKRET(mutex_unlock(lock)) + +/** Thread creation, create a default thread. */ +typedef thread_t ub_thread_type; +#define ub_thread_create(thr, func, arg) LOCKRET(thr_create(NULL, NULL, func, arg, NULL, thr)) +#define ub_thread_self() thr_self() +#define ub_thread_join(thread) LOCKRET(thr_join(thread, NULL, NULL)) +typedef thread_key_t ub_thread_key_type; +#define ub_thread_key_create(key, f) LOCKRET(thr_keycreate(key, f)) +#define ub_thread_key_set(key, v) LOCKRET(thr_setspecific(key, v)) +void* ub_thread_key_get(ub_thread_key_type key); + + +#else /* we do not HAVE_SOLARIS_THREADS and no PTHREADS */ +/******************* WINDOWS THREADS ************************/ +#ifdef HAVE_WINDOWS_THREADS +#include + +/* Use a mutex */ +typedef LONG lock_rw_type; +#define lock_rw_init(lock) lock_basic_init(lock) +#define lock_rw_destroy(lock) lock_basic_destroy(lock) +#define lock_rw_rdlock(lock) lock_basic_lock(lock) +#define lock_rw_wrlock(lock) lock_basic_lock(lock) +#define lock_rw_unlock(lock) lock_basic_unlock(lock) + +/** the basic lock is a mutex, implemented opaquely, for error handling. */ +typedef LONG lock_basic_type; +void lock_basic_init(lock_basic_type* lock); +void lock_basic_destroy(lock_basic_type* lock); +void lock_basic_lock(lock_basic_type* lock); +void lock_basic_unlock(lock_basic_type* lock); + +/** on windows no spinlock, use mutex too. */ +typedef LONG lock_quick_type; +#define lock_quick_init(lock) lock_basic_init(lock) +#define lock_quick_destroy(lock) lock_basic_destroy(lock) +#define lock_quick_lock(lock) lock_basic_lock(lock) +#define lock_quick_unlock(lock) lock_basic_unlock(lock) + +/** Thread creation, create a default thread. */ +typedef HANDLE ub_thread_type; +void ub_thread_create(ub_thread_type* thr, void* (*func)(void*), void* arg); +ub_thread_type ub_thread_self(void); +void ub_thread_join(ub_thread_type thr); +typedef DWORD ub_thread_key_type; +void ub_thread_key_create(ub_thread_key_type* key, void* f); +void ub_thread_key_set(ub_thread_key_type key, void* v); +void* ub_thread_key_get(ub_thread_key_type key); + +#else /* we do not HAVE_SOLARIS_THREADS, PTHREADS or WINDOWS_THREADS */ + +/******************* NO THREADS ************************/ +#define THREADS_DISABLED 1 +/** In case there is no thread support, define locks to do nothing */ +typedef int lock_rw_type; +#define lock_rw_init(lock) /* nop */ +#define lock_rw_destroy(lock) /* nop */ +#define lock_rw_rdlock(lock) /* nop */ +#define lock_rw_wrlock(lock) /* nop */ +#define lock_rw_unlock(lock) /* nop */ + +/** define locks to do nothing */ +typedef int lock_basic_type; +#define lock_basic_init(lock) /* nop */ +#define lock_basic_destroy(lock) /* nop */ +#define lock_basic_lock(lock) /* nop */ +#define lock_basic_unlock(lock) /* nop */ + +/** define locks to do nothing */ +typedef int lock_quick_type; +#define lock_quick_init(lock) /* nop */ +#define lock_quick_destroy(lock) /* nop */ +#define lock_quick_lock(lock) /* nop */ +#define lock_quick_unlock(lock) /* nop */ + +/** Thread creation, threads do not exist */ +typedef pid_t ub_thread_type; +/** ub_thread_create is simulated with fork (extremely heavy threads, + * with no shared memory). */ +#define ub_thread_create(thr, func, arg) \ + ub_thr_fork_create(thr, func, arg) +#define ub_thread_self() getpid() +#define ub_thread_join(thread) ub_thr_fork_wait(thread) +void ub_thr_fork_wait(ub_thread_type thread); +void ub_thr_fork_create(ub_thread_type* thr, void* (*func)(void*), void* arg); +typedef void* ub_thread_key_type; +#define ub_thread_key_create(key, f) (*(key)) = NULL +#define ub_thread_key_set(key, v) (key) = (v) +#define ub_thread_key_get(key) (key) + +#endif /* HAVE_WINDOWS_THREADS */ +#endif /* HAVE_SOLARIS_THREADS */ +#endif /* HAVE_PTHREAD */ +#endif /* USE_THREAD_DEBUG */ + +/** + * Block all signals for this thread. + * fatal exit on error. + */ +void ub_thread_blocksigs(void); + +/** + * unblock one signal for this thread. + */ +void ub_thread_sig_unblock(int sig); + +#endif /* UTIL_LOCKS_H */ diff --git a/src/util/orig-headers/lookup3.h b/src/util/orig-headers/lookup3.h new file mode 100644 index 00000000..59dad7c4 --- /dev/null +++ b/src/util/orig-headers/lookup3.h @@ -0,0 +1,71 @@ +/* + * util/storage/lookup3.h - header file for hashing functions. + * + * Copyright (c) 2007, NLnet Labs. All rights reserved. + * + * This software is open source. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the NLNET LABS nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file + * + * This file contains header definitions for the hash functions we use. + * The hash functions are public domain (see lookup3.c). + */ + +#ifndef UTIL_STORAGE_LOOKUP3_H +#define UTIL_STORAGE_LOOKUP3_H + +/** + * Hash key made of 4byte chunks. + * @param k: the key, an array of uint32_t values + * @param length: the length of the key, in uint32_ts + * @param initval: the previous hash, or an arbitrary value + * @return: hash value. + */ +uint32_t hashword(const uint32_t *k, size_t length, uint32_t initval); + +/** + * Hash key data. + * @param k: the key, array of uint8_t + * @param length: the length of the key, in uint8_ts + * @param initval: the previous hash, or an arbitrary value + * @return: hash value. + */ +uint32_t hashlittle(const void *k, size_t length, uint32_t initval); + +/** + * Set the randomisation initial value, set this before threads start, + * and before hashing stuff (because it changes subsequent results). + * @param v: value + */ +void hash_set_raninit(uint32_t v); + +#endif /* UTIL_STORAGE_LOOKUP3_H */ diff --git a/src/util/orig-headers/lruhash.h b/src/util/orig-headers/lruhash.h new file mode 100644 index 00000000..c3937408 --- /dev/null +++ b/src/util/orig-headers/lruhash.h @@ -0,0 +1,414 @@ +/* + * util/storage/lruhash.h - hashtable, hash function, LRU keeping. + * + * Copyright (c) 2007, NLnet Labs. All rights reserved. + * + * This software is open source. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * Neither the name of the NLNET LABS nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file + * + * This file contains a hashtable with LRU keeping of entries. + * + * The hash table keeps a maximum memory size. Old entries are removed + * to make space for new entries. + * + * The locking strategy is as follows: + * o since (almost) every read also implies a LRU update, the + * hashtable lock is a spinlock, not rwlock. + * o the idea is to move every thread through the hash lock quickly, + * so that the next thread can access the lookup table. + * o User performs hash function. + * + * For read: + * o lock hashtable. + * o lookup hash bin. + * o lock hash bin. + * o find entry (if failed, unlock hash, unl bin, exit). + * o swizzle pointers for LRU update. + * o unlock hashtable. + * o lock entry (rwlock). + * o unlock hash bin. + * o work on entry. + * o unlock entry. + * + * To update an entry, gain writelock and change the entry. + * (the entry must keep the same hashvalue, so a data update.) + * (you cannot upgrade a readlock to a writelock, because the item may + * be deleted, it would cause race conditions. So instead, unlock and + * relookup it in the hashtable.) + * + * To delete an entry: + * o unlock the entry if you hold the lock already. + * o lock hashtable. + * o lookup hash bin. + * o lock hash bin. + * o find entry (if failed, unlock hash, unl bin, exit). + * o remove entry from hashtable bin overflow chain. + * o unlock hashtable. + * o lock entry (writelock). + * o unlock hash bin. + * o unlock entry (nobody else should be waiting for this lock, + * since you removed it from hashtable, and you got writelock while + * holding the hashbinlock so you are the only one.) + * Note you are only allowed to obtain a lock while holding hashbinlock. + * o delete entry. + * + * The above sequence is: + * o race free, works with read, write and delete. + * o but has a queue, imagine someone needing a writelock on an item. + * but there are still readlocks. The writelocker waits, but holds + * the hashbinlock. The next thread that comes in and needs the same + * hashbin will wait for the lock while holding the hashtable lock. + * thus halting the entire system on hashtable. + * This is because of the delete protection. + * Readlocks will be easier on the rwlock on entries. + * While the writer is holding writelock, similar problems happen with + * a reader or writer needing the same item. + * the scenario requires more than three threads. + * o so the queue length is 3 threads in a bad situation. The fourth is + * unable to use the hashtable. + * + * If you need to acquire locks on multiple items from the hashtable. + * o you MUST release all locks on items from the hashtable before + * doing the next lookup/insert/delete/whatever. + * o To acquire multiple items you should use a special routine that + * obtains the locks on those multiple items in one go. + */ + +#ifndef UTIL_STORAGE_LRUHASH_H +#define UTIL_STORAGE_LRUHASH_H +#include "util/locks.h" +struct lruhash_bin; +struct lruhash_entry; + +/** default start size for hash arrays */ +#define HASH_DEFAULT_STARTARRAY 1024 /* entries in array */ +/** default max memory for hash arrays */ +#define HASH_DEFAULT_MAXMEM 4*1024*1024 /* bytes */ + +/** the type of a hash value */ +typedef uint32_t hashvalue_type; + +/** + * Type of function that calculates the size of an entry. + * Result must include the size of struct lruhash_entry. + * Keys that are identical must also calculate to the same size. + * size = func(key, data). + */ +typedef size_t (*lruhash_sizefunc_type)(void*, void*); + +/** type of function that compares two keys. return 0 if equal. */ +typedef int (*lruhash_compfunc_type)(void*, void*); + +/** old keys are deleted. + * The RRset type has to revoke its ID number, markdel() is used first. + * This function is called: func(key, userarg) */ +typedef void (*lruhash_delkeyfunc_type)(void*, void*); + +/** old data is deleted. This function is called: func(data, userarg). */ +typedef void (*lruhash_deldatafunc_type)(void*, void*); + +/** mark a key as pending to be deleted (and not to be used by anyone). + * called: func(key) */ +typedef void (*lruhash_markdelfunc_type)(void*); + +/** + * Hash table that keeps LRU list of entries. + */ +struct lruhash { + /** lock for exclusive access, to the lookup array */ + lock_quick_type lock; + /** the size function for entries in this table */ + lruhash_sizefunc_type sizefunc; + /** the compare function for entries in this table. */ + lruhash_compfunc_type compfunc; + /** how to delete keys. */ + lruhash_delkeyfunc_type delkeyfunc; + /** how to delete data. */ + lruhash_deldatafunc_type deldatafunc; + /** how to mark a key pending deletion */ + lruhash_markdelfunc_type markdelfunc; + /** user argument for user functions */ + void* cb_arg; + + /** the size of the lookup array */ + size_t size; + /** size bitmask - since size is a power of 2 */ + int size_mask; + /** lookup array of bins */ + struct lruhash_bin* array; + + /** the lru list, start and end, noncyclical double linked list. */ + struct lruhash_entry* lru_start; + /** lru list end item (least recently used) */ + struct lruhash_entry* lru_end; + + /** the number of entries in the hash table. */ + size_t num; + /** the amount of space used, roughly the number of bytes in use. */ + size_t space_used; + /** the amount of space the hash table is maximally allowed to use. */ + size_t space_max; +}; + +/** + * A single bin with a linked list of entries in it. + */ +struct lruhash_bin { + /** + * Lock for exclusive access to the linked list + * This lock makes deletion of items safe in this overflow list. + */ + lock_quick_type lock; + /** linked list of overflow entries */ + struct lruhash_entry* overflow_list; +}; + +/** + * An entry into the hash table. + * To change overflow_next you need to hold the bin lock. + * To change the lru items you need to hold the hashtable lock. + * This structure is designed as part of key struct. And key pointer helps + * to get the surrounding structure. Data should be allocated on its own. + */ +struct lruhash_entry { + /** + * rwlock for access to the contents of the entry + * Note that it does _not_ cover the lru_ and overflow_ ptrs. + * Even with a writelock, you cannot change hash and key. + * You need to delete it to change hash or key. + */ + lock_rw_type lock; + /** next entry in overflow chain. Covered by hashlock and binlock. */ + struct lruhash_entry* overflow_next; + /** next entry in lru chain. covered by hashlock. */ + struct lruhash_entry* lru_next; + /** prev entry in lru chain. covered by hashlock. */ + struct lruhash_entry* lru_prev; + /** hash value of the key. It may not change, until entry deleted. */ + hashvalue_type hash; + /** key */ + void* key; + /** data */ + void* data; +}; + +/** + * Create new hash table. + * @param start_size: size of hashtable array at start, must be power of 2. + * @param maxmem: maximum amount of memory this table is allowed to use. + * @param sizefunc: calculates memory usage of entries. + * @param compfunc: compares entries, 0 on equality. + * @param delkeyfunc: deletes key. + * Calling both delkey and deldata will also free the struct lruhash_entry. + * Make it part of the key structure and delete it in delkeyfunc. + * @param deldatafunc: deletes data. + * @param arg: user argument that is passed to user function calls. + * @return: new hash table or NULL on malloc failure. + */ +struct lruhash* lruhash_create(size_t start_size, size_t maxmem, + lruhash_sizefunc_type sizefunc, lruhash_compfunc_type compfunc, + lruhash_delkeyfunc_type delkeyfunc, + lruhash_deldatafunc_type deldatafunc, void* arg); + +/** + * Delete hash table. Entries are all deleted. + * @param table: to delete. + */ +void lruhash_delete(struct lruhash* table); + +/** + * Clear hash table. Entries are all deleted, while locking them before + * doing so. At end the table is empty. + * @param table: to make empty. + */ +void lruhash_clear(struct lruhash* table); + +/** + * Insert a new element into the hashtable. + * If key is already present data pointer in that entry is updated. + * The space calculation function is called with the key, data. + * If necessary the least recently used entries are deleted to make space. + * If necessary the hash array is grown up. + * + * @param table: hash table. + * @param hash: hash value. User calculates the hash. + * @param entry: identifies the entry. + * If key already present, this entry->key is deleted immediately. + * But entry->data is set to NULL before deletion, and put into + * the existing entry. The data is then freed. + * @param data: the data. + * @param cb_override: if not null overrides the cb_arg for the deletefunc. + */ +void lruhash_insert(struct lruhash* table, hashvalue_type hash, + struct lruhash_entry* entry, void* data, void* cb_override); + +/** + * Lookup an entry in the hashtable. + * At the end of the function you hold a (read/write)lock on the entry. + * The LRU is updated for the entry (if found). + * @param table: hash table. + * @param hash: hash of key. + * @param key: what to look for, compared against entries in overflow chain. + * the hash value must be set, and must work with compare function. + * @param wr: set to true if you desire a writelock on the entry. + * with a writelock you can update the data part. + * @return: pointer to the entry or NULL. The entry is locked. + * The user must unlock the entry when done. + */ +struct lruhash_entry* lruhash_lookup(struct lruhash* table, + hashvalue_type hash, void* key, int wr); + +/** + * Touch entry, so it becomes the most recently used in the LRU list. + * Caller must hold hash table lock. The entry must be inserted already. + * @param table: hash table. + * @param entry: entry to make first in LRU. + */ +void lru_touch(struct lruhash* table, struct lruhash_entry* entry); + +/** + * Set the markdelfunction (or NULL) + */ +void lruhash_setmarkdel(struct lruhash* table, lruhash_markdelfunc_type md); + +/************************* Internal functions ************************/ +/*** these are only exposed for unit tests. ***/ + +/** + * Remove entry from hashtable. Does nothing if not found in hashtable. + * Delfunc is called for the entry. + * @param table: hash table. + * @param hash: hash of key. + * @param key: what to look for. + */ +void lruhash_remove(struct lruhash* table, hashvalue_type hash, void* key); + +/** init the hash bins for the table */ +void bin_init(struct lruhash_bin* array, size_t size); + +/** delete the hash bin and entries inside it */ +void bin_delete(struct lruhash* table, struct lruhash_bin* bin); + +/** + * Find entry in hash bin. You must have locked the bin. + * @param table: hash table with function pointers. + * @param bin: hash bin to look into. + * @param hash: hash value to look for. + * @param key: key to look for. + * @return: the entry or NULL if not found. + */ +struct lruhash_entry* bin_find_entry(struct lruhash* table, + struct lruhash_bin* bin, hashvalue_type hash, void* key); + +/** + * Remove entry from bin overflow chain. + * You must have locked the bin. + * @param bin: hash bin to look into. + * @param entry: entry ptr that needs removal. + */ +void bin_overflow_remove(struct lruhash_bin* bin, + struct lruhash_entry* entry); + +/** + * Split hash bin into two new ones. Based on increased size_mask. + * Caller must hold hash table lock. + * At the end the routine acquires all hashbin locks (in the old array). + * This makes it wait for other threads to finish with the bins. + * So the bins are ready to be deleted after this function. + * @param table: hash table with function pointers. + * @param newa: new increased array. + * @param newmask: new lookup mask. + */ +void bin_split(struct lruhash* table, struct lruhash_bin* newa, + int newmask); + +/** + * Try to make space available by deleting old entries. + * Assumes that the lock on the hashtable is being held by caller. + * Caller must not hold bin locks. + * @param table: hash table. + * @param list: list of entries that are to be deleted later. + * Entries have been removed from the hash table and writelock is held. + */ +void reclaim_space(struct lruhash* table, struct lruhash_entry** list); + +/** + * Grow the table lookup array. Becomes twice as large. + * Caller must hold the hash table lock. Must not hold any bin locks. + * Tries to grow, on malloc failure, nothing happened. + * @param table: hash table. + */ +void table_grow(struct lruhash* table); + +/** + * Put entry at front of lru. entry must be unlinked from lru. + * Caller must hold hash table lock. + * @param table: hash table with lru head and tail. + * @param entry: entry to make most recently used. + */ +void lru_front(struct lruhash* table, struct lruhash_entry* entry); + +/** + * Remove entry from lru list. + * Caller must hold hash table lock. + * @param table: hash table with lru head and tail. + * @param entry: entry to remove from lru. + */ +void lru_remove(struct lruhash* table, struct lruhash_entry* entry); + +/** + * Output debug info to the log as to state of the hash table. + * @param table: hash table. + * @param id: string printed with table to identify the hash table. + * @param extended: set to true to print statistics on overflow bin lengths. + */ +void lruhash_status(struct lruhash* table, const char* id, int extended); + +/** + * Get memory in use now by the lruhash table. + * @param table: hash table. Will be locked before use. And unlocked after. + * @return size in bytes. + */ +size_t lruhash_get_mem(struct lruhash* table); + +/** + * Traverse a lruhash. Call back for every element in the table. + * @param h: hash table. Locked before use. + * @param wr: if true writelock is obtained on element, otherwise readlock. + * @param func: function for every element. Do not lock or unlock elements. + * @param arg: user argument to func. + */ +void lruhash_traverse(struct lruhash* h, int wr, + void (*func)(struct lruhash_entry*, void*), void* arg); + +#endif /* UTIL_STORAGE_LRUHASH_H */ From 82c92f8dc7c8e11776c47b696f3b9863c6915ddc Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Thu, 9 Mar 2017 13:02:05 +0100 Subject: [PATCH 18/25] Better dependency rewriting --- src/Makefile.in | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Makefile.in b/src/Makefile.in index abbb076e..6497cc6a 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -193,11 +193,12 @@ depend: (cd $(srcdir) ; awk 'BEGIN{P=1}{if(P)print}/^# Dependencies/{P=0}' Makefile.in > Makefile.in.new ) (blddir=`pwd`; cd $(srcdir) ; gcc -MM -I. -I"$$blddir" -Iutil/auxiliary *.c gldns/*.c compat/*.c util/*.c jsmn/*.c extension/*.c| \ sed -e "s? $$blddir/? ?g" \ - -e 's?gldns/?$$(srcdir)/gldns/?g' \ - -e 's?compat/?$$(srcdir)/compat/?g' \ - -e 's?util/?$$(srcdir)/util/?g' \ - -e 's?jsmn/?$$(srcdir)/jsmn/?g' \ - -e 's?extension/?$$(srcdir)/extension/?g' \ + -e 's? gldns/? $$(srcdir)/gldns/?g' \ + -e 's? compat/? $$(srcdir)/compat/?g' \ + -e 's? util/auxiliary/util/? $$(srcdir)/util/auxiliary/util/?g' \ + -e 's? util/? $$(srcdir)/util/?g' \ + -e 's? jsmn/? $$(srcdir)/jsmn/?g' \ + -e 's? extension/? $$(srcdir)/extension/?g' \ -e 's? \([a-z_-]*\)\.\([ch]\)? $$(srcdir)/\1.\2?g' \ -e 's? \$$(srcdir)/config\.h? config.h?g' \ -e 's? \$$(srcdir)/getdns/getdns_extra\.h? getdns/getdns_extra.h?g' \ @@ -342,22 +343,22 @@ inet_pton.lo inet_pton.o: $(srcdir)/compat/inet_pton.c config.h sha512.lo sha512.o: $(srcdir)/compat/sha512.c config.h strlcpy.lo strlcpy.o: $(srcdir)/compat/strlcpy.c config.h locks.lo locks.o: $(srcdir)/util/locks.c config.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ - $(srcdir)/util/auxiliary/$(srcdir)/util/log.h $(srcdir)/debug.h config.h -lookup3.lo lookup3.o: $(srcdir)/util/lookup3.c config.h $(srcdir)/util/auxiliary/$(srcdir)/util/storage/lookup3.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h config.h +lookup3.lo lookup3.o: $(srcdir)/util/lookup3.c config.h $(srcdir)/util/auxiliary/util/storage/lookup3.h \ $(srcdir)/util/lookup3.h $(srcdir)/util/orig-headers/lookup3.h -lruhash.lo lruhash.o: $(srcdir)/util/lruhash.c config.h $(srcdir)/util/auxiliary/$(srcdir)/util/storage/lruhash.h \ +lruhash.lo lruhash.o: $(srcdir)/util/lruhash.c config.h $(srcdir)/util/auxiliary/util/storage/lruhash.h \ $(srcdir)/util/lruhash.h $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h \ - $(srcdir)/util/orig-headers/locks.h $(srcdir)/util/auxiliary/$(srcdir)/util/log.h $(srcdir)/debug.h config.h \ - $(srcdir)/util/auxiliary/$(srcdir)/util/fptr_wlist.h + $(srcdir)/util/orig-headers/locks.h $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h config.h \ + $(srcdir)/util/auxiliary/util/fptr_wlist.h rbtree.lo rbtree.o: $(srcdir)/util/rbtree.c config.h $(srcdir)/util/auxiliary/log.h \ - $(srcdir)/util/auxiliary/$(srcdir)/util/log.h $(srcdir)/debug.h config.h $(srcdir)/util/auxiliary/fptr_wlist.h \ - $(srcdir)/util/auxiliary/$(srcdir)/util/fptr_wlist.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h config.h $(srcdir)/util/auxiliary/fptr_wlist.h \ + $(srcdir)/util/auxiliary/util/fptr_wlist.h $(srcdir)/util/rbtree.h \ $(srcdir)/util/orig-headers/rbtree.h val_secalgo.lo val_secalgo.o: $(srcdir)/util/val_secalgo.c config.h \ - $(srcdir)/util/auxiliary/$(srcdir)/util/data/packed_rrset.h \ + $(srcdir)/util/auxiliary/util/data/packed_rrset.h \ $(srcdir)/util/auxiliary/validator/val_secalgo.h $(srcdir)/util/val_secalgo.h \ $(srcdir)/util/orig-headers/val_secalgo.h $(srcdir)/util/auxiliary/validator/val_nsec3.h \ - $(srcdir)/util/auxiliary/$(srcdir)/util/log.h $(srcdir)/debug.h config.h $(srcdir)/util/auxiliary/sldns/rrdef.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h config.h $(srcdir)/util/auxiliary/sldns/rrdef.h \ $(srcdir)/gldns/rrdef.h $(srcdir)/util/auxiliary/sldns/keyraw.h $(srcdir)/gldns/keyraw.h \ $(srcdir)/util/auxiliary/sldns/sbuffer.h $(srcdir)/gldns/gbuffer.h jsmn.lo jsmn.o: $(srcdir)/jsmn/jsmn.c $(srcdir)/jsmn/jsmn.h From 79ce0cff85ab22fcd1808acb4e0c8714416efc13 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Thu, 9 Mar 2017 14:36:20 +0100 Subject: [PATCH 19/25] Make mdns compile on Linux --- configure.ac | 4 ++-- src/context.h | 3 +++ src/mdns.c | 55 +++++++++++++++++++++++---------------------------- src/mdns.h | 7 ++++++- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/configure.ac b/configure.ac index 011760ef..bb2fa22f 100644 --- a/configure.ac +++ b/configure.ac @@ -667,7 +667,7 @@ AC_TYPE_UINT16_T AC_TYPE_UINT32_T AC_TYPE_UINT64_T AC_TYPE_UINT8_T -AC_CHECK_TYPE([u_char]) +AC_CHECK_TYPES([u_char]) AC_CHECK_FUNCS([fcntl]) # check ioctlsocket @@ -752,7 +752,7 @@ AS_IF([test x_$withval = x_no], [AC_MSG_ERROR([event2/event.h and event.h missing, try without libevent])] [have_libevent=0], [AC_INCLUDES_DEFAULT] - [#if HAVE_U_CHAR == 0 + [#ifndef HAVE_U_CHAR typedef unsigned char u_char; #endif])], [AC_INCLUDES_DEFAULT])], diff --git a/src/context.h b/src/context.h index 5745fcee..8fde6178 100644 --- a/src/context.h +++ b/src/context.h @@ -45,6 +45,9 @@ #include "util/rbtree.h" #include "ub_loop.h" #include "server.h" +#ifdef HAVE_MDNS_SUPPORT +#include "util/lruhash.h" +#endif struct getdns_dns_req; struct ub_ctx; diff --git a/src/mdns.c b/src/mdns.c index 414b3586..e55a3916 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -38,17 +38,20 @@ typedef u_short sa_family_t; #else #define _getdns_EWOULDBLOCK (errno == EAGAIN || errno == EWOULDBLOCK) #define _getdns_EINPROGRESS (errno == EINPROGRESS) +#define SOCKADDR struct sockaddr +#define SOCKADDR_IN struct sockaddr_in +#define SOCKADDR_IN6 struct sockaddr_in6 +#define SOCKET int +#define IP_MREQ struct ip_mreq +#define IPV6_MREQ struct ipv6_mreq +#define BOOL int +#define TRUE 1 #endif uint64_t _getdns_get_time_as_uintt64(); -#include "util/storage/lruhash.h" -#include "util/storage/lookup3.h" - -#ifndef HAVE_LIBUNBOUND -#include "util/storage/lruhash.c" -#include "util/storage/lookup3.c" -#endif +#include "util/fptr_wlist.h" +#include "util/lookup3.h" /* * Constants defined in RFC 6762 @@ -352,8 +355,6 @@ mdns_util_canonical_record(uint8_t *message, int message_length, { int ret = 0; int current_index = record_index; - int buffer_index = 0; - int name_len = 0; /* Check whether the record needs canonization */ *actual_record = message + record_index; *actual_length = record_length; @@ -781,7 +782,6 @@ mdns_update_cache_ttl_and_prune(struct getdns_context *context, int current_record_data_len; uint32_t current_record_ttl; int not_matched_yet = (record_data == NULL) ? 0 : 1; - int current_record_match; int last_copied_index; int current_hole_index = 0; int record_name_length = 0; @@ -819,15 +819,12 @@ mdns_update_cache_ttl_and_prune(struct getdns_context *context, current_record_data_len == record_data_len && memcmp(old_record + record_ttl_index + 4 + 2, record_data, record_data_len) == 0) { - current_record_match = 1; not_matched_yet = 0; current_record_ttl = ttl; } else { /* Not a match */ - current_record_match = 0; - if (current_record_ttl > delta_t_sec) { current_record_ttl -= delta_t_sec; @@ -945,7 +942,6 @@ mdns_propose_entry_to_cache( uint64_t current_time) { int ret = 0; - size_t required_memory = 0; uint8_t temp_key[256 + sizeof(getdns_mdns_cached_key_header)]; hashvalue_type hash; struct lruhash_entry *entry, *new_entry; @@ -988,7 +984,7 @@ mdns_propose_entry_to_cache( new_entry = &((getdns_mdns_cached_key_header*)key)->entry; memset(new_entry, 0, sizeof(struct lruhash_entry)); - lock_rw_init(new_entry->lock); + lock_rw_init(&new_entry->lock); new_entry->hash = hash; new_entry->key = key; new_entry->data = data; @@ -1036,7 +1032,7 @@ mdns_propose_entry_to_cache( } /* then, unlock the entry */ - lock_rw_unlock(entry->lock); + lock_rw_unlock(&entry->lock); } return ret; @@ -1139,7 +1135,6 @@ mdns_cache_complete_queries( int record_type, int record_class) { int ret = 0; - size_t required_memory = 0; struct lruhash_entry *entry; getdns_mdns_cached_record_header * header; getdns_network_req * netreq; @@ -1157,7 +1152,7 @@ mdns_cache_complete_queries( mdns_complete_query_from_cache_entry(netreq, entry); } } - lock_rw_unlock(entry->lock); + lock_rw_unlock(&entry->lock); } return ret; @@ -1173,8 +1168,6 @@ mdns_mcast_timeout_cb(void *userarg) getdns_dns_req *dnsreq = netreq->owner; getdns_context *context = dnsreq->context; - int ret = 0; - size_t required_memory = 0; uint8_t temp_key[256 + sizeof(getdns_mdns_cached_key_header)]; hashvalue_type hash; struct lruhash_entry *entry; @@ -1201,7 +1194,7 @@ mdns_mcast_timeout_cb(void *userarg) found = 1; mdns_complete_query_from_cache_entry(netreq, entry); } - lock_rw_unlock(entry->lock); + lock_rw_unlock(&entry->lock); } if (!found) @@ -1251,7 +1244,7 @@ mdns_udp_multicast_read_cb(void *userarg) } else { - size_t current_index = 12; + ssize_t current_index = 12; uint8_t name[256]; int name_len; int record_type; @@ -1388,7 +1381,8 @@ static int mdns_open_ipv4_multicast(SOCKADDR_STORAGE* mcast_dest, int* mcast_des SOCKET fd4 = -1; SOCKADDR_IN ipv4_dest; SOCKADDR_IN ipv4_port; - uint8_t so_reuse_bool = 1; + BOOL so_reuse_bool = TRUE; + int so_reuse_bool_OptLen = sizeof(BOOL); uint8_t ttl = 255; IP_MREQ mreq4; @@ -1398,9 +1392,11 @@ static int mdns_open_ipv4_multicast(SOCKADDR_STORAGE* mcast_dest, int* mcast_des memset(&ipv4_port, 0, sizeof(ipv4_dest)); ipv4_dest.sin_family = AF_INET; ipv4_dest.sin_port = htons(MDNS_MCAST_PORT); - ipv4_dest.sin_addr.S_un.S_addr = htonl(MDNS_MCAST_IPV4_LONG); + ipv4_dest.sin_addr.s_addr = htonl(MDNS_MCAST_IPV4_LONG); ipv4_port.sin_family = AF_INET; ipv4_port.sin_port = htons(MDNS_MCAST_PORT); + /* memcpy(&ipv4_dest.sin_addr, mdns_mcast_ipv4, sizeof(mdns_mcast_ipv4)); */ + fd4 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -1412,7 +1408,7 @@ static int mdns_open_ipv4_multicast(SOCKADDR_STORAGE* mcast_dest, int* mcast_des * since the only result that matters is that of bind. */ (void)setsockopt(fd4, SOL_SOCKET, SO_REUSEADDR - , (const char*)&so_reuse_bool, (int) sizeof(BOOL)); + , (const char*)&so_reuse_bool, so_reuse_bool_OptLen); if (bind(fd4, (SOCKADDR*)&ipv4_port, sizeof(ipv4_port)) != 0) { @@ -1633,7 +1629,6 @@ static getdns_return_t mdns_initialize_continuous_request(getdns_network_req *ne getdns_dns_req *dnsreq = netreq->owner; struct getdns_context *context = dnsreq->context; - size_t required_memory = 0; uint8_t temp_key[256 + sizeof(getdns_mdns_cached_key_header)]; hashvalue_type hash; struct lruhash_entry *entry; @@ -1697,7 +1692,7 @@ static getdns_return_t mdns_initialize_continuous_request(getdns_network_req *ne , (SOCKADDR*)&context->mdns_connection[fd_index].addr_mcast , context->mdns_connection[fd_index].addr_mcast_len); - if (pkt_len != sent) + if (sent < 0 || pkt_len != (size_t)sent) { ret = GETDNS_RETURN_GENERIC_ERROR; } @@ -1887,7 +1882,7 @@ mdns_udp_write_cb(void *userarg) mdns_mcast_v4.sin_family = AF_INET; mdns_mcast_v4.sin_port = htons(MDNS_MCAST_PORT); mdns_mcast_v4.sin_addr.s_addr = htonl(MDNS_MCAST_IPV4_LONG); - + /* memcpy(&mdns_mcast_v4.sin_addr.s_addr, mdns_mcast_ipv4, sizeof(mdns_mcast_ipv4)); */ /* Set TTL=255 for compliance with RFC 6762 */ r = setsockopt(netreq->fd, IPPROTO_IP, IP_TTL, (const char *)&ttl, sizeof(ttl)); @@ -2143,7 +2138,7 @@ int mdns_finalize_lru_test(struct getdns_context* context, } } - lock_rw_unlock(entry->lock); + lock_rw_unlock(&entry->lock); } return ret; @@ -2244,4 +2239,4 @@ int mdns_test_prepare(struct getdns_context* context) return mdns_delayed_network_init(context); } -#endif \ No newline at end of file +#endif diff --git a/src/mdns.h b/src/mdns.h index 91f3c2a3..b7c7d20c 100644 --- a/src/mdns.h +++ b/src/mdns.h @@ -24,7 +24,12 @@ #ifdef HAVE_MDNS_SUPPORT #include "getdns/getdns.h" #include "types-internal.h" -#include "util/storage/lruhash.h" +#include "util/lruhash.h" +#include "config.h" + +#ifndef USE_WINSOCK +#define SOCKADDR_STORAGE struct sockaddr_storage +#endif getdns_return_t _getdns_submit_mdns_request(getdns_network_req *netreq); From c4a93b2c5394bfcd44c5e58369605d3aaaad4a1b Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Thu, 9 Mar 2017 15:19:57 +0100 Subject: [PATCH 20/25] Newline at end of mdns.c --- src/mdns.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mdns.c b/src/mdns.c index 414b3586..9fa872ac 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -2244,4 +2244,4 @@ int mdns_test_prepare(struct getdns_context* context) return mdns_delayed_network_init(context); } -#endif \ No newline at end of file +#endif From ed66edf52ae272ea3a955495de7de9af713fb699 Mon Sep 17 00:00:00 2001 From: Christian Huitema Date: Fri, 17 Mar 2017 12:19:54 -0700 Subject: [PATCH 21/25] Making sure that the project compiles on Windows when HAVE_MDNS_SUPPORT is present. Moving the 2 additional LRU functions from mdns.c to lruhash.c Defining the 2 additional functions in lruhash.h --- src/mdns.c | 91 +--------------------------------------------- src/util/lookup3.c | 1 + src/util/lruhash.c | 87 ++++++++++++++++++++++++++++++++++++++++++++ src/util/lruhash.h | 35 ++++++++++++++++++ 4 files changed, 124 insertions(+), 90 deletions(-) diff --git a/src/mdns.c b/src/mdns.c index e55a3916..22319bf0 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -110,96 +110,7 @@ static uint8_t mdns_suffix_b_e_f_ip6_arpa[] = { * deleted, using a "compression" procedure. */ -/* - * Missing LRU hash function: create only if not currently in cache, - * and return the created or found entry. - * - * TODO: move this to lruhash.c, once source control issues are fixed. - */ -static struct lruhash_entry* -lruhash_insert_or_retrieve(struct lruhash* table, hashvalue_type hash, -struct lruhash_entry* entry, void* data, void* cb_arg) -{ - struct lruhash_bin* bin; - struct lruhash_entry* found, *reclaimlist = NULL; - size_t need_size; - fptr_ok(fptr_whitelist_hash_sizefunc(table->sizefunc)); - fptr_ok(fptr_whitelist_hash_delkeyfunc(table->delkeyfunc)); - fptr_ok(fptr_whitelist_hash_deldatafunc(table->deldatafunc)); - fptr_ok(fptr_whitelist_hash_compfunc(table->compfunc)); - fptr_ok(fptr_whitelist_hash_markdelfunc(table->markdelfunc)); - need_size = table->sizefunc(entry->key, data); - if (cb_arg == NULL) cb_arg = table->cb_arg; - /* find bin */ - lock_quick_lock(&table->lock); - bin = &table->array[hash & table->size_mask]; - lock_quick_lock(&bin->lock); - - /* see if entry exists already */ - if ((found = bin_find_entry(table, bin, hash, entry->key)) != NULL) { - /* if so: keep the existing data - acquire a writelock */ - lock_rw_wrlock(&found->lock); - } - else - { - /* if not: add to bin */ - entry->overflow_next = bin->overflow_list; - bin->overflow_list = entry; - lru_front(table, entry); - table->num++; - table->space_used += need_size; - /* return the entry that was presented, and lock it */ - found = entry; - lock_rw_wrlock(&found->lock); - } - lock_quick_unlock(&bin->lock); - if (table->space_used > table->space_max) - reclaim_space(table, &reclaimlist); - if (table->num >= table->size) - table_grow(table); - lock_quick_unlock(&table->lock); - - /* finish reclaim if any (outside of critical region) */ - while (reclaimlist) { - struct lruhash_entry* n = reclaimlist->overflow_next; - void* d = reclaimlist->data; - (*table->delkeyfunc)(reclaimlist->key, cb_arg); - (*table->deldatafunc)(d, cb_arg); - reclaimlist = n; - } - - /* return the entry that was selected */ - return found; -} - -/* - * Another missing LRU hash function: demote, move an entry to the bottom - * of the LRU pile. - */ - -static void -lru_demote(struct lruhash* table, struct lruhash_entry* entry) -{ - log_assert(table && entry); - if (entry == table->lru_end) - return; /* nothing to do */ - /* remove from current lru position */ - lru_remove(table, entry); - /* add at end */ - entry->lru_next = NULL; - entry->lru_prev = table->lru_end; - - if (table->lru_end == NULL) - { - table->lru_start = entry; - } - else - { - table->lru_end->lru_next = entry; - } - table->lru_end = entry; -} /* * For the data part, we want to allocate in rounded increments, so as to reduce the @@ -2153,7 +2064,7 @@ int mdns_addition_test(struct getdns_context* context, if (ret == 0) { - ret = mdns_finalize_lru_test(context, &mdns_exampleRRAM[12], 15, 1, 1, + ret = mdns_finalize_lru_test(context, mdns_test_name, sizeof(mdns_test_name), 1, 1, 1, buffer, buffer_max, entry_length); } diff --git a/src/util/lookup3.c b/src/util/lookup3.c index e9b05af3..dedcdcaf 100644 --- a/src/util/lookup3.c +++ b/src/util/lookup3.c @@ -45,6 +45,7 @@ on 1 byte), but shoehorning those bytes into integers efficiently is messy. /*#define SELF_TEST 1*/ #include "config.h" +#include "util/lookup3.h" #include "util/storage/lookup3.h" #include /* defines printf for tests */ #include /* defines time_t for timings in the test */ diff --git a/src/util/lruhash.c b/src/util/lruhash.c index 97e99960..fd091ef9 100644 --- a/src/util/lruhash.c +++ b/src/util/lruhash.c @@ -41,6 +41,7 @@ */ #include "config.h" +#include "util/lruhash.h" #include "util/storage/lruhash.h" #include "util/fptr_wlist.h" @@ -296,6 +297,92 @@ lru_touch(struct lruhash* table, struct lruhash_entry* entry) lru_front(table, entry); } + +/* + * Demote: the opposite of touch, move an entry to the bottom + * of the LRU pile. + */ + +void +lru_demote(struct lruhash* table, struct lruhash_entry* entry) +{ + log_assert(table && entry); + if (entry == table->lru_end) + return; /* nothing to do */ + /* remove from current lru position */ + lru_remove(table, entry); + /* add at end */ + entry->lru_next = NULL; + entry->lru_prev = table->lru_end; + + if (table->lru_end == NULL) + { + table->lru_start = entry; + } + else + { + table->lru_end->lru_next = entry; + } + table->lru_end = entry; +} + +struct lruhash_entry* +lruhash_insert_or_retrieve(struct lruhash* table, hashvalue_type hash, +struct lruhash_entry* entry, void* data, void* cb_arg) +{ + struct lruhash_bin* bin; + struct lruhash_entry* found, *reclaimlist = NULL; + size_t need_size; + fptr_ok(fptr_whitelist_hash_sizefunc(table->sizefunc)); + fptr_ok(fptr_whitelist_hash_delkeyfunc(table->delkeyfunc)); + fptr_ok(fptr_whitelist_hash_deldatafunc(table->deldatafunc)); + fptr_ok(fptr_whitelist_hash_compfunc(table->compfunc)); + fptr_ok(fptr_whitelist_hash_markdelfunc(table->markdelfunc)); + need_size = table->sizefunc(entry->key, data); + if (cb_arg == NULL) cb_arg = table->cb_arg; + + /* find bin */ + lock_quick_lock(&table->lock); + bin = &table->array[hash & table->size_mask]; + lock_quick_lock(&bin->lock); + + /* see if entry exists already */ + if ((found = bin_find_entry(table, bin, hash, entry->key)) != NULL) { + /* if so: keep the existing data - acquire a writelock */ + lock_rw_wrlock(&found->lock); + } + else + { + /* if not: add to bin */ + entry->overflow_next = bin->overflow_list; + bin->overflow_list = entry; + lru_front(table, entry); + table->num++; + table->space_used += need_size; + /* return the entry that was presented, and lock it */ + found = entry; + lock_rw_wrlock(&found->lock); + } + lock_quick_unlock(&bin->lock); + if (table->space_used > table->space_max) + reclaim_space(table, &reclaimlist); + if (table->num >= table->size) + table_grow(table); + lock_quick_unlock(&table->lock); + + /* finish reclaim if any (outside of critical region) */ + while (reclaimlist) { + struct lruhash_entry* n = reclaimlist->overflow_next; + void* d = reclaimlist->data; + (*table->delkeyfunc)(reclaimlist->key, cb_arg); + (*table->deldatafunc)(d, cb_arg); + reclaimlist = n; + } + + /* return the entry that was selected */ + return found; +} + void lruhash_insert(struct lruhash* table, hashvalue_type hash, struct lruhash_entry* entry, void* data, void* cb_arg) diff --git a/src/util/lruhash.h b/src/util/lruhash.h index 17c8b551..8c0d1a18 100644 --- a/src/util/lruhash.h +++ b/src/util/lruhash.h @@ -46,8 +46,10 @@ #define lruhash_delete _getdns_lruhash_delete #define lruhash_clear _getdns_lruhash_clear #define lruhash_insert _getdns_lruhash_insert +#define lruhash_insert_or_retrieve _getdns_lruhash_insert_or_retrieve #define lruhash_lookup _getdns_lruhash_lookup #define lru_touch _getdns_lru_touch +#define lru_demote _getdns_lru_demote #define lruhash_setmarkdel _getdns_lruhash_setmarkdel #define lruhash_remove _getdns_lruhash_remove @@ -65,4 +67,37 @@ #define lruhash_traverse _getdns_lruhash_traverse #include "util/orig-headers/lruhash.h" + +/* + * Additional function definitions, not found in original header. + */ + + /** + * Demote entry, so it becomes the least recently used in the LRU list. + * Caller must hold hash table lock. The entry must be inserted already. + * @param table: hash table. + * @param entry: entry to make last in LRU. + */ +void lru_demote(struct lruhash* table, struct lruhash_entry* entry); + +/** + * Insert a new element into the hashtable, or retrieve the corresponding + * element of it exits. + * + * If key is already present data pointer in that entry is kept. + * If it is not present, a new entry is created. In that case, + * the space calculation function is called with the key, data. + * If necessary the least recently used entries are deleted to make space. + * If necessary the hash array is grown up. + * + * @param table: hash table. + * @param hash: hash value. User calculates the hash. + * @param entry: identifies the entry. + * @param data: the data. + * @param cb_override: if not null overrides the cb_arg for the deletefunc. + * @return: pointer to the existing entry if the key was already present, + * or to the entry argument if it was not. + */ +struct lruhash_entry* lruhash_insert_or_retrieve(struct lruhash* table, hashvalue_type hash, + struct lruhash_entry* entry, void* data, void* cb_arg); #endif From a3fe9583879b7b510a1efb1662cb2fa14a643cb4 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Mon, 20 Mar 2017 16:41:57 +0100 Subject: [PATCH 22/25] Sync with unbound --- configure.ac | 9 ++ src/util/lookup3.c | 1 - src/util/lruhash.c | 173 ++++++++++++++++---------------- src/util/lruhash.h | 38 +------ src/util/orig-headers/lruhash.h | 32 ++++++ src/util/val_secalgo.c | 92 ++++++++++++++--- 6 files changed, 207 insertions(+), 138 deletions(-) diff --git a/configure.ac b/configure.ac index bb2fa22f..3cb6d24c 100644 --- a/configure.ac +++ b/configure.ac @@ -311,6 +311,15 @@ AC_INCLUDES_DEFAULT fi +AC_ARG_ENABLE(sha1, AC_HELP_STRING([--disable-sha1], [Disable SHA1 RRSIG support, does not disable nsec3 support])) + case "$enable_sha1" in + no) + ;; + yes|*) + AC_DEFINE([USE_SHA1], [1], [Define this to enable SHA1 support.]) + ;; +esac + AC_ARG_ENABLE(sha2, AC_HELP_STRING([--disable-sha2], [Disable SHA256 and SHA512 RRSIG support])) case "$enable_sha2" in no) diff --git a/src/util/lookup3.c b/src/util/lookup3.c index dedcdcaf..e9b05af3 100644 --- a/src/util/lookup3.c +++ b/src/util/lookup3.c @@ -45,7 +45,6 @@ on 1 byte), but shoehorning those bytes into integers efficiently is messy. /*#define SELF_TEST 1*/ #include "config.h" -#include "util/lookup3.h" #include "util/storage/lookup3.h" #include /* defines printf for tests */ #include /* defines time_t for timings in the test */ diff --git a/src/util/lruhash.c b/src/util/lruhash.c index fd091ef9..0003ff49 100644 --- a/src/util/lruhash.c +++ b/src/util/lruhash.c @@ -41,7 +41,6 @@ */ #include "config.h" -#include "util/lruhash.h" #include "util/storage/lruhash.h" #include "util/fptr_wlist.h" @@ -297,92 +296,6 @@ lru_touch(struct lruhash* table, struct lruhash_entry* entry) lru_front(table, entry); } - -/* - * Demote: the opposite of touch, move an entry to the bottom - * of the LRU pile. - */ - -void -lru_demote(struct lruhash* table, struct lruhash_entry* entry) -{ - log_assert(table && entry); - if (entry == table->lru_end) - return; /* nothing to do */ - /* remove from current lru position */ - lru_remove(table, entry); - /* add at end */ - entry->lru_next = NULL; - entry->lru_prev = table->lru_end; - - if (table->lru_end == NULL) - { - table->lru_start = entry; - } - else - { - table->lru_end->lru_next = entry; - } - table->lru_end = entry; -} - -struct lruhash_entry* -lruhash_insert_or_retrieve(struct lruhash* table, hashvalue_type hash, -struct lruhash_entry* entry, void* data, void* cb_arg) -{ - struct lruhash_bin* bin; - struct lruhash_entry* found, *reclaimlist = NULL; - size_t need_size; - fptr_ok(fptr_whitelist_hash_sizefunc(table->sizefunc)); - fptr_ok(fptr_whitelist_hash_delkeyfunc(table->delkeyfunc)); - fptr_ok(fptr_whitelist_hash_deldatafunc(table->deldatafunc)); - fptr_ok(fptr_whitelist_hash_compfunc(table->compfunc)); - fptr_ok(fptr_whitelist_hash_markdelfunc(table->markdelfunc)); - need_size = table->sizefunc(entry->key, data); - if (cb_arg == NULL) cb_arg = table->cb_arg; - - /* find bin */ - lock_quick_lock(&table->lock); - bin = &table->array[hash & table->size_mask]; - lock_quick_lock(&bin->lock); - - /* see if entry exists already */ - if ((found = bin_find_entry(table, bin, hash, entry->key)) != NULL) { - /* if so: keep the existing data - acquire a writelock */ - lock_rw_wrlock(&found->lock); - } - else - { - /* if not: add to bin */ - entry->overflow_next = bin->overflow_list; - bin->overflow_list = entry; - lru_front(table, entry); - table->num++; - table->space_used += need_size; - /* return the entry that was presented, and lock it */ - found = entry; - lock_rw_wrlock(&found->lock); - } - lock_quick_unlock(&bin->lock); - if (table->space_used > table->space_max) - reclaim_space(table, &reclaimlist); - if (table->num >= table->size) - table_grow(table); - lock_quick_unlock(&table->lock); - - /* finish reclaim if any (outside of critical region) */ - while (reclaimlist) { - struct lruhash_entry* n = reclaimlist->overflow_next; - void* d = reclaimlist->data; - (*table->delkeyfunc)(reclaimlist->key, cb_arg); - (*table->deldatafunc)(d, cb_arg); - reclaimlist = n; - } - - /* return the entry that was selected */ - return found; -} - void lruhash_insert(struct lruhash* table, hashvalue_type hash, struct lruhash_entry* entry, void* data, void* cb_arg) @@ -630,3 +543,89 @@ lruhash_traverse(struct lruhash* h, int wr, } lock_quick_unlock(&h->lock); } + +/* + * Demote: the opposite of touch, move an entry to the bottom + * of the LRU pile. + */ + +void +lru_demote(struct lruhash* table, struct lruhash_entry* entry) +{ + log_assert(table && entry); + if (entry == table->lru_end) + return; /* nothing to do */ + /* remove from current lru position */ + lru_remove(table, entry); + /* add at end */ + entry->lru_next = NULL; + entry->lru_prev = table->lru_end; + + if (table->lru_end == NULL) + { + table->lru_start = entry; + } + else + { + table->lru_end->lru_next = entry; + } + table->lru_end = entry; +} + +struct lruhash_entry* +lruhash_insert_or_retrieve(struct lruhash* table, hashvalue_type hash, + struct lruhash_entry* entry, void* data, void* cb_arg) +{ + struct lruhash_bin* bin; + struct lruhash_entry* found, *reclaimlist = NULL; + size_t need_size; + fptr_ok(fptr_whitelist_hash_sizefunc(table->sizefunc)); + fptr_ok(fptr_whitelist_hash_delkeyfunc(table->delkeyfunc)); + fptr_ok(fptr_whitelist_hash_deldatafunc(table->deldatafunc)); + fptr_ok(fptr_whitelist_hash_compfunc(table->compfunc)); + fptr_ok(fptr_whitelist_hash_markdelfunc(table->markdelfunc)); + need_size = table->sizefunc(entry->key, data); + if (cb_arg == NULL) cb_arg = table->cb_arg; + + /* find bin */ + lock_quick_lock(&table->lock); + bin = &table->array[hash & table->size_mask]; + lock_quick_lock(&bin->lock); + + /* see if entry exists already */ + if ((found = bin_find_entry(table, bin, hash, entry->key)) != NULL) { + /* if so: keep the existing data - acquire a writelock */ + lock_rw_wrlock(&found->lock); + } + else + { + /* if not: add to bin */ + entry->overflow_next = bin->overflow_list; + bin->overflow_list = entry; + lru_front(table, entry); + table->num++; + table->space_used += need_size; + /* return the entry that was presented, and lock it */ + found = entry; + lock_rw_wrlock(&found->lock); + } + lock_quick_unlock(&bin->lock); + if (table->space_used > table->space_max) + reclaim_space(table, &reclaimlist); + if (table->num >= table->size) + table_grow(table); + lock_quick_unlock(&table->lock); + + /* finish reclaim if any (outside of critical region) */ + while (reclaimlist) { + struct lruhash_entry* n = reclaimlist->overflow_next; + void* d = reclaimlist->data; + (*table->delkeyfunc)(reclaimlist->key, cb_arg); + (*table->deldatafunc)(d, cb_arg); + reclaimlist = n; + } + + /* return the entry that was selected */ + return found; +} + diff --git a/src/util/lruhash.h b/src/util/lruhash.h index 8c0d1a18..6796a68d 100644 --- a/src/util/lruhash.h +++ b/src/util/lruhash.h @@ -46,12 +46,13 @@ #define lruhash_delete _getdns_lruhash_delete #define lruhash_clear _getdns_lruhash_clear #define lruhash_insert _getdns_lruhash_insert -#define lruhash_insert_or_retrieve _getdns_lruhash_insert_or_retrieve #define lruhash_lookup _getdns_lruhash_lookup #define lru_touch _getdns_lru_touch -#define lru_demote _getdns_lru_demote #define lruhash_setmarkdel _getdns_lruhash_setmarkdel +#define lru_demote _getdns_lru_demote +#define lruhash_insert_or_retrieve _getdns_lruhash_insert_or_retrieve + #define lruhash_remove _getdns_lruhash_remove #define bin_init _getdns_bin_init #define bin_delete _getdns_bin_delete @@ -67,37 +68,4 @@ #define lruhash_traverse _getdns_lruhash_traverse #include "util/orig-headers/lruhash.h" - -/* - * Additional function definitions, not found in original header. - */ - - /** - * Demote entry, so it becomes the least recently used in the LRU list. - * Caller must hold hash table lock. The entry must be inserted already. - * @param table: hash table. - * @param entry: entry to make last in LRU. - */ -void lru_demote(struct lruhash* table, struct lruhash_entry* entry); - -/** - * Insert a new element into the hashtable, or retrieve the corresponding - * element of it exits. - * - * If key is already present data pointer in that entry is kept. - * If it is not present, a new entry is created. In that case, - * the space calculation function is called with the key, data. - * If necessary the least recently used entries are deleted to make space. - * If necessary the hash array is grown up. - * - * @param table: hash table. - * @param hash: hash value. User calculates the hash. - * @param entry: identifies the entry. - * @param data: the data. - * @param cb_override: if not null overrides the cb_arg for the deletefunc. - * @return: pointer to the existing entry if the key was already present, - * or to the entry argument if it was not. - */ -struct lruhash_entry* lruhash_insert_or_retrieve(struct lruhash* table, hashvalue_type hash, - struct lruhash_entry* entry, void* data, void* cb_arg); #endif diff --git a/src/util/orig-headers/lruhash.h b/src/util/orig-headers/lruhash.h index c3937408..af06b622 100644 --- a/src/util/orig-headers/lruhash.h +++ b/src/util/orig-headers/lruhash.h @@ -301,6 +301,38 @@ void lru_touch(struct lruhash* table, struct lruhash_entry* entry); */ void lruhash_setmarkdel(struct lruhash* table, lruhash_markdelfunc_type md); +/************************* getdns functions ************************/ +/*** these are used by getdns only and not by unbound. ***/ + +/** + * Demote entry, so it becomes the least recently used in the LRU list. + * Caller must hold hash table lock. The entry must be inserted already. + * @param table: hash table. + * @param entry: entry to make last in LRU. + */ +void lru_demote(struct lruhash* table, struct lruhash_entry* entry); + +/** + * Insert a new element into the hashtable, or retrieve the corresponding + * element of it exits. + * + * If key is already present data pointer in that entry is kept. + * If it is not present, a new entry is created. In that case, + * the space calculation function is called with the key, data. + * If necessary the least recently used entries are deleted to make space. + * If necessary the hash array is grown up. + * + * @param table: hash table. + * @param hash: hash value. User calculates the hash. + * @param entry: identifies the entry. + * @param data: the data. + * @param cb_override: if not null overrides the cb_arg for the deletefunc. + * @return: pointer to the existing entry if the key was already present, + * or to the entry argument if it was not. + */ +struct lruhash_entry* lruhash_insert_or_retrieve(struct lruhash* table, hashvalue_type hash, + struct lruhash_entry* entry, void* data, void* cb_arg); + /************************* Internal functions ************************/ /*** these are only exposed for unit tests. ***/ diff --git a/src/util/val_secalgo.c b/src/util/val_secalgo.c index 302820fc..be88ff43 100644 --- a/src/util/val_secalgo.c +++ b/src/util/val_secalgo.c @@ -74,6 +74,8 @@ /** fake DSA support for unit tests */ int fake_dsa = 0; +/** fake SHA1 support for unit tests */ +int fake_sha1 = 0; /* return size of digest if supported, or 0 otherwise */ size_t @@ -116,9 +118,12 @@ size_t ds_digest_size_supported(int algo) { switch(algo) { -#ifdef HAVE_EVP_SHA1 case LDNS_SHA1: +#if defined(HAVE_EVP_SHA1) && defined(USE_SHA1) return SHA_DIGEST_LENGTH; +#else + if(fake_sha1) return 20; + return 0; #endif #ifdef HAVE_EVP_SHA256 case LDNS_SHA256: @@ -158,7 +163,7 @@ secalgo_ds_digest(int algo, unsigned char* buf, size_t len, unsigned char* res) { switch(algo) { -#ifdef HAVE_EVP_SHA1 +#if defined(HAVE_EVP_SHA1) && defined(USE_SHA1) case LDNS_SHA1: (void)SHA1(buf, len, res); return 1; @@ -197,14 +202,22 @@ dnskey_algo_id_is_supported(int id) return 0; case LDNS_DSA: case LDNS_DSA_NSEC3: -#ifdef USE_DSA +#if defined(USE_DSA) && defined(USE_SHA1) return 1; #else - if(fake_dsa) return 1; + if(fake_dsa || fake_sha1) return 1; return 0; #endif + case LDNS_RSASHA1: case LDNS_RSASHA1_NSEC3: +#ifdef USE_SHA1 + return 1; +#else + if(fake_sha1) return 1; + return 0; +#endif + #if defined(HAVE_EVP_SHA256) && defined(USE_SHA2) case LDNS_RSASHA256: #endif @@ -215,7 +228,10 @@ dnskey_algo_id_is_supported(int id) case LDNS_ECDSAP256SHA256: case LDNS_ECDSAP384SHA384: #endif +#if (defined(HAVE_EVP_SHA256) && defined(USE_SHA2)) || (defined(HAVE_EVP_SHA512) && defined(USE_SHA2)) || defined(USE_ECDSA) return 1; +#endif + #ifdef USE_GOST case LDNS_ECC_GOST: /* we support GOST if it can be loaded */ @@ -392,13 +408,13 @@ static int setup_key_digest(int algo, EVP_PKEY** evp_key, const EVP_MD** digest_type, unsigned char* key, size_t keylen) { -#ifdef USE_DSA +#if defined(USE_DSA) && defined(USE_SHA1) DSA* dsa; #endif RSA* rsa; switch(algo) { -#ifdef USE_DSA +#if defined(USE_DSA) && defined(USE_SHA1) case LDNS_DSA: case LDNS_DSA_NSEC3: *evp_key = EVP_PKEY_new(); @@ -424,9 +440,13 @@ setup_key_digest(int algo, EVP_PKEY** evp_key, const EVP_MD** digest_type, #endif break; -#endif /* USE_DSA */ +#endif /* USE_DSA && USE_SHA1 */ + +#if defined(USE_SHA1) || (defined(HAVE_EVP_SHA256) && defined(USE_SHA2)) || (defined(HAVE_EVP_SHA512) && defined(USE_SHA2)) +#ifdef USE_SHA1 case LDNS_RSASHA1: case LDNS_RSASHA1_NSEC3: +#endif #if defined(HAVE_EVP_SHA256) && defined(USE_SHA2) case LDNS_RSASHA256: #endif @@ -461,9 +481,14 @@ setup_key_digest(int algo, EVP_PKEY** evp_key, const EVP_MD** digest_type, *digest_type = EVP_sha512(); else #endif +#ifdef USE_SHA1 *digest_type = EVP_sha1(); - +#else + { verbose(VERB_QUERY, "no digest available"); return 0; } +#endif break; +#endif /* defined(USE_SHA1) || (defined(HAVE_EVP_SHA256) && defined(USE_SHA2)) || (defined(HAVE_EVP_SHA512) && defined(USE_SHA2)) */ + case LDNS_RSAMD5: *evp_key = EVP_PKEY_new(); if(!*evp_key) { @@ -562,7 +587,11 @@ verify_canonrrset(sldns_buffer* buf, int algo, unsigned char* sigblock, EVP_PKEY *evp_key = NULL; #ifndef USE_DSA - if((algo == LDNS_DSA || algo == LDNS_DSA_NSEC3) && fake_dsa) + if((algo == LDNS_DSA || algo == LDNS_DSA_NSEC3) &&(fake_dsa||fake_sha1)) + return sec_status_secure; +#endif +#ifndef USE_SHA1 + if(fake_sha1 && (algo == LDNS_DSA || algo == LDNS_DSA_NSEC3 || algo == LDNS_RSASHA1 || algo == LDNS_RSASHA1_NSEC3)) return sec_status_secure; #endif @@ -706,8 +735,10 @@ ds_digest_size_supported(int algo) { /* uses libNSS */ switch(algo) { +#ifdef USE_SHA1 case LDNS_SHA1: return SHA1_LENGTH; +#endif #ifdef USE_SHA2 case LDNS_SHA256: return SHA256_LENGTH; @@ -729,9 +760,11 @@ secalgo_ds_digest(int algo, unsigned char* buf, size_t len, { /* uses libNSS */ switch(algo) { +#ifdef USE_SHA1 case LDNS_SHA1: return HASH_HashBuf(HASH_AlgSHA1, res, buf, len) == SECSuccess; +#endif #if defined(USE_SHA2) case LDNS_SHA256: return HASH_HashBuf(HASH_AlgSHA256, res, buf, len) @@ -759,12 +792,15 @@ dnskey_algo_id_is_supported(int id) case LDNS_RSAMD5: /* RFC 6725 deprecates RSAMD5 */ return 0; -#ifdef USE_DSA +#if defined(USE_SHA1) || defined(USE_SHA2) +#if defined(USE_DSA) && defined(USE_SHA1) case LDNS_DSA: case LDNS_DSA_NSEC3: #endif +#ifdef USE_SHA1 case LDNS_RSASHA1: case LDNS_RSASHA1_NSEC3: +#endif #ifdef USE_SHA2 case LDNS_RSASHA256: #endif @@ -772,6 +808,8 @@ dnskey_algo_id_is_supported(int id) case LDNS_RSASHA512: #endif return 1; +#endif /* SHA1 or SHA2 */ + #ifdef USE_ECDSA case LDNS_ECDSAP256SHA256: case LDNS_ECDSAP384SHA384: @@ -1003,7 +1041,9 @@ nss_setup_key_digest(int algo, SECKEYPublicKey** pubkey, HASH_HashType* htype, */ switch(algo) { -#ifdef USE_DSA + +#if defined(USE_SHA1) || defined(USE_SHA2) +#if defined(USE_DSA) && defined(USE_SHA1) case LDNS_DSA: case LDNS_DSA_NSEC3: *pubkey = nss_buf2dsa(key, keylen); @@ -1015,8 +1055,10 @@ nss_setup_key_digest(int algo, SECKEYPublicKey** pubkey, HASH_HashType* htype, /* no prefix for DSA verification */ break; #endif +#ifdef USE_SHA1 case LDNS_RSASHA1: case LDNS_RSASHA1_NSEC3: +#endif #ifdef USE_SHA2 case LDNS_RSASHA256: #endif @@ -1043,13 +1085,22 @@ nss_setup_key_digest(int algo, SECKEYPublicKey** pubkey, HASH_HashType* htype, *prefixlen = sizeof(p_sha512); } else #endif +#ifdef USE_SHA1 { *htype = HASH_AlgSHA1; *prefix = p_sha1; *prefixlen = sizeof(p_sha1); } +#else + { + verbose(VERB_QUERY, "verify: no digest algo"); + return 0; + } +#endif break; +#endif /* SHA1 or SHA2 */ + case LDNS_RSAMD5: *pubkey = nss_buf2rsa(key, keylen); if(!*pubkey) { @@ -1131,7 +1182,7 @@ verify_canonrrset(sldns_buffer* buf, int algo, unsigned char* sigblock, return sec_status_bogus; } -#ifdef USE_DSA +#if defined(USE_DSA) && defined(USE_SHA1) /* need to convert DSA, ECDSA signatures? */ if((algo == LDNS_DSA || algo == LDNS_DSA_NSEC3)) { if(sigblock_len == 1+2*SHA1_LENGTH) { @@ -1312,7 +1363,12 @@ ds_digest_size_supported(int algo) { switch(algo) { case LDNS_SHA1: +#ifdef USE_SHA1 return SHA1_DIGEST_SIZE; +#else + if(fake_sha1) return 20; + return 0; +#endif #ifdef USE_SHA2 case LDNS_SHA256: return SHA256_DIGEST_SIZE; @@ -1334,8 +1390,10 @@ secalgo_ds_digest(int algo, unsigned char* buf, size_t len, unsigned char* res) { switch(algo) { +#ifdef USE_SHA1 case LDNS_SHA1: return _digest_nettle(SHA1_DIGEST_SIZE, buf, len, res); +#endif #if defined(USE_SHA2) case LDNS_SHA256: return _digest_nettle(SHA256_DIGEST_SIZE, buf, len, res); @@ -1359,12 +1417,14 @@ dnskey_algo_id_is_supported(int id) { /* uses libnettle */ switch(id) { -#ifdef USE_DSA +#if defined(USE_DSA) && defined(USE_SHA1) case LDNS_DSA: case LDNS_DSA_NSEC3: #endif +#ifdef USE_SHA1 case LDNS_RSASHA1: case LDNS_RSASHA1_NSEC3: +#endif #ifdef USE_SHA2 case LDNS_RSASHA256: case LDNS_RSASHA512: @@ -1381,7 +1441,7 @@ dnskey_algo_id_is_supported(int id) } } -#ifdef USE_DSA +#if defined(USE_DSA) && defined(USE_SHA1) static char * _verify_nettle_dsa(sldns_buffer* buf, unsigned char* sigblock, unsigned int sigblock_len, unsigned char* key, unsigned int keylen) @@ -1641,7 +1701,7 @@ verify_canonrrset(sldns_buffer* buf, int algo, unsigned char* sigblock, } switch(algo) { -#ifdef USE_DSA +#if defined(USE_DSA) && defined(USE_SHA1) case LDNS_DSA: case LDNS_DSA_NSEC3: *reason = _verify_nettle_dsa(buf, sigblock, sigblock_len, key, keylen); @@ -1651,9 +1711,11 @@ verify_canonrrset(sldns_buffer* buf, int algo, unsigned char* sigblock, return sec_status_secure; #endif /* USE_DSA */ +#ifdef USE_SHA1 case LDNS_RSASHA1: case LDNS_RSASHA1_NSEC3: digest_size = (digest_size ? digest_size : SHA1_DIGEST_SIZE); +#endif #ifdef USE_SHA2 case LDNS_RSASHA256: digest_size = (digest_size ? digest_size : SHA256_DIGEST_SIZE); From 24abf43de17fff8d58f0ded804bc3b6def8b9947 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Mon, 20 Mar 2017 21:33:19 +0100 Subject: [PATCH 23/25] Fit mdns code with pending dns netreqs on EMFILE --- configure.ac | 5 ++++- src/general.c | 15 ++++++++++----- src/mdns.c | 14 +++++++------- src/request-internal.c | 1 + .../070-coding-practice.test | 5 +++-- src/types-internal.h | 1 + 6 files changed, 26 insertions(+), 15 deletions(-) diff --git a/configure.ac b/configure.ac index 3cb6d24c..fa6a2436 100644 --- a/configure.ac +++ b/configure.ac @@ -1011,7 +1011,10 @@ fi #---- check for pthreads library -AC_SEARCH_LIBS([pthread_mutex_init],[pthread],[AC_DEFINE([HAVE_PTHREAD], [1], [Have pthreads library])], [AC_MSG_WARN([pthreads not available])]) +AC_SEARCH_LIBS([pthread_mutex_init],[pthread], [ + AC_DEFINE([HAVE_PTHREAD], [1], [Have pthreads library]) + LIBS="-lpthread $LIBS" +], [AC_MSG_WARN([pthreads not available])]) AC_MSG_CHECKING([whether the C compiler (${CC-cc}) supports the __func__ variable]) AC_LANG_PUSH(C) diff --git a/src/general.c b/src/general.c index 6ed44004..8d780eb7 100644 --- a/src/general.c +++ b/src/general.c @@ -110,7 +110,8 @@ _getdns_check_dns_req_complete(getdns_dns_req *dns_req) /* Do we have to check more suffixes on nxdomain/nodata? */ - if (dns_req->suffix_appended && /* Something was appended */ + if (dns_req->is_dns_request && + dns_req->suffix_appended && /* Something was appended */ dns_req->suffix_len > 1 && /* Next suffix available */ no_answer(dns_req)) { /* Remove suffix from name */ @@ -146,6 +147,7 @@ _getdns_check_dns_req_complete(getdns_dns_req *dns_req) return; } } else if ( + dns_req->is_dns_request && ( dns_req->append_name == GETDNS_APPEND_NAME_ONLY_TO_SINGLE_LABEL_AFTER_FAILURE || dns_req->append_name == @@ -189,7 +191,9 @@ _getdns_check_dns_req_complete(getdns_dns_req *dns_req) dns_req->internal_cb(dns_req); } else if (! results_found) _getdns_call_user_callback(dns_req, NULL); - else if (dns_req->dnssec_return_validation_chain + else if ( + dns_req->is_dns_request && + (dns_req->dnssec_return_validation_chain #ifdef DNSSEC_ROADBLOCK_AVOIDANCE || ( dns_req->dnssec_roadblock_avoidance && !dns_req->avoid_dnssec_roadblocks) @@ -202,7 +206,7 @@ _getdns_check_dns_req_complete(getdns_dns_req *dns_req) dns_req->dnssec_return_all_statuses )) #endif - ) + )) _getdns_get_validation_chain(dns_req); else _getdns_call_user_callback( @@ -301,7 +305,7 @@ _getdns_netreq_change_state( uint64_t now_ms; getdns_network_req *prev; - if (!netreq) + if (!netreq || !netreq->owner->is_dns_request) return; context = netreq->owner->context; @@ -571,7 +575,7 @@ getdns_general_ns(getdns_context *context, getdns_eventloop *loop, if (!(r = _getdns_context_local_namespace_resolve( req, &localnames_response))) { - + req->is_dns_request = 0; _getdns_call_user_callback ( req, localnames_response); break; @@ -581,6 +585,7 @@ getdns_general_ns(getdns_context *context, getdns_eventloop *loop, /* Check whether the name belongs in the MDNS space */ if (!(r = _getdns_mdns_namespace_check(req))) { + req->is_dns_request = 0; // Submit the query to the MDNS transport. for (netreq_p = req->netreqs ; !r && (netreq = *netreq_p) diff --git a/src/mdns.c b/src/mdns.c index b18368ff..6373b265 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -513,7 +513,7 @@ static void msdn_cache_deldata(void* vdata, void* vcontext) /* TODO: treating as a timeout for now, may consider treating as error */ netreq->debug_end_time = _getdns_get_time_as_uintt64(); - netreq->state = NET_REQ_TIMED_OUT; + _getdns_netreq_change_state(netreq, NET_REQ_TIMED_OUT); if (netreq->owner->user_callback) { (void)_getdns_context_request_timed_out(netreq->owner); } @@ -1008,7 +1008,7 @@ mdns_complete_query_from_cache_entry( netreq->response_len = packet_length; netreq->debug_end_time = _getdns_get_time_as_uintt64(); - netreq->state = NET_REQ_FINISHED; + _getdns_netreq_change_state(netreq, NET_REQ_FINISHED); _getdns_check_dns_req_complete(netreq->owner); } else @@ -1016,7 +1016,7 @@ mdns_complete_query_from_cache_entry( /* Fail the query? */ netreq->response_len = 0; netreq->debug_end_time = _getdns_get_time_as_uintt64(); - netreq->state = NET_REQ_ERRORED; + _getdns_netreq_change_state(netreq, NET_REQ_ERRORED); _getdns_check_dns_req_complete(netreq->owner); } } @@ -1026,7 +1026,7 @@ mdns_complete_query_from_cache_entry( /* Failure */ netreq->response_len = 0; netreq->debug_end_time = _getdns_get_time_as_uintt64(); - netreq->state = NET_REQ_ERRORED; + _getdns_netreq_change_state(netreq, NET_REQ_ERRORED); _getdns_check_dns_req_complete(netreq->owner); } @@ -1085,7 +1085,7 @@ mdns_mcast_timeout_cb(void *userarg) int found = 0; DEBUG_MDNS("%s %-35s: MSG: %p\n", - MDNS_DEBUG_CLEANUP, __FUNCTION__, netreq); + MDNS_DEBUG_CLEANUP, __FUNC__, netreq); msdn_cache_create_key_in_buffer(temp_key, dnsreq->name, dnsreq->name_len, netreq->request_type, dnsreq->request_class); @@ -1113,7 +1113,7 @@ mdns_mcast_timeout_cb(void *userarg) /* Fail the request on timeout */ netreq->response_len = 0; netreq->debug_end_time = _getdns_get_time_as_uintt64(); - netreq->state = NET_REQ_ERRORED; + _getdns_netreq_change_state(netreq, NET_REQ_ERRORED); _getdns_check_dns_req_complete(netreq->owner); } } @@ -1128,7 +1128,7 @@ mdns_udp_multicast_read_cb(void *userarg) uint64_t current_time; ssize_t read; DEBUG_MDNS("%s %-35s: CTX: %p, NET=%d \n", MDNS_DEBUG_MREAD, - __FUNCTION__, cnx->context, cnx->addr_mcast.ss_family); + __FUNC__, cnx->context, cnx->addr_mcast.ss_family); current_time = _getdns_get_time_as_uintt64(); diff --git a/src/request-internal.c b/src/request-internal.c index 2259286e..a74fe8f0 100644 --- a/src/request-internal.c +++ b/src/request-internal.c @@ -938,6 +938,7 @@ _getdns_dns_req_new(getdns_context *context, getdns_eventloop *loop, result->finished_next = NULL; result->freed = NULL; result->validating = 0; + result->is_dns_request = 1; result->chain = NULL; network_req_init(result->netreqs[0], result, diff --git a/src/test/tpkg/070-coding-practice.tpkg/070-coding-practice.test b/src/test/tpkg/070-coding-practice.tpkg/070-coding-practice.test index b0360279..b628a657 100644 --- a/src/test/tpkg/070-coding-practice.tpkg/070-coding-practice.test +++ b/src/test/tpkg/070-coding-practice.tpkg/070-coding-practice.test @@ -15,7 +15,8 @@ rm -f report.txt echo "*** running out of filedescriptors (sockets) and for the" echo "*** limit_outstanding_queries feature." echo "*** " - grep '[^!=]=[ ][ ]*NET_REQ_' *.[ch] */*.[ch] + grep -n '[^!=]=[ ][ ]*NET_REQ_' *.[ch] */*.[ch] | \ + grep -v '^request-internal.c:[12][0-9][0-9]: *net_req->state = NET_REQ_NOT_SENT;$' echo "" fi ) >> report.txt @@ -28,7 +29,7 @@ rm -f report.txt echo "*** __FUNC__ is aliases in config.h to name to be used" echo "*** for the system with a #define" echo "*** " - grep '__FUNCION__' *.[ch] */*.[ch] + grep -n '__FUNCTION__' *.[ch] */*.[ch] echo "" fi ) >> report.txt diff --git a/src/types-internal.h b/src/types-internal.h index ad1e9806..856651a3 100644 --- a/src/types-internal.h +++ b/src/types-internal.h @@ -313,6 +313,7 @@ typedef struct getdns_dns_req { /* Internally used by return_validation_chain */ unsigned dnssec_ok_checking_disabled : 1; unsigned is_sync_request : 1; + unsigned is_dns_request : 1; /* The validating and freed variables are used to make sure a single * code path is followed while processing a DNS request, even when From a77a3353704dbd9b833ca55a463468d4b2dbaefd Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Mon, 20 Mar 2017 21:57:57 +0100 Subject: [PATCH 24/25] Comment out dead assignement To silence static code analysis --- src/mdns.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mdns.c b/src/mdns.c index 6373b265..8a63f013 100644 --- a/src/mdns.c +++ b/src/mdns.c @@ -788,7 +788,9 @@ mdns_update_cache_ttl_and_prune(struct getdns_context *context, memmove(old_record + last_copied_index, old_record + current_hole_index, answer_index - current_hole_index); last_copied_index += answer_index - current_hole_index; - answer_index = last_copied_index; + + /* dead assignment */ + /* answer_index = last_copied_index; */ } /* if some records were deleted, update the record headers */ From a5876d57fea29647429cf7f13d5487bfac019969 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Mon, 20 Mar 2017 21:58:45 +0100 Subject: [PATCH 25/25] Dependencies --- spec/example/Makefile.in | 24 ++- src/Makefile.in | 434 +++++++++++++++++++++++++-------------- src/test/Makefile.in | 71 +++++-- src/tools/Makefile.in | 7 +- 4 files changed, 346 insertions(+), 190 deletions(-) diff --git a/spec/example/Makefile.in b/spec/example/Makefile.in index 7bf5e016..8ff7f2d1 100644 --- a/spec/example/Makefile.in +++ b/spec/example/Makefile.in @@ -149,16 +149,24 @@ depend: # Dependencies for the examples example-all-functions.lo example-all-functions.o: $(srcdir)/example-all-functions.c $(srcdir)/getdns_libevent.h \ - ../../src/config.h ../../src/getdns/getdns.h \ - $(srcdir)/../../src/getdns/getdns_ext_libevent.h ../../src/getdns/getdns_extra.h -example-reverse.lo example-reverse.o: $(srcdir)/example-reverse.c $(srcdir)/getdns_libevent.h ../../src/config.h \ - ../../src/getdns/getdns.h $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ + ../../src/config.h \ + ../../src/getdns/getdns.h \ + $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ + ../../src/getdns/getdns_extra.h +example-reverse.lo example-reverse.o: $(srcdir)/example-reverse.c $(srcdir)/getdns_libevent.h \ + ../../src/config.h \ + ../../src/getdns/getdns.h \ + $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ ../../src/getdns/getdns_extra.h example-simple-answers.lo example-simple-answers.o: $(srcdir)/example-simple-answers.c $(srcdir)/getdns_libevent.h \ - ../../src/config.h ../../src/getdns/getdns.h \ - $(srcdir)/../../src/getdns/getdns_ext_libevent.h ../../src/getdns/getdns_extra.h + ../../src/config.h \ + ../../src/getdns/getdns.h \ + $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ + ../../src/getdns/getdns_extra.h example-synchronous.lo example-synchronous.o: $(srcdir)/example-synchronous.c $(srcdir)/getdns_core_only.h \ ../../src/getdns/getdns.h -example-tree.lo example-tree.o: $(srcdir)/example-tree.c $(srcdir)/getdns_libevent.h ../../src/config.h \ - ../../src/getdns/getdns.h $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ +example-tree.lo example-tree.o: $(srcdir)/example-tree.c $(srcdir)/getdns_libevent.h \ + ../../src/config.h \ + ../../src/getdns/getdns.h \ + $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ ../../src/getdns/getdns_extra.h diff --git a/src/Makefile.in b/src/Makefile.in index 6497cc6a..c1cb8f12 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -216,169 +216,287 @@ depend: FORCE: # Dependencies for gldns, utils, the extensions and compat functions -const-info.lo const-info.o: $(srcdir)/const-info.c getdns/getdns.h getdns/getdns_extra.h \ - getdns/getdns.h $(srcdir)/const-info.h -context.lo context.o: $(srcdir)/context.c config.h $(srcdir)/debug.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h \ - $(srcdir)/gldns/wire2str.h $(srcdir)/context.h getdns/getdns.h getdns/getdns_extra.h \ - getdns/getdns.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h config.h \ - $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ - $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ - $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/stub.h $(srcdir)/list.h $(srcdir)/dict.h \ - $(srcdir)/pubkey-pinning.h -convert.lo convert.o: $(srcdir)/convert.c config.h getdns/getdns.h getdns/getdns_extra.h \ - getdns/getdns.h $(srcdir)/util-internal.h $(srcdir)/context.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h config.h \ - $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ - $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ - $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h \ - $(srcdir)/gldns/parseutil.h $(srcdir)/const-info.h $(srcdir)/dict.h $(srcdir)/list.h $(srcdir)/jsmn/jsmn.h $(srcdir)/convert.h -dict.lo dict.o: $(srcdir)/dict.c config.h $(srcdir)/types-internal.h getdns/getdns.h \ - getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/util-internal.h $(srcdir)/context.h \ - $(srcdir)/extension/default_eventloop.h config.h $(srcdir)/extension/poll_eventloop.h \ - getdns/getdns_extra.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ - $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dict.h $(srcdir)/list.h \ - $(srcdir)/const-info.h $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/parseutil.h -dnssec.lo dnssec.o: $(srcdir)/dnssec.c config.h $(srcdir)/debug.h getdns/getdns.h $(srcdir)/context.h \ - getdns/getdns_extra.h getdns/getdns.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h config.h \ - $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ - $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ - $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h \ - $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/keyraw.h $(srcdir)/gldns/parseutil.h $(srcdir)/general.h $(srcdir)/dict.h \ - $(srcdir)/list.h $(srcdir)/util/val_secalgo.h $(srcdir)/util/orig-headers/val_secalgo.h -general.lo general.o: $(srcdir)/general.c config.h $(srcdir)/general.h getdns/getdns.h $(srcdir)/types-internal.h \ - getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/gldns/wire2str.h $(srcdir)/context.h \ - $(srcdir)/extension/default_eventloop.h config.h $(srcdir)/extension/poll_eventloop.h \ - getdns/getdns_extra.h $(srcdir)/types-internal.h $(srcdir)/server.h $(srcdir)/util-internal.h \ - $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h \ - $(srcdir)/gldns/rrdef.h $(srcdir)/stub.h $(srcdir)/dict.h $(srcdir)/mdns.h -list.lo list.o: $(srcdir)/list.c $(srcdir)/types-internal.h getdns/getdns.h getdns/getdns_extra.h \ - getdns/getdns.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/util-internal.h \ - config.h $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h config.h \ - $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ - $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ - $(srcdir)/gldns/pkthdr.h $(srcdir)/list.h $(srcdir)/dict.h -mdns.lo mdns.o: $(srcdir)/mdns.c config.h $(srcdir)/debug.h $(srcdir)/context.h getdns/getdns.h \ - getdns/getdns_extra.h getdns/getdns.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h config.h \ - $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ - $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/general.h $(srcdir)/gldns/pkthdr.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h \ - $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/mdns.h -pubkey-pinning.lo pubkey-pinning.o: $(srcdir)/pubkey-pinning.c config.h $(srcdir)/debug.h getdns/getdns.h \ - $(srcdir)/context.h getdns/getdns.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ - $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h \ - config.h $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h \ - $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ - $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h -request-internal.lo request-internal.o: $(srcdir)/request-internal.c config.h $(srcdir)/types-internal.h \ - getdns/getdns.h getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/util-internal.h $(srcdir)/context.h \ - $(srcdir)/extension/default_eventloop.h config.h $(srcdir)/extension/poll_eventloop.h \ - getdns/getdns_extra.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ - $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/rrdef.h \ - $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/dict.h $(srcdir)/convert.h -rr-dict.lo rr-dict.o: $(srcdir)/rr-dict.c $(srcdir)/rr-dict.h config.h getdns/getdns.h $(srcdir)/gldns/gbuffer.h \ - $(srcdir)/util-internal.h $(srcdir)/context.h getdns/getdns_extra.h getdns/getdns.h \ +const-info.lo const-info.o: $(srcdir)/const-info.c \ + getdns/getdns.h \ + getdns/getdns_extra.h \ + $(srcdir)/const-info.h +context.lo context.o: $(srcdir)/context.c \ + config.h \ + $(srcdir)/debug.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/wire2str.h $(srcdir)/context.h \ + getdns/getdns.h \ + getdns/getdns_extra.h \ $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h \ - $(srcdir)/extension/default_eventloop.h config.h $(srcdir)/extension/poll_eventloop.h \ - getdns/getdns_extra.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ - $(srcdir)/rr-iter.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dict.h -rr-iter.lo rr-iter.o: $(srcdir)/rr-iter.c $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h config.h getdns/getdns.h \ - $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/rrdef.h -server.lo server.o: $(srcdir)/server.c config.h getdns/getdns_extra.h getdns/getdns.h \ - $(srcdir)/context.h getdns/getdns.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h config.h \ - $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ - $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h -stub.lo stub.o: $(srcdir)/stub.c config.h $(srcdir)/debug.h $(srcdir)/stub.h getdns/getdns.h $(srcdir)/types-internal.h \ - getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/rrdef.h \ - $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/wire2str.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ - $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h config.h \ - $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ - $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util-internal.h $(srcdir)/general.h $(srcdir)/pubkey-pinning.h -sync.lo sync.o: $(srcdir)/sync.c getdns/getdns.h config.h $(srcdir)/context.h getdns/getdns_extra.h \ - getdns/getdns.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h config.h \ - $(srcdir)/extension/poll_eventloop.h getdns/getdns_extra.h $(srcdir)/types-internal.h \ - $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/general.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ - $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/stub.h \ - $(srcdir)/gldns/wire2str.h -ub_loop.lo ub_loop.o: $(srcdir)/ub_loop.c $(srcdir)/ub_loop.h config.h getdns/getdns.h \ - getdns/getdns_extra.h getdns/getdns.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/debug.h -util-internal.lo util-internal.o: $(srcdir)/util-internal.c config.h getdns/getdns.h $(srcdir)/dict.h \ - $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/types-internal.h \ - getdns/getdns_extra.h getdns/getdns.h $(srcdir)/list.h $(srcdir)/util-internal.h $(srcdir)/context.h \ - $(srcdir)/extension/default_eventloop.h config.h $(srcdir)/extension/poll_eventloop.h \ - getdns/getdns_extra.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ - $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/str2wire.h \ - $(srcdir)/gldns/rrdef.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h -version.lo version.o: version.c -gbuffer.lo gbuffer.o: $(srcdir)/gldns/gbuffer.c config.h $(srcdir)/gldns/gbuffer.h -keyraw.lo keyraw.o: $(srcdir)/gldns/keyraw.c config.h $(srcdir)/gldns/keyraw.h $(srcdir)/gldns/rrdef.h -parse.lo parse.o: $(srcdir)/gldns/parse.c config.h $(srcdir)/gldns/parse.h $(srcdir)/gldns/parseutil.h \ - $(srcdir)/gldns/gbuffer.h -parseutil.lo parseutil.o: $(srcdir)/gldns/parseutil.c config.h $(srcdir)/gldns/parseutil.h -rrdef.lo rrdef.o: $(srcdir)/gldns/rrdef.c config.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/parseutil.h -str2wire.lo str2wire.o: $(srcdir)/gldns/str2wire.c config.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h \ - $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/parse.h $(srcdir)/gldns/parseutil.h -wire2str.lo wire2str.o: $(srcdir)/gldns/wire2str.c config.h $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/str2wire.h \ - $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/parseutil.h $(srcdir)/gldns/gbuffer.h \ - $(srcdir)/gldns/keyraw.h -arc4_lock.lo arc4_lock.o: $(srcdir)/compat/arc4_lock.c config.h -arc4random.lo arc4random.o: $(srcdir)/compat/arc4random.c config.h $(srcdir)/compat/chacha_private.h -arc4random_uniform.lo arc4random_uniform.o: $(srcdir)/compat/arc4random_uniform.c config.h -explicit_bzero.lo explicit_bzero.o: $(srcdir)/compat/explicit_bzero.c config.h -getentropy_linux.lo getentropy_linux.o: $(srcdir)/compat/getentropy_linux.c config.h -getentropy_osx.lo getentropy_osx.o: $(srcdir)/compat/getentropy_osx.c config.h -getentropy_solaris.lo getentropy_solaris.o: $(srcdir)/compat/getentropy_solaris.c config.h -getentropy_win.lo getentropy_win.o: $(srcdir)/compat/getentropy_win.c -gettimeofday.lo gettimeofday.o: $(srcdir)/compat/gettimeofday.c config.h -inet_ntop.lo inet_ntop.o: $(srcdir)/compat/inet_ntop.c config.h -inet_pton.lo inet_pton.o: $(srcdir)/compat/inet_pton.c config.h -sha512.lo sha512.o: $(srcdir)/compat/sha512.c config.h -strlcpy.lo strlcpy.o: $(srcdir)/compat/strlcpy.c config.h -locks.lo locks.o: $(srcdir)/util/locks.c config.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ - $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h config.h -lookup3.lo lookup3.o: $(srcdir)/util/lookup3.c config.h $(srcdir)/util/auxiliary/util/storage/lookup3.h \ - $(srcdir)/util/lookup3.h $(srcdir)/util/orig-headers/lookup3.h -lruhash.lo lruhash.o: $(srcdir)/util/lruhash.c config.h $(srcdir)/util/auxiliary/util/storage/lruhash.h \ + $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ + $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util/lruhash.h \ + $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ + $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/stub.h $(srcdir)/list.h \ + $(srcdir)/dict.h $(srcdir)/pubkey-pinning.h +convert.lo convert.o: $(srcdir)/convert.c \ + config.h \ + getdns/getdns.h \ + getdns/getdns_extra.h \ + $(srcdir)/util-internal.h $(srcdir)/context.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h \ + $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/extension/default_eventloop.h \ + $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ $(srcdir)/util/lruhash.h $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h \ - $(srcdir)/util/orig-headers/locks.h $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h config.h \ - $(srcdir)/util/auxiliary/util/fptr_wlist.h -rbtree.lo rbtree.o: $(srcdir)/util/rbtree.c config.h $(srcdir)/util/auxiliary/log.h \ - $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h config.h $(srcdir)/util/auxiliary/fptr_wlist.h \ - $(srcdir)/util/auxiliary/util/fptr_wlist.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h -val_secalgo.lo val_secalgo.o: $(srcdir)/util/val_secalgo.c config.h \ + $(srcdir)/util/orig-headers/locks.h $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/rr-iter.h \ + $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/wire2str.h \ + $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/parseutil.h $(srcdir)/const-info.h $(srcdir)/dict.h \ + $(srcdir)/list.h $(srcdir)/jsmn/jsmn.h $(srcdir)/convert.h +dict.lo dict.o: $(srcdir)/dict.c \ + config.h \ + $(srcdir)/types-internal.h \ + getdns/getdns.h \ + getdns/getdns_extra.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/util-internal.h $(srcdir)/context.h \ + $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ + $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/util/lruhash.h \ + $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ + $(srcdir)/gldns/pkthdr.h $(srcdir)/dict.h $(srcdir)/list.h $(srcdir)/const-info.h $(srcdir)/gldns/wire2str.h \ + $(srcdir)/gldns/parseutil.h +dnssec.lo dnssec.o: $(srcdir)/dnssec.c \ + config.h \ + $(srcdir)/debug.h \ + getdns/getdns.h \ + $(srcdir)/context.h \ + getdns/getdns_extra.h \ + $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h \ + $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ + $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util/lruhash.h \ + $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ + $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/str2wire.h \ + $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/keyraw.h $(srcdir)/gldns/parseutil.h \ + $(srcdir)/general.h $(srcdir)/dict.h $(srcdir)/list.h $(srcdir)/util/val_secalgo.h \ + $(srcdir)/util/orig-headers/val_secalgo.h +general.lo general.o: $(srcdir)/general.c \ + config.h \ + $(srcdir)/general.h \ + getdns/getdns.h \ + $(srcdir)/types-internal.h \ + getdns/getdns_extra.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/ub_loop.h $(srcdir)/debug.h \ + $(srcdir)/gldns/wire2str.h $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h \ + $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/server.h $(srcdir)/util/lruhash.h \ + $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ + $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/stub.h $(srcdir)/dict.h \ + $(srcdir)/mdns.h +list.lo list.o: $(srcdir)/list.c $(srcdir)/types-internal.h \ + getdns/getdns.h \ + getdns/getdns_extra.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/util-internal.h \ + config.h \ + $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ + $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/util/lruhash.h \ + $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ + $(srcdir)/gldns/pkthdr.h $(srcdir)/list.h $(srcdir)/dict.h +mdns.lo mdns.o: $(srcdir)/mdns.c \ + config.h \ + $(srcdir)/debug.h $(srcdir)/context.h \ + getdns/getdns.h \ + getdns/getdns_extra.h \ + $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h \ + $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ + $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util/lruhash.h \ + $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/general.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/rrdef.h \ + $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/mdns.h \ + $(srcdir)/util/auxiliary/util/fptr_wlist.h $(srcdir)/util/lookup3.h \ + $(srcdir)/util/orig-headers/lookup3.h +pubkey-pinning.lo pubkey-pinning.o: $(srcdir)/pubkey-pinning.c \ + config.h \ + $(srcdir)/debug.h \ + getdns/getdns.h \ + $(srcdir)/context.h \ + getdns/getdns_extra.h \ + $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h \ + $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ + $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/server.h $(srcdir)/util/lruhash.h \ + $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ + $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h +request-internal.lo request-internal.o: $(srcdir)/request-internal.c \ + config.h \ + $(srcdir)/types-internal.h \ + getdns/getdns.h \ + getdns/getdns_extra.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/util-internal.h $(srcdir)/context.h \ + $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ + $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/util/lruhash.h \ + $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h \ + $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/dict.h \ + $(srcdir)/convert.h $(srcdir)/general.h +rr-dict.lo rr-dict.o: $(srcdir)/rr-dict.c $(srcdir)/rr-dict.h \ + config.h \ + getdns/getdns.h \ + $(srcdir)/gldns/gbuffer.h $(srcdir)/util-internal.h $(srcdir)/context.h \ + getdns/getdns_extra.h \ + $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h \ + $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ + $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/util/lruhash.h \ + $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/rr-iter.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dict.h +rr-iter.lo rr-iter.o: $(srcdir)/rr-iter.c $(srcdir)/rr-iter.h $(srcdir)/rr-dict.h \ + config.h \ + getdns/getdns.h \ + $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/rrdef.h +server.lo server.o: $(srcdir)/server.c \ + config.h \ + getdns/getdns_extra.h \ + getdns/getdns.h \ + $(srcdir)/context.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h \ + $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ + $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/util/lruhash.h \ + $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h +stub.lo stub.o: $(srcdir)/stub.c \ + config.h \ + $(srcdir)/debug.h $(srcdir)/stub.h \ + getdns/getdns.h \ + $(srcdir)/types-internal.h \ + getdns/getdns_extra.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h \ + $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/wire2str.h $(srcdir)/rr-iter.h \ + $(srcdir)/rr-dict.h $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h \ + $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/server.h \ + $(srcdir)/util/lruhash.h $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h \ + $(srcdir)/util/orig-headers/locks.h $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h \ + $(srcdir)/util-internal.h $(srcdir)/general.h $(srcdir)/pubkey-pinning.h +sync.lo sync.o: $(srcdir)/sync.c \ + getdns/getdns.h \ + config.h \ + $(srcdir)/context.h \ + getdns/getdns_extra.h \ + $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h \ + $(srcdir)/extension/default_eventloop.h $(srcdir)/extension/poll_eventloop.h \ + $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h $(srcdir)/util/lruhash.h \ + $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/general.h $(srcdir)/util-internal.h $(srcdir)/rr-iter.h \ + $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h $(srcdir)/stub.h \ + $(srcdir)/gldns/wire2str.h +ub_loop.lo ub_loop.o: $(srcdir)/ub_loop.c $(srcdir)/ub_loop.h \ + config.h \ + getdns/getdns.h \ + getdns/getdns_extra.h \ + $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/debug.h +util-internal.lo util-internal.o: $(srcdir)/util-internal.c \ + config.h \ + getdns/getdns.h \ + $(srcdir)/dict.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/types-internal.h \ + getdns/getdns_extra.h \ + $(srcdir)/list.h $(srcdir)/util-internal.h $(srcdir)/context.h $(srcdir)/extension/default_eventloop.h \ + $(srcdir)/extension/poll_eventloop.h $(srcdir)/types-internal.h $(srcdir)/ub_loop.h $(srcdir)/debug.h $(srcdir)/server.h \ + $(srcdir)/util/lruhash.h $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h \ + $(srcdir)/util/orig-headers/locks.h $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/rr-iter.h \ + $(srcdir)/rr-dict.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/pkthdr.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h \ + $(srcdir)/dnssec.h $(srcdir)/gldns/rrdef.h +gbuffer.lo gbuffer.o: $(srcdir)/gldns/gbuffer.c \ + config.h \ + $(srcdir)/gldns/gbuffer.h +keyraw.lo keyraw.o: $(srcdir)/gldns/keyraw.c \ + config.h \ + $(srcdir)/gldns/keyraw.h $(srcdir)/gldns/rrdef.h +parse.lo parse.o: $(srcdir)/gldns/parse.c \ + config.h \ + $(srcdir)/gldns/parse.h $(srcdir)/gldns/parseutil.h $(srcdir)/gldns/gbuffer.h +parseutil.lo parseutil.o: $(srcdir)/gldns/parseutil.c \ + config.h \ + $(srcdir)/gldns/parseutil.h +rrdef.lo rrdef.o: $(srcdir)/gldns/rrdef.c \ + config.h \ + $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/parseutil.h +str2wire.lo str2wire.o: $(srcdir)/gldns/str2wire.c \ + config.h \ + $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/gbuffer.h \ + $(srcdir)/gldns/parse.h $(srcdir)/gldns/parseutil.h +wire2str.lo wire2str.o: $(srcdir)/gldns/wire2str.c \ + config.h \ + $(srcdir)/gldns/wire2str.h $(srcdir)/gldns/str2wire.h $(srcdir)/gldns/rrdef.h $(srcdir)/gldns/pkthdr.h \ + $(srcdir)/gldns/parseutil.h $(srcdir)/gldns/gbuffer.h $(srcdir)/gldns/keyraw.h +arc4_lock.lo arc4_lock.o: $(srcdir)/compat/arc4_lock.c \ + config.h +arc4random.lo arc4random.o: $(srcdir)/compat/arc4random.c \ + config.h \ + $(srcdir)/compat/chacha_private.h +arc4random_uniform.lo arc4random_uniform.o: $(srcdir)/compat/arc4random_uniform.c \ + config.h +explicit_bzero.lo explicit_bzero.o: $(srcdir)/compat/explicit_bzero.c \ + config.h +getentropy_linux.lo getentropy_linux.o: $(srcdir)/compat/getentropy_linux.c \ + config.h +getentropy_osx.lo getentropy_osx.o: $(srcdir)/compat/getentropy_osx.c \ + config.h +getentropy_solaris.lo getentropy_solaris.o: $(srcdir)/compat/getentropy_solaris.c \ + config.h +getentropy_win.lo getentropy_win.o: $(srcdir)/compat/getentropy_win.c +gettimeofday.lo gettimeofday.o: $(srcdir)/compat/gettimeofday.c \ + config.h +inet_ntop.lo inet_ntop.o: $(srcdir)/compat/inet_ntop.c \ + config.h +inet_pton.lo inet_pton.o: $(srcdir)/compat/inet_pton.c \ + config.h +sha512.lo sha512.o: $(srcdir)/compat/sha512.c \ + config.h +strlcpy.lo strlcpy.o: $(srcdir)/compat/strlcpy.c \ + config.h +locks.lo locks.o: $(srcdir)/util/locks.c \ + config.h \ + $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h +lookup3.lo lookup3.o: $(srcdir)/util/lookup3.c \ + config.h \ + $(srcdir)/util/auxiliary/util/storage/lookup3.h $(srcdir)/util/lookup3.h \ + $(srcdir)/util/orig-headers/lookup3.h +lruhash.lo lruhash.o: $(srcdir)/util/lruhash.c \ + config.h \ + $(srcdir)/util/auxiliary/util/storage/lruhash.h $(srcdir)/util/lruhash.h \ + $(srcdir)/util/orig-headers/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/orig-headers/locks.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/util/auxiliary/util/fptr_wlist.h +rbtree.lo rbtree.o: $(srcdir)/util/rbtree.c \ + config.h \ + $(srcdir)/util/auxiliary/log.h $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h \ + $(srcdir)/util/auxiliary/fptr_wlist.h $(srcdir)/util/auxiliary/util/fptr_wlist.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h +val_secalgo.lo val_secalgo.o: $(srcdir)/util/val_secalgo.c \ + config.h \ $(srcdir)/util/auxiliary/util/data/packed_rrset.h \ $(srcdir)/util/auxiliary/validator/val_secalgo.h $(srcdir)/util/val_secalgo.h \ $(srcdir)/util/orig-headers/val_secalgo.h $(srcdir)/util/auxiliary/validator/val_nsec3.h \ - $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h config.h $(srcdir)/util/auxiliary/sldns/rrdef.h \ + $(srcdir)/util/auxiliary/util/log.h $(srcdir)/debug.h $(srcdir)/util/auxiliary/sldns/rrdef.h \ $(srcdir)/gldns/rrdef.h $(srcdir)/util/auxiliary/sldns/keyraw.h $(srcdir)/gldns/keyraw.h \ $(srcdir)/util/auxiliary/sldns/sbuffer.h $(srcdir)/gldns/gbuffer.h jsmn.lo jsmn.o: $(srcdir)/jsmn/jsmn.c $(srcdir)/jsmn/jsmn.h -libev.lo libev.o: $(srcdir)/extension/libev.c config.h $(srcdir)/types-internal.h getdns/getdns.h \ - getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/getdns/getdns_ext_libev.h \ - getdns/getdns_extra.h -libevent.lo libevent.o: $(srcdir)/extension/libevent.c config.h $(srcdir)/types-internal.h \ - getdns/getdns.h getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/getdns/getdns_ext_libevent.h \ - getdns/getdns_extra.h -libuv.lo libuv.o: $(srcdir)/extension/libuv.c config.h $(srcdir)/debug.h config.h $(srcdir)/types-internal.h \ - getdns/getdns.h getdns/getdns_extra.h getdns/getdns.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/getdns/getdns_ext_libuv.h \ - getdns/getdns_extra.h -poll_eventloop.lo poll_eventloop.o: $(srcdir)/extension/poll_eventloop.c config.h \ - $(srcdir)/extension/poll_eventloop.h getdns/getdns.h getdns/getdns_extra.h \ - $(srcdir)/types-internal.h getdns/getdns.h getdns/getdns_extra.h $(srcdir)/util/rbtree.h \ - $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/debug.h config.h -select_eventloop.lo select_eventloop.o: $(srcdir)/extension/select_eventloop.c config.h \ - $(srcdir)/extension/select_eventloop.h getdns/getdns.h getdns/getdns_extra.h \ - $(srcdir)/debug.h config.h $(srcdir)/types-internal.h getdns/getdns.h getdns/getdns_extra.h \ - $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h +libev.lo libev.o: $(srcdir)/extension/libev.c \ + config.h \ + $(srcdir)/types-internal.h \ + getdns/getdns.h \ + getdns/getdns_extra.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/getdns/getdns_ext_libev.h +libevent.lo libevent.o: $(srcdir)/extension/libevent.c \ + config.h \ + $(srcdir)/types-internal.h \ + getdns/getdns.h \ + getdns/getdns_extra.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/getdns/getdns_ext_libevent.h +libuv.lo libuv.o: $(srcdir)/extension/libuv.c \ + config.h \ + $(srcdir)/debug.h $(srcdir)/types-internal.h \ + getdns/getdns.h \ + getdns/getdns_extra.h \ + $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/getdns/getdns_ext_libuv.h +poll_eventloop.lo poll_eventloop.o: $(srcdir)/extension/poll_eventloop.c \ + config.h \ + $(srcdir)/extension/poll_eventloop.h \ + getdns/getdns.h \ + getdns/getdns_extra.h \ + $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h $(srcdir)/debug.h +select_eventloop.lo select_eventloop.o: $(srcdir)/extension/select_eventloop.c \ + config.h \ + $(srcdir)/extension/select_eventloop.h \ + getdns/getdns.h \ + getdns/getdns_extra.h \ + $(srcdir)/debug.h $(srcdir)/types-internal.h $(srcdir)/util/rbtree.h $(srcdir)/util/orig-headers/rbtree.h diff --git a/src/test/Makefile.in b/src/test/Makefile.in index c481fdab..758435c1 100644 --- a/src/test/Makefile.in +++ b/src/test/Makefile.in @@ -216,10 +216,13 @@ depend: .PHONY: clean test # Dependencies for the unit tests -check_getdns.lo check_getdns.o: $(srcdir)/check_getdns.c ../getdns/getdns.h $(srcdir)/check_getdns_common.h \ - ../getdns/getdns_extra.h $(srcdir)/check_getdns_address.h \ - $(srcdir)/check_getdns_address_sync.h $(srcdir)/check_getdns_cancel_callback.h \ - $(srcdir)/check_getdns_context_create.h $(srcdir)/check_getdns_context_destroy.h \ +check_getdns.lo check_getdns.o: $(srcdir)/check_getdns.c \ + ../getdns/getdns.h \ + $(srcdir)/check_getdns_common.h \ + ../getdns/getdns_extra.h \ + $(srcdir)/check_getdns_address.h $(srcdir)/check_getdns_address_sync.h \ + $(srcdir)/check_getdns_cancel_callback.h $(srcdir)/check_getdns_context_create.h \ + $(srcdir)/check_getdns_context_destroy.h \ $(srcdir)/check_getdns_context_set_context_update_callback.h \ $(srcdir)/check_getdns_context_set_dns_transport.h \ $(srcdir)/check_getdns_context_set_timeout.h \ @@ -239,34 +242,58 @@ check_getdns.lo check_getdns.o: $(srcdir)/check_getdns.c ../getdns/getdns.h $(sr $(srcdir)/check_getdns_list_get_list.h $(srcdir)/check_getdns_pretty_print_dict.h \ $(srcdir)/check_getdns_service.h $(srcdir)/check_getdns_service_sync.h \ $(srcdir)/check_getdns_transport.h -check_getdns_common.lo check_getdns_common.o: $(srcdir)/check_getdns_common.c ../getdns/getdns.h \ - ../config.h $(srcdir)/check_getdns_common.h ../getdns/getdns_extra.h \ +check_getdns_common.lo check_getdns_common.o: $(srcdir)/check_getdns_common.c \ + ../getdns/getdns.h \ + ../config.h \ + $(srcdir)/check_getdns_common.h \ + ../getdns/getdns_extra.h \ $(srcdir)/check_getdns_eventloop.h check_getdns_context_set_timeout.lo check_getdns_context_set_timeout.o: $(srcdir)/check_getdns_context_set_timeout.c \ $(srcdir)/check_getdns_context_set_timeout.h $(srcdir)/check_getdns_common.h \ - ../getdns/getdns.h ../getdns/getdns_extra.h + ../getdns/getdns.h \ + ../getdns/getdns_extra.h check_getdns_libev.lo check_getdns_libev.o: $(srcdir)/check_getdns_libev.c $(srcdir)/check_getdns_eventloop.h \ - ../config.h ../getdns/getdns.h $(srcdir)/../getdns/getdns_ext_libev.h \ - ../getdns/getdns_extra.h $(srcdir)/check_getdns_common.h + ../config.h \ + ../getdns/getdns.h \ + $(srcdir)/../getdns/getdns_ext_libev.h \ + ../getdns/getdns_extra.h \ + $(srcdir)/check_getdns_common.h check_getdns_libevent.lo check_getdns_libevent.o: $(srcdir)/check_getdns_libevent.c $(srcdir)/check_getdns_eventloop.h \ - ../config.h ../getdns/getdns.h $(srcdir)/../getdns/getdns_ext_libevent.h \ - ../getdns/getdns_extra.h $(srcdir)/check_getdns_libevent.h $(srcdir)/check_getdns_common.h + ../config.h \ + ../getdns/getdns.h \ + $(srcdir)/../getdns/getdns_ext_libevent.h \ + ../getdns/getdns_extra.h \ + $(srcdir)/check_getdns_libevent.h $(srcdir)/check_getdns_common.h check_getdns_libuv.lo check_getdns_libuv.o: $(srcdir)/check_getdns_libuv.c $(srcdir)/check_getdns_eventloop.h \ - ../config.h ../getdns/getdns.h $(srcdir)/../getdns/getdns_ext_libuv.h \ - ../getdns/getdns_extra.h $(srcdir)/check_getdns_common.h + ../config.h \ + ../getdns/getdns.h \ + $(srcdir)/../getdns/getdns_ext_libuv.h \ + ../getdns/getdns_extra.h \ + $(srcdir)/check_getdns_common.h check_getdns_selectloop.lo check_getdns_selectloop.o: $(srcdir)/check_getdns_selectloop.c \ - $(srcdir)/check_getdns_eventloop.h ../config.h ../getdns/getdns.h \ + $(srcdir)/check_getdns_eventloop.h \ + ../config.h \ + ../getdns/getdns.h \ ../getdns/getdns_extra.h check_getdns_transport.lo check_getdns_transport.o: $(srcdir)/check_getdns_transport.c \ - $(srcdir)/check_getdns_transport.h $(srcdir)/check_getdns_common.h ../getdns/getdns.h \ + $(srcdir)/check_getdns_transport.h $(srcdir)/check_getdns_common.h \ + ../getdns/getdns.h \ ../getdns/getdns_extra.h -scratchpad.template.lo scratchpad.template.o: scratchpad.template.c ../getdns/getdns.h \ +scratchpad.template.lo scratchpad.template.o: scratchpad.template.c \ + ../getdns/getdns.h \ ../getdns/getdns_extra.h testmessages.lo testmessages.o: $(srcdir)/testmessages.c $(srcdir)/testmessages.h -tests_dict.lo tests_dict.o: $(srcdir)/tests_dict.c $(srcdir)/testmessages.h ../getdns/getdns.h -tests_list.lo tests_list.o: $(srcdir)/tests_list.c $(srcdir)/testmessages.h ../getdns/getdns.h -tests_namespaces.lo tests_namespaces.o: $(srcdir)/tests_namespaces.c $(srcdir)/testmessages.h ../getdns/getdns.h -tests_stub_async.lo tests_stub_async.o: $(srcdir)/tests_stub_async.c ../config.h $(srcdir)/testmessages.h \ - ../getdns/getdns.h ../getdns/getdns_extra.h -tests_stub_sync.lo tests_stub_sync.o: $(srcdir)/tests_stub_sync.c $(srcdir)/testmessages.h ../getdns/getdns.h \ +tests_dict.lo tests_dict.o: $(srcdir)/tests_dict.c $(srcdir)/testmessages.h \ + ../getdns/getdns.h +tests_list.lo tests_list.o: $(srcdir)/tests_list.c $(srcdir)/testmessages.h \ + ../getdns/getdns.h +tests_namespaces.lo tests_namespaces.o: $(srcdir)/tests_namespaces.c $(srcdir)/testmessages.h \ + ../getdns/getdns.h +tests_stub_async.lo tests_stub_async.o: $(srcdir)/tests_stub_async.c \ + ../config.h \ + $(srcdir)/testmessages.h \ + ../getdns/getdns.h \ + ../getdns/getdns_extra.h +tests_stub_sync.lo tests_stub_sync.o: $(srcdir)/tests_stub_sync.c $(srcdir)/testmessages.h \ + ../getdns/getdns.h \ ../getdns/getdns_extra.h diff --git a/src/tools/Makefile.in b/src/tools/Makefile.in index d98cf437..d066e824 100644 --- a/src/tools/Makefile.in +++ b/src/tools/Makefile.in @@ -113,5 +113,8 @@ depend: .PHONY: clean test # Dependencies for getdns_query -getdns_query.lo getdns_query.o: $(srcdir)/getdns_query.c ../config.h $(srcdir)/../debug.h ../config.h \ - ../getdns/getdns.h ../getdns/getdns_extra.h +getdns_query.lo getdns_query.o: $(srcdir)/getdns_query.c \ + ../config.h \ + $(srcdir)/../debug.h \ + ../getdns/getdns.h \ + ../getdns/getdns_extra.h