From a20326590739ea4a6d9194172ec95bd23d5787b1 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 25 Dec 2017 08:48:33 +0100 Subject: [PATCH 1/2] src: remove unused GetHostByNameWrap It was a wrapper for `ares_gethostbyname()` that I'm unsure about if it was ever exposed at the binding layer, let alone the public API. --- src/cares_wrap.cc | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 80335f2bea6cf0..b9ab1b215e0482 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -1772,33 +1772,6 @@ class GetHostByAddrWrap: public QueryWrap { }; -class GetHostByNameWrap: public QueryWrap { - public: - explicit GetHostByNameWrap(ChannelWrap* channel, Local req_wrap_obj) - : QueryWrap(channel, req_wrap_obj) { - } - - int Send(const char* name, int family) override { - ares_gethostbyname(channel_->cares_channel(), - name, - family, - Callback, - static_cast(static_cast(this))); - return 0; - } - - protected: - void Parse(struct hostent* host) override { - HandleScope scope(env()->isolate()); - - Local addresses = HostentToAddresses(env(), host); - Local family = Integer::New(env()->isolate(), host->h_addrtype); - - this->CallOnComplete(addresses, family); - } -}; - - template static void Query(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); From 543e1fa0a235b830a819e3436982546af2d90f96 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 25 Dec 2017 08:48:33 +0100 Subject: [PATCH 2/2] src: inline HostentToAddresses() With the removal of `GetHostByNameWrap` in the previous commit, there is only one remaining call site. Inlining it there lets us simplify the logic. --- src/cares_wrap.cc | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index b9ab1b215e0482..de3cb8f89c1ea2 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -367,26 +367,6 @@ void ares_sockstate_cb(void* data, } -Local HostentToAddresses(Environment* env, - struct hostent* host, - Local append_to = Local()) { - EscapableHandleScope scope(env->isolate()); - auto context = env->context(); - bool append = !append_to.IsEmpty(); - Local addresses = append ? append_to : Array::New(env->isolate()); - size_t offset = addresses->Length(); - - char ip[INET6_ADDRSTRLEN]; - for (uint32_t i = 0; host->h_addr_list[i] != nullptr; ++i) { - uv_inet_ntop(host->h_addrtype, host->h_addr_list[i], ip, sizeof(ip)); - Local address = OneByteString(env->isolate(), ip); - addresses->Set(context, i + offset, address).FromJust(); - } - - return append ? addresses : scope.Escape(addresses); -} - - Local HostentToNames(Environment* env, struct hostent* host, Local append_to = Local()) { @@ -843,12 +823,17 @@ int ParseGeneralReply(Environment* env, } else if (*type == ns_t_ptr) { uint32_t offset = ret->Length(); for (uint32_t i = 0; host->h_aliases[i] != nullptr; i++) { - ret->Set(context, - i + offset, - OneByteString(env->isolate(), host->h_aliases[i])).FromJust(); + auto alias = OneByteString(env->isolate(), host->h_aliases[i]); + ret->Set(context, i + offset, alias).FromJust(); } } else { - HostentToAddresses(env, host, ret); + uint32_t offset = ret->Length(); + char ip[INET6_ADDRSTRLEN]; + for (uint32_t i = 0; host->h_addr_list[i] != nullptr; ++i) { + uv_inet_ntop(host->h_addrtype, host->h_addr_list[i], ip, sizeof(ip)); + auto address = OneByteString(env->isolate(), ip); + ret->Set(context, i + offset, address).FromJust(); + } } ares_free_hostent(host);