/* Licensed under BSD-MIT - see LICENSE file for details */
#include <ccan/net/net.h>
#include <ccan/noerr/noerr.h>
-#include <sys/types.h>
-#include <sys/socket.h>
#include <poll.h>
-#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
close_noerr(pfds[i].fd);
return -1;
}
- return pfds[0].fd;
+ return pfds[i].fd;
}
}
if (pfds[i].fd == -1)
continue;
+ if (pfds[i].revents & POLLHUP) {
+ /* Linux gives this if connecting to local
+ * non-listening port */
+ close(pfds[i].fd);
+ pfds[i].fd = -1;
+ if (pfds[!i].fd == -1) {
+ errno = ECONNREFUSED;
+ return -1;
+ }
+ continue;
+ }
+ if (!(pfds[i].revents & POLLOUT))
+ continue;
+
if (getsockopt(pfds[i].fd, SOL_SOCKET, SO_ERROR, &err,
&errlen) != 0) {
net_connect_abort(pfds);