diff options
author | Rob Landley <rob@landley.net> | 2019-01-12 13:26:29 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-01-12 13:26:29 -0600 |
commit | 7d355098320a0cbc54001a07e44cf9f7e7a3636c (patch) | |
tree | 767782750413f514bc53018b1b9f587138be3085 | |
parent | 41b5019a2052f24ccb4f729bfcbfe3c1d082366e (diff) | |
download | toybox-7d355098320a0cbc54001a07e44cf9f7e7a3636c.tar.gz |
Merge xconnect/xbind.
-rw-r--r-- | lib/lib.h | 4 | ||||
-rw-r--r-- | lib/net.c | 25 |
2 files changed, 11 insertions, 18 deletions
@@ -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); @@ -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) |