/* 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);
static int make_listen_fd(const struct addrinfo *addrinfo)
{
- int saved_errno, fd, on = 1;
+ int fd, on = 1;
fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
addrinfo->ai_protocol);
if (fd < 0)
return -1;
- setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
+ goto fail;
+
if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0)
goto fail;
return fd;
fail:
- saved_errno = errno;
- close(fd);
- errno = saved_errno;
+ close_noerr(fd);
return -1;
}