File: dns_resolve.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,468 kB
  • sloc: ansic: 15,821; perl: 674; sh: 63; makefile: 29
file content (54 lines) | stat: -rw-r--r-- 1,395 bytes parent folder | download | duplicates (3)
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
#include <string.h>

#include "dns.h"

static void iopause(iopoll_fd *x,unsigned int len,struct timeval *deadline,struct timeval *stamp)
{
  int millisecs;
  unsigned int i;

  if (TV_LESS(deadline,stamp))
    millisecs = 0;
  else {
    if (deadline->tv_sec - stamp->tv_sec > 1000)
      millisecs = 1000000;
    else
      millisecs = (deadline->tv_sec - stamp->tv_sec) * 1000 + (deadline->tv_usec - stamp->tv_usec) / 1000;
    millisecs += 20;
  }

  for (i = 0;i < len;++i)
    x[i].revents = 0;

  iopoll(x,len,millisecs);
}

/** Resolve a DNS query. 

    This is the base query handler of the DNS library. It takes a domain
    name and query type, sends it to all configured nameservers, and
    handles reading the response packet. Callers are responsible for
    parsing the desired records out of the resulting packet.
*/
int dns_resolve(struct dns_transmit *tx,const char *q,uint16 qtype)
{
  struct timeval stamp;
  struct timeval deadline;
  ipv4addr servers[DNS_MAX_IPS];
  iopoll_fd x[1];
  int r;

  if (dns_resolvconfip(servers) == -1) return -1;
  if (dns_transmit_start(tx,servers,1,q,qtype,0) == -1) return -1;

  for (;;) {
    gettimeofday(&stamp,0);
    deadline = stamp;
    deadline.tv_sec += 120;
    dns_transmit_io(tx,x,&deadline);
    iopause(x,1,&deadline,&stamp);
    r = dns_transmit_get(tx,x,&stamp);
    if (r == -1) return -1;
    if (r == 1) return 0;
  }
}