dns: Support Musl

This commit is contained in:
klzgrad 2019-11-30 22:21:18 +08:00
parent 6c6045eeb8
commit 72769b7abb
3 changed files with 21 additions and 2 deletions

View File

@ -93,7 +93,7 @@ absl::optional<DnsConfig> ReadDnsConfig() {
absl::optional<DnsConfig> dns_config; absl::optional<DnsConfig> dns_config;
// TODO(fuchsia): Use res_ninit() when it's implemented on Fuchsia. // TODO(fuchsia): Use res_ninit() when it's implemented on Fuchsia.
#if defined(OS_OPENBSD) || defined(OS_FUCHSIA) #if defined(OS_OPENBSD) || defined(OS_FUCHSIA) || defined(__MUSL__)
// Note: res_ninit in glibc always returns 0 and sets RES_INIT. // Note: res_ninit in glibc always returns 0 and sets RES_INIT.
// res_init behaves the same way. // res_init behaves the same way.
memset(&_res, 0, sizeof(_res)); memset(&_res, 0, sizeof(_res));

View File

@ -63,20 +63,32 @@ class DnsReloader : public NetworkChangeNotifier::DNSObserver {
if (!reload_state) { if (!reload_state) {
auto new_reload_state = std::make_unique<ReloadState>(); auto new_reload_state = std::make_unique<ReloadState>();
new_reload_state->resolver_generation = resolver_generation_; new_reload_state->resolver_generation = resolver_generation_;
#ifdef __MUSL__
res_init();
#else
res_ninit(&_res); res_ninit(&_res);
#endif
tls_reload_state_.Set(std::move(new_reload_state)); tls_reload_state_.Set(std::move(new_reload_state));
} else if (reload_state->resolver_generation != resolver_generation_) { } else if (reload_state->resolver_generation != resolver_generation_) {
reload_state->resolver_generation = resolver_generation_; reload_state->resolver_generation = resolver_generation_;
// It is safe to call res_nclose here since we know res_ninit will have // It is safe to call res_nclose here since we know res_ninit will have
// been called above. // been called above.
#ifdef __MUSL__
res_init();
#else
res_nclose(&_res); res_nclose(&_res);
res_ninit(&_res); res_ninit(&_res);
#endif
} }
} }
private: private:
struct ReloadState { struct ReloadState {
~ReloadState() { res_nclose(&_res); } ~ReloadState() {
#ifndef __MUSL__
res_nclose(&_res);
#endif
}
int resolver_generation; int resolver_generation;
}; };

View File

@ -23,16 +23,23 @@ std::unique_ptr<struct __res_state> ResolvReader::GetResState() {
auto res = std::make_unique<struct __res_state>(); auto res = std::make_unique<struct __res_state>();
memset(res.get(), 0, sizeof(struct __res_state)); memset(res.get(), 0, sizeof(struct __res_state));
#if defined(__MUSL__)
if (res_init() != 0)
return nullptr;
#else
if (res_ninit(res.get()) != 0) { if (res_ninit(res.get()) != 0) {
CloseResState(res.get()); CloseResState(res.get());
return nullptr; return nullptr;
} }
#endif
return res; return res;
} }
void ResolvReader::CloseResState(struct __res_state* res) { void ResolvReader::CloseResState(struct __res_state* res) {
#if !defined(__MUSL__)
res_nclose(res); res_nclose(res);
#endif
} }
absl::optional<std::vector<IPEndPoint>> GetNameservers( absl::optional<std::vector<IPEndPoint>> GetNameservers(