| 1 | #include "str.h" |
| 2 | #include "byte.h" |
| 3 | #include "scan.h" |
| 4 | #include "exit.h" |
| 5 | #include "stralloc.h" |
| 6 | #include "buffer.h" |
| 7 | #include "strerr.h" |
| 8 | #include "uint16.h" |
| 9 | #include "response.h" |
| 10 | #include "case.h" |
| 11 | #include "printpacket.h" |
| 12 | #include "parsetype.h" |
| 13 | #include "ip4.h" |
| 14 | #include "dns.h" |
| 15 | |
| 16 | extern int respond(char *,char *,char *); |
| 17 | |
| 18 | #define FATAL "tinydns-get: fatal: " |
| 19 | |
| 20 | void usage(void) |
| 21 | { |
| 22 | strerr_die1x(100,"tinydns-get: usage: tinydns-get type name [ip]"); |
| 23 | } |
| 24 | void oops(void) |
| 25 | { |
| 26 | strerr_die2sys(111,FATAL,"unable to parse: "); |
| 27 | } |
| 28 | |
| 29 | static char ip[4]; |
| 30 | static char type[2]; |
| 31 | static char *q; |
| 32 | |
| 33 | static stralloc out; |
| 34 | |
| 35 | int main(int argc,char **argv) |
| 36 | { |
| 37 | uint16 u16; |
| 38 | |
| 39 | if (!*argv) usage(); |
| 40 | |
| 41 | if (!*++argv) usage(); |
| 42 | if (!parsetype(*argv,type)) usage(); |
| 43 | |
| 44 | if (!*++argv) usage(); |
| 45 | if (!dns_domain_fromdot(&q,*argv,str_len(*argv))) oops(); |
| 46 | |
| 47 | if (*++argv) { |
| 48 | if (!ip4_scan(*argv,ip)) usage(); |
| 49 | } |
| 50 | |
| 51 | if (!stralloc_copys(&out,"")) oops(); |
| 52 | uint16_unpack_big(type,&u16); |
| 53 | if (!stralloc_catulong0(&out,u16,0)) oops(); |
| 54 | if (!stralloc_cats(&out," ")) oops(); |
| 55 | if (!dns_domain_todot_cat(&out,q)) oops(); |
| 56 | if (!stralloc_cats(&out,":\n")) oops(); |
| 57 | |
| 58 | if (!response_query(q,type,DNS_C_IN)) oops(); |
| 59 | response[3] &= ~128; |
| 60 | response[2] &= ~1; |
| 61 | response[2] |= 4; |
| 62 | case_lowerb(q,dns_domain_length(q)); |
| 63 | |
| 64 | if (byte_equal(type,2,DNS_T_AXFR)) { |
| 65 | response[3] &= ~15; |
| 66 | response[3] |= 4; |
| 67 | } |
| 68 | else |
| 69 | if (!respond(q,type,ip)) goto DONE; |
| 70 | |
| 71 | if (!printpacket_cat(&out,response,response_len)) oops(); |
| 72 | |
| 73 | DONE: |
| 74 | buffer_putflush(buffer_1,out.s,out.len); |
| 75 | _exit(0); |
| 76 | } |