From c4e8a3194d6b92f596a6483e486c645de7d2ddd1 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 16 Dec 2016 21:00:14 +0100 Subject: dirmngr: Use one context for all libdns queries. * dirmngr/dns-stuff.c (libdns_reinit_pending): New var. (enable_recursive_resolver): Set var. (set_dns_nameserver): Ditto. (libdns_init): Avoid double initialization. (libdns_deinit): New. (reload_dns_stuff): New. (libdns_res_open): Act upon LIBDNS_REINIT_PENDING. * dirmngr/t-dns-stuff.c (main): Call reload_dns_stuff to release memory. * dirmngr/dirmngr.c (cleanup): Ditto. (dirmngr_sighup_action): Call reload_dns_stuff to set LIBDNS_REINIT_PENDING. Signed-off-by: Werner Koch --- dirmngr/dns-stuff.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'dirmngr/dns-stuff.c') diff --git a/dirmngr/dns-stuff.c b/dirmngr/dns-stuff.c index 0f1f0ede9..63951e5eb 100644 --- a/dirmngr/dns-stuff.c +++ b/dirmngr/dns-stuff.c @@ -133,8 +133,13 @@ struct libdns_s struct sockaddr_storage socks_host; } libdns; + +/* If this flag is set, libdns shall be reinited for the next use. */ +static int libdns_reinit_pending; + #endif /*USE_LIBDNS*/ + /* Calling this function with YES set to True forces the use of the * standard resolver even if dirmngr has been built with support for * an alternative resolver. */ @@ -159,6 +164,7 @@ void enable_recursive_resolver (int yes) { recursive_resolver = yes; + libdns_reinit_pending = 1; } @@ -203,6 +209,7 @@ set_dns_nameserver (const char *ipaddr) strncpy (tor_nameserver, ipaddr? ipaddr : DEFAULT_NAMESERVER, sizeof tor_nameserver -1); tor_nameserver[sizeof tor_nameserver -1] = 0; + libdns_reinit_pending = 1; } @@ -315,6 +322,9 @@ libdns_init (void) const char *fname; char *cfgstr = NULL; + if (libdns.resolv_conf) + return 0; /* Already initialized. */ + memset (&ld, 0, sizeof ld); ld.resolv_conf = dns_resconf_open (&derr); @@ -409,6 +419,41 @@ libdns_init (void) #endif /*USE_LIBDNS*/ +#ifdef USE_LIBDNS +/* Deinitialize libdns. */ +static void +libdns_deinit (void) +{ + struct libdns_s ld; + + if (!libdns.resolv_conf) + return; /* Not initialized. */ + + ld = libdns; + memset (&libdns, 0, sizeof libdns); + dns_hints_close (ld.hints); + dns_hosts_close (ld.hosts); + dns_resconf_close (ld.resolv_conf); +} +#endif /*USE_LIBDNS*/ + +/* SIGHUP action handler for this module. With FORCE set objects are + * all immediately released. */ +void +reload_dns_stuff (int force) +{ + if (force) + { +#ifdef USE_LIBDNS + libdns_deinit (); +#endif + libdns_reinit_pending = 0; + } + else + libdns_reinit_pending = 1; +} + + #ifdef USE_LIBDNS /* * Initialize libdns if needed and open a dns_resolver context. @@ -424,6 +469,12 @@ libdns_res_open (struct dns_resolver **r_res) *r_res = NULL; + if (libdns_reinit_pending) + { + libdns_reinit_pending = 0; + libdns_deinit (); + } + err = libdns_init (); if (err) return err; -- cgit v1.2.3