| 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 "iopause.h" |
| 9 | #include "printpacket.h" |
| 10 | #include "parsetype.h" |
| 11 | #include "dns.h" |
| 12 | |
| 13 | #define FATAL "dnsqr: fatal: " |
| 14 | |
| 15 | void usage(void) |
| 16 | { |
| 17 | strerr_die1x(100,"dnsqr: usage: dnsqr type name"); |
| 18 | } |
| 19 | void oops(void) |
| 20 | { |
| 21 | strerr_die2sys(111,FATAL,"unable to parse: "); |
| 22 | } |
| 23 | |
| 24 | char type[2]; |
| 25 | static char *q; |
| 26 | |
| 27 | static stralloc out; |
| 28 | |
| 29 | static char seed[128]; |
| 30 | |
| 31 | int main(int argc,char **argv) |
| 32 | { |
| 33 | uint16 u16; |
| 34 | |
| 35 | dns_random_init(seed); |
| 36 | |
| 37 | if (!*argv) usage(); |
| 38 | if (!*++argv) usage(); |
| 39 | if (!parsetype(*argv,type)) usage(); |
| 40 | |
| 41 | if (!*++argv) usage(); |
| 42 | if (!dns_domain_fromdot(&q,*argv,str_len(*argv))) oops(); |
| 43 | |
| 44 | if (*++argv) usage(); |
| 45 | |
| 46 | if (!stralloc_copys(&out,"")) oops(); |
| 47 | uint16_unpack_big(type,&u16); |
| 48 | if (!stralloc_catulong0(&out,u16,0)) oops(); |
| 49 | if (!stralloc_cats(&out," ")) oops(); |
| 50 | if (!dns_domain_todot_cat(&out,q)) oops(); |
| 51 | if (!stralloc_cats(&out,":\n")) oops(); |
| 52 | |
| 53 | if (dns_resolve(q,type) == -1) { |
| 54 | if (!stralloc_cats(&out,error_str(errno))) oops(); |
| 55 | if (!stralloc_cats(&out,"\n")) oops(); |
| 56 | } |
| 57 | else { |
| 58 | if (dns_resolve_tx.packetlen < 4) oops(); |
| 59 | dns_resolve_tx.packet[2] &= ~1; |
| 60 | dns_resolve_tx.packet[3] &= ~128; |
| 61 | if (!printpacket_cat(&out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) oops(); |
| 62 | } |
| 63 | |
| 64 | buffer_putflush(buffer_1,out.s,out.len); |
| 65 | _exit(0); |
| 66 | } |