aboutsummaryrefslogtreecommitdiff
path: root/tinydns-get.c
blob: d17305d23c6782c1152e4d5c47810e08b3cc94ab (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "str.h"
#include "byte.h"
#include "scan.h"
#include "exit.h"
#include "stralloc.h"
#include "buffer.h"
#include "strerr.h"
#include "uint16.h"
#include "response.h"
#include "case.h"
#include "printpacket.h"
#include "parsetype.h"
#include "ip4.h"
#include "ip6.h"
#include "dns.h"

extern int respond(char *,char *,char *);

#define FATAL "tinydns-get: fatal: "

void usage(void)
{
  strerr_die1x(100,"tinydns-get: usage: tinydns-get [-s | -S] type name [ip]");
}
void oops(void)
{
  strerr_die2sys(111,FATAL,"unable to parse: ");
}

static char ip[16];
static char type[2];
static char *q;

static stralloc out;

int main(int argc,char **argv)
{
  uint16 u16;

  if (!*argv) usage();

  if (!*++argv) usage();

  max_response_len = 512;
  if ((*argv)[0] == '-') {
    if ((*argv)[1] != 's' && (*argv)[1] != 'S' || (*argv)[2]) usage();
    do_dnssec = 1;
    max_response_len = (*argv)[1] == 's' ? 1220 : 4000;
    if (!*++argv) usage();
  }
  if (!parsetype(*argv,type)) usage();

  if (!*++argv) usage();
  if (!dns_domain_fromdot(&q,*argv,str_len(*argv))) oops();

  if (*++argv) {
    if (!ip6_scan(*argv,ip)) usage();
  }

  if (!stralloc_copys(&out,"")) oops();
  uint16_unpack_big(type,&u16);
  if (!stralloc_catulong0(&out,u16,0)) oops();
  if (!stralloc_cats(&out," ")) oops();
  if (!dns_domain_todot_cat(&out,q)) oops();
  if (!stralloc_cats(&out,":\n")) oops();

  if (!response_query(q,type,DNS_C_IN)) oops();
  response[3] &= ~128;
  response[2] &= ~1;
  response[2] |= 4;
  case_lowerb(q,dns_domain_length(q));

  if (byte_equal(type,2,DNS_T_AXFR)) {
    response[3] &= ~15;
    response[3] |= 4;
  }
  else
    if (!respond(q,type,ip)) goto DONE;

  if (!printpacket_cat(&out,response,response_len)) oops();

  DONE:
  buffer_putflush(buffer_1,out.s,out.len);
  _exit(0);
}