]> git.ozlabs.org Git - ccan/blobdiff - ccan/net/net.c
net: use close_noerr.
[ccan] / ccan / net / net.c
index 394c4e49f2bb16024ea576b0aa68ed1376c25bcc..e9199839936e85aebb93b0bcd486669b150d73da 100644 (file)
@@ -1,10 +1,7 @@
 /* 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>
@@ -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;
                }
        }
 
@@ -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,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);
@@ -247,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;
 }