handle EALREADY and EINPROGRESS while connecting; fixes #3168
Costa Tsaousis (ktsaou) committed
Jan 3, 2018 at 15:17 UTC
6962a7a2b2407485b7c51495c179722b42c19de8
1 file changed
+30
-4
src/socket.c
+30
-4
@@ -615,13 +615,39 @@ static inline int connect_to_this_ip46(int protocol, int socktype, const char *h
615
error("Failed to set timeout on the socket to ip '%s' port '%s'", hostBfr, servBfr);
616
}
617
618
+ errno = 0;
619
if(connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
619
- error("Failed to connect to '%s', port '%s'", hostBfr, servBfr);
620
- close(fd);
621
- fd = -1;
620
+ if(errno == EALREADY || errno == EINPROGRESS) {
621
+ info("Waiting for connection to ip %s port %s to be established", hostBfr, servBfr);
622
+
623
+ fd_set fds;
624
+ FD_ZERO(&fds);
625
+ FD_SET(0, &fds);
626
+ int rc = select (1, NULL, &fds, NULL, timeout);
627
+
628
+ if(rc > 0 && FD_ISSET(fd, &fds)) {
629
+ info("connect() to ip %s port %s completed successfully", hostBfr, servBfr);
630
+ }
631
+ else if(rc == -1) {
632
+ error("Failed to connect to '%s', port '%s'. select() returned %d", hostBfr, servBfr, rc);
633
+ close(fd);
634
+ fd = -1;
635
+ }
636
+ else {
637
+ error("Timed out while connecting to '%s', port '%s'. select() returned %d", hostBfr, servBfr, rc);
638
+ close(fd);
639
+ fd = -1;
640
+ }
641
+ }
642
+ else {
643
+ error("Failed to connect to '%s', port '%s'", hostBfr, servBfr);
644
+ close(fd);
645
+ fd = -1;
646
+ }
647
}
648
624
- debug(D_CONNECT_TO, "Connected to '%s' on port '%s'.", hostBfr, servBfr);
649
+ if(fd != -1)
650
+ debug(D_CONNECT_TO, "Connected to '%s' on port '%s'.", hostBfr, servBfr);
651
}
652
}
653