Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: remove ParseIP() in cares_wrap.cc #44771

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1532,28 +1532,18 @@ void AfterGetNameInfo(uv_getnameinfo_t* req,
req_wrap->MakeCallback(env->oncomplete_string(), arraysize(argv), argv);
}

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;
}

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;
int af;
unsigned char result[sizeof(ares_addr_port_node::addr)];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unsigned char result[sizeof(ares_addr_port_node::addr)];
unsigned char result[16]; // 128 bits IPv6 address.

Just a suggestion. Using sizeof isn't wrong but a literal may be clearer.

if (uv_inet_pton(af = AF_INET, *ip, result) != 0 &&
uv_inet_pton(af = AF_INET6, *ip, result) != 0)
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)));
CHECK_EQ(0, uv_inet_ntop(af, result, canonical_ip, sizeof(canonical_ip)));
Local<String> val = String::NewFromUtf8(isolate, canonical_ip)
.ToLocalChecked();
args.GetReturnValue().Set(val);
Expand Down