From b9127ca07c37288f16b1fdc267d3f106721ed301 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Thu, 22 Aug 2019 13:11:00 +0200 Subject: srvrec: Use re-entrant resolver functions This should probably make those functions thread-safe, which might be useful for some external users. --- CMakeLists.txt | 2 +- apt-pkg/contrib/srvrec.cc | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0487429c6..160f7f913 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,7 +186,7 @@ if (NOT HAVE_SIGHANDLER_T) endif() # Handle resolving -check_function_exists(res_init HAVE_LIBC_RESOLV) +check_function_exists(res_ninit HAVE_LIBC_RESOLV) if(HAVE_LIBC_RESOLV) set(RESOLV_LIBRARIES) else() diff --git a/apt-pkg/contrib/srvrec.cc b/apt-pkg/contrib/srvrec.cc index a97d9c615..7d9bf116e 100644 --- a/apt-pkg/contrib/srvrec.cc +++ b/apt-pkg/contrib/srvrec.cc @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -61,11 +62,15 @@ bool GetSrvRecords(std::string name, std::vector &Result) unsigned char answer[PACKETSZ]; int answer_len, compressed_name_len; int answer_count; + struct __res_state res; - if (res_init() != 0) + if (res_ninit(&res) != 0) return _error->Errno("res_init", "Failed to init resolver"); - answer_len = res_query(name.c_str(), C_IN, T_SRV, answer, sizeof(answer)); + // Close on return + std::shared_ptr guard(&res, res_nclose); + + answer_len = res_nquery(&res, name.c_str(), C_IN, T_SRV, answer, sizeof(answer)); if (answer_len == -1) return false; if (answer_len < (int)sizeof(HEADER)) -- cgit v1.2.3