X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fnet%2Fnet.c;h=f57b8dd6ea1b4becf24763b351ea093586635a56;hb=7b11e7441018fa9e260fa7bd2e67864a1b2c5d94;hp=4c84ba7ac3b1702ac0467bb7e3fd29244eecf298;hpb=ac7f55756015e12dc3e00f4c20995ea31670caef;p=ccan diff --git a/ccan/net/net.c b/ccan/net/net.c index 4c84ba7a..f57b8dd6 100644 --- a/ccan/net/net.c +++ b/ccan/net/net.c @@ -1,10 +1,7 @@ /* Licensed under BSD-MIT - see LICENSE file for details */ #include #include -#include -#include #include -#include #include #include #include @@ -78,7 +75,7 @@ close: } -int net_connect_async(const struct addrinfo *addrinfo, struct pollfd pfds[2]) +int net_connect_async(const struct addrinfo *addrinfo, struct pollfd *pfds) { const struct addrinfo *addr[2] = { NULL, NULL }; unsigned int i; @@ -125,7 +122,7 @@ int net_connect_async(const struct addrinfo *addrinfo, struct pollfd pfds[2]) return -1; } -void net_connect_abort(struct pollfd pfds[2]) +void net_connect_abort(struct pollfd *pfds) { unsigned int i; @@ -136,7 +133,7 @@ void net_connect_abort(struct pollfd pfds[2]) } } -int net_connect_complete(struct pollfd pfds[2]) +int net_connect_complete(struct pollfd *pfds) { unsigned int i; @@ -159,6 +156,9 @@ int net_connect_complete(struct pollfd pfds[2]) } continue; } + if (!(pfds[i].revents & POLLOUT)) + continue; + if (getsockopt(pfds[i].fd, SOL_SOCKET, SO_ERROR, &err, &errlen) != 0) { net_connect_abort(pfds); @@ -231,14 +231,16 @@ static bool should_listen(const struct addrinfo *addrinfo) 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; @@ -247,9 +249,7 @@ static int make_listen_fd(const struct addrinfo *addrinfo) return fd; fail: - saved_errno = errno; - close(fd); - errno = saved_errno; + close_noerr(fd); return -1; }