Skip to content

Commit 84f8e62

Browse files
bnoordhuisevanlucas
authored andcommitted
src: DRY ip address parsing code in cares_wrap.cc
PR-URL: #18398 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 2efa7d1 commit 84f8e62

File tree

1 file changed

+18
-36
lines changed

1 file changed

+18
-36
lines changed

src/cares_wrap.cc

+18-36
Original file line numberDiff line numberDiff line change
@@ -1876,60 +1876,42 @@ void AfterGetNameInfo(uv_getnameinfo_t* req,
18761876
delete req_wrap;
18771877
}
18781878

1879+
using ParseIPResult = decltype(static_cast<ares_addr_port_node*>(0)->addr);
1880+
1881+
int ParseIP(const char* ip, ParseIPResult* result = nullptr) {
1882+
ParseIPResult tmp;
1883+
if (result == nullptr) result = &tmp;
1884+
if (0 == uv_inet_pton(AF_INET, ip, result)) return 4;
1885+
if (0 == uv_inet_pton(AF_INET6, ip, result)) return 6;
1886+
return 0;
1887+
}
18791888

18801889
void IsIP(const FunctionCallbackInfo<Value>& args) {
18811890
node::Utf8Value ip(args.GetIsolate(), args[0]);
1882-
char address_buffer[sizeof(struct in6_addr)];
1883-
1884-
int rc = 0;
1885-
if (uv_inet_pton(AF_INET, *ip, &address_buffer) == 0)
1886-
rc = 4;
1887-
else if (uv_inet_pton(AF_INET6, *ip, &address_buffer) == 0)
1888-
rc = 6;
1889-
1890-
args.GetReturnValue().Set(rc);
1891+
args.GetReturnValue().Set(ParseIP(*ip));
18911892
}
18921893

18931894
void IsIPv4(const FunctionCallbackInfo<Value>& args) {
18941895
node::Utf8Value ip(args.GetIsolate(), args[0]);
1895-
char address_buffer[sizeof(struct in_addr)];
1896-
1897-
if (uv_inet_pton(AF_INET, *ip, &address_buffer) == 0) {
1898-
args.GetReturnValue().Set(true);
1899-
} else {
1900-
args.GetReturnValue().Set(false);
1901-
}
1896+
args.GetReturnValue().Set(4 == ParseIP(*ip));
19021897
}
19031898

19041899
void IsIPv6(const FunctionCallbackInfo<Value>& args) {
19051900
node::Utf8Value ip(args.GetIsolate(), args[0]);
1906-
char address_buffer[sizeof(struct in6_addr)];
1907-
1908-
if (uv_inet_pton(AF_INET6, *ip, &address_buffer) == 0) {
1909-
args.GetReturnValue().Set(true);
1910-
} else {
1911-
args.GetReturnValue().Set(false);
1912-
}
1901+
args.GetReturnValue().Set(6 == ParseIP(*ip));
19131902
}
19141903

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

1921-
int af;
1922-
if (uv_inet_pton(AF_INET, *ip, &address_buffer) == 0)
1923-
af = AF_INET;
1924-
else if (uv_inet_pton(AF_INET6, *ip, &address_buffer) == 0)
1925-
af = AF_INET6;
1926-
else
1927-
return;
1928-
1929-
int err = uv_inet_ntop(af, address_buffer, canonical_ip,
1930-
sizeof(canonical_ip));
1931-
CHECK_EQ(err, 0);
1908+
ParseIPResult result;
1909+
const int rc = ParseIP(*ip, &result);
1910+
if (rc == 0) return;
19321911

1912+
char canonical_ip[INET6_ADDRSTRLEN];
1913+
const int af = (rc == 4 ? AF_INET : AF_INET6);
1914+
CHECK_EQ(0, uv_inet_ntop(af, &result, canonical_ip, sizeof(canonical_ip)));
19331915
args.GetReturnValue().Set(String::NewFromUtf8(isolate, canonical_ip));
19341916
}
19351917

0 commit comments

Comments
 (0)