summaryrefslogtreecommitdiff
path: root/src/libs6dns/s6dns_resolven_loop.c
blob: 6e0c3b42519ebbbb7684fbf3ccb3c60e77ce6515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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 ;
}