summaryrefslogtreecommitdiff
path: root/src/libs6dns/s6dns_resolven_loop.c
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2014-12-10 03:05:47 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2014-12-10 03:05:47 +0000
commit416ef5e2bf59bb2e45066a1d5d91ac677c0f48e5 (patch)
tree1c746d673dcec7a8488c6ac51db8245411034376 /src/libs6dns/s6dns_resolven_loop.c
downloads6-dns-416ef5e2bf59bb2e45066a1d5d91ac677c0f48e5.tar.xz
Initial commit
Diffstat (limited to 'src/libs6dns/s6dns_resolven_loop.c')
-rw-r--r--src/libs6dns/s6dns_resolven_loop.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/libs6dns/s6dns_resolven_loop.c b/src/libs6dns/s6dns_resolven_loop.c
new file mode 100644
index 0000000..6e0c3b4
--- /dev/null
+++ b/src/libs6dns/s6dns_resolven_loop.c
@@ -0,0 +1,54 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <skalibs/tai.h>
+#include <skalibs/iopause.h>
+#include <s6-dns/s6dns-engine.h>
+#include <s6-dns/s6dns-resolve.h>
+
+ /*
+ This is basically a synchronous interface to s6dns_engine.
+ It resolves n dts at the same time.
+ */
+
+int s6dns_resolven_loop (s6dns_engine_t *dt, unsigned int n, unsigned int or, tain_t const *deadline, tain_t *stamp)
+{
+ iopause_fd x[n] ;
+ unsigned int count = 0 ;
+ for (;;)
+ {
+ tain_t localdeadline = *deadline ;
+ register int r ;
+ register unsigned int i = 0 ;
+ register unsigned int j = 0 ;
+ for (; i < n ; i++) if (dt[i].status == EAGAIN)
+ {
+ s6dns_engine_nextdeadline(dt + i, &localdeadline) ;
+ x[j].fd = dt[i].fd ;
+ x[j].events = (s6dns_engine_isreadable(dt + i) ? IOPAUSE_READ : 0) | (s6dns_engine_iswritable(dt + i) ? IOPAUSE_WRITE : 0) ;
+ j++ ;
+ }
+ if (!j) break ;
+ r = iopause_stamp(x, j, &localdeadline, stamp) ;
+ if (r < 0) return -1 ;
+ else if (!r)
+ {
+ if (tain_less(deadline, stamp)) return (errno = ETIMEDOUT, -1) ;
+ for (i = 0 ; i < n ; i++) if (dt[i].status == EAGAIN)
+ if (s6dns_engine_timeout(dt + i, stamp) && (or >= 2)) return i ;
+ }
+ else
+ {
+ for (i = 0 ; i < n ; i++) if (dt[i].status == EAGAIN)
+ {
+ r = s6dns_engine_event(dt + i, stamp) ;
+ if (or)
+ {
+ if (r && ((r > 0) || (or >= 2))) return i ;
+ }
+ else if (r > 0) count++ ;
+ }
+ }
+ }
+ return or ? (errno = ENOENT, -1) : (int)count ;
+}