Skip to content

Commit

Permalink
src: simplify ParseIP() in cares_wrap.cc
Browse files Browse the repository at this point in the history
The result argument is never nullptr, so remove special handling of that
case. Also, instead of returning magic values 0/4/6 and then later
translating those into error/AF_INET/AF_INET6, return
AF_UNSPEC/AF_INET/AF_INET6 directly.
  • Loading branch information
tniessen committed Sep 24, 2022
1 parent 7e0097d commit 2f7f308
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1535,24 +1535,21 @@ void AfterGetNameInfo(uv_getnameinfo_t* req,
using ParseIPResult =
decltype(static_cast<ares_addr_port_node*>(nullptr)->addr);

int ParseIP(const char* ip, ParseIPResult* result = nullptr) {
ParseIPResult tmp;
if (result == nullptr) result = &tmp;
if (0 == uv_inet_pton(AF_INET, ip, result)) return 4;
if (0 == uv_inet_pton(AF_INET6, ip, result)) return 6;
return 0;
int ParseIP(const char* ip, ParseIPResult* result) {
if (0 == uv_inet_pton(AF_INET, ip, result)) return AF_INET;
if (0 == uv_inet_pton(AF_INET6, ip, result)) return AF_INET6;
return AF_UNSPEC;
}

void CanonicalizeIP(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
node::Utf8Value ip(isolate, args[0]);

ParseIPResult result;
const int rc = ParseIP(*ip, &result);
if (rc == 0) return;
const int af = ParseIP(*ip, &result);
if (af == AF_UNSPEC) return;

char canonical_ip[INET6_ADDRSTRLEN];
const int af = (rc == 4 ? AF_INET : AF_INET6);
CHECK_EQ(0, uv_inet_ntop(af, &result, canonical_ip, sizeof(canonical_ip)));
Local<String> val = String::NewFromUtf8(isolate, canonical_ip)
.ToLocalChecked();
Expand Down

0 comments on commit 2f7f308

Please sign in to comment.