djbdnscurve6  38
djbdnscurve6
dnsq.c
Go to the documentation of this file.
1 #include "uint_t.h"
2 #include "logmsg.h"
3 #include "buffer.h"
4 #include "scan.h"
5 #include "str.h"
6 #include "byte.h"
7 #include "ip.h"
8 #include "iopause.h"
9 #include "printpacket.h"
10 #include "parsetype.h"
11 #include "dns.h"
12 #include "ip.h"
13 #include "exit.h"
14 #include "curvedns.h"
15 
16 #define WHO "dnsq"
17 
18 void usage(void)
19 {
20  logmsg(WHO,100,USAGE,"type name server");
21 }
22 void oops(void)
23 {
24  logmsg(WHO,111,FATAL,"unable to parse");
25 }
26 
27 static struct dns_transmit tx;
28 
29 int resolve(char *q,char qtype[2],char servers[QUERY_MAXIPLEN])
30 {
31  struct taia stamp;
32  struct taia deadline;
33  iopause_fd x[1];
34  int r;
35 
36  if (cns_transmit_start(&tx,servers,0,q,qtype,V6any,0,0,0) < 0) return DNS_COM;
37 
38  for (;;) {
39  taia_now(&stamp);
40  taia_uint(&deadline,120);
41  taia_add(&deadline,&deadline,&stamp);
42  dns_transmit_io(&tx,x,&deadline);
43  r = iopause(x,1,&deadline,&stamp);
44  r = dns_transmit_get(&tx,x,&stamp);
45  if (r < 0) return DNS_ERR;
46  if (r == 1) break;
47  }
48 
49  return 0;
50 }
51 
53 static stralloc ip;
54 static stralloc fqdn;
55 
56 char type[2];
57 static char *q;
58 
59 static stralloc out;
60 
61 static char seed[128];
62 
63 int main(int argc,char **argv)
64 {
65  uint16 u16;
66 
68 
69  if (!*argv) usage();
70  if (!*++argv) usage();
71  if (!parsetype(*argv,type)) usage();
72 
73  if (!*++argv) usage();
74  if (dns_domain_fromdot(&q,*argv,str_len(*argv)) <= 0) oops();
75 
76  if (!*++argv) usage();
77  if (!stralloc_copys(&out,*argv)) oops();
78  if (dns_ip_qualify(&ip,&fqdn,&out) < 0) oops();
79  if (ip.len >= QUERY_MAXIPLEN) ip.len = QUERY_MAXIPLEN;
80  byte_zero(servers,QUERY_MAXIPLEN);
81  byte_copy(servers,ip.len,ip.s);
82 
83  if (!stralloc_copys(&out,"")) oops();
84  uint16_unpack_big(type,&u16);
85  if (!stralloc_catulong0(&out,u16,0)) oops();
86  if (!stralloc_cats(&out," ")) oops();
87  if (dns_domain_todot_cat(&out,q) <= 0) oops();
88  if (!stralloc_cats(&out,":\n")) oops();
89 
90  if (resolve(q,type,servers) < 0) {
91  if (!stralloc_cats(&out,errstr(errno))) oops();
92  if (!stralloc_cats(&out,"\n")) oops();
93  }
94  else {
95  if (!printpacket_cat(&out,tx.packet,tx.packetlen)) oops();
96  }
97 
98  buffer_putflush(buffer_1,out.s,out.len);
99  _exit(0);
100 }
char ip[16]
Definition: axfrdns.c:126
int cns_transmit_start(struct dns_transmit *d, const char servers[QUERY_MAXIPLEN], int flagrecursive, const char *q, const char qtype[2], const char localip[16], const char keys[1024], const char pubkey[32], const char *suffix)
Definition: curvedns.c:256
void dns_transmit_io(struct dns_transmit *, iopause_fd *, struct taia *)
Definition: dns_transmit.c:284
#define DNS_ERR
Definition: dns.h:22
#define QUERY_MAXIPLEN
Definition: dns.h:30
int dns_domain_fromdot(char **, const char *, unsigned int)
Definition: dns_dfd.c:6
int dns_domain_todot_cat(stralloc *, const char *)
Definition: dns_dtda.c:11
int dns_ip_qualify(stralloc *, stralloc *, const stralloc *)
Definition: dns_ipq.c:228
void dns_random_init(const char *)
#define DNS_COM
Definition: dns.h:23
int dns_transmit_get(struct dns_transmit *, const iopause_fd *, const struct taia *)
Definition: dns_transmit.c:301
uint32 seed[32]
Definition: dnscache-conf.c:36
struct line * x
int main(int argc, char **argv)
Definition: dnsq.c:63
void oops(void)
Definition: dnsq.c:22
int resolve(char *q, char qtype[2], char servers[QUERY_MAXIPLEN])
Definition: dnsq.c:29
char servers[QUERY_MAXIPLEN]
Definition: dnsq.c:52
char type[2]
Definition: dnsq.c:56
#define WHO
Definition: dnsq.c:16
void usage(void)
Definition: dnsq.c:18
int parsetype(char *s, char type[2])
Definition: parsetype.c:8
unsigned int printpacket_cat(stralloc *out, char *buf, unsigned int len)
Definition: printpacket.c:14
char qtype[2]
Definition: dns.h:93
unsigned int packetlen
Definition: dns.h:77
char * packet
Definition: dns.h:76