aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/lib.h1
-rw-r--r--lib/net.c18
2 files changed, 19 insertions, 0 deletions
diff --git a/lib/lib.h b/lib/lib.h
index 14bb7cf6..6dc2ca2b 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -301,6 +301,7 @@ 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 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 8969306b..880ad89b 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -53,6 +53,24 @@ int xconnect(struct addrinfo *ai_arg)
return fd;
}
+int xbind(struct addrinfo *ai_arg)
+{
+ struct addrinfo *ai;
+ int fd = -1;
+
+ // 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 xpoll(struct pollfd *fds, int nfds, int timeout)
{
int i;