From ed6c7a6b586c9cb5aa61013a5a7d5f1031d24391 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Wed, 22 Nov 2017 15:49:30 +0100 Subject: [PATCH] getdns_context_create2 and family that set an ... ... alternative resolvconf file --- src/context.c | 59 ++++++++++++++++++++++++++-------- src/context.h | 2 +- src/getdns/getdns_extra.h.in | 61 ++++++++++++++++++++++++++++++++++++ src/libgetdns.symbols | 3 ++ 4 files changed, 111 insertions(+), 14 deletions(-) diff --git a/src/context.c b/src/context.c index 6db0745d..e12f4c04 100644 --- a/src/context.c +++ b/src/context.c @@ -1090,7 +1090,7 @@ static int get_dns_suffix_windows(getdns_list *suffix, char* domain) static getdns_return_t -set_os_defaults_windows(struct getdns_context *context) +set_os_defaults(getdns_context *context, const char *resolvconf_file) { char domain[1024] = ""; size_t upstreams_limit = 10; @@ -1107,7 +1107,7 @@ set_os_defaults_windows(struct getdns_context *context) GETDNS_MALLOC(context->my_mf, struct filechg); if (context->fchg_resolvconf == NULL) return GETDNS_RETURN_MEMORY_ERROR; - context->fchg_resolvconf->fn = "InvalidOnWindows"; + context->fchg_resolvconf->fn = resolvconf_file; context->fchg_resolvconf->prevstat = NULL; context->fchg_resolvconf->changes = GETDNS_FCHG_NOCHANGES; context->fchg_resolvconf->errors = GETDNS_FCHG_NOERROR; @@ -1186,7 +1186,7 @@ set_os_defaults_windows(struct getdns_context *context) #else static getdns_return_t -set_os_defaults(struct getdns_context *context) +set_os_defaults(getdns_context *context, const char *resolvconf_file) { FILE *in; char line[1024], domain[1024]; @@ -1203,7 +1203,7 @@ set_os_defaults(struct getdns_context *context) GETDNS_MALLOC(context->my_mf, struct filechg); if(context->fchg_resolvconf == NULL) return GETDNS_RETURN_MEMORY_ERROR; - context->fchg_resolvconf->fn = GETDNS_FN_RESOLVCONF; + context->fchg_resolvconf->fn = resolvconf_file; context->fchg_resolvconf->prevstat = NULL; context->fchg_resolvconf->changes = GETDNS_FCHG_NOCHANGES; context->fchg_resolvconf->errors = GETDNS_FCHG_NOERROR; @@ -1395,9 +1395,9 @@ static const char *_getdns_default_trust_anchors_verify_email = * Call this to initialize the context that is used in other getdns calls. */ getdns_return_t -getdns_context_create_with_extended_memory_functions( - struct getdns_context ** context, - int set_from_os, +getdns_context_create_with_extended_memory_functions2( + getdns_context **context, + const char *resolvconf_file, void *userarg, void *(*malloc)(void *userarg, size_t), void *(*realloc)(void *userarg, void *, size_t), @@ -1571,13 +1571,8 @@ getdns_context_create_with_extended_memory_functions( result->fchg_hosts = NULL; // resolv.conf does not exist on Windows, handle differently -#ifndef USE_WINSOCK - if ((set_from_os & 1) && (r = set_os_defaults(result))) + if (resolvconf_file && (r = set_os_defaults(result, resolvconf_file))) goto error; -#else - if ((set_from_os & 1) && (r = set_os_defaults_windows(result))) - goto error; -#endif result->dnssec_allowed_skew = 0; result->edns_maximum_udp_payload_size = -1; @@ -1638,6 +1633,21 @@ error: return r; } /* getdns_context_create_with_extended_memory_functions */ +getdns_return_t +getdns_context_create_with_extended_memory_functions( + getdns_context **context, + int set_from_os, + void *userarg, + void *(*malloc)(void *userarg, size_t), + void *(*realloc)(void *userarg, void *, size_t), + void (*free)(void *userarg, void *) + ) +{ + return getdns_context_create_with_extended_memory_functions2(context, + ((set_from_os & 1) ? GETDNS_FN_RESOLVCONF : NULL), userarg, + malloc, realloc, free); +} + /* * getdns_context_create * @@ -1672,6 +1682,29 @@ getdns_context_create(struct getdns_context ** context, int set_from_os) set_from_os, malloc, realloc, free); } /* getdns_context_create */ +getdns_return_t +getdns_context_create_with_memory_functions2(getdns_context **context, + const char *resolvconf_file, + void *(*malloc)(size_t), + void *(*realloc)(void *, size_t), + void (*free)(void *) + ) +{ + mf_union mf; + mf.pln.malloc = malloc; + mf.pln.realloc = realloc; + mf.pln.free = free; + return getdns_context_create_with_extended_memory_functions2( + context, resolvconf_file, MF_PLAIN, + mf.ext.malloc, mf.ext.realloc, mf.ext.free); +} + +getdns_return_t +getdns_context_create2(getdns_context **context, const char *resolvconf_file) +{ + return getdns_context_create_with_memory_functions2(context, + resolvconf_file, malloc, realloc, free); +} /* * getdns_context_destroy diff --git a/src/context.h b/src/context.h index 048137b5..54a46306 100644 --- a/src/context.h +++ b/src/context.h @@ -69,7 +69,7 @@ typedef void (*getdns_update_callback2) (struct getdns_context *, /* internal use only for detecting changes to system files */ struct filechg { - char *fn; + const char *fn; int changes; int errors; struct stat *prevstat; diff --git a/src/getdns/getdns_extra.h.in b/src/getdns/getdns_extra.h.in index ca62b2fb..b2e362ee 100644 --- a/src/getdns/getdns_extra.h.in +++ b/src/getdns/getdns_extra.h.in @@ -377,6 +377,67 @@ getdns_context_run(getdns_context *context); * @{ */ +/** + * creates a new getdns context with default settings. + * If used multi-threaded, user must define appropriate OpenSSL callback locking functions + * (e.g. CRYPTO_THREADID_set_call) depending on the library version used. + * @param context context that can be used immediately with other API calls + * @param resolvconf is the location of the resolv.conf file from which to + * read the OS settings. When NULL, the context will not be + * initialized with the settings herein. + * @return GETDNS_RETURN_GOOD on success +*/ +getdns_return_t +getdns_context_create2(getdns_context ** context, const char *resolvconf); + + +/** + * creates a new getdns context with default settings using custom memory functions. + * If used multi-threaded, user must define appropriate OpenSSL callback locking functions + * (e.g. CRYPTO_THREADID_set_call) depending on the library version used. + * @param context context that can be used immediately with other API calls + * @param resolvconf is the location of the resolv.conf file from which to + * read the OS settings. When NULL, the context will not be + * initialized with the settings herein. + * @param malloc custom malloc function + * @param realloc custom realloc function + * @param free custom free function + * @return GETDNS_RETURN_GOOD on success +*/ +getdns_return_t +getdns_context_create_with_memory_functions2( + getdns_context ** context, + const char *resolvconf, + void *(*malloc) (size_t), + void *(*realloc) (void *, size_t), + void (*free) (void *) +); + + +/** + * creates a new getdns context with default settings using extended custom memory functions. + * If used multi-threaded, user must define appropriate OpenSSL callback locking functions + * (e.g. CRYPTO_THREADID_set_call) depending on the library version used. + * @param context context that can be used immediately with other API calls + * @param resolvconf is the location of the resolv.conf file from which to + * read the OS settings. When NULL, the context will not be + * initialized with the settings herein. + * @param userarg parameter passed to the custom malloc, realloc and free functions + * @param malloc custom malloc function + * @param realloc custom realloc function + * @param free custom free function + * @return GETDNS_RETURN_GOOD on success +*/ +getdns_return_t +getdns_context_create_with_extended_memory_functions2( + getdns_context **context, + const char *resolvconf, + void *userarg, + void *(*malloc) (void *userarg, size_t), + void *(*realloc) (void *userarg, void *, size_t), + void (*free) (void *userarg, void *) +); + /** * Register a callback function for context changes. * @see getdns_context_set_context_update_callback diff --git a/src/libgetdns.symbols b/src/libgetdns.symbols index 3f26578a..46199c59 100644 --- a/src/libgetdns.symbols +++ b/src/libgetdns.symbols @@ -3,8 +3,11 @@ getdns_address_sync getdns_cancel_callback getdns_context_config getdns_context_create +getdns_context_create2 getdns_context_create_with_extended_memory_functions +getdns_context_create_with_extended_memory_functions2 getdns_context_create_with_memory_functions +getdns_context_create_with_memory_functions2 getdns_context_destroy getdns_context_detach_eventloop getdns_context_get_api_information