@@ -215,24 +215,17 @@ std::string NetworkUtils::intToIPv4(IPv4 ip) {
215
215
return buf;
216
216
}
217
217
218
- StatusOr<std::vector<HostAddr>> NetworkUtils::toHostAddr (folly::StringPiece ip, int32_t port) {
218
+ StatusOr<std::vector<HostAddr>> NetworkUtils::resolveHost ( const std::string & ip, int32_t port) {
219
219
std::vector<HostAddr> addrs;
220
- IPv4 ipV4;
221
- std::string str = ip.toString ();
222
- if (ipv4ToInt (str, ipV4)) {
223
- addrs.emplace_back (std::move (ipV4), port);
224
- return addrs;
225
- }
226
-
227
220
struct addrinfo hints, *res, *rp;
228
221
::memset (&hints, 0 , sizeof (struct addrinfo ));
229
222
230
223
hints.ai_family = AF_UNSPEC;
231
224
hints.ai_socktype = SOCK_STREAM;
232
225
hints.ai_flags = AI_ADDRCONFIG;
233
226
234
- if (getaddrinfo (str .c_str (), nullptr , &hints, &res) != 0 ) {
235
- return Status::Error (" host not found:%s" , ip.start ());
227
+ if (getaddrinfo (ip .c_str (), nullptr , &hints, &res) != 0 ) {
228
+ return Status::Error (" host not found:%s" , ip.c_str ());
236
229
}
237
230
238
231
for (rp = res; rp != nullptr ; rp = rp->ai_next ) {
@@ -255,26 +248,19 @@ StatusOr<std::vector<HostAddr>> NetworkUtils::toHostAddr(folly::StringPiece ip,
255
248
freeaddrinfo (res);
256
249
257
250
if (addrs.empty ()) {
258
- return Status::Error (" host not found: %s" , str .c_str ());
251
+ return Status::Error (" host not found: %s" , ip .c_str ());
259
252
}
260
253
261
254
return addrs;
262
255
}
263
256
264
- StatusOr<std::vector<HostAddr>> NetworkUtils::toHostAddr (folly::StringPiece ipPort) {
265
- auto pos = ipPort.find (' :' );
266
- if (pos == folly::StringPiece::npos) {
267
- return Status::Error (" Bad peer format: %s" , ipPort.start ());
268
- }
269
257
270
- int32_t port;
271
- try {
272
- port = folly::to<int32_t >(ipPort.subpiece (pos + 1 ));
273
- } catch (const std::exception & ex) {
274
- return Status::Error (" Bad port number, error: %s" , ex.what ());
258
+ StatusOr<HostAddr> NetworkUtils::toHostAddr (const std::string &ip, int32_t port) {
259
+ IPv4 ipV4;
260
+ if (!ipv4ToInt (ip, ipV4)) {
261
+ return Status::Error (" Bad ip format:%s" , ip.c_str ());
275
262
}
276
-
277
- return toHostAddr (ipPort.subpiece (0 , pos), port);
263
+ return std::make_pair (ipV4, port);
278
264
}
279
265
280
266
StatusOr<std::vector<HostAddr>> NetworkUtils::toHosts (const std::string& peersStr) {
@@ -283,12 +269,34 @@ StatusOr<std::vector<HostAddr>> NetworkUtils::toHosts(const std::string& peersSt
283
269
folly::split (" ," , peersStr, peers, true );
284
270
hosts.reserve (peers.size ());
285
271
for (auto & peerStr : peers) {
286
- auto hostAddr = network::NetworkUtils::toHostAddr (folly::trimWhitespace (peerStr));
287
- if (!hostAddr.ok ()) {
288
- return hostAddr.status ();
272
+ auto ipPort = folly::trimWhitespace (peerStr);
273
+ auto pos = ipPort.find (' :' );
274
+ if (pos == folly::StringPiece::npos) {
275
+ return Status::Error (" Bad peer format: %s" , ipPort.start ());
289
276
}
290
- hosts.insert (hosts.end (), std::make_move_iterator (hostAddr.value ().begin ()),
291
- std::make_move_iterator (hostAddr.value ().end ()));
277
+
278
+ int32_t port;
279
+ try {
280
+ port = folly::to<int32_t >(ipPort.subpiece (pos + 1 ));
281
+ } catch (const std::exception & ex) {
282
+ return Status::Error (" Bad port number, error: %s" , ex.what ());
283
+ }
284
+
285
+ auto ipAddr = ipPort.subpiece (0 , pos).toString ();
286
+ auto hostAddr = toHostAddr (ipAddr, port);
287
+ if (hostAddr.ok ()) {
288
+ hosts.emplace_back (hostAddr.value ());
289
+ continue ;
290
+ }
291
+
292
+ auto resolveAddr = resolveHost (ipAddr, port);
293
+ if (resolveAddr.ok ()) {
294
+ hosts.insert (hosts.end (), std::make_move_iterator (resolveAddr.value ().begin ()),
295
+ std::make_move_iterator (resolveAddr.value ().end ()));
296
+ continue ;
297
+ }
298
+
299
+ return resolveAddr.status ();
292
300
}
293
301
return hosts;
294
302
}
0 commit comments