@@ -42,6 +42,50 @@ bool fNameLookup = DEFAULT_NAME_LOOKUP;
42
42
int g_socks5_recv_timeout = 20 * 1000 ;
43
43
static std::atomic<bool > interruptSocks5Recv (false );
44
44
45
+ std::vector<CNetAddr> WrappedGetAddrInfo (const std::string& name, bool allow_lookup)
46
+ {
47
+ addrinfo ai_hint{};
48
+ // We want a TCP port, which is a streaming socket type
49
+ ai_hint.ai_socktype = SOCK_STREAM;
50
+ ai_hint.ai_protocol = IPPROTO_TCP;
51
+ // We don't care which address family (IPv4 or IPv6) is returned
52
+ ai_hint.ai_family = AF_UNSPEC;
53
+ // If we allow lookups of hostnames, use the AI_ADDRCONFIG flag to only
54
+ // return addresses whose family we have an address configured for.
55
+ //
56
+ // If we don't allow lookups, then use the AI_NUMERICHOST flag for
57
+ // getaddrinfo to only decode numerical network addresses and suppress
58
+ // hostname lookups.
59
+ ai_hint.ai_flags = allow_lookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
60
+
61
+ addrinfo* ai_res{nullptr };
62
+ const int n_err{getaddrinfo (name.c_str (), nullptr , &ai_hint, &ai_res)};
63
+ if (n_err != 0 ) {
64
+ return {};
65
+ }
66
+
67
+ // Traverse the linked list starting with ai_trav.
68
+ addrinfo* ai_trav{ai_res};
69
+ std::vector<CNetAddr> resolved_addresses;
70
+ while (ai_trav != nullptr ) {
71
+ if (ai_trav->ai_family == AF_INET) {
72
+ assert (ai_trav->ai_addrlen >= sizeof (sockaddr_in));
73
+ resolved_addresses.emplace_back (reinterpret_cast <sockaddr_in*>(ai_trav->ai_addr )->sin_addr );
74
+ }
75
+ if (ai_trav->ai_family == AF_INET6) {
76
+ assert (ai_trav->ai_addrlen >= sizeof (sockaddr_in6));
77
+ const sockaddr_in6* s6{reinterpret_cast <sockaddr_in6*>(ai_trav->ai_addr )};
78
+ resolved_addresses.emplace_back (s6->sin6_addr , s6->sin6_scope_id );
79
+ }
80
+ ai_trav = ai_trav->ai_next ;
81
+ }
82
+ freeaddrinfo (ai_res);
83
+
84
+ return resolved_addresses;
85
+ }
86
+
87
+ DNSLookupFn g_dns_lookup{WrappedGetAddrInfo};
88
+
45
89
enum Network ParseNetwork (const std::string& net_in) {
46
90
std::string net = ToLower (net_in);
47
91
if (net == " ipv4" ) return NET_IPV4;
@@ -87,7 +131,7 @@ std::vector<std::string> GetNetworkNames(bool append_unroutable)
87
131
return names;
88
132
}
89
133
90
- bool static LookupIntern (const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup )
134
+ static bool LookupIntern (const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup , DNSLookupFn dns_lookup_function )
91
135
{
92
136
vIP.clear ();
93
137
@@ -109,54 +153,16 @@ bool static LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, un
109
153
}
110
154
}
111
155
112
- struct addrinfo aiHint;
113
- memset (&aiHint, 0 , sizeof (struct addrinfo ));
114
-
115
- // We want a TCP port, which is a streaming socket type
116
- aiHint.ai_socktype = SOCK_STREAM;
117
- aiHint.ai_protocol = IPPROTO_TCP;
118
- // We don't care which address family (IPv4 or IPv6) is returned
119
- aiHint.ai_family = AF_UNSPEC;
120
- // If we allow lookups of hostnames, use the AI_ADDRCONFIG flag to only
121
- // return addresses whose family we have an address configured for.
122
- //
123
- // If we don't allow lookups, then use the AI_NUMERICHOST flag for
124
- // getaddrinfo to only decode numerical network addresses and suppress
125
- // hostname lookups.
126
- aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
127
- struct addrinfo *aiRes = nullptr ;
128
- int nErr = getaddrinfo (name.c_str (), nullptr , &aiHint, &aiRes);
129
- if (nErr)
130
- return false ;
131
-
132
- // Traverse the linked list starting with aiTrav, add all non-internal
133
- // IPv4,v6 addresses to vIP while respecting nMaxSolutions.
134
- struct addrinfo *aiTrav = aiRes;
135
- while (aiTrav != nullptr && (nMaxSolutions == 0 || vIP.size () < nMaxSolutions))
136
- {
137
- CNetAddr resolved;
138
- if (aiTrav->ai_family == AF_INET)
139
- {
140
- assert (aiTrav->ai_addrlen >= sizeof (sockaddr_in));
141
- resolved = CNetAddr (((struct sockaddr_in *)(aiTrav->ai_addr ))->sin_addr );
142
- }
143
-
144
- if (aiTrav->ai_family == AF_INET6)
145
- {
146
- assert (aiTrav->ai_addrlen >= sizeof (sockaddr_in6));
147
- struct sockaddr_in6 * s6 = (struct sockaddr_in6 *) aiTrav->ai_addr ;
148
- resolved = CNetAddr (s6->sin6_addr , s6->sin6_scope_id );
156
+ for (const CNetAddr& resolved : dns_lookup_function (name, fAllowLookup )) {
157
+ if (nMaxSolutions > 0 && vIP.size () >= nMaxSolutions) {
158
+ break ;
149
159
}
150
160
/* Never allow resolving to an internal address. Consider any such result invalid */
151
161
if (!resolved.IsInternal ()) {
152
162
vIP.push_back (resolved);
153
163
}
154
-
155
- aiTrav = aiTrav->ai_next ;
156
164
}
157
165
158
- freeaddrinfo (aiRes);
159
-
160
166
return (vIP.size () > 0 );
161
167
}
162
168
@@ -175,7 +181,7 @@ bool static LookupIntern(const std::string& name, std::vector<CNetAddr>& vIP, un
175
181
* @see Lookup(const char *, std::vector<CService>&, int, bool, unsigned int)
176
182
* for additional parameter descriptions.
177
183
*/
178
- bool LookupHost (const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup )
184
+ bool LookupHost (const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup , DNSLookupFn dns_lookup_function )
179
185
{
180
186
if (!ValidAsCString (name)) {
181
187
return false ;
@@ -187,7 +193,7 @@ bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned in
187
193
strHost = strHost.substr (1 , strHost.size () - 2 );
188
194
}
189
195
190
- return LookupIntern (strHost, vIP, nMaxSolutions, fAllowLookup );
196
+ return LookupIntern (strHost, vIP, nMaxSolutions, fAllowLookup , dns_lookup_function );
191
197
}
192
198
193
199
/* *
@@ -196,13 +202,13 @@ bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned in
196
202
* @see LookupHost(const std::string&, std::vector<CNetAddr>&, unsigned int, bool) for
197
203
* additional parameter descriptions.
198
204
*/
199
- bool LookupHost (const std::string& name, CNetAddr& addr, bool fAllowLookup )
205
+ bool LookupHost (const std::string& name, CNetAddr& addr, bool fAllowLookup , DNSLookupFn dns_lookup_function )
200
206
{
201
207
if (!ValidAsCString (name)) {
202
208
return false ;
203
209
}
204
210
std::vector<CNetAddr> vIP;
205
- LookupHost (name, vIP, 1 , fAllowLookup );
211
+ LookupHost (name, vIP, 1 , fAllowLookup , dns_lookup_function );
206
212
if (vIP.empty ())
207
213
return false ;
208
214
addr = vIP.front ();
@@ -229,7 +235,7 @@ bool LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup)
229
235
* @returns Whether or not the service string successfully resolved to any
230
236
* resulting services.
231
237
*/
232
- bool Lookup (const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup , unsigned int nMaxSolutions)
238
+ bool Lookup (const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup , unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function )
233
239
{
234
240
if (name.empty () || !ValidAsCString (name)) {
235
241
return false ;
@@ -239,7 +245,7 @@ bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefau
239
245
SplitHostPort (name, port, hostname);
240
246
241
247
std::vector<CNetAddr> vIP;
242
- bool fRet = LookupIntern (hostname, vIP, nMaxSolutions, fAllowLookup );
248
+ bool fRet = LookupIntern (hostname, vIP, nMaxSolutions, fAllowLookup , dns_lookup_function );
243
249
if (!fRet )
244
250
return false ;
245
251
vAddr.resize (vIP.size ());
@@ -254,13 +260,13 @@ bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefau
254
260
* @see Lookup(const char *, std::vector<CService>&, int, bool, unsigned int)
255
261
* for additional parameter descriptions.
256
262
*/
257
- bool Lookup (const std::string& name, CService& addr, int portDefault, bool fAllowLookup )
263
+ bool Lookup (const std::string& name, CService& addr, int portDefault, bool fAllowLookup , DNSLookupFn dns_lookup_function )
258
264
{
259
265
if (!ValidAsCString (name)) {
260
266
return false ;
261
267
}
262
268
std::vector<CService> vService;
263
- bool fRet = Lookup (name, vService, portDefault, fAllowLookup , 1 );
269
+ bool fRet = Lookup (name, vService, portDefault, fAllowLookup , 1 , dns_lookup_function );
264
270
if (!fRet )
265
271
return false ;
266
272
addr = vService[0 ];
@@ -277,15 +283,15 @@ bool Lookup(const std::string& name, CService& addr, int portDefault, bool fAllo
277
283
* @see Lookup(const char *, CService&, int, bool) for additional parameter
278
284
* descriptions.
279
285
*/
280
- CService LookupNumeric (const std::string& name, int portDefault)
286
+ CService LookupNumeric (const std::string& name, int portDefault, DNSLookupFn dns_lookup_function )
281
287
{
282
288
if (!ValidAsCString (name)) {
283
289
return {};
284
290
}
285
291
CService addr;
286
292
// "1.2:345" will fail to resolve the ip, but will still set the port.
287
293
// If the ip fails to resolve, re-init the result.
288
- if (!Lookup (name, addr, portDefault, false ))
294
+ if (!Lookup (name, addr, portDefault, false , dns_lookup_function ))
289
295
addr = CService ();
290
296
return addr;
291
297
}
@@ -811,7 +817,7 @@ bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, int
811
817
*
812
818
* @returns Whether the operation succeeded or not.
813
819
*/
814
- bool LookupSubNet (const std::string& strSubnet, CSubNet& ret)
820
+ bool LookupSubNet (const std::string& strSubnet, CSubNet& ret, DNSLookupFn dns_lookup_function )
815
821
{
816
822
if (!ValidAsCString (strSubnet)) {
817
823
return false ;
@@ -822,7 +828,7 @@ bool LookupSubNet(const std::string& strSubnet, CSubNet& ret)
822
828
std::string strAddress = strSubnet.substr (0 , slash);
823
829
// TODO: Use LookupHost(const std::string&, CNetAddr&, bool) instead to just get
824
830
// one CNetAddr.
825
- if (LookupHost (strAddress, vIP, 1 , false ))
831
+ if (LookupHost (strAddress, vIP, 1 , false , dns_lookup_function ))
826
832
{
827
833
CNetAddr network = vIP[0 ];
828
834
if (slash != strSubnet.npos )
@@ -837,7 +843,7 @@ bool LookupSubNet(const std::string& strSubnet, CSubNet& ret)
837
843
else // If not a valid number, try full netmask syntax
838
844
{
839
845
// Never allow lookup for netmask
840
- if (LookupHost (strNetmask, vIP, 1 , false )) {
846
+ if (LookupHost (strNetmask, vIP, 1 , false , dns_lookup_function )) {
841
847
ret = CSubNet (network, vIP[0 ]);
842
848
return ret.IsValid ();
843
849
}
0 commit comments