@@ -1876,60 +1876,42 @@ void AfterGetNameInfo(uv_getnameinfo_t* req,
1876
1876
delete req_wrap;
1877
1877
}
1878
1878
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
+ }
1879
1888
1880
1889
void IsIP (const FunctionCallbackInfo<Value>& args) {
1881
1890
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));
1891
1892
}
1892
1893
1893
1894
void IsIPv4 (const FunctionCallbackInfo<Value>& args) {
1894
1895
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));
1902
1897
}
1903
1898
1904
1899
void IsIPv6 (const FunctionCallbackInfo<Value>& args) {
1905
1900
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));
1913
1902
}
1914
1903
1915
1904
void CanonicalizeIP (const FunctionCallbackInfo<Value>& args) {
1916
1905
v8::Isolate* isolate = args.GetIsolate ();
1917
1906
node::Utf8Value ip (isolate, args[0 ]);
1918
- char address_buffer[sizeof (struct in6_addr )];
1919
- char canonical_ip[INET6_ADDRSTRLEN];
1920
1907
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 ;
1932
1911
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)));
1933
1915
args.GetReturnValue ().Set (String::NewFromUtf8 (isolate, canonical_ip));
1934
1916
}
1935
1917
0 commit comments