@@ -3296,81 +3296,83 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
3296
3296
return INVALID_SOCKET;
3297
3297
}
3298
3298
3299
- for (auto rp = result; rp; rp = rp->ai_next ) {
3300
- // Create a socket
3299
+ // Create a socket
3301
3300
#ifdef _WIN32
3302
- auto sock =
3303
- WSASocketW (rp ->ai_family , rp ->ai_socktype , rp ->ai_protocol , nullptr , 0 ,
3304
- WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED);
3305
- /* *
3306
- * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1
3307
- * and above the socket creation fails on older Windows Systems.
3308
- *
3309
- * Let's try to create a socket the old way in this case.
3310
- *
3311
- * Reference:
3312
- * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa
3313
- *
3314
- * WSA_FLAG_NO_HANDLE_INHERIT:
3315
- * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with
3316
- * SP1, and later
3317
- *
3318
- */
3319
- if (sock == INVALID_SOCKET) {
3320
- sock = socket (rp ->ai_family , rp ->ai_socktype , rp ->ai_protocol );
3321
- }
3301
+ auto sock =
3302
+ WSASocketW (result ->ai_family , result ->ai_socktype , result ->ai_protocol ,
3303
+ nullptr , 0 , WSA_FLAG_NO_HANDLE_INHERIT | WSA_FLAG_OVERLAPPED);
3304
+ /* *
3305
+ * Since the WSA_FLAG_NO_HANDLE_INHERIT is only supported on Windows 7 SP1
3306
+ * and above the socket creation fails on older Windows Systems.
3307
+ *
3308
+ * Let's try to create a socket the old way in this case.
3309
+ *
3310
+ * Reference:
3311
+ * https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsasocketa
3312
+ *
3313
+ * WSA_FLAG_NO_HANDLE_INHERIT:
3314
+ * This flag is supported on Windows 7 with SP1, Windows Server 2008 R2 with
3315
+ * SP1, and later
3316
+ *
3317
+ */
3318
+ if (sock == INVALID_SOCKET) {
3319
+ sock = socket (result ->ai_family , result ->ai_socktype , result ->ai_protocol );
3320
+ }
3322
3321
#else
3323
3322
3324
3323
#ifdef SOCK_CLOEXEC
3325
- auto sock =
3326
- socket (rp-> ai_family , rp-> ai_socktype | SOCK_CLOEXEC, rp ->ai_protocol );
3324
+ auto sock = socket (result-> ai_family , result-> ai_socktype | SOCK_CLOEXEC,
3325
+ result ->ai_protocol );
3327
3326
#else
3328
- auto sock = socket (rp->ai_family , rp->ai_socktype , rp->ai_protocol );
3327
+ auto sock =
3328
+ socket (result->ai_family , result->ai_socktype , result->ai_protocol );
3329
3329
#endif
3330
3330
3331
3331
#endif
3332
- if (sock == INVALID_SOCKET) { continue ; }
3332
+ if (sock == INVALID_SOCKET) {
3333
+ freeaddrinfo (result);
3334
+ return INVALID_SOCKET;
3335
+ }
3333
3336
3334
3337
#if !defined _WIN32 && !defined SOCK_CLOEXEC
3335
- if (fcntl (sock, F_SETFD, FD_CLOEXEC) == -1 ) {
3336
- close_socket (sock);
3337
- continue ;
3338
- }
3338
+ if (fcntl (sock, F_SETFD, FD_CLOEXEC) == -1 ) {
3339
+ close_socket (sock);
3340
+ freeaddrinfo (result);
3341
+ return INVALID_SOCKET;
3342
+ }
3339
3343
#endif
3340
3344
3341
- if (tcp_nodelay) {
3342
- auto yes = 1 ;
3345
+ if (tcp_nodelay) {
3346
+ auto yes = 1 ;
3343
3347
#ifdef _WIN32
3344
- setsockopt (sock, IPPROTO_TCP, TCP_NODELAY,
3345
- reinterpret_cast <const char *>(&yes), sizeof (yes));
3348
+ setsockopt (sock, IPPROTO_TCP, TCP_NODELAY,
3349
+ reinterpret_cast <const char *>(&yes), sizeof (yes));
3346
3350
#else
3347
- setsockopt (sock, IPPROTO_TCP, TCP_NODELAY,
3348
- reinterpret_cast <const void *>(&yes), sizeof (yes));
3351
+ setsockopt (sock, IPPROTO_TCP, TCP_NODELAY,
3352
+ reinterpret_cast <const void *>(&yes), sizeof (yes));
3349
3353
#endif
3350
- }
3354
+ }
3351
3355
3352
- if (socket_options) { socket_options (sock); }
3356
+ if (socket_options) { socket_options (sock); }
3353
3357
3354
- if (rp ->ai_family == AF_INET6) {
3355
- auto no = 0 ;
3358
+ if (result ->ai_family == AF_INET6) {
3359
+ auto no = 0 ;
3356
3360
#ifdef _WIN32
3357
- setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
3358
- reinterpret_cast <const char *>(&no), sizeof (no));
3361
+ setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
3362
+ reinterpret_cast <const char *>(&no), sizeof (no));
3359
3363
#else
3360
- setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
3361
- reinterpret_cast <const void *>(&no), sizeof (no));
3364
+ setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY,
3365
+ reinterpret_cast <const void *>(&no), sizeof (no));
3362
3366
#endif
3363
- }
3364
-
3365
- // bind or connect
3366
- if (bind_or_connect (sock, *rp)) {
3367
- freeaddrinfo (result);
3368
- return sock;
3369
- }
3367
+ }
3370
3368
3371
- close_socket (sock);
3369
+ // bind or connect
3370
+ if (bind_or_connect (sock, *result)) {
3371
+ freeaddrinfo (result);
3372
+ return sock;
3372
3373
}
3373
3374
3375
+ close_socket (sock);
3374
3376
freeaddrinfo (result);
3375
3377
return INVALID_SOCKET;
3376
3378
}
0 commit comments