diff options
Diffstat (limited to 'src/dnsstub/dns_ip.c')
-rw-r--r-- | src/dnsstub/dns_ip.c | 140 |
1 files changed, 76 insertions, 64 deletions
diff --git a/src/dnsstub/dns_ip.c b/src/dnsstub/dns_ip.c index f89728c..0a63022 100644 --- a/src/dnsstub/dns_ip.c +++ b/src/dnsstub/dns_ip.c @@ -1,8 +1,8 @@ -#include "stralloc.h" -#include "uint_t.h" #include "byte.h" -#include "ip.h" #include "dnsresolv.h" +#include "ip.h" +#include "stralloc.h" +#include "uint_t.h" /** @file dns_ip.c @@ -13,7 +13,7 @@ static char *q = 0; -int dns_ip4_packet(stralloc *out,const char *buf,unsigned int len) +int dns_ip4_packet(stralloc *out, const char *buf, unsigned int len) { unsigned int pos; char header[12]; @@ -21,53 +21,60 @@ int dns_ip4_packet(stralloc *out,const char *buf,unsigned int len) uint16 datalen; int ranswers = 0; - if (!stralloc_copys(out,"")) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; - pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 6,&numanswers); - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, 0, header, 12); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 6, &numanswers); + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; pos += 4; while (numanswers--) { - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; - pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 8,&datalen); - if (byte_equal(header,2,DNS_T_A)) - if (byte_equal(header + 2,2,DNS_C_IN)) + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, pos, header, 10); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 8, &datalen); + if (byte_equal(header, 2, DNS_T_A)) + if (byte_equal(header + 2, 2, DNS_C_IN)) if (datalen == 4) { - if (!dns_packet_copy(buf,len,pos,header,4)) return DNS_ERR; - if (!stralloc_catb(out,header,4)) return DNS_MEM; - } + if (!dns_packet_copy(buf, len, pos, header, 4)) return DNS_ERR; + if (!stralloc_catb(out, header, 4)) return DNS_MEM; + } pos += datalen; ++ranswers; } - dns_sortip4(out->s,out->len); + dns_sortip4(out->s, out->len); return ranswers; } -int dns_ip4(stralloc *out,stralloc *fqdn) +int dns_ip4(stralloc *out, stralloc *fqdn) { unsigned int i; char code = 0; - int dot = 0; - char ch; + int dot = 0; + char ch; char ip[4]; int r; int rc = 0; - if (!stralloc_copys(out,"")) return DNS_MEM; - if (!stralloc_readyplus(fqdn,1)) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; + if (!stralloc_readyplus(fqdn, 1)) return DNS_MEM; - fqdn->s[fqdn->len] = 0; /* test FQDN string */ - for (i = 1; i < fqdn->len; i++) { - if (fqdn->s[i] >= '_') { code = 127; break; } - if (fqdn->s[i] == '.') dot++; - } + fqdn->s[fqdn->len] = 0; /* test FQDN string */ + for (i = 1; i < fqdn->len; i++) { + if (fqdn->s[i] >= '_') { + code = 127; + break; + } + if (fqdn->s[i] == '.') dot++; + } - if (code != 127 && dot == 3) /* if FQDN is just IPv4 */ - if (ip4_scan(fqdn->s,ip) || ip4_scanbracket(fqdn->s,ip)) { - if (!stralloc_copyb(out,ip,4)) return DNS_MEM; + if (code != 127 && dot == 3) /* if FQDN is just IPv4 */ + if (ip4_scan(fqdn->s, ip) || ip4_scanbracket(fqdn->s, ip)) { + if (!stralloc_copyb(out, ip, 4)) return DNS_MEM; return 1; } @@ -80,7 +87,7 @@ int dns_ip4(stralloc *out,stralloc *fqdn) if ((ch == '[') || (ch == ']')) continue; if (ch == '.') { - if (!stralloc_append(out,&code)) return DNS_MEM; + if (!stralloc_append(out, &code)) return DNS_MEM; code = 0; continue; } @@ -90,22 +97,22 @@ int dns_ip4(stralloc *out,stralloc *fqdn) continue; } - if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR; // fdqn -> A query -> response - if (dns_resolve(q,DNS_T_A) >= 0) { - if ((r = dns_ip4_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR; + if (dns_domain_fromdot(&q, fqdn->s, fqdn->len) <= 0) return DNS_ERR; // fdqn -> A query -> response + if (dns_resolve(q, DNS_T_A) >= 0) { + if ((r = dns_ip4_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR; dns_transmit_free(&dns_resolve_tx); dns_domain_free(&q); rc += r; } - return rc; + return rc; } out->len &= ~3; return 0; } -int dns_ip6_packet(stralloc *out,const char *buf,unsigned int len) +int dns_ip6_packet(stralloc *out, const char *buf, unsigned int len) { unsigned int pos; char header[16]; @@ -113,39 +120,43 @@ int dns_ip6_packet(stralloc *out,const char *buf,unsigned int len) uint16 datalen; int ranswers = 0; - if (!stralloc_copys(out,"")) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; - pos = dns_packet_copy(buf,len,0,header,12); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 6,&numanswers); - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, 0, header, 12); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 6, &numanswers); + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; pos += 4; while (numanswers--) { - pos = dns_packet_skipname(buf,len,pos); if (!pos) return DNS_ERR; - pos = dns_packet_copy(buf,len,pos,header,10); if (!pos) return DNS_ERR; - uint16_unpack_big(header + 8,&datalen); - if (byte_equal(header,2,DNS_T_AAAA)) { - if (byte_equal(header + 2,2,DNS_C_IN)) + pos = dns_packet_skipname(buf, len, pos); + if (!pos) return DNS_ERR; + pos = dns_packet_copy(buf, len, pos, header, 10); + if (!pos) return DNS_ERR; + uint16_unpack_big(header + 8, &datalen); + if (byte_equal(header, 2, DNS_T_AAAA)) { + if (byte_equal(header + 2, 2, DNS_C_IN)) if (datalen == 16) { - if (!dns_packet_copy(buf,len,pos,header,16)) return DNS_ERR; - if (!stralloc_catb(out,header,16)) return DNS_MEM; + if (!dns_packet_copy(buf, len, pos, header, 16)) return DNS_ERR; + if (!stralloc_catb(out, header, 16)) return DNS_MEM; } - } else if (byte_equal(header,2,DNS_T_A)) - if (byte_equal(header + 2,2,DNS_C_IN)) + } else if (byte_equal(header, 2, DNS_T_A)) + if (byte_equal(header + 2, 2, DNS_C_IN)) if (datalen == 4) { - byte_copy(header,12,V4mappedprefix); - if (!dns_packet_copy(buf,len,pos,header + 12,4)) return DNS_ERR; - if (!stralloc_catb(out,header,16)) return DNS_MEM; + byte_copy(header, 12, V4mappedprefix); + if (!dns_packet_copy(buf, len, pos, header + 12, 4)) return DNS_ERR; + if (!stralloc_catb(out, header, 16)) return DNS_MEM; } pos += datalen; ++ranswers; } - dns_sortip6(out->s,out->len); + dns_sortip6(out->s, out->len); return ranswers; } -int dns_ip6(stralloc *out,stralloc *fqdn) +int dns_ip6(stralloc *out, stralloc *fqdn) { unsigned int i; char code; @@ -154,12 +165,12 @@ int dns_ip6(stralloc *out,stralloc *fqdn) int r; int rc = 0; - if (!stralloc_copys(out,"")) return DNS_MEM; - if (!stralloc_readyplus(fqdn,1)) return DNS_MEM; + if (!stralloc_copys(out, "")) return DNS_MEM; + if (!stralloc_readyplus(fqdn, 1)) return DNS_MEM; - fqdn->s[fqdn->len] = 0; /* if FQDN is just IPv6 */ - if (ip6_scan(fqdn->s,ip) || ip6_scanbracket(fqdn->s,ip)) { - if (!stralloc_copyb(out,ip,16)) return DNS_MEM; + fqdn->s[fqdn->len] = 0; /* if FQDN is just IPv6 */ + if (ip6_scan(fqdn->s, ip) || ip6_scanbracket(fqdn->s, ip)) { + if (!stralloc_copyb(out, ip, 16)) return DNS_MEM; return 1; } @@ -172,7 +183,7 @@ int dns_ip6(stralloc *out,stralloc *fqdn) if ((ch == '[') || (ch == ']')) continue; if (ch == '.') { - if (!stralloc_append(out,&code)) return DNS_MEM; + if (!stralloc_append(out, &code)) return DNS_MEM; code = 0; continue; } @@ -182,9 +193,10 @@ int dns_ip6(stralloc *out,stralloc *fqdn) continue; } - if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR; // fqdn -> AAAA query -> response - if (dns_resolve(q,DNS_T_AAAA) >= 0) { - if ((r = dns_ip6_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR; + if (dns_domain_fromdot(&q, fqdn->s, fqdn->len) <= 0) + return DNS_ERR; // fqdn -> AAAA query -> response + if (dns_resolve(q, DNS_T_AAAA) >= 0) { + if ((r = dns_ip6_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR; dns_transmit_free(&dns_resolve_tx); dns_domain_free(&q); rc += r; @@ -192,7 +204,7 @@ int dns_ip6(stralloc *out,stralloc *fqdn) return rc; } - + out->len &= ~3; return 0; } |