summaryrefslogtreecommitdiff
path: root/src/cares_wrap.cc
diff options
context:
space:
mode:
authorBen Noordhuis <info@bnoordhuis.nl>2018-01-26 18:39:10 +0100
committerBen Noordhuis <info@bnoordhuis.nl>2018-01-29 14:44:59 +0100
commit1598ec73dff10631847b7b7f31a661ac3efb8e7d (patch)
treec1db6969c2f94d3923da2d9dc640ca0a08fbb398 /src/cares_wrap.cc
parent98d1110721faf0a5c62f5402e863f2c203783677 (diff)
downloadandroid-node-v8-1598ec73dff10631847b7b7f31a661ac3efb8e7d.tar.gz
android-node-v8-1598ec73dff10631847b7b7f31a661ac3efb8e7d.tar.bz2
android-node-v8-1598ec73dff10631847b7b7f31a661ac3efb8e7d.zip
src: DRY ip address parsing code in cares_wrap.cc
PR-URL: https://github.com/nodejs/node/pull/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>
Diffstat (limited to 'src/cares_wrap.cc')
-rw-r--r--src/cares_wrap.cc54
1 files changed, 18 insertions, 36 deletions
diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc
index de3cb8f89c..165a8cda20 100644
--- a/src/cares_wrap.cc
+++ b/src/cares_wrap.cc
@@ -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));
}