summaryrefslogtreecommitdiff
path: root/src/dnsstub
diff options
context:
space:
mode:
Diffstat (limited to 'src/dnsstub')
-rw-r--r--src/dnsstub/dns_cname.c46
-rw-r--r--src/dnsstub/dns_dfd.c24
-rw-r--r--src/dnsstub/dns_domain.c21
-rw-r--r--src/dnsstub/dns_dtda.c30
-rw-r--r--src/dnsstub/dns_ip.c140
-rw-r--r--src/dnsstub/dns_ipq.c147
-rw-r--r--src/dnsstub/dns_mx.c48
-rw-r--r--src/dnsstub/dns_name.c56
-rw-r--r--src/dnsstub/dns_nd.c21
-rw-r--r--src/dnsstub/dns_packet.c35
-rw-r--r--src/dnsstub/dns_random.c45
-rw-r--r--src/dnsstub/dns_rcip.c77
-rw-r--r--src/dnsstub/dns_rcrw.c80
-rw-r--r--src/dnsstub/dns_resolve.c23
-rw-r--r--src/dnsstub/dns_sortip.c22
-rw-r--r--src/dnsstub/dns_transmit.c286
-rw-r--r--src/dnsstub/dns_txt.c49
17 files changed, 627 insertions, 523 deletions
diff --git a/src/dnsstub/dns_cname.c b/src/dnsstub/dns_cname.c
index 408949a..d1ea110 100644
--- a/src/dnsstub/dns_cname.c
+++ b/src/dnsstub/dns_cname.c
@@ -1,9 +1,9 @@
-#include "stralloc.h"
-#include "uint_t.h"
#include "byte.h"
-#include "ip.h"
#include "case.h"
#include "dnsresolv.h"
+#include "ip.h"
+#include "stralloc.h"
+#include "uint_t.h"
/**
@file dns_cname.c
@@ -13,7 +13,7 @@
static char *q = 0;
-int dns_cname_packet(stralloc *out,const char *buf,unsigned int len)
+int dns_cname_packet(stralloc *out, const char *buf, unsigned int len)
{
unsigned int pos;
char header[12];
@@ -21,21 +21,25 @@ int dns_cname_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_CNAME))
- if (byte_equal(header + 2,2,DNS_C_IN)) {
- if (!dns_packet_getname(buf,len,pos,&q)) return DNS_ERR;
- if (dns_domain_todot_cat(out,q) <= 0) return DNS_ERR;
+ 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_CNAME))
+ if (byte_equal(header + 2, 2, DNS_C_IN)) {
+ if (!dns_packet_getname(buf, len, pos, &q)) return DNS_ERR;
+ if (dns_domain_todot_cat(out, q) <= 0) return DNS_ERR;
}
pos += datalen;
++ranswers;
@@ -44,16 +48,16 @@ int dns_cname_packet(stralloc *out,const char *buf,unsigned int len)
return ranswers;
}
-int dns_cname(stralloc *out,stralloc *fqdn)
+int dns_cname(stralloc *out, stralloc *fqdn)
{
- int rc;
+ int rc;
- if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR;
- if (dns_resolve(q,DNS_T_CNAME) < 0) return DNS_ERR;
- if ((rc = dns_cname_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;
+ if (dns_resolve(q, DNS_T_CNAME) < 0) return DNS_ERR;
+ if ((rc = dns_cname_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR;
dns_transmit_free(&dns_resolve_tx);
dns_domain_free(&q);
- if (case_equals(out->s,fqdn->s)) rc = -6; // loop DNS_HARD
+ if (case_equals(out->s, fqdn->s)) rc = -6; // loop DNS_HARD
return rc;
}
diff --git a/src/dnsstub/dns_dfd.c b/src/dnsstub/dns_dfd.c
index 756a1f8..645d068 100644
--- a/src/dnsstub/dns_dfd.c
+++ b/src/dnsstub/dns_dfd.c
@@ -1,7 +1,7 @@
-#include "error.h"
#include "alloc.h"
#include "byte.h"
#include "dnsresolv.h"
+#include "error.h"
/**
@file dns_dfd.c
@@ -10,7 +10,7 @@
@brief domain name qualification (domain from dot)
*/
-int dns_domain_fromdot(char **out,const char *buf,unsigned int n)
+int dns_domain_fromdot(char **out, const char *buf, unsigned int n)
{
char label[63];
unsigned int labellen = 0; /* <= sizeof label */
@@ -23,30 +23,34 @@ int dns_domain_fromdot(char **out,const char *buf,unsigned int n)
for (;;) {
if (!n) break;
- ch = *buf++; --n;
+ ch = *buf++;
+ --n;
if (ch == '.') {
if (labellen) {
if (namelen + labellen + 1 > sizeof(name)) return 0;
name[namelen++] = labellen;
- byte_copy(name + namelen,labellen,label);
+ byte_copy(name + namelen, labellen, label);
namelen += labellen;
labellen = 0;
}
continue;
}
- if (ch == '\\') { // octal -> decimal
+ if (ch == '\\') { // octal -> decimal
if (!n) break;
- ch = *buf++; --n;
+ ch = *buf++;
+ --n;
if ((ch >= '0') && (ch <= '7')) {
ch -= '0';
if (n && (*buf >= '0') && (*buf <= '7')) {
ch <<= 3;
ch += *buf - '0';
- ++buf; --n;
+ ++buf;
+ --n;
if (n && (*buf >= '0') && (*buf <= '7')) {
ch <<= 3;
ch += *buf - '0';
- ++buf; --n;
+ ++buf;
+ --n;
}
}
}
@@ -58,7 +62,7 @@ int dns_domain_fromdot(char **out,const char *buf,unsigned int n)
if (labellen) {
if (namelen + labellen + 1 > sizeof(name)) return 0;
name[namelen++] = labellen;
- byte_copy(name + namelen,labellen,label);
+ byte_copy(name + namelen, labellen, label);
namelen += labellen;
labellen = 0;
}
@@ -68,7 +72,7 @@ int dns_domain_fromdot(char **out,const char *buf,unsigned int n)
x = alloc(namelen);
if (!x) return DNS_MEM;
- byte_copy(x,namelen,name);
+ byte_copy(x, namelen, name);
if (*out) alloc_free(*out);
*out = x;
diff --git a/src/dnsstub/dns_domain.c b/src/dnsstub/dns_domain.c
index 654a827..4ee7ba3 100644
--- a/src/dnsstub/dns_domain.c
+++ b/src/dnsstub/dns_domain.c
@@ -1,6 +1,6 @@
#include "alloc.h"
-#include "case.h"
#include "byte.h"
+#include "case.h"
#include "dnsresolv.h"
/**
@@ -16,8 +16,7 @@ unsigned int dns_domain_length(const char *dn)
unsigned char c;
x = dn;
- while ((c = *x++))
- x += (unsigned int) c;
+ while ((c = *x++)) x += (unsigned int)c;
return x - dn;
}
@@ -29,7 +28,7 @@ void dns_domain_free(char **out)
}
}
-int dns_domain_copy(char **out,const char *in)
+int dns_domain_copy(char **out, const char *in)
{
unsigned int len;
char *x;
@@ -37,42 +36,42 @@ int dns_domain_copy(char **out,const char *in)
len = dns_domain_length(in);
x = alloc(len);
if (!x) return 0;
- byte_copy(x,len,in);
+ byte_copy(x, len, in);
if (*out) alloc_free(*out);
*out = x;
return 1;
}
-int dns_domain_equal(const char *dn1,const char *dn2)
+int dns_domain_equal(const char *dn1, const char *dn2)
{
unsigned int len;
len = dns_domain_length(dn1);
if (len != dns_domain_length(dn2)) return 0;
- if (case_diffb((char *)dn1,len,(char *)dn2)) return 0; /* safe since 63 < 'A' */
+ if (case_diffb((char *)dn1, len, (char *)dn2)) return 0; /* safe since 63 < 'A' */
return 1;
}
-int dns_domain_suffix(const char *big,const char *little)
+int dns_domain_suffix(const char *big, const char *little)
{
unsigned char c;
for (;;) {
- if (dns_domain_equal(big,little)) return 1;
+ if (dns_domain_equal(big, little)) return 1;
c = *big++;
if (!c) return 0;
big += c;
}
}
-unsigned int dns_domain_suffixpos(const char *big,const char *little)
+unsigned int dns_domain_suffixpos(const char *big, const char *little)
{
const char *orig = big;
unsigned char c;
for (;;) {
- if (dns_domain_equal(big,little)) return big - orig;
+ if (dns_domain_equal(big, little)) return big - orig;
c = *big++;
if (!c) return 0;
big += c;
diff --git a/src/dnsstub/dns_dtda.c b/src/dnsstub/dns_dtda.c
index 38358a2..13e6051 100644
--- a/src/dnsstub/dns_dtda.c
+++ b/src/dnsstub/dns_dtda.c
@@ -1,5 +1,5 @@
-#include "stralloc.h"
#include "dnsresolv.h"
+#include "stralloc.h"
/**
@file dns_dtda.c
@@ -8,36 +8,36 @@
@brief domain to dot append
*/
-int dns_domain_todot_cat(stralloc *out,const char *d)
+int dns_domain_todot_cat(stralloc *out, const char *d)
{
char ch;
char ch2;
unsigned char ch3;
char buf[4];
- if (!*d)
- return stralloc_append(out,".");
+ if (!*d) return stralloc_append(out, ".");
for (;;) {
ch = *d++;
while (ch--) {
ch2 = *d++;
- if ((ch2 >= 'A') && (ch2 <= 'Z')) ch2 += 32; // FQDN -> lowercase
- if (((ch2 >= 'a') && (ch2 <= 'z')) ||
- ((ch2 >= '0') && (ch2 <= '9')) ||
- (ch2 == '-') || (ch2 == '_')) {
- if (!stralloc_append(out,&ch2)) return DNS_MEM;
- }
- else { // decimal -> octal
+ if ((ch2 >= 'A') && (ch2 <= 'Z')) ch2 += 32; // FQDN -> lowercase
+ if (((ch2 >= 'a') && (ch2 <= 'z')) || ((ch2 >= '0') && (ch2 <= '9')) || (ch2 == '-')
+ || (ch2 == '_'))
+ {
+ if (!stralloc_append(out, &ch2)) return DNS_MEM;
+ } else { // decimal -> octal
ch3 = ch2;
- buf[3] = '0' + (ch3 & 7); ch3 >>= 3;
- buf[2] = '0' + (ch3 & 7); ch3 >>= 3;
+ buf[3] = '0' + (ch3 & 7);
+ ch3 >>= 3;
+ buf[2] = '0' + (ch3 & 7);
+ ch3 >>= 3;
buf[1] = '0' + (ch3 & 7);
buf[0] = '\\';
- if (!stralloc_catb(out,buf,4)) return DNS_MEM;
+ if (!stralloc_catb(out, buf, 4)) return DNS_MEM;
}
}
if (!*d) return 1;
- if (!stralloc_append(out,".")) return DNS_MEM;
+ if (!stralloc_append(out, ".")) return DNS_MEM;
}
}
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;
}
diff --git a/src/dnsstub/dns_ipq.c b/src/dnsstub/dns_ipq.c
index 26c3818..d1a40e5 100644
--- a/src/dnsstub/dns_ipq.c
+++ b/src/dnsstub/dns_ipq.c
@@ -1,10 +1,10 @@
-#include "case.h"
#include "byte.h"
-#include "str.h"
-#include "stralloc.h"
+#include "case.h"
#include "dnsresolv.h"
-#include "socket_if.h"
#include "ip.h"
+#include "socket_if.h"
+#include "str.h"
+#include "stralloc.h"
/**
@file dns_ipq.c
@@ -16,7 +16,7 @@
/**
@fn int doit -> @return number of added chars to name
*/
-static int doit(stralloc *work,const char *rule)
+static int doit(stralloc *work, const char *rule)
{
char ch;
unsigned int colon;
@@ -24,27 +24,27 @@ static int doit(stralloc *work,const char *rule)
ch = *rule++;
if ((ch != '?') && (ch != '=') && (ch != '*') && (ch != '-')) return 1;
- colon = str_chr((char *)rule,':');
+ colon = str_chr((char *)rule, ':');
if (!rule[colon]) return 1;
if (work->len < colon) return 1;
prefixlen = work->len - colon;
if ((ch == '=') && prefixlen) return 1;
- if (case_diffb((char *)rule,colon,work->s + prefixlen)) return 1;
+ if (case_diffb((char *)rule, colon, work->s + prefixlen)) return 1;
if (ch == '?') {
- if (byte_chr(work->s,prefixlen,'.') < prefixlen) return 1;
- if (byte_chr(work->s,prefixlen,'[') < prefixlen) return 1;
- if (byte_chr(work->s,prefixlen,']') < prefixlen) return 1;
+ if (byte_chr(work->s, prefixlen, '.') < prefixlen) return 1;
+ if (byte_chr(work->s, prefixlen, '[') < prefixlen) return 1;
+ if (byte_chr(work->s, prefixlen, ']') < prefixlen) return 1;
}
work->len = prefixlen;
if (ch == '-') work->len = 0;
- return stralloc_cats(work,rule + colon + 1);
+ return stralloc_cats(work, rule + colon + 1);
}
/** @fn int dns_ip4_qualify_rules -> @return number of IPv4 addresss with rules */
-int dns_ip4_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const stralloc *rules)
+int dns_ip4_qualify_rules(stralloc *ipout, stralloc *fqdn, const stralloc *in, const stralloc *rules)
{
unsigned int i;
unsigned int j;
@@ -52,25 +52,24 @@ int dns_ip4_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,cons
unsigned int fqdnlen;
int rc = 0;
- if (!stralloc_copy(fqdn,(stralloc *)in)) return DNS_MEM;
+ if (!stralloc_copy(fqdn, (stralloc *)in)) return DNS_MEM;
for (j = i = 0; j < rules->len; ++j)
if (!rules->s[j]) {
- if (!doit(fqdn,rules->s + i)) return DNS_INT;
+ if (!doit(fqdn, rules->s + i)) return DNS_INT;
i = j + 1;
}
fqdnlen = fqdn->len;
- plus = byte_chr(fqdn->s,fqdnlen,'+');
- if (plus >= fqdnlen)
- return dns_ip4(ipout,fqdn);
+ plus = byte_chr(fqdn->s, fqdnlen, '+');
+ if (plus >= fqdnlen) return dns_ip4(ipout, fqdn);
i = plus + 1;
for (;;) {
- j = byte_chr(fqdn->s + i,fqdnlen - i,'+');
- byte_copy(fqdn->s + plus,j,fqdn->s + i);
+ j = byte_chr(fqdn->s + i, fqdnlen - i, '+');
+ byte_copy(fqdn->s + plus, j, fqdn->s + i);
fqdn->len = plus + j;
- if (rc += dns_ip4(ipout,fqdn) < 0) return DNS_ERR;
+ if (rc += dns_ip4(ipout, fqdn) < 0) return DNS_ERR;
i += j;
if (i >= fqdnlen) return rc;
++i;
@@ -80,45 +79,44 @@ int dns_ip4_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,cons
/** @fn int dns_ip4_qualify -> @return number of IPv4 addresss qualified */
-int dns_ip4_qualify(stralloc *ipout,stralloc *fqdn,const stralloc *in)
+int dns_ip4_qualify(stralloc *ipout, stralloc *fqdn, const stralloc *in)
{
int r;
static stralloc rules;
- if ((r = dns_ip_qualify_localhost(ipout,fqdn,in)) > 0 ) return r;
+ if ((r = dns_ip_qualify_localhost(ipout, fqdn, in)) > 0) return r;
if (dns_resolvconfrewrite(&rules) < 0) return DNS_INT;
- return dns_ip4_qualify_rules(ipout,fqdn,in,&rules);
+ return dns_ip4_qualify_rules(ipout, fqdn, in, &rules);
}
/** @fn int dns_ip4_qualify_rules -> @return number of IPv6 addresss with rules */
-int dns_ip6_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const stralloc *rules)
+int dns_ip6_qualify_rules(stralloc *ipout, stralloc *fqdn, const stralloc *in, const stralloc *rules)
{
unsigned int i;
unsigned int j;
unsigned int plus;
unsigned int fqdnlen;
- int rc = 0;
+ int rc = 0;
- if (!stralloc_copy(fqdn,(stralloc *)in)) return DNS_MEM;
+ if (!stralloc_copy(fqdn, (stralloc *)in)) return DNS_MEM;
for (j = i = 0; j < rules->len; ++j)
if (!rules->s[j]) {
- if (!doit(fqdn,rules->s + i)) return DNS_INT;
+ if (!doit(fqdn, rules->s + i)) return DNS_INT;
i = j + 1;
}
fqdnlen = fqdn->len;
- plus = byte_chr(fqdn->s,fqdnlen,'+');
- if (plus >= fqdnlen)
- return dns_ip6(ipout,fqdn);
+ plus = byte_chr(fqdn->s, fqdnlen, '+');
+ if (plus >= fqdnlen) return dns_ip6(ipout, fqdn);
i = plus + 1;
for (;;) {
- j = byte_chr(fqdn->s + i,fqdnlen - i,'+');
- byte_copy(fqdn->s + plus,j,fqdn->s + i);
+ j = byte_chr(fqdn->s + i, fqdnlen - i, '+');
+ byte_copy(fqdn->s + plus, j, fqdn->s + i);
fqdn->len = plus + j;
- if ((rc += dns_ip6(ipout,fqdn)) < 0) return DNS_ERR;
+ if ((rc += dns_ip6(ipout, fqdn)) < 0) return DNS_ERR;
i += j;
if (i >= fqdnlen) return rc;
++i;
@@ -128,19 +126,19 @@ int dns_ip6_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,cons
/** @fn int dns_ip6_qualify -> @return number of IPv6 addresss qualified */
-int dns_ip6_qualify(stralloc *ipout,stralloc *fqdn,const stralloc *in)
+int dns_ip6_qualify(stralloc *ipout, stralloc *fqdn, const stralloc *in)
{
- int r;
+ int r;
static stralloc rules;
-
- if ((r = dns_ip_qualify_localhost(ipout,fqdn,in)) > 0) return r;
+
+ if ((r = dns_ip_qualify_localhost(ipout, fqdn, in)) > 0) return r;
if (dns_resolvconfrewrite(&rules) < 0) return DNS_INT;
- return dns_ip6_qualify_rules(ipout,fqdn,in,&rules);
+ return dns_ip6_qualify_rules(ipout, fqdn, in, &rules);
}
/** @fn int dns_ip_qualify_rules -> @return number of IPv6+IPv4 addresss with rules */
-int dns_ip_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const stralloc *rules)
+int dns_ip_qualify_rules(stralloc *ipout, stralloc *fqdn, const stralloc *in, const stralloc *rules)
{
unsigned int i;
unsigned int j;
@@ -148,25 +146,25 @@ int dns_ip_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const
unsigned int plus;
unsigned int fqdnlen;
stralloc tmp = {0};
- int rc = 0;
+ int rc = 0;
- if (!stralloc_copy(fqdn,(stralloc *)in)) return DNS_MEM;
- if (!stralloc_copys(ipout,"")) return DNS_MEM;
+ if (!stralloc_copy(fqdn, (stralloc *)in)) return DNS_MEM;
+ if (!stralloc_copys(ipout, "")) return DNS_MEM;
for (j = i = 0; j < rules->len; ++j)
if (!rules->s[j]) {
- if (!doit(fqdn,rules->s + i)) return DNS_INT;
+ if (!doit(fqdn, rules->s + i)) return DNS_INT;
i = j + 1;
}
fqdnlen = fqdn->len;
- plus = byte_chr(fqdn->s,fqdnlen,'+');
+ plus = byte_chr(fqdn->s, fqdnlen, '+');
if (plus >= fqdnlen) {
- rc = dns_ip6(ipout,fqdn);
- if (dns_ip4(&tmp,fqdn) > 0) {
+ rc = dns_ip6(ipout, fqdn);
+ if (dns_ip4(&tmp, fqdn) > 0) {
for (k = 0; k < tmp.len; k += 4) {
- if (!stralloc_catb(ipout,(const char *) V4mappedprefix,12)) return DNS_MEM;
- if (!stralloc_catb(ipout,tmp.s + k,4)) return DNS_MEM;
+ if (!stralloc_catb(ipout, (const char *)V4mappedprefix, 12)) return DNS_MEM;
+ if (!stralloc_catb(ipout, tmp.s + k, 4)) return DNS_MEM;
rc++;
}
}
@@ -175,20 +173,21 @@ int dns_ip_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const
i = plus + 1;
for (;;) {
- j = byte_chr(fqdn->s + i,fqdnlen - i,'+');
- byte_copy(fqdn->s + plus,j,fqdn->s + i);
+ j = byte_chr(fqdn->s + i, fqdnlen - i, '+');
+ byte_copy(fqdn->s + plus, j, fqdn->s + i);
fqdn->len = plus + j;
- if (!stralloc_copys(ipout,"")) return DNS_MEM;
- rc = dns_ip6(&tmp,fqdn);
- if (rc) if (!stralloc_cat(ipout,&tmp)) return DNS_MEM;
- if (dns_ip4(&tmp,fqdn) > 0) {
+ if (!stralloc_copys(ipout, "")) return DNS_MEM;
+ rc = dns_ip6(&tmp, fqdn);
+ if (rc)
+ if (!stralloc_cat(ipout, &tmp)) return DNS_MEM;
+ if (dns_ip4(&tmp, fqdn) > 0) {
for (k = 0; k < tmp.len; k += 4) {
- if (!stralloc_catb(ipout,(const char *) V4mappedprefix,12)) return DNS_MEM;
- if (!stralloc_catb(ipout,tmp.s + k,4)) return DNS_MEM;
+ if (!stralloc_catb(ipout, (const char *)V4mappedprefix, 12)) return DNS_MEM;
+ if (!stralloc_catb(ipout, tmp.s + k, 4)) return DNS_MEM;
rc++;
}
}
-
+
if (rc < 0) return DNS_ERR;
i += j;
if (i >= fqdnlen) return rc;
@@ -199,38 +198,38 @@ int dns_ip_qualify_rules(stralloc *ipout,stralloc *fqdn,const stralloc *in,const
/** @fn int dns_ip_qualify_localhost -> @return number of IP addresss */
-int dns_ip_qualify_localhost(stralloc *ipout,stralloc *fqdn,const stralloc *in)
+int dns_ip_qualify_localhost(stralloc *ipout, stralloc *fqdn, const stralloc *in)
{
- if (!stralloc_copys(ipout,"")) return DNS_MEM;
- if (!stralloc_copys(fqdn,"")) return DNS_MEM;
+ if (!stralloc_copys(ipout, "")) return DNS_MEM;
+ if (!stralloc_copys(fqdn, "")) return DNS_MEM;
ipout->len = 0;
- if (byte_equal(in->s,9,LOCALHOST)) {
- if (!stralloc_copyb(ipout,(const char *) V6loopback,16)) return DNS_MEM;
- if (!stralloc_catb(ipout,(const char *) V46loopback,16)) return DNS_MEM;
- if (!stralloc_copys(fqdn,"localhost.localhost.")) return DNS_MEM;
+ if (byte_equal(in->s, 9, LOCALHOST)) {
+ if (!stralloc_copyb(ipout, (const char *)V6loopback, 16)) return DNS_MEM;
+ if (!stralloc_catb(ipout, (const char *)V46loopback, 16)) return DNS_MEM;
+ if (!stralloc_copys(fqdn, "localhost.localhost.")) return DNS_MEM;
}
- if (byte_equal(in->s,13,IP4_LOOPBACK)) {
- if (!stralloc_copyb(ipout,(const char *) V46loopback,16)) return DNS_MEM;
- if (!stralloc_copys(fqdn,"ip4-loopback.localhost.")) return DNS_MEM;
+ if (byte_equal(in->s, 13, IP4_LOOPBACK)) {
+ if (!stralloc_copyb(ipout, (const char *)V46loopback, 16)) return DNS_MEM;
+ if (!stralloc_copys(fqdn, "ip4-loopback.localhost.")) return DNS_MEM;
}
- if (byte_equal(in->s,13,IP6_LOOPBACK)) {
- if (!stralloc_copyb(ipout,(const char *) V6loopback,16)) return DNS_MEM;
- if (!stralloc_copys(fqdn,"ip6-loopback.localhost.")) return DNS_MEM;
+ if (byte_equal(in->s, 13, IP6_LOOPBACK)) {
+ if (!stralloc_copyb(ipout, (const char *)V6loopback, 16)) return DNS_MEM;
+ if (!stralloc_copys(fqdn, "ip6-loopback.localhost.")) return DNS_MEM;
}
-// if (!stralloc_0(fqdn)) return DNS_MEM; // don't do it
+ // if (!stralloc_0(fqdn)) return DNS_MEM; // don't do it
return ipout->len ? ipout->len % 15 : 0;
}
/** @fn int dns_ip_qualify -> @return number of IP addresss */
-int dns_ip_qualify(stralloc *ipout,stralloc *fqdn,const stralloc *in)
+int dns_ip_qualify(stralloc *ipout, stralloc *fqdn, const stralloc *in)
{
int r;
static stralloc rules;
- if ((r = dns_ip_qualify_localhost(ipout,fqdn,in)) > 0 ) return r;
+ if ((r = dns_ip_qualify_localhost(ipout, fqdn, in)) > 0) return r;
if (dns_resolvconfrewrite(&rules) < 0) return DNS_INT;
- return dns_ip_qualify_rules(ipout,fqdn,in,&rules);
+ return dns_ip_qualify_rules(ipout, fqdn, in, &rules);
}
diff --git a/src/dnsstub/dns_mx.c b/src/dnsstub/dns_mx.c
index c0845ef..3a140d2 100644
--- a/src/dnsstub/dns_mx.c
+++ b/src/dnsstub/dns_mx.c
@@ -1,7 +1,7 @@
-#include "stralloc.h"
#include "byte.h"
-#include "uint_t.h"
#include "dnsresolv.h"
+#include "stralloc.h"
+#include "uint_t.h"
/**
@file dns_mx.c
@@ -13,7 +13,7 @@
static char *q = 0;
-int dns_mx_packet(stralloc *out,const char *buf,unsigned int len)
+int dns_mx_packet(stralloc *out, const char *buf, unsigned int len)
{
unsigned int pos;
char header[12];
@@ -22,23 +22,27 @@ int dns_mx_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_MX))
- if (byte_equal(header + 2,2,DNS_C_IN)) {
- if (!dns_packet_copy(buf,len,pos,pref,2)) return DNS_ERR;
- if (!dns_packet_getname(buf,len,pos + 2,&q)) return DNS_ERR;
- if (!stralloc_catb(out,pref,2)) return DNS_MEM;
- if (dns_domain_todot_cat(out,q) <= 0) return DNS_ERR;
+ 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_MX))
+ if (byte_equal(header + 2, 2, DNS_C_IN)) {
+ if (!dns_packet_copy(buf, len, pos, pref, 2)) return DNS_ERR;
+ if (!dns_packet_getname(buf, len, pos + 2, &q)) return DNS_ERR;
+ if (!stralloc_catb(out, pref, 2)) return DNS_MEM;
+ if (dns_domain_todot_cat(out, q) <= 0) return DNS_ERR;
if (!stralloc_0(out)) return DNS_MEM;
}
pos += datalen;
@@ -48,16 +52,16 @@ int dns_mx_packet(stralloc *out,const char *buf,unsigned int len)
return ranswers;
}
-int dns_mx(stralloc *out,const stralloc *fqdn)
+int dns_mx(stralloc *out, const stralloc *fqdn)
{
int rc = 0;
- if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR;
- if (dns_resolve(q,DNS_T_MX) >= 0) {
- if ((rc = dns_mx_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;
+ if (dns_resolve(q, DNS_T_MX) >= 0) {
+ if ((rc = dns_mx_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR;
dns_transmit_free(&dns_resolve_tx);
- dns_domain_free(&q);
+ dns_domain_free(&q);
}
return rc;
-}
+}
diff --git a/src/dnsstub/dns_name.c b/src/dnsstub/dns_name.c
index 0723a8f..db4565a 100644
--- a/src/dnsstub/dns_name.c
+++ b/src/dnsstub/dns_name.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_name.c
@@ -13,28 +13,32 @@
static char *q = 0;
-int dns_name_packet(stralloc *out,const char *buf,unsigned int len)
+int dns_name_packet(stralloc *out, const char *buf, unsigned int len)
{
unsigned int pos;
char header[12];
uint16 numanswers;
uint16 datalen;
- 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_PTR))
- if (byte_equal(header + 2,2,DNS_C_IN)) {
- if (!dns_packet_getname(buf,len,pos,&q)) return DNS_ERR;
- if (dns_domain_todot_cat(out,q) <= 0) return DNS_ERR;
+ 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_PTR))
+ if (byte_equal(header + 2, 2, DNS_C_IN)) {
+ if (!dns_packet_getname(buf, len, pos, &q)) return DNS_ERR;
+ if (dns_domain_todot_cat(out, q) <= 0) return DNS_ERR;
return 1;
}
pos += datalen;
@@ -43,38 +47,38 @@ int dns_name_packet(stralloc *out,const char *buf,unsigned int len)
return 0;
}
-int dns_name4(stralloc *out,const char ip[4])
+int dns_name4(stralloc *out, const char ip[4])
{
int rc;
char name[DNS_NAME4_DOMAIN];
- dns_name4_domain(name,ip);
- if (dns_resolve(name,DNS_T_PTR) < 0) return DNS_ERR;
- if ((rc = dns_name_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR;
+ dns_name4_domain(name, ip);
+ if (dns_resolve(name, DNS_T_PTR) < 0) return DNS_ERR;
+ if ((rc = dns_name_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR;
dns_transmit_free(&dns_resolve_tx);
dns_domain_free(&q);
return rc;
}
-int dns_name6(stralloc *out,const char ip[16])
+int dns_name6(stralloc *out, const char ip[16])
{
int rc;
char name[DNS_NAME6_DOMAIN];
- dns_name6_domain(name,ip);
- if (dns_resolve(name,DNS_T_PTR) < 0) return DNS_ERR;
- if ((rc = dns_name_packet(out,dns_resolve_tx.packet,dns_resolve_tx.packetlen)) < 0) return DNS_ERR;
+ dns_name6_domain(name, ip);
+ if (dns_resolve(name, DNS_T_PTR) < 0) return DNS_ERR;
+ if ((rc = dns_name_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR;
dns_transmit_free(&dns_resolve_tx);
dns_domain_free(&q);
return rc;
}
-int dns_name(stralloc *out,const char ip[16])
+int dns_name(stralloc *out, const char ip[16])
{
if (ip6_isv4mapped(ip))
- return dns_name4(out,ip+12);
+ return dns_name4(out, ip + 12);
else
- return dns_name6(out,ip);
+ return dns_name6(out, ip);
}
diff --git a/src/dnsstub/dns_nd.c b/src/dnsstub/dns_nd.c
index 6ce8ed9..3bbbeca 100644
--- a/src/dnsstub/dns_nd.c
+++ b/src/dnsstub/dns_nd.c
@@ -1,7 +1,7 @@
#include "byte.h"
+#include "dnsresolv.h"
#include "fmt.h"
#include "ip.h"
-#include "dnsresolv.h"
/**
@file dns_nd.c
@@ -10,29 +10,29 @@
@brief DNS domain name for ip (wire format)
*/
-int dns_name4_domain(char name[DNS_NAME4_DOMAIN],const char ip[4])
+int dns_name4_domain(char name[DNS_NAME4_DOMAIN], const char ip[4])
{
unsigned int namelen;
unsigned int i;
namelen = 0;
- i = fmt_ulong(name + namelen + 1,(unsigned long) (unsigned char) ip[3]);
+ i = fmt_ulong(name + namelen + 1, (unsigned long)(unsigned char)ip[3]);
name[namelen++] = i;
namelen += i;
- i = fmt_ulong(name + namelen + 1,(unsigned long) (unsigned char) ip[2]);
+ i = fmt_ulong(name + namelen + 1, (unsigned long)(unsigned char)ip[2]);
name[namelen++] = i;
namelen += i;
- i = fmt_ulong(name + namelen + 1,(unsigned long) (unsigned char) ip[1]);
+ i = fmt_ulong(name + namelen + 1, (unsigned long)(unsigned char)ip[1]);
name[namelen++] = i;
namelen += i;
- i = fmt_ulong(name + namelen + 1,(unsigned long) (unsigned char) ip[0]);
+ i = fmt_ulong(name + namelen + 1, (unsigned long)(unsigned char)ip[0]);
name[namelen++] = i;
namelen += i;
- byte_copy(name + namelen,14,"\7in-addr\4arpa\0");
- return namelen+14;
+ byte_copy(name + namelen, 14, "\7in-addr\4arpa\0");
+ return namelen + 14;
}
-int dns_name6_domain(char name[DNS_NAME6_DOMAIN],const char ip[16])
+int dns_name6_domain(char name[DNS_NAME6_DOMAIN], const char ip[16])
{
unsigned int j;
@@ -42,7 +42,6 @@ int dns_name6_domain(char name[DNS_NAME6_DOMAIN],const char ip[16])
name[j * 4 + 2] = 1;
name[j * 4 + 3] = tohex((unsigned char)ip[15 - j] >> 4);
}
- byte_copy(name + 4 * 16,10,"\3ip6\4arpa\0");
+ byte_copy(name + 4 * 16, 10, "\3ip6\4arpa\0");
return 4 * 16 + 10;
}
-
diff --git a/src/dnsstub/dns_packet.c b/src/dnsstub/dns_packet.c
index ce322ea..2b2040f 100644
--- a/src/dnsstub/dns_packet.c
+++ b/src/dnsstub/dns_packet.c
@@ -1,5 +1,5 @@
-#include "error.h"
#include "dnsresolv.h"
+#include "error.h"
/**
@file dns_packet.c
@@ -9,17 +9,22 @@
@brief DNS should have used LZ77 instead of its own sophomoric compression algorithm.
*/
-unsigned int dns_packet_copy(const char *buf,unsigned int len,unsigned int pos,char *out,unsigned int outlen)
+unsigned int dns_packet_copy(
+ const char *buf, unsigned int len, unsigned int pos, char *out, unsigned int outlen)
{
while (outlen) {
- if (pos >= len) { errno = EPROTO; return 0; }
+ if (pos >= len) {
+ errno = EPROTO;
+ return 0;
+ }
*out = buf[pos++];
- ++out; --outlen;
+ ++out;
+ --outlen;
}
return pos;
}
-unsigned int dns_packet_skipname(const char *buf,unsigned int len,unsigned int pos)
+unsigned int dns_packet_skipname(const char *buf, unsigned int len, unsigned int pos)
{
unsigned char ch;
@@ -36,7 +41,7 @@ unsigned int dns_packet_skipname(const char *buf,unsigned int len,unsigned int p
return 0;
}
-unsigned int dns_packet_getname(const char *buf,unsigned int len,unsigned int pos,char **d)
+unsigned int dns_packet_getname(const char *buf, unsigned int len, unsigned int pos, char **d)
{
unsigned int loop = 0;
unsigned int state = 0;
@@ -47,39 +52,41 @@ unsigned int dns_packet_getname(const char *buf,unsigned int len,unsigned int po
unsigned int namelen = 0;
for (;;) {
- if (pos >= len) goto PROTO;
+ if (pos >= len) goto PROTO;
ch = buf[pos++];
if (++loop >= 1000) goto PROTO;
if (state) {
- if (namelen + 1 > sizeof(name)) goto PROTO;
+ if (namelen + 1 > sizeof(name)) goto PROTO;
name[namelen++] = ch;
--state;
} else {
while (ch >= 192) {
- where = ch; where -= 192; where <<= 8;
- if (pos >= len) goto PROTO;
+ where = ch;
+ where -= 192;
+ where <<= 8;
+ if (pos >= len) goto PROTO;
ch = buf[pos++];
if (!firstcompress) firstcompress = pos;
pos = where + ch;
- if (pos >= len) goto PROTO;
+ if (pos >= len) goto PROTO;
ch = buf[pos++];
if (++loop >= 1000) goto PROTO;
}
if (ch >= 64) goto PROTO;
- if (namelen + 1 > sizeof(name)) goto PROTO;
+ if (namelen + 1 > sizeof(name)) goto PROTO;
name[namelen++] = ch;
if (!ch) break;
state = ch;
}
}
- if (!dns_domain_copy(d,name)) return 0;
+ if (!dns_domain_copy(d, name)) return 0;
if (firstcompress) return firstcompress;
return pos;
- PROTO:
+PROTO:
errno = EPROTO;
return 0;
}
diff --git a/src/dnsstub/dns_random.c b/src/dnsstub/dns_random.c
index 200cd6c..8b89244 100644
--- a/src/dnsstub/dns_random.c
+++ b/src/dnsstub/dns_random.c
@@ -1,7 +1,8 @@
#include <unistd.h>
+
+#include "dnsresolv.h"
#include "taia.h"
#include "uint_t.h"
-#include "dnsresolv.h"
/**
@file dns_random.c
@@ -15,13 +16,17 @@ static uint32 in[12];
static uint32 out[8];
static int outleft = 0;
-#define ROTATE(x,b) (((x) << (b)) | ((x) >> (32 - (b))))
-#define MUSH(i,b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x,b));
+#define ROTATE(x, b) (((x) << (b)) | ((x) >> (32 - (b))))
+#define MUSH(i, b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x, b))
static void surf(void)
{
- uint32 t[12]; uint32 x; uint32 sum = 0;
- int r; int i; int loop;
+ uint32 t[12];
+ uint32 x;
+ uint32 sum = 0;
+ int r;
+ int i;
+ int loop;
for (i = 0; i < 12; ++i) t[i] = in[i] ^ seed[12 + i];
for (i = 0; i < 8; ++i) out[i] = seed[24 + i];
@@ -29,9 +34,21 @@ static void surf(void)
for (loop = 0; loop < 2; ++loop) {
for (r = 0; r < 16; ++r) {
sum += 0x9e3779b9;
- MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13)
- MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13)
- MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13)
+
+ MUSH(0, 5);
+ MUSH(1, 7);
+ MUSH(2, 9);
+ MUSH(3, 13);
+
+ MUSH(4, 5);
+ MUSH(5, 7);
+ MUSH(6, 9);
+ MUSH(7, 13);
+
+ MUSH(8, 5);
+ MUSH(9, 7);
+ MUSH(10, 9);
+ MUSH(11, 13);
}
for (i = 0; i < 8; ++i) out[i] ^= t[i + 4];
}
@@ -43,13 +60,11 @@ void dns_random_init(const char data[128])
struct taia t;
char tpack[16];
- for (i = 0; i < 32; ++i)
- uint32_unpack((char *)data + 4 * i,seed + i);
+ for (i = 0; i < 32; ++i) uint32_unpack((char *)data + 4 * i, seed + i);
taia_now(&t);
- taia_pack(tpack,&t);
- for (i = 0; i < 4; ++i)
- uint32_unpack(tpack + 4 * i,in + 4 + i);
+ taia_pack(tpack, &t);
+ for (i = 0; i < 4; ++i) uint32_unpack(tpack + 4 * i, in + 4 + i);
in[8] = getpid();
in[9] = getppid();
@@ -61,7 +76,9 @@ unsigned int dns_random(unsigned int n)
if (!n) return 0;
if (!outleft) {
- if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
+ if (!++in[0])
+ if (!++in[1])
+ if (!++in[2]) ++in[3];
surf();
outleft = 8;
}
diff --git a/src/dnsstub/dns_rcip.c b/src/dnsstub/dns_rcip.c
index 93b0daa..fd86df9 100644
--- a/src/dnsstub/dns_rcip.c
+++ b/src/dnsstub/dns_rcip.c
@@ -1,10 +1,10 @@
-#include "taia.h"
-#include "readclose.h"
#include "byte.h"
-#include "ip.h"
-#include "env.h"
#include "dnsresolv.h"
+#include "env.h"
+#include "ip.h"
+#include "readclose.h"
#include "socket_if.h"
+#include "taia.h"
/**
@file dns_rcip.c
@@ -16,7 +16,7 @@
static stralloc data = {0};
static stralloc ifname = {0};
-static int init(char ip[QUERY_MAXIPLEN],uint32 sid[QUERY_MAXNS])
+static int init(char ip[QUERY_MAXIPLEN], uint32 sid[QUERY_MAXNS])
{
int i;
int j;
@@ -25,7 +25,7 @@ static int init(char ip[QUERY_MAXIPLEN],uint32 sid[QUERY_MAXNS])
char *x;
char ip4[4];
-/* Read (compactified) IPv4|v6 addresses of resolvers
+ /* Read (compactified) IPv4|v6 addresses of resolvers
Store them in array IP with fixed length :
ip(64) -> 16 IPv4 addresses (not used anymore)
ip(512) -> 16*2 IPv6 addresses (we use IPv4 mapped IPv6 addresses)
@@ -36,39 +36,40 @@ static int init(char ip[QUERY_MAXIPLEN],uint32 sid[QUERY_MAXNS])
x = env_get("DNSCACHEIP");
if (x)
while (iplen <= 240 && *x != '\0') {
- if (*x == ' ')
- ++x;
- else
- if ((i = ip6_ifscan(x,ip + iplen,&ifname))) {
- if (ifname.len > 2) sid[k] = socket_getifidx(ifname.s);
- iplen += 16; k++;
- if (*(x += i) == '\0') break;
- }
+ if (*x == ' ')
+ ++x;
+ else if ((i = ip6_ifscan(x, ip + iplen, &ifname))) {
+ if (ifname.len > 2) sid[k] = socket_getifidx(ifname.s);
+ iplen += 16;
+ k++;
+ if (*(x += i) == '\0') break;
+ }
}
if (!iplen) {
- i = openreadclose("/etc/resolv.conf",&data,64);
+ i = openreadclose("/etc/resolv.conf", &data, 64);
if (i == -1) return DNS_INT;
if (i) {
- if (!stralloc_append(&data,"\n")) return DNS_MEM;
+ if (!stralloc_append(&data, "\n")) return DNS_MEM;
i = 0;
for (j = 0; j < data.len; ++j)
if (data.s[j] == '\n') {
- if (byte_equal("nameserver ",11,data.s + i) || byte_equal("nameserver\t",11,data.s + i)) {
- i += 10;
- while ((data.s[i] == ' ') || (data.s[i] == '\t'))
- i++;
+ if (byte_equal("nameserver ", 11, data.s + i) || byte_equal("nameserver\t", 11, data.s + i)) {
+ i += 10;
+ while ((data.s[i] == ' ') || (data.s[i] == '\t')) i++;
if (iplen <= 240) {
data.s[j] = '\0'; /* ip6_ifscan needs terminated string on input */
- if (ip4_scan(data.s + i,ip4)) {
- if (byte_equal(ip4,4,"\0\0\0\0"))
- byte_copy(ip4,4,"\177\0\0\1");
- byte_copy(ip + iplen,12,V4mappedprefix);
- byte_copy(ip + iplen + 12,4,ip4);
- sid[k] = 0; iplen += 16; k++;
- } else if (ip6_ifscan(data.s + i,ip + iplen,&ifname)) {
+ if (ip4_scan(data.s + i, ip4)) {
+ if (byte_equal(ip4, 4, "\0\0\0\0")) byte_copy(ip4, 4, "\177\0\0\1");
+ byte_copy(ip + iplen, 12, V4mappedprefix);
+ byte_copy(ip + iplen + 12, 4, ip4);
+ sid[k] = 0;
+ iplen += 16;
+ k++;
+ } else if (ip6_ifscan(data.s + i, ip + iplen, &ifname)) {
if (ifname.len > 2) sid[k] = socket_getifidx(ifname.s);
- iplen += 16; k++;
+ iplen += 16;
+ k++;
}
}
}
@@ -78,10 +79,10 @@ static int init(char ip[QUERY_MAXIPLEN],uint32 sid[QUERY_MAXNS])
}
if (!iplen) {
- byte_copy(ip,16,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1");
+ byte_copy(ip, 16, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1");
iplen = 16;
}
- byte_zero(ip + iplen,QUERY_MAXIPLEN - iplen);
+ byte_zero(ip + iplen, QUERY_MAXIPLEN - iplen);
return 0;
}
@@ -89,26 +90,26 @@ static int ok = 0;
static unsigned int uses;
static struct taia deadline;
static char ip[QUERY_MAXIPLEN]; /* defined if ok */
-static uint32 scopes[QUERY_MAXNS];
+static uint32 scopes[QUERY_MAXNS];
-int dns_resolvconfip(char s[QUERY_MAXIPLEN],uint32 scope[QUERY_MAXNS])
+int dns_resolvconfip(char s[QUERY_MAXIPLEN], uint32 scope[QUERY_MAXNS])
{
struct taia now;
taia_now(&now);
- if (taia_less(&deadline,&now)) ok = 0;
+ if (taia_less(&deadline, &now)) ok = 0;
if (!uses) ok = 0;
if (!ok) {
- if (init(ip,scopes) < 0) return DNS_INT;
- taia_uint(&deadline,600);
- taia_add(&deadline,&now,&deadline);
+ if (init(ip, scopes) < 0) return DNS_INT;
+ taia_uint(&deadline, 600);
+ taia_add(&deadline, &now, &deadline);
uses = 10000;
ok = 1;
}
--uses;
- byte_copy(s,QUERY_MAXIPLEN,ip);
- byte_copy(scope,128,scopes);
+ byte_copy(s, QUERY_MAXIPLEN, ip);
+ byte_copy(scope, 128, scopes);
return 0;
}
diff --git a/src/dnsstub/dns_rcrw.c b/src/dnsstub/dns_rcrw.c
index 4633fed..8c15591 100644
--- a/src/dnsstub/dns_rcrw.c
+++ b/src/dnsstub/dns_rcrw.c
@@ -1,10 +1,11 @@
#include <unistd.h>
-#include "taia.h"
-#include "env.h"
+
#include "byte.h"
-#include "str.h"
-#include "readclose.h"
#include "dnsresolv.h"
+#include "env.h"
+#include "readclose.h"
+#include "str.h"
+#include "taia.h"
/**
@file dns_rcrw.c
@@ -23,25 +24,24 @@ static int init(stralloc *rules)
int j;
int k;
- if (!stralloc_copys(rules,"")) return DNS_MEM;
+ if (!stralloc_copys(rules, "")) return DNS_MEM;
x = env_get("DNSREWRITEFILE");
if (!x) x = "/etc/dnsrewrite";
- i = openreadclose(x,&data,64);
+ i = openreadclose(x, &data, 64);
if (i == -1) return DNS_INT;
if (i) {
- if (!stralloc_append(&data,"\n")) return DNS_MEM;
+ if (!stralloc_append(&data, "\n")) return DNS_MEM;
i = 0;
for (j = 0; j < data.len; ++j)
if (data.s[j] == '\n') {
- if (!stralloc_catb(rules,data.s + i,j - i)) return DNS_MEM;
+ if (!stralloc_catb(rules, data.s + i, j - i)) return DNS_MEM;
while (rules->len) {
if (rules->s[rules->len - 1] != ' ')
- if (rules->s[rules->len - 1] != '\t')
- if (rules->s[rules->len - 1] != '\r')
- break;
+ if (rules->s[rules->len - 1] != '\t')
+ if (rules->s[rules->len - 1] != '\r') break;
--rules->len;
}
if (!stralloc_0(rules)) return DNS_MEM;
@@ -52,46 +52,48 @@ static int init(stralloc *rules)
x = env_get("LOCALDOMAIN");
if (x) {
- if (!stralloc_copys(&data,x)) return DNS_MEM;
- if (!stralloc_append(&data," ")) return DNS_MEM;
- if (!stralloc_copys(rules,"?:")) return DNS_MEM;
+ if (!stralloc_copys(&data, x)) return DNS_MEM;
+ if (!stralloc_append(&data, " ")) return DNS_MEM;
+ if (!stralloc_copys(rules, "?:")) return DNS_MEM;
i = 0;
for (j = 0; j < data.len; ++j)
if (data.s[j] == ' ') {
- if (!stralloc_cats(rules,"+.")) return DNS_MEM;
- if (!stralloc_catb(rules,data.s + i,j - i)) return DNS_MEM;
+ if (!stralloc_cats(rules, "+.")) return DNS_MEM;
+ if (!stralloc_catb(rules, data.s + i, j - i)) return DNS_MEM;
i = j + 1;
}
if (!stralloc_0(rules)) return DNS_MEM;
- if (!stralloc_cats(rules,"*.:")) return DNS_MEM;
+ if (!stralloc_cats(rules, "*.:")) return DNS_MEM;
if (!stralloc_0(rules)) return DNS_MEM;
return 0;
}
- i = openreadclose("/etc/resolv.conf",&data,64);
+ i = openreadclose("/etc/resolv.conf", &data, 64);
if (i == -1) return DNS_INT;
if (i) {
- if (!stralloc_append(&data,"\n")) return DNS_MEM;
+ if (!stralloc_append(&data, "\n")) return DNS_MEM;
i = 0;
for (j = 0; j < data.len; ++j)
if (data.s[j] == '\n') {
- if (byte_equal("search ",7,data.s + i) ||
- byte_equal("search\t",7,data.s + i) ||
- byte_equal("domain ",7,data.s + i) ||
- byte_equal("domain\t",7,data.s + i)) {
- if (!stralloc_copys(rules,"?:")) return DNS_MEM;
+ if (byte_equal("search ", 7, data.s + i) || byte_equal("search\t", 7, data.s + i)
+ || byte_equal("domain ", 7, data.s + i) || byte_equal("domain\t", 7, data.s + i))
+ {
+ if (!stralloc_copys(rules, "?:")) return DNS_MEM;
i += 7;
while (i < j) {
- k = byte_chr(data.s + i,j - i,' ');
- k = byte_chr(data.s + i,k,'\t');
- if (!k) { ++i; continue; }
- if (!stralloc_cats(rules,"+.")) return DNS_MEM;
- if (!stralloc_catb(rules,data.s + i,k)) return DNS_MEM;
+ k = byte_chr(data.s + i, j - i, ' ');
+ k = byte_chr(data.s + i, k, '\t');
+ if (!k) {
+ ++i;
+ continue;
+ }
+ if (!stralloc_cats(rules, "+.")) return DNS_MEM;
+ if (!stralloc_catb(rules, data.s + i, k)) return DNS_MEM;
i += k;
}
if (!stralloc_0(rules)) return DNS_MEM;
- if (!stralloc_cats(rules,"*.:")) return DNS_MEM;
+ if (!stralloc_cats(rules, "*.:")) return DNS_MEM;
if (!stralloc_0(rules)) return DNS_MEM;
return 0;
}
@@ -100,15 +102,15 @@ static int init(stralloc *rules)
}
host[0] = 0;
- if (gethostname(host,sizeof(host)) == -1) return DNS_ERR;
+ if (gethostname(host, sizeof(host)) == -1) return DNS_ERR;
host[(sizeof(host)) - 1] = 0;
- i = str_chr(host,'.');
+ i = str_chr(host, '.');
if (host[i]) {
- if (!stralloc_copys(rules,"?:")) return DNS_MEM;
- if (!stralloc_cats(rules,host + i)) return DNS_MEM;
+ if (!stralloc_copys(rules, "?:")) return DNS_MEM;
+ if (!stralloc_cats(rules, host + i)) return DNS_MEM;
if (!stralloc_0(rules)) return DNS_MEM;
}
- if (!stralloc_cats(rules,"*.:")) return DNS_MEM;
+ if (!stralloc_cats(rules, "*.:")) return DNS_MEM;
if (!stralloc_0(rules)) return DNS_MEM;
return 0;
@@ -124,18 +126,18 @@ int dns_resolvconfrewrite(stralloc *out)
struct taia now;
taia_now(&now);
- if (taia_less(&deadline,&now)) ok = 0;
+ if (taia_less(&deadline, &now)) ok = 0;
if (!uses) ok = 0;
if (!ok) {
if (init(&rules) < 0) return DNS_INT;
- taia_uint(&deadline,600);
- taia_add(&deadline,&now,&deadline);
+ taia_uint(&deadline, 600);
+ taia_add(&deadline, &now, &deadline);
uses = 10000;
ok = 1;
}
--uses;
- if (!stralloc_copy(out,&rules)) return DNS_MEM;
+ if (!stralloc_copy(out, &rules)) return DNS_MEM;
return 0;
}
diff --git a/src/dnsstub/dns_resolve.c b/src/dnsstub/dns_resolve.c
index bcc4308..90c5615 100644
--- a/src/dnsstub/dns_resolve.c
+++ b/src/dnsstub/dns_resolve.c
@@ -1,8 +1,8 @@
-#include "iopause.h"
-#include "taia.h"
#include "byte.h"
-#include "ip.h"
#include "dnsresolv.h"
+#include "iopause.h"
+#include "ip.h"
+#include "taia.h"
/**
@file dns_resolve.c
@@ -13,7 +13,7 @@
struct dns_transmit dns_resolve_tx = {0};
-int dns_resolve(const char *q,const char qtype[2])
+int dns_resolve(const char *q, const char qtype[2])
{
struct taia stamp;
struct taia deadline;
@@ -22,17 +22,18 @@ int dns_resolve(const char *q,const char qtype[2])
iopause_fd x[1];
int r;
- if (dns_resolvconfip(servers,scopes) < 0) return DNS_INT;
+ if (dns_resolvconfip(servers, scopes) < 0) return DNS_INT;
- if (dns_transmit_start6(&dns_resolve_tx,servers,1,q,qtype,(const char *)V6localnet,scopes) < 0) return DNS_COM;
+ if (dns_transmit_start6(&dns_resolve_tx, servers, 1, q, qtype, (const char *)V6localnet, scopes) < 0)
+ return DNS_COM;
for (;;) {
taia_now(&stamp);
- taia_uint(&deadline,120);
- taia_add(&deadline,&deadline,&stamp);
- dns_transmit_io(&dns_resolve_tx,x,&deadline);
- iopause(x,1,&deadline,&stamp);
- r = dns_transmit_get(&dns_resolve_tx,x,&stamp);
+ taia_uint(&deadline, 120);
+ taia_add(&deadline, &deadline, &stamp);
+ dns_transmit_io(&dns_resolve_tx, x, &deadline);
+ iopause(x, 1, &deadline, &stamp);
+ r = dns_transmit_get(&dns_resolve_tx, x, &stamp);
if (r < 0) return DNS_COM;
if (r == 1) return 0;
}
diff --git a/src/dnsstub/dns_sortip.c b/src/dnsstub/dns_sortip.c
index 56742e0..886e989 100644
--- a/src/dnsstub/dns_sortip.c
+++ b/src/dnsstub/dns_sortip.c
@@ -1,6 +1,6 @@
#include "byte.h"
-#include "ip.h"
#include "dnsresolv.h"
+#include "ip.h"
/**
@file dns_sortip.c
@@ -14,32 +14,32 @@
/* XXX: pay attention to qualification (DNSSec, DNSCurve) of each server? */
/* YYY: we use a randomly sorted list of NS; not depending on answer */
-void dns_sortip4(char *s,unsigned int n)
+void dns_sortip4(char *s, unsigned int n)
{
unsigned int i;
char tmp[4];
- n >>= 2; /* 4 byte per IPv4 address */
+ n >>= 2; /* 4 byte per IPv4 address */
while (n > 1) {
i = dns_random(n);
--n;
- byte_copy(tmp,4,s + (i << 2));
- byte_copy(s + (i << 2),4,s + (n << 2));
- byte_copy(s + (n << 2),4,tmp);
+ byte_copy(tmp, 4, s + (i << 2));
+ byte_copy(s + (i << 2), 4, s + (n << 2));
+ byte_copy(s + (n << 2), 4, tmp);
}
}
-void dns_sortip6(char *s,unsigned int n)
+void dns_sortip6(char *s, unsigned int n)
{
unsigned int i;
char tmp[16];
- n >>= 4; /* 16 byte per IPv4 address */
+ n >>= 4; /* 16 byte per IPv4 address */
while (n > 1) {
i = dns_random(n);
--n;
- byte_copy(tmp,16,s + (i << 4));
- byte_copy(s + (i << 4),16,s + (n << 4));
- byte_copy(s + (n << 4),16,tmp);
+ byte_copy(tmp, 16, s + (i << 4));
+ byte_copy(s + (i << 4), 16, s + (n << 4));
+ byte_copy(s + (n << 4), 16, tmp);
}
}
diff --git a/src/dnsstub/dns_transmit.c b/src/dnsstub/dns_transmit.c
index 2513565..e041312 100644
--- a/src/dnsstub/dns_transmit.c
+++ b/src/dnsstub/dns_transmit.c
@@ -1,13 +1,14 @@
-#include <sys/types.h>
#include <sys/socket.h>
+#include <sys/types.h>
#include <unistd.h>
-#include "socket_if.h"
+
#include "alloc.h"
-#include "error.h"
#include "byte.h"
-#include "uint_t.h"
-#include "ip.h"
#include "dnsresolv.h"
+#include "error.h"
+#include "ip.h"
+#include "socket_if.h"
+#include "uint_t.h"
/**
@file dns_transmit.c
@@ -21,62 +22,70 @@
uint32 scope_ids[QUERY_MAXNS];
-static const int timeouts[5] = { 1, 2, 4, 8, 16 }; /* quadratic, not exponentially */
-
-int getscopeid(const struct dns_transmit *d,const char *ip)
+static const int timeouts[5] = {1, 2, 4, 8, 16}; /* quadratic, not exponentially */
+
+int getscopeid(const struct dns_transmit *d, const char *ip)
{
int i;
- if (byte_diff(ip,2,V6linklocal)) return 0;
- for (i = 0; i < QUERY_MAXNS; ++i)
- if (byte_equal(d->servers + 16 * i,16,ip))
- return scope_ids[i];
+ if (byte_diff(ip, 2, V6linklocal)) return 0;
+ for (i = 0; i < QUERY_MAXNS; ++i)
+ if (byte_equal(d->servers + 16 * i, 16, ip)) return scope_ids[i];
return 0;
}
-int serverwantstcp(const char *buf,unsigned int len)
+int serverwantstcp(const char *buf, unsigned int len)
{
char out[12];
- if (!dns_packet_copy(buf,len,0,out,12)) return 1;
+ if (!dns_packet_copy(buf, len, 0, out, 12)) return 1;
if (out[2] & 2) return 1;
return 0;
}
-int serverfailed(const char *buf,unsigned int len)
+int serverfailed(const char *buf, unsigned int len)
{
char out[12];
unsigned int rcode;
- if (!dns_packet_copy(buf,len,0,out,12)) return 1;
+ if (!dns_packet_copy(buf, len, 0, out, 12)) return 1;
rcode = out[3];
rcode &= 15;
- if (rcode && (rcode != 3)) { errno = EAGAIN; return 1; }
+ if (rcode && (rcode != 3)) {
+ errno = EAGAIN;
+ return 1;
+ }
return 0;
}
-int irrelevant(const struct dns_transmit *d,const char *buf,unsigned int len)
+int irrelevant(const struct dns_transmit *d, const char *buf, unsigned int len)
{
char out[12];
char *dn;
unsigned int pos;
- pos = dns_packet_copy(buf,len,0,out,12); if (!pos) return 1;
- if (byte_diff(out,2,d->query + 2)) return 1;
+ pos = dns_packet_copy(buf, len, 0, out, 12);
+ if (!pos) return 1;
+ if (byte_diff(out, 2, d->query + 2)) return 1;
if (out[4] != 0) return 1;
if (out[5] != 1) return 1;
dn = 0;
- pos = dns_packet_getname(buf,len,pos,&dn); if (!pos) return 1;
- if (!dns_domain_equal(dn,d->query + 14)) { alloc_free(dn); return 1; }
+ pos = dns_packet_getname(buf, len, pos, &dn);
+ if (!pos) return 1;
+ if (!dns_domain_equal(dn, d->query + 14)) {
+ alloc_free(dn);
+ return 1;
+ }
alloc_free(dn);
- pos = dns_packet_copy(buf,len,pos,out,4); if (!pos) return 1;
- if (byte_diff(out,2,d->qtype)) return 1;
- if (byte_diff(out + 2,2,DNS_C_IN)) return 1;
+ pos = dns_packet_copy(buf, len, pos, out, 4);
+ if (!pos) return 1;
+ if (byte_diff(out, 2, d->qtype)) return 1;
+ if (byte_diff(out + 2, 2, DNS_C_IN)) return 1;
return 0;
}
@@ -112,13 +121,11 @@ void dns_transmit_free(struct dns_transmit *d)
int randombind6(struct dns_transmit *d)
{
int j;
-
+
for (j = 0; j < 10; ++j) {
- if (socket_bind6(d->s1 - 1,d->localip,1025 + dns_random(64510),d->scope_id) == 0)
- return 0;
- }
- if (socket_bind6(d->s1 - 1,d->localip,0,d->scope_id) == 0)
- return 0;
+ if (socket_bind6(d->s1 - 1, d->localip, 1025 + dns_random(64510), d->scope_id) == 0) return 0;
+ }
+ if (socket_bind6(d->s1 - 1, d->localip, 0, d->scope_id) == 0) return 0;
return DNS_COM;
}
@@ -126,13 +133,11 @@ int randombind6(struct dns_transmit *d)
int randombind4(struct dns_transmit *d)
{
int j;
-
+
for (j = 0; j < 10; ++j) {
- if (socket_bind4(d->s1 - 1,d->localip + 12,1025 + dns_random(64510)) == 0)
- return 0;
- }
- if (socket_bind4(d->s1 - 1,d->localip + 12,0) == 0)
- return 0;
+ if (socket_bind4(d->s1 - 1, d->localip + 12, 1025 + dns_random(64510)) == 0) return 0;
+ }
+ if (socket_bind4(d->s1 - 1, d->localip + 12, 0) == 0) return 0;
return DNS_COM;
}
@@ -146,28 +151,39 @@ int thisudp(struct dns_transmit *d)
while (d->udploop < 5) {
for (; d->curserver < QUERY_MAXNS; ++d->curserver) {
ip = d->servers + 16 * d->curserver;
- if (byte_diff(ip,16,V6localnet)) {
+ if (byte_diff(ip, 16, V6localnet)) {
d->query[2] = dns_random(256);
d->query[3] = dns_random(256);
-
+
if (ip6_isv4mapped(ip)) {
- d->s1 = 1 + socket_udp4();
- if (!d->s1) { dns_transmit_free(d); return DNS_COM; }
- if (randombind4(d) < 0) { dns_transmit_free(d); return DNS_COM; }
+ d->s1 = 1 + socket_udp4();
+ if (!d->s1) {
+ dns_transmit_free(d);
+ return DNS_COM;
+ }
+ if (randombind4(d) < 0) {
+ dns_transmit_free(d);
+ return DNS_COM;
+ }
} else {
- d->s1 = 1 + socket_udp6();
- if (!d->s1) { dns_transmit_free(d); return DNS_COM; }
- if (randombind6(d) < 0) { dns_transmit_free(d); return DNS_COM; }
- }
-
- if (byte_equal(ip,2,V6linklocal) && !d->scope_id)
- d->scope_id = getscopeid(d,ip);
- if (socket_connect(d->s1 - 1,ip,DNSPORT,d->scope_id) == 0)
- if (send(d->s1 - 1,d->query + 2,d->querylen - 2,0) == d->querylen - 2) {
+ d->s1 = 1 + socket_udp6();
+ if (!d->s1) {
+ dns_transmit_free(d);
+ return DNS_COM;
+ }
+ if (randombind6(d) < 0) {
+ dns_transmit_free(d);
+ return DNS_COM;
+ }
+ }
+
+ if (byte_equal(ip, 2, V6linklocal) && !d->scope_id) d->scope_id = getscopeid(d, ip);
+ if (socket_connect(d->s1 - 1, ip, DNSPORT, d->scope_id) == 0)
+ if (send(d->s1 - 1, d->query + 2, d->querylen - 2, 0) == d->querylen - 2) {
struct taia now;
taia_now(&now);
- taia_uint(&d->deadline,timeouts[d->udploop]);
- taia_add(&d->deadline,&d->deadline,&now);
+ taia_uint(&d->deadline, timeouts[d->udploop]);
+ taia_add(&d->deadline, &d->deadline, &now);
d->tcpstate = 0;
return 0;
}
@@ -178,7 +194,8 @@ int thisudp(struct dns_transmit *d)
d->curserver = 0;
}
- dns_transmit_free(d); return DNS_COM;
+ dns_transmit_free(d);
+ return DNS_COM;
}
int firstudp(struct dns_transmit *d)
@@ -203,27 +220,38 @@ int thistcp(struct dns_transmit *d)
for (; d->curserver < QUERY_MAXNS; ++d->curserver) {
ip = d->servers + 16 * d->curserver;
- if (byte_diff(ip,16,V6localnet)) {
+ if (byte_diff(ip, 16, V6localnet)) {
d->query[2] = dns_random(256);
d->query[3] = dns_random(256);
if (ip6_isv4mapped(ip)) {
d->s1 = 1 + socket_tcp4();
- if (!d->s1) { dns_transmit_free(d); return DNS_COM; }
- if (randombind4(d) < 0) { dns_transmit_free(d); return DNS_COM; }
+ if (!d->s1) {
+ dns_transmit_free(d);
+ return DNS_COM;
+ }
+ if (randombind4(d) < 0) {
+ dns_transmit_free(d);
+ return DNS_COM;
+ }
} else {
- d->s1 = 1 + socket_tcp6();
- if (!d->s1) { dns_transmit_free(d); return DNS_COM; }
- if (randombind6(d) < 0) { dns_transmit_free(d); return DNS_COM; }
- }
-
+ d->s1 = 1 + socket_tcp6();
+ if (!d->s1) {
+ dns_transmit_free(d);
+ return DNS_COM;
+ }
+ if (randombind6(d) < 0) {
+ dns_transmit_free(d);
+ return DNS_COM;
+ }
+ }
+
taia_now(&now);
- taia_uint(&d->deadline,10);
- taia_add(&d->deadline,&d->deadline,&now);
+ taia_uint(&d->deadline, 10);
+ taia_add(&d->deadline, &d->deadline, &now);
- if (byte_equal(ip,2,V6linklocal) && !d->scope_id)
- d->scope_id = getscopeid(d,ip);
- if (socket_connect(d->s1 - 1,ip,DNSPORT,d->scope_id) == 0) {
+ if (byte_equal(ip, 2, V6linklocal) && !d->scope_id) d->scope_id = getscopeid(d, ip);
+ if (socket_connect(d->s1 - 1, ip, DNSPORT, d->scope_id) == 0) {
d->tcpstate = 2;
return 0;
}
@@ -231,12 +259,12 @@ int thistcp(struct dns_transmit *d)
d->tcpstate = 1;
return 0;
}
-
+
socketfree(d);
}
}
- dns_transmit_free(d);
+ dns_transmit_free(d);
return DNS_COM;
}
@@ -252,8 +280,13 @@ int nexttcp(struct dns_transmit *d)
return thistcp(d);
}
-int dns_transmit_start(struct dns_transmit *d,const char servers[QUERY_MAXIPLEN], \
- int flagrecursive,const char *q,const char qtype[2],const char localip[16])
+int dns_transmit_start(
+ struct dns_transmit *d,
+ const char servers[QUERY_MAXIPLEN],
+ int flagrecursive,
+ const char *q,
+ const char qtype[2],
+ const char localip[16])
{
unsigned int len;
@@ -264,17 +297,19 @@ int dns_transmit_start(struct dns_transmit *d,const char servers[QUERY_MAXIPLEN]
d->querylen = len + 18;
d->query = alloc(d->querylen);
if (!d->query) return DNS_COM;
-
- uint16_pack_big(d->query,len + 16);
- byte_copy(d->query + 2,12,flagrecursive ? "\0\0\1\0\0\1\0\0\0\0\0\0" : \
- "\0\0\0\0\0\1\0\0\0\0\0\0gcc-bug-workaround");
- byte_copy(d->query + 14,len,q);
- byte_copy(d->query + 14 + len,2,qtype);
- byte_copy(d->query + 16 + len,2,DNS_C_IN);
-
- byte_copy(d->qtype,2,(char *) qtype);
+
+ uint16_pack_big(d->query, len + 16);
+ byte_copy(
+ d->query + 2,
+ 12,
+ flagrecursive ? "\0\0\1\0\0\1\0\0\0\0\0\0" : "\0\0\0\0\0\1\0\0\0\0\0\0gcc-bug-workaround");
+ byte_copy(d->query + 14, len, q);
+ byte_copy(d->query + 14 + len, 2, qtype);
+ byte_copy(d->query + 16 + len, 2, DNS_C_IN);
+
+ byte_copy(d->qtype, 2, (char *)qtype);
d->servers = servers;
- byte_copy(d->localip,16,(char *) localip);
+ byte_copy(d->localip, 16, (char *)localip);
d->udploop = flagrecursive ? 1 : 0;
@@ -282,33 +317,37 @@ int dns_transmit_start(struct dns_transmit *d,const char servers[QUERY_MAXIPLEN]
return firstudp(d);
}
-int dns_transmit_start6(struct dns_transmit *d,const char servers[QUERY_MAXIPLEN], \
- int flagrecursive,const char *q,const char qtype[2], \
- const char localip[16],const uint32 scopes[QUERY_MAXNS])
+int dns_transmit_start6(
+ struct dns_transmit *d,
+ const char servers[QUERY_MAXIPLEN],
+ int flagrecursive,
+ const char *q,
+ const char qtype[2],
+ const char localip[16],
+ const uint32 scopes[QUERY_MAXNS])
{
- byte_copy(scope_ids,128,(char *) scopes);
+ byte_copy(scope_ids, 128, (char *)scopes);
- return dns_transmit_start(d,servers,flagrecursive,q,qtype,localip);
+ return dns_transmit_start(d, servers, flagrecursive, q, qtype, localip);
}
-void dns_transmit_io(struct dns_transmit *d,iopause_fd *x,struct taia *deadline)
+void dns_transmit_io(struct dns_transmit *d, iopause_fd *x, struct taia *deadline)
{
x->fd = d->s1 - 1;
switch (d->tcpstate) {
- case 0: case 3: case 4: case 5:
- x->events = IOPAUSE_READ;
- break;
- case 1: case 2:
- x->events = IOPAUSE_WRITE;
- break;
+ case 0:
+ case 3:
+ case 4:
+ case 5: x->events = IOPAUSE_READ; break;
+ case 1:
+ case 2: x->events = IOPAUSE_WRITE; break;
}
- if (taia_less(&d->deadline,deadline))
- *deadline = d->deadline;
+ if (taia_less(&d->deadline, deadline)) *deadline = d->deadline;
}
-int dns_transmit_get(struct dns_transmit *d,const iopause_fd *x,const struct taia *when)
+int dns_transmit_get(struct dns_transmit *d, const iopause_fd *x, const struct taia *when)
{
char udpbuf[MSGSIZE + 1];
unsigned char ch;
@@ -319,27 +358,28 @@ int dns_transmit_get(struct dns_transmit *d,const iopause_fd *x,const struct tai
fd = d->s1 - 1;
if (!x->revents) {
- if (taia_less((struct taia *)when,&d->deadline)) return 0;
+ if (taia_less((struct taia *)when, &d->deadline)) return 0;
errno = ETIMEDOUT;
if (d->tcpstate == 0) return nextudp(d);
return nexttcp(d);
}
-/*
+ /*
have attempted to send UDP query to each server udploop times
have sent query to curserver on UDP socket s
*/
if (d->tcpstate == 0) {
- r = recv(fd,udpbuf,sizeof(udpbuf),0);
+ r = recv(fd, udpbuf, sizeof(udpbuf), 0);
if (r <= 0) {
- if (errno == ECONNREFUSED) if (d->udploop == 2) return 0;
+ if (errno == ECONNREFUSED)
+ if (d->udploop == 2) return 0;
return nextudp(d);
}
if (r + 1 > sizeof(udpbuf)) return 0;
- if (irrelevant(d,udpbuf,r)) return 0;
- if (serverwantstcp(udpbuf,r)) return firsttcp(d);
- if (serverfailed(udpbuf,r)) {
+ if (irrelevant(d, udpbuf, r)) return 0;
+ if (serverwantstcp(udpbuf, r)) return firsttcp(d);
+ if (serverfailed(udpbuf, r)) {
if (d->udploop == 2) return 0;
return nextudp(d);
}
@@ -347,13 +387,16 @@ have sent query to curserver on UDP socket s
d->packetlen = r;
d->packet = alloc(d->packetlen);
- if (!d->packet) { dns_transmit_free(d); return DNS_COM; }
- byte_copy(d->packet,d->packetlen,udpbuf);
+ if (!d->packet) {
+ dns_transmit_free(d);
+ return DNS_COM;
+ }
+ byte_copy(d->packet, d->packetlen, udpbuf);
queryfree(d);
return 1;
}
-/*
+ /*
have sent connection attempt to curserver on TCP socket s
pos not defined
*/
@@ -364,69 +407,72 @@ pos not defined
return 0;
}
-/*
+ /*
have connection to curserver on TCP socket s
have sent pos bytes of query
*/
if (d->tcpstate == 2) {
- r = write(fd,d->query + d->pos,d->querylen - d->pos);
+ r = write(fd, d->query + d->pos, d->querylen - d->pos);
if (r <= 0) return nexttcp(d);
d->pos += r;
if (d->pos == d->querylen) {
struct taia now;
taia_now(&now);
- taia_uint(&d->deadline,10);
- taia_add(&d->deadline,&d->deadline,&now);
+ taia_uint(&d->deadline, 10);
+ taia_add(&d->deadline, &d->deadline, &now);
d->tcpstate = 3;
}
return 0;
}
-/*
+ /*
have sent entire query to curserver on TCP socket s
pos not defined
*/
if (d->tcpstate == 3) {
- r = read(fd,&ch,1);
+ r = read(fd, &ch, 1);
if (r <= 0) return nexttcp(d);
d->packetlen = ch;
d->tcpstate = 4;
return 0;
}
-/*
+ /*
have sent entire query to curserver on TCP socket s
pos not defined
have received one byte of packet length into packetlen
*/
if (d->tcpstate == 4) {
- r = read(fd,&ch,1);
+ r = read(fd, &ch, 1);
if (r <= 0) return nexttcp(d);
d->packetlen <<= 8;
d->packetlen += ch;
d->tcpstate = 5;
d->pos = 0;
d->packet = alloc(d->packetlen);
- if (!d->packet) { dns_transmit_free(d); return DNS_COM; }
+ if (!d->packet) {
+ dns_transmit_free(d);
+ return DNS_COM;
+ }
return 0;
}
-/*
+ /*
have sent entire query to curserver on TCP socket s
have received entire packet length into packetlen
packet is allocated
have received pos bytes of packet
*/
if (d->tcpstate == 5) {
- r = read(fd,d->packet + d->pos,d->packetlen - d->pos);
+ r = read(fd, d->packet + d->pos, d->packetlen - d->pos);
if (r <= 0) return nexttcp(d);
d->pos += r;
if (d->pos < d->packetlen) return 0;
socketfree(d);
- if (irrelevant(d,d->packet,d->packetlen)) return nexttcp(d);
- if (serverwantstcp(d->packet,d->packetlen)) return nexttcp(d);
- if (serverfailed(d->packet,d->packetlen)) return nexttcp(d);
+ if (irrelevant(d, d->packet, d->packetlen)) return nexttcp(d);
+ if (serverwantstcp(d->packet, d->packetlen)) return nexttcp(d);
+ if (serverfailed(d->packet, d->packetlen)) return nexttcp(d);
queryfree(d);
return 1;
diff --git a/src/dnsstub/dns_txt.c b/src/dnsstub/dns_txt.c
index 9a1b56a..ce0afc4 100644
--- a/src/dnsstub/dns_txt.c
+++ b/src/dnsstub/dns_txt.c
@@ -1,9 +1,9 @@
-#include "stralloc.h"
-#include "uint_t.h"
#include "byte.h"
#include "dnsresolv.h"
+#include "stralloc.h"
+#include "uint_t.h"
-int dns_txt_packet(stralloc *out,const char *buf,unsigned int len)
+int dns_txt_packet(stralloc *out, const char *buf, unsigned int len)
{
unsigned int pos;
char header[12];
@@ -14,35 +14,40 @@ int dns_txt_packet(stralloc *out,const char *buf,unsigned int len)
int i;
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_TXT))
- 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_TXT))
+ if (byte_equal(header + 2, 2, DNS_C_IN)) {
if (pos + datalen > len) return DNS_ERR;
- txtlen = (unsigned char) buf[pos];
+ txtlen = (unsigned char)buf[pos];
for (i = 1; i < datalen; ++i) {
ch = buf[pos + i];
- if (i == txtlen + 1) // next label
- txtlen += (unsigned char) ch + 1;
+ if (i == txtlen + 1) // next label
+ txtlen += (unsigned char)ch + 1;
else {
if (ch < 32) ch = '?';
if (ch > 126) ch = '?';
- if (!stralloc_append(out,&ch)) return DNS_MEM;
+ if (!stralloc_append(out, &ch)) return DNS_MEM;
}
}
}
pos += datalen;
++ranswers;
- if (numanswers) if (!stralloc_append(out,"\n")) return DNS_MEM;
+ if (numanswers)
+ if (!stralloc_append(out, "\n")) return DNS_MEM;
}
return ranswers;
@@ -50,13 +55,13 @@ int dns_txt_packet(stralloc *out,const char *buf,unsigned int len)
static char *q = 0;
-int dns_txt(stralloc *out,const stralloc *fqdn)
+int dns_txt(stralloc *out, const stralloc *fqdn)
{
- int rc;
+ int rc;
- if (dns_domain_fromdot(&q,fqdn->s,fqdn->len) <= 0) return DNS_ERR;
- if (dns_resolve(q,DNS_T_TXT) < 0) return DNS_ERR;
- if ((rc = dns_txt_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;
+ if (dns_resolve(q, DNS_T_TXT) < 0) return DNS_ERR;
+ if ((rc = dns_txt_packet(out, dns_resolve_tx.packet, dns_resolve_tx.packetlen)) < 0) return DNS_ERR;
dns_transmit_free(&dns_resolve_tx);
dns_domain_free(&q);