release
[hcoop/zz_old/debian/djbdns.git] / dnsq.c
CommitLineData
dc0d77d7
CE
1#include "uint16.h"
2#include "strerr.h"
3#include "buffer.h"
4#include "scan.h"
5#include "str.h"
6#include "byte.h"
7#include "error.h"
8#include "ip4.h"
9#include "iopause.h"
10#include "printpacket.h"
11#include "parsetype.h"
12#include "dns.h"
13
14#define FATAL "dnsq: fatal: "
15
16void usage(void)
17{
18 strerr_die1x(100,"dnsq: usage: dnsq type name server");
19}
20void oops(void)
21{
22 strerr_die2sys(111,FATAL,"unable to parse: ");
23}
24
25static struct dns_transmit tx;
26
27int resolve(char *q,char qtype[2],char servers[64])
28{
29 struct taia stamp;
30 struct taia deadline;
31 iopause_fd x[1];
32 int r;
33
34 if (dns_transmit_start(&tx,servers,0,q,qtype,"\0\0\0\0") == -1) return -1;
35
36 for (;;) {
37 taia_now(&stamp);
38 taia_uint(&deadline,120);
39 taia_add(&deadline,&deadline,&stamp);
40 dns_transmit_io(&tx,x,&deadline);
41 iopause(x,1,&deadline,&stamp);
42 r = dns_transmit_get(&tx,x,&stamp);
43 if (r == -1) return -1;
44 if (r == 1) break;
45 }
46
47 return 0;
48}
49
50char servers[64];
51static stralloc ip;
52static stralloc fqdn;
53
54char type[2];
55static char *q;
56
57static stralloc out;
58
59static char seed[128];
60
61int main(int argc,char **argv)
62{
63 uint16 u16;
64
65 dns_random_init(seed);
66
67 if (!*argv) usage();
68 if (!*++argv) usage();
69 if (!parsetype(*argv,type)) usage();
70
71 if (!*++argv) usage();
72 if (!dns_domain_fromdot(&q,*argv,str_len(*argv))) oops();
73
74 if (!*++argv) usage();
75 if (!stralloc_copys(&out,*argv)) oops();
76 if (dns_ip4_qualify(&ip,&fqdn,&out) == -1) oops();
77 if (ip.len >= 64) ip.len = 64;
78 byte_zero(servers,64);
79 byte_copy(servers,ip.len,ip.s);
80
81 if (!stralloc_copys(&out,"")) oops();
82 uint16_unpack_big(type,&u16);
83 if (!stralloc_catulong0(&out,u16,0)) oops();
84 if (!stralloc_cats(&out," ")) oops();
85 if (!dns_domain_todot_cat(&out,q)) oops();
86 if (!stralloc_cats(&out,":\n")) oops();
87
88 if (resolve(q,type,servers) == -1) {
89 if (!stralloc_cats(&out,error_str(errno))) oops();
90 if (!stralloc_cats(&out,"\n")) oops();
91 }
92 else {
93 if (!printpacket_cat(&out,tx.packet,tx.packetlen)) oops();
94 }
95
96 buffer_putflush(buffer_1,out.s,out.len);
97 _exit(0);
98}