Commit | Line | Data |
---|---|---|
dc0d77d7 CE |
1 | #include "stralloc.h" |
2 | #include "uint16.h" | |
3 | #include "byte.h" | |
4 | #include "dns.h" | |
5 | ||
6 | static char *q = 0; | |
7 | ||
8 | int dns_name_packet(stralloc *out,const char *buf,unsigned int len) | |
9 | { | |
10 | unsigned int pos; | |
11 | char header[12]; | |
12 | uint16 numanswers; | |
13 | uint16 datalen; | |
14 | ||
15 | if (!stralloc_copys(out,"")) return -1; | |
16 | ||
17 | pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return -1; | |
18 | uint16_unpack_big(header + 6,&numanswers); | |
19 | pos = dns_packet_skipname(buf,len,pos); if (!pos) return -1; | |
20 | pos += 4; | |
21 | ||
22 | while (numanswers--) { | |
23 | pos = dns_packet_skipname(buf,len,pos); if (!pos) return -1; | |
24 | pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return -1; | |
25 | uint16_unpack_big(header + 8,&datalen); | |
26 | if (byte_equal(header,2,DNS_T_PTR)) | |
27 | if (byte_equal(header + 2,2,DNS_C_IN)) { | |
28 | if (!dns_packet_getname(buf,len,pos,&q)) return -1; | |
29 | if (!dns_domain_todot_cat(out,q)) return -1; | |
30 | return 0; | |
31 | } | |
32 | pos += datalen; | |
33 | } | |
34 | ||
35 | return 0; | |
36 | } | |
37 | ||
38 | int dns_name4(stralloc *out,const char ip[4]) | |
39 | { | |
40 | char name[DNS_NAME4_DOMAIN]; | |
41 | ||
42 | dns_name4_domain(name,ip); | |
43 | if (dns_resolve(name,DNS_T_PTR) == -1) return -1; | |
44 | if (dns_name_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen) == -1) return -1; | |
45 | dns_transmit_free(&dns_resolve_tx); | |
46 | dns_domain_free(&q); | |
47 | return 0; | |
48 | } |