7
7
8
8
#include < sync.h>
9
9
#include < tinyformat.h>
10
- #include < util/system.h>
11
10
#include < util/strencodings.h>
11
+ #include < util/string.h>
12
+ #include < util/system.h>
12
13
13
14
#include < atomic>
14
15
@@ -59,10 +60,14 @@ std::string GetNetworkName(enum Network net) {
59
60
}
60
61
}
61
62
62
- bool static LookupIntern (const char *pszName , std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup )
63
+ bool static LookupIntern (const std::string& name , std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup )
63
64
{
64
65
vIP.clear ();
65
66
67
+ if (!ValidAsCString (name)) {
68
+ return false ;
69
+ }
70
+
66
71
{
67
72
CNetAddr addr;
68
73
// From our perspective, onion addresses are not hostnames but rather
@@ -71,7 +76,7 @@ bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsign
71
76
// getaddrinfo to decode them and it wouldn't make sense to resolve
72
77
// them, we return a network address representing it instead. See
73
78
// CNetAddr::SetSpecial(const std::string&) for more details.
74
- if (addr.SetSpecial (std::string (pszName) )) {
79
+ if (addr.SetSpecial (name )) {
75
80
vIP.push_back (addr);
76
81
return true ;
77
82
}
@@ -93,7 +98,7 @@ bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsign
93
98
// hostname lookups.
94
99
aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
95
100
struct addrinfo *aiRes = nullptr ;
96
- int nErr = getaddrinfo (pszName , nullptr , &aiHint, &aiRes);
101
+ int nErr = getaddrinfo (name. c_str () , nullptr , &aiHint, &aiRes);
97
102
if (nErr)
98
103
return false ;
99
104
@@ -131,7 +136,7 @@ bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsign
131
136
/* *
132
137
* Resolve a host string to its corresponding network addresses.
133
138
*
134
- * @param pszName The string representing a host. Could be a name or a numerical
139
+ * @param name The string representing a host. Could be a name or a numerical
135
140
* IP address (IPv6 addresses in their bracketed form are
136
141
* allowed).
137
142
* @param[out] vIP The resulting network addresses to which the specified host
@@ -143,28 +148,34 @@ bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsign
143
148
* @see Lookup(const char *, std::vector<CService>&, int, bool, unsigned int)
144
149
* for additional parameter descriptions.
145
150
*/
146
- bool LookupHost (const char *pszName , std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup )
151
+ bool LookupHost (const std::string& name , std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup )
147
152
{
148
- std::string strHost (pszName);
153
+ if (!ValidAsCString (name)) {
154
+ return false ;
155
+ }
156
+ std::string strHost = name;
149
157
if (strHost.empty ())
150
158
return false ;
151
159
if (strHost.front () == ' [' && strHost.back () == ' ]' ) {
152
160
strHost = strHost.substr (1 , strHost.size () - 2 );
153
161
}
154
162
155
- return LookupIntern (strHost. c_str () , vIP, nMaxSolutions, fAllowLookup );
163
+ return LookupIntern (strHost, vIP, nMaxSolutions, fAllowLookup );
156
164
}
157
165
158
166
/* *
159
167
* Resolve a host string to its first corresponding network address.
160
168
*
161
- * @see LookupHost(const char * , std::vector<CNetAddr>&, unsigned int, bool) for
169
+ * @see LookupHost(const std::string& , std::vector<CNetAddr>&, unsigned int, bool) for
162
170
* additional parameter descriptions.
163
171
*/
164
- bool LookupHost (const char *pszName , CNetAddr& addr, bool fAllowLookup )
172
+ bool LookupHost (const std::string& name , CNetAddr& addr, bool fAllowLookup )
165
173
{
174
+ if (!ValidAsCString (name)) {
175
+ return false ;
176
+ }
166
177
std::vector<CNetAddr> vIP;
167
- LookupHost (pszName , vIP, 1 , fAllowLookup );
178
+ LookupHost (name , vIP, 1 , fAllowLookup );
168
179
if (vIP.empty ())
169
180
return false ;
170
181
addr = vIP.front ();
@@ -174,7 +185,7 @@ bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup)
174
185
/* *
175
186
* Resolve a service string to its corresponding service.
176
187
*
177
- * @param pszName The string representing a service. Could be a name or a
188
+ * @param name The string representing a service. Could be a name or a
178
189
* numerical IP address (IPv6 addresses should be in their
179
190
* disambiguated bracketed form), optionally followed by a port
180
191
* number. (e.g. example.com:8333 or
@@ -191,16 +202,17 @@ bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup)
191
202
* @returns Whether or not the service string successfully resolved to any
192
203
* resulting services.
193
204
*/
194
- bool Lookup (const char *pszName , std::vector<CService>& vAddr, int portDefault, bool fAllowLookup , unsigned int nMaxSolutions)
205
+ bool Lookup (const std::string& name , std::vector<CService>& vAddr, int portDefault, bool fAllowLookup , unsigned int nMaxSolutions)
195
206
{
196
- if (pszName[ 0 ] == 0 )
207
+ if (name. empty () || ! ValidAsCString (name)) {
197
208
return false ;
209
+ }
198
210
int port = portDefault;
199
211
std::string hostname;
200
- SplitHostPort (std::string (pszName) , port, hostname);
212
+ SplitHostPort (name , port, hostname);
201
213
202
214
std::vector<CNetAddr> vIP;
203
- bool fRet = LookupIntern (hostname. c_str () , vIP, nMaxSolutions, fAllowLookup );
215
+ bool fRet = LookupIntern (hostname, vIP, nMaxSolutions, fAllowLookup );
204
216
if (!fRet )
205
217
return false ;
206
218
vAddr.resize (vIP.size ());
@@ -215,10 +227,13 @@ bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault,
215
227
* @see Lookup(const char *, std::vector<CService>&, int, bool, unsigned int)
216
228
* for additional parameter descriptions.
217
229
*/
218
- bool Lookup (const char *pszName , CService& addr, int portDefault, bool fAllowLookup )
230
+ bool Lookup (const std::string& name , CService& addr, int portDefault, bool fAllowLookup )
219
231
{
232
+ if (!ValidAsCString (name)) {
233
+ return false ;
234
+ }
220
235
std::vector<CService> vService;
221
- bool fRet = Lookup (pszName , vService, portDefault, fAllowLookup , 1 );
236
+ bool fRet = Lookup (name , vService, portDefault, fAllowLookup , 1 );
222
237
if (!fRet )
223
238
return false ;
224
239
addr = vService[0 ];
@@ -235,12 +250,15 @@ bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLoo
235
250
* @see Lookup(const char *, CService&, int, bool) for additional parameter
236
251
* descriptions.
237
252
*/
238
- CService LookupNumeric (const char *pszName , int portDefault)
253
+ CService LookupNumeric (const std::string& name , int portDefault)
239
254
{
255
+ if (!ValidAsCString (name)) {
256
+ return {};
257
+ }
240
258
CService addr;
241
259
// "1.2:345" will fail to resolve the ip, but will still set the port.
242
260
// If the ip fails to resolve, re-init the result.
243
- if (!Lookup (pszName , addr, portDefault, false ))
261
+ if (!Lookup (name , addr, portDefault, false ))
244
262
addr = CService ();
245
263
return addr;
246
264
}
@@ -768,12 +786,11 @@ bool IsProxy(const CNetAddr &addr) {
768
786
*
769
787
* @returns Whether or not the operation succeeded.
770
788
*/
771
- bool ConnectThroughProxy (const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocket, int nTimeout, bool * outProxyConnectionFailed)
789
+ bool ConnectThroughProxy (const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocket, int nTimeout, bool & outProxyConnectionFailed)
772
790
{
773
791
// first connect to proxy server
774
792
if (!ConnectSocketDirectly (proxy.proxy , hSocket, nTimeout, true )) {
775
- if (outProxyConnectionFailed)
776
- *outProxyConnectionFailed = true ;
793
+ outProxyConnectionFailed = true ;
777
794
return false ;
778
795
}
779
796
// do socks negotiation
@@ -796,23 +813,25 @@ bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int
796
813
* Parse and resolve a specified subnet string into the appropriate internal
797
814
* representation.
798
815
*
799
- * @param pszName A string representation of a subnet of the form `network
816
+ * @param strSubnet A string representation of a subnet of the form `network
800
817
* address [ "/", ( CIDR-style suffix | netmask ) ]`(e.g.
801
818
* `2001:db8::/32`, `192.0.2.0/255.255.255.0`, or `8.8.8.8`).
802
819
* @param ret The resulting internal representation of a subnet.
803
820
*
804
821
* @returns Whether the operation succeeded or not.
805
822
*/
806
- bool LookupSubNet (const char * pszName , CSubNet& ret)
823
+ bool LookupSubNet (const std::string& strSubnet , CSubNet& ret)
807
824
{
808
- std::string strSubnet (pszName);
825
+ if (!ValidAsCString (strSubnet)) {
826
+ return false ;
827
+ }
809
828
size_t slash = strSubnet.find_last_of (' /' );
810
829
std::vector<CNetAddr> vIP;
811
830
812
831
std::string strAddress = strSubnet.substr (0 , slash);
813
- // TODO: Use LookupHost(const char * , CNetAddr&, bool) instead to just get
832
+ // TODO: Use LookupHost(const std::string& , CNetAddr&, bool) instead to just get
814
833
// one CNetAddr.
815
- if (LookupHost (strAddress. c_str () , vIP, 1 , false ))
834
+ if (LookupHost (strAddress, vIP, 1 , false ))
816
835
{
817
836
CNetAddr network = vIP[0 ];
818
837
if (slash != strSubnet.npos )
@@ -827,7 +846,7 @@ bool LookupSubNet(const char* pszName, CSubNet& ret)
827
846
else // If not a valid number, try full netmask syntax
828
847
{
829
848
// Never allow lookup for netmask
830
- if (LookupHost (strNetmask. c_str () , vIP, 1 , false )) {
849
+ if (LookupHost (strNetmask, vIP, 1 , false )) {
831
850
ret = CSubNet (network, vIP[0 ]);
832
851
return ret.IsValid ();
833
852
}
0 commit comments