Skip to content

Commit 3e8569c

Browse files
tniessenjuanarbol
authored andcommitted
src: remove ParseIP() in cares_wrap.cc
This function is only used in one place where 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, use AF_INET/AF_INET6 directly. Lastly, inline the function, which is simpler overall. PR-URL: #44771 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Daeyeon Jeong <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent a7666ab commit 3e8569c

File tree

1 file changed

+6
-16
lines changed

1 file changed

+6
-16
lines changed

src/cares_wrap.cc

+6-16
Original file line numberDiff line numberDiff line change
@@ -1532,28 +1532,18 @@ void AfterGetNameInfo(uv_getnameinfo_t* req,
15321532
req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
15331533
}
15341534

1535-
using ParseIPResult =
1536-
decltype(static_cast<ares_addr_port_node*>(nullptr)->addr);
1537-
1538-
int ParseIP(const char* ip, ParseIPResult* result = nullptr) {
1539-
ParseIPResult tmp;
1540-
if (result == nullptr) result = &tmp;
1541-
if (0 == uv_inet_pton(AF_INET, ip, result)) return 4;
1542-
if (0 == uv_inet_pton(AF_INET6, ip, result)) return 6;
1543-
return 0;
1544-
}
1545-
15461535
void CanonicalizeIP(const FunctionCallbackInfo<Value>& args) {
15471536
Isolate* isolate = args.GetIsolate();
15481537
node::Utf8Value ip(isolate, args[0]);
15491538

1550-
ParseIPResult result;
1551-
const int rc = ParseIP(*ip, &result);
1552-
if (rc == 0) return;
1539+
int af;
1540+
unsigned char result[sizeof(ares_addr_port_node::addr)];
1541+
if (uv_inet_pton(af = AF_INET, *ip, result) != 0 &&
1542+
uv_inet_pton(af = AF_INET6, *ip, result) != 0)
1543+
return;
15531544

15541545
char canonical_ip[INET6_ADDRSTRLEN];
1555-
const int af = (rc == 4 ? AF_INET : AF_INET6);
1556-
CHECK_EQ(0, uv_inet_ntop(af, &result, canonical_ip, sizeof(canonical_ip)));
1546+
CHECK_EQ(0, uv_inet_ntop(af, result, canonical_ip, sizeof(canonical_ip)));
15571547
Local<String> val = String::NewFromUtf8(isolate, canonical_ip)
15581548
.ToLocalChecked();
15591549
args.GetReturnValue().Set(val);

0 commit comments

Comments
 (0)