Skip to content

Commit

Permalink
src: DRY ip address parsing code in cares_wrap.cc
Browse files Browse the repository at this point in the history
PR-URL: nodejs#18398
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
  • Loading branch information
bnoordhuis committed Jan 29, 2018
1 parent 98d1110 commit 1598ec7
Showing 1 changed file with 18 additions and 36 deletions.
54 changes: 18 additions & 36 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1876,60 +1876,42 @@ void AfterGetNameInfo(uv_getnameinfo_t* req,
delete req_wrap;
}

using ParseIPResult = decltype(static_cast<ares_addr_port_node*>(0)->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 IsIP(const FunctionCallbackInfo<Value>& args) {
node::Utf8Value ip(args.GetIsolate(), args[0]);
char address_buffer[sizeof(struct in6_addr)];

int rc = 0;
if (uv_inet_pton(AF_INET, *ip, &address_buffer) == 0)
rc = 4;
else if (uv_inet_pton(AF_INET6, *ip, &address_buffer) == 0)
rc = 6;

args.GetReturnValue().Set(rc);
args.GetReturnValue().Set(ParseIP(*ip));
}

void IsIPv4(const FunctionCallbackInfo<Value>& args) {
node::Utf8Value ip(args.GetIsolate(), args[0]);
char address_buffer[sizeof(struct in_addr)];

if (uv_inet_pton(AF_INET, *ip, &address_buffer) == 0) {
args.GetReturnValue().Set(true);
} else {
args.GetReturnValue().Set(false);
}
args.GetReturnValue().Set(4 == ParseIP(*ip));
}

void IsIPv6(const FunctionCallbackInfo<Value>& args) {
node::Utf8Value ip(args.GetIsolate(), args[0]);
char address_buffer[sizeof(struct in6_addr)];

if (uv_inet_pton(AF_INET6, *ip, &address_buffer) == 0) {
args.GetReturnValue().Set(true);
} else {
args.GetReturnValue().Set(false);
}
args.GetReturnValue().Set(6 == ParseIP(*ip));
}

void CanonicalizeIP(const FunctionCallbackInfo<Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
node::Utf8Value ip(isolate, args[0]);
char address_buffer[sizeof(struct in6_addr)];
char canonical_ip[INET6_ADDRSTRLEN];

int af;
if (uv_inet_pton(AF_INET, *ip, &address_buffer) == 0)
af = AF_INET;
else if (uv_inet_pton(AF_INET6, *ip, &address_buffer) == 0)
af = AF_INET6;
else
return;

int err = uv_inet_ntop(af, address_buffer, canonical_ip,
sizeof(canonical_ip));
CHECK_EQ(err, 0);
ParseIPResult result;
const int rc = ParseIP(*ip, &result);
if (rc == 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)));
args.GetReturnValue().Set(String::NewFromUtf8(isolate, canonical_ip));
}

Expand Down

0 comments on commit 1598ec7

Please sign in to comment.