summaryrefslogtreecommitdiff
path: root/src/dnsip.c
blob: d6e3c0ddbeaef924127cedc1d4d8af4b93f60d6d (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
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include "buffer.h"
#include "dnsresolv.h"
#include "exit.h"
#include "fmt.h"
#include "ip.h"
#include "logmsg.h"
#include "stralloc.h"

#include "dns.h"

#define WHO "dnsip"

stralloc sa = {0};
stralloc out = {0};

int main(int argc, char **argv)
{
  int i;
  char ip4str[IP4_FMT];
  char ip6str[IP6_FMT];

  if (!argv[1]) logmsg(WHO, 100, USAGE, "dnsip fqdn");

  if (!stralloc_copys(&sa, argv[1])) logmsg(WHO, 111, FATAL, "out of memory");

  DNS_INIT
  if (dns_ip6(&out, &sa) > 0) { /* IPv6 first */
    for (i = 0; i + 16 <= out.len; i += 16) {
      if (ip6_isv4mapped(out.s + i)) continue;
      buffer_put(buffer_1, ip6str, ip6_fmt(ip6str, out.s + i));
      buffer_puts(buffer_1, "\n");
    }
  }

  if (dns_ip4(&out, &sa) > 0) {
    for (i = 0; i + 4 <= out.len; i += 4) {
      buffer_put(buffer_1, ip4str, ip4_fmt(ip4str, out.s + i));
      buffer_puts(buffer_1, "\n");
    }
  }
  buffer_putsflush(buffer_1, "");

  _exit(0);
}