X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Fnet%2Fnet.c;h=e9199839936e85aebb93b0bcd486669b150d73da;hb=cedcaa1bbb44cad610973c449340c0fde9ae0280;hp=d2eaa9ecb45e58660fb80ea607210226d3385a7f;hpb=641b511049e5c03d45ada0c3fd829691b173e5d1;p=ccan diff --git a/ccan/net/net.c b/ccan/net/net.c index d2eaa9ec..e9199839 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 @@ -116,7 +113,7 @@ int net_connect_async(const struct addrinfo *addrinfo, struct pollfd pfds[2]) close_noerr(pfds[i].fd); return -1; } - return pfds[0].fd; + return pfds[i].fd; } } @@ -148,6 +145,20 @@ int net_connect_complete(struct pollfd pfds[2]) 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); @@ -220,7 +231,7 @@ 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); @@ -236,9 +247,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; }