From 7d355098320a0cbc54001a07e44cf9f7e7a3636c Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 12 Jan 2019 13:26:29 -0600 Subject: Merge xconnect/xbind. --- lib/lib.h | 4 ++-- lib/net.c | 25 +++++++++---------------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/lib/lib.h b/lib/lib.h index bc54a384..3e6838ea 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -305,8 +305,8 @@ int xsocket(int domain, int type, int protocol); void xsetsockopt(int fd, int level, int opt, void *val, socklen_t len); struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype, int protocol, int flags); -int xconnect(struct addrinfo *ai_arg); -int xbind(struct addrinfo *ai_arg); +int xconnect(struct addrinfo *ai); +int xbind(struct addrinfo *ai); int xpoll(struct pollfd *fds, int nfds, int timeout); int pollinate(int in1, int in2, int out1, int out2, int timeout, int shutdown_timeout); char *ntop(struct sockaddr *sa); diff --git a/lib/net.c b/lib/net.c index 880ad89b..136536fe 100644 --- a/lib/net.c +++ b/lib/net.c @@ -35,7 +35,7 @@ struct addrinfo *xgetaddrinfo(char *host, char *port, int family, int socktype, return ai; } -int xconnect(struct addrinfo *ai_arg) +int xconnbind(struct addrinfo *ai_arg, int dobind) { struct addrinfo *ai; int fd = -1; @@ -44,8 +44,8 @@ int xconnect(struct addrinfo *ai_arg) for (ai = ai_arg; ai; ai = ai->ai_next) { fd = (ai->ai_next ? socket : xsocket)(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - if (!connect(fd, ai->ai_addr, ai->ai_addrlen)) break; - else if (!ai->ai_next) perror_exit("connect"); + if (!(dobind ? bind : connect)(fd, ai->ai_addr, ai->ai_addrlen)) break; + else if (!ai->ai_next) perror_exit_raw(dobind ? "bind" : "connect"); close(fd); } freeaddrinfo(ai_arg); @@ -53,22 +53,15 @@ int xconnect(struct addrinfo *ai_arg) return fd; } -int xbind(struct addrinfo *ai_arg) +int xconnect(struct addrinfo *ai) { - struct addrinfo *ai; - int fd = -1; + return xconnbind(ai, 0); +} - // Try all the returned addresses. Report errors if last entry can't connect. - for (ai = ai_arg; ai; ai = ai->ai_next) { - fd = (ai->ai_next ? socket : xsocket)(ai->ai_family, ai->ai_socktype, - ai->ai_protocol); - if (!bind(fd, ai->ai_addr, ai->ai_addrlen)) break; - else if (!ai->ai_next) perror_exit("connect"); - close(fd); - } - freeaddrinfo(ai_arg); - return fd; +int xbind(struct addrinfo *ai) +{ + return xconnbind(ai, 1); } int xpoll(struct pollfd *fds, int nfds, int timeout) -- cgit v1.2.3