From 7e6a7da60042dbf033cc3136877a2862bf825bd1 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 13 Feb 2019 20:00:53 -0600 Subject: Move nanomove(), nanodiff(), union socksaddr, and xrecvwait() to lib. --- lib/net.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lib/net.c') diff --git a/lib/net.c b/lib/net.c index 346d17e9..d4373fbe 100644 --- a/lib/net.c +++ b/lib/net.c @@ -138,3 +138,22 @@ void xsendto(int sockfd, void *buf, size_t len, struct sockaddr *dest) if (rc != len) perror_exit("sendto"); } + +// xrecvfrom with timeout in milliseconds +int xrecvwait(int fd, char *buf, int len, union socksaddr *sa, int timeout) +{ + socklen_t sl = sizeof(*sa); + + if (timeout >= 0) { + struct pollfd pfd; + + pfd.fd = fd; + pfd.events = POLLIN; + if (!xpoll(&pfd, 1, timeout)) return 0; + } + + len = recvfrom(fd, buf, len, 0, (void *)sa, &sl); + if (len<0) perror_exit("recvfrom"); + + return len; +} -- cgit v1.2.3