@@ -196,15 +196,23 @@ void ChannelWrap::New(const FunctionCallbackInfo<Value>& args) {
196
196
197
197
class GetAddrInfoReqWrap : public ReqWrap <uv_getaddrinfo_t > {
198
198
public:
199
- GetAddrInfoReqWrap (Environment* env, Local<Object> req_wrap_obj);
199
+ GetAddrInfoReqWrap (Environment* env,
200
+ Local<Object> req_wrap_obj,
201
+ bool verbatim);
200
202
~GetAddrInfoReqWrap ();
201
203
202
204
size_t self_size () const override { return sizeof (*this ); }
205
+ bool verbatim () const { return verbatim_; }
206
+
207
+ private:
208
+ const bool verbatim_;
203
209
};
204
210
205
211
GetAddrInfoReqWrap::GetAddrInfoReqWrap (Environment* env,
206
- Local<Object> req_wrap_obj)
207
- : ReqWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_GETADDRINFOREQWRAP) {
212
+ Local<Object> req_wrap_obj,
213
+ bool verbatim)
214
+ : ReqWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_GETADDRINFOREQWRAP)
215
+ , verbatim_(verbatim) {
208
216
Wrap (req_wrap_obj, this );
209
217
}
210
218
@@ -1811,70 +1819,38 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) {
1811
1819
};
1812
1820
1813
1821
if (status == 0 ) {
1814
- // Success
1815
- struct addrinfo *address;
1816
1822
int n = 0 ;
1817
-
1818
- // Create the response array.
1819
1823
Local<Array> results = Array::New (env->isolate ());
1820
1824
1821
- char ip[INET6_ADDRSTRLEN];
1822
- const char *addr;
1823
-
1824
- // Iterate over the IPv4 responses again this time creating javascript
1825
- // strings for each IP and filling the results array.
1826
- address = res;
1827
- while (address) {
1828
- CHECK_EQ (address->ai_socktype , SOCK_STREAM);
1829
-
1830
- // Ignore random ai_family types.
1831
- if (address->ai_family == AF_INET) {
1832
- // Juggle pointers
1833
- addr = reinterpret_cast <char *>(&(reinterpret_cast <struct sockaddr_in *>(
1834
- address->ai_addr )->sin_addr ));
1835
- int err = uv_inet_ntop (address->ai_family ,
1836
- addr,
1837
- ip,
1838
- INET6_ADDRSTRLEN);
1839
- if (err)
1825
+ auto add = [&] (bool want_ipv4, bool want_ipv6) {
1826
+ for (auto p = res; p != nullptr ; p = p->ai_next ) {
1827
+ CHECK_EQ (p->ai_socktype , SOCK_STREAM);
1828
+
1829
+ const char * addr;
1830
+ if (want_ipv4 && p->ai_family == AF_INET) {
1831
+ addr = reinterpret_cast <char *>(
1832
+ &(reinterpret_cast <struct sockaddr_in *>(p->ai_addr )->sin_addr ));
1833
+ } else if (want_ipv6 && p->ai_family == AF_INET6) {
1834
+ addr = reinterpret_cast <char *>(
1835
+ &(reinterpret_cast <struct sockaddr_in6 *>(p->ai_addr )->sin6_addr ));
1836
+ } else {
1840
1837
continue ;
1838
+ }
1841
1839
1842
- // Create JavaScript string
1843
- Local<String> s = OneByteString (env->isolate (), ip);
1844
- results->Set (n, s);
1845
- n++;
1846
- }
1847
-
1848
- // Increment
1849
- address = address->ai_next ;
1850
- }
1851
-
1852
- // Iterate over the IPv6 responses putting them in the array.
1853
- address = res;
1854
- while (address) {
1855
- CHECK_EQ (address->ai_socktype , SOCK_STREAM);
1856
-
1857
- // Ignore random ai_family types.
1858
- if (address->ai_family == AF_INET6) {
1859
- // Juggle pointers
1860
- addr = reinterpret_cast <char *>(&(reinterpret_cast <struct sockaddr_in6 *>(
1861
- address->ai_addr )->sin6_addr ));
1862
- int err = uv_inet_ntop (address->ai_family ,
1863
- addr,
1864
- ip,
1865
- INET6_ADDRSTRLEN);
1866
- if (err)
1840
+ char ip[INET6_ADDRSTRLEN];
1841
+ if (uv_inet_ntop (p->ai_family , addr, ip, sizeof (ip)))
1867
1842
continue ;
1868
1843
1869
- // Create JavaScript string
1870
1844
Local<String> s = OneByteString (env->isolate (), ip);
1871
1845
results->Set (n, s);
1872
1846
n++;
1873
1847
}
1848
+ };
1874
1849
1875
- // Increment
1876
- address = address->ai_next ;
1877
- }
1850
+ const bool verbatim = req_wrap->verbatim ();
1851
+ add (true , verbatim);
1852
+ if (verbatim == false )
1853
+ add (false , true );
1878
1854
1879
1855
// No responses were found to return
1880
1856
if (n == 0 ) {
@@ -1965,6 +1941,7 @@ void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
1965
1941
CHECK (args[0 ]->IsObject ());
1966
1942
CHECK (args[1 ]->IsString ());
1967
1943
CHECK (args[2 ]->IsInt32 ());
1944
+ CHECK (args[4 ]->IsBoolean ());
1968
1945
Local<Object> req_wrap_obj = args[0 ].As <Object>();
1969
1946
node::Utf8Value hostname (env->isolate (), args[1 ]);
1970
1947
@@ -1985,7 +1962,7 @@ void GetAddrInfo(const FunctionCallbackInfo<Value>& args) {
1985
1962
CHECK (0 && " bad address family" );
1986
1963
}
1987
1964
1988
- GetAddrInfoReqWrap* req_wrap = new GetAddrInfoReqWrap (env, req_wrap_obj);
1965
+ auto req_wrap = new GetAddrInfoReqWrap (env, req_wrap_obj, args[ 4 ]-> IsTrue () );
1989
1966
1990
1967
struct addrinfo hints;
1991
1968
memset (&hints, 0 , sizeof (struct addrinfo ));
0 commit comments