1 #include "stralloc.h"
2 #include "writeall.h"
3 #include "iptostr.h"
4 #include "porttostr.h"
5 #include "numtostr.h"
6 #include "e.h"
7 #include "uint16_unpack_big.h"
8 #include "byte.h"
9 #include "dns.h"
10 
11 int dns_verbosity_flag = 1;
12 const char *dns_verbosity_message = "dns: info: ";
13 
dns_verbosity_setflag(int x)14 void dns_verbosity_setflag(int x) {
15     dns_verbosity_flag = x;
16 }
dns_verbosity_setmessage(const char * x)17 void dns_verbosity_setmessage(const char *x) {
18     dns_verbosity_message = x;
19 }
20 
21 static stralloc out = {0};
22 
dns_verbosity_writehex(const char * message,const unsigned char * x,long long xlen)23 void dns_verbosity_writehex(const char *message, const unsigned char *x, long long xlen) {
24 
25     if (dns_verbosity_flag < 3) return;
26     if (!stralloc_copys(&out, dns_verbosity_message)) return;
27     if (!stralloc_cats(&out, message)) return;
28 
29     while (xlen > 0) {
30         if (!stralloc_catb(&out, "0123456789abcdef" + ((*x >> 4) & 15), 1)) return;
31         if (!stralloc_catb(&out, "0123456789abcdef" + (*x & 15), 1)) return;
32         ++x; --xlen;
33     }
34     if (!stralloc_cats(&out, "\n")) return;
35     writeall(2, out.s, out.len);
36 }
37 
dns_verbosity_writedomain(const char * message,unsigned char * x)38 void dns_verbosity_writedomain(const char *message, unsigned char *x) {
39 
40     if (dns_verbosity_flag < 3) return;
41     if (!stralloc_copys(&out, dns_verbosity_message)) return;
42     if (!stralloc_cats(&out, message)) return;
43     if (!dns_domain_todot_cat(&out, x)) return;
44     if (!stralloc_cats(&out, "\n")) return;
45     writeall(2, out.s, out.len);
46 }
47 
qtypetostr(const unsigned char * qtype)48 static const unsigned char *qtypetostr(const unsigned char *qtype) {
49 
50     crypto_uint16 u16;
51 
52     if (byte_isequal(qtype, 2, DNS_T_A)) return (unsigned char *)"A";
53     if (byte_isequal(qtype, 2, DNS_T_NS)) return (unsigned char *)"NS";
54     if (byte_isequal(qtype, 2, DNS_T_MX)) return (unsigned char *)"MX";
55     if (byte_isequal(qtype, 2, DNS_T_ANY)) return (unsigned char *)"ANY";
56     if (byte_isequal(qtype, 2, DNS_T_PTR)) return (unsigned char *)"PTR";
57     if (byte_isequal(qtype, 2, DNS_T_TXT)) return (unsigned char *)"TXT";
58     if (byte_isequal(qtype, 2, DNS_T_SOA)) return (unsigned char *)"SOA";
59     if (byte_isequal(qtype, 2, DNS_T_SRV)) return (unsigned char *)"SRV";
60     if (byte_isequal(qtype, 2, DNS_T_AAAA)) return (unsigned char *)"AAAA";
61     if (byte_isequal(qtype, 2, DNS_T_PTR)) return (unsigned char *)"PTR";
62     if (byte_isequal(qtype, 2, DNS_T_CNAME)) return (unsigned char *)"CNAME";
63 
64     u16 = uint16_unpack_big(qtype);
65     return (const unsigned char *)numtostr(0, u16);
66 
67 }
68 
dns_verbosity_resolving(const char * x)69 void dns_verbosity_resolving(const char *x) {
70 
71     if (dns_verbosity_flag < 3) return;
72     if (!stralloc_copys(&out, dns_verbosity_message)) return;
73     if (!stralloc_cats(&out, "resolving: ")) return;
74     if (!stralloc_cats(&out, x)) return;
75     if (!stralloc_cats(&out, "\n")) return;
76     writeall(2, out.s, out.len);
77 }
78 
dns_verbosity_resolved(struct dns_data * r,const char * x)79 void dns_verbosity_resolved(struct dns_data *r, const char *x) {
80 
81     long long j;
82 
83     if (dns_verbosity_flag < 3) return;
84 
85     if (!stralloc_copys(&out, dns_verbosity_message)) return;
86     if (!stralloc_cats(&out, "resolved: ")) return;
87     if (!stralloc_cats(&out, x)) return;
88     if (!stralloc_cats(&out, ": ")) return;
89     if (!stralloc_cat(&out, &r->fqdn)) return;
90     if (!stralloc_cats(&out, " ")) return;
91     for (j = 0; j + 16 <= r->result.len; j += 16) {
92         if (!stralloc_cats(&out, iptostr(0, r->result.s + j))) return;
93         if (!stralloc_cats(&out, ",")) return;
94     }
95     out.len -= 1;
96     if (!stralloc_cats(&out, "\n")) return;
97     writeall(2, out.s, out.len);
98 }
99 
100 
dns_verbosity_querysent(struct dns_transmit * d,int flagtcp)101 void dns_verbosity_querysent(struct dns_transmit *d, int flagtcp) {
102 
103     if (dns_verbosity_flag < 3) return;
104     if (!stralloc_copys(&out, dns_verbosity_message)) return;
105     if (DNS_HASKEY(d)) {
106         if (!stralloc_cats(&out, "DNSCurve query: ")) return;
107     }
108     else {
109         if (!stralloc_cats(&out, "DNS query: ")) return;
110     }
111     if (!dns_domain_todot_cat(&out, d->name)) return;
112     if (!stralloc_cats(&out, " ")) return;
113     if (!stralloc_cats(&out, qtypetostr(d->qtype))) return;
114     if (!stralloc_cats(&out, " ")) return;
115     if (flagtcp) {
116         if (!stralloc_cats(&out, "TCP ")) return;
117     }
118     else {
119         if (!stralloc_cats(&out, "UDP ")) return;
120     }
121     if (!stralloc_cats(&out, iptostr(0, d->servers + 16 * d->curserver))) return;
122     if (!stralloc_cats(&out, " ")) return;
123     if (!stralloc_cats(&out, porttostr(0, d->port))) return;
124     if (!stralloc_cats(&out, ": sent\n")) return;
125     writeall(2, out.s, out.len);
126 }
127 
dns_verbosity_queryfailed(struct dns_transmit * d,int flagtcp)128 void dns_verbosity_queryfailed(struct dns_transmit *d, int flagtcp) {
129 
130     if (dns_verbosity_flag < 2) return;
131 
132     if (!stralloc_copys(&out, dns_verbosity_message)) return;
133     if (DNS_HASKEY(d)) {
134         if (!stralloc_cats(&out, "DNSCurve query: ")) return;
135     }
136     else {
137         if (!stralloc_cats(&out, "DNS query: ")) return;
138     }
139     if (!dns_domain_todot_cat(&out, d->name)) return;
140     if (!stralloc_cats(&out, " ")) return;
141     if (!stralloc_cats(&out, qtypetostr(d->qtype))) return;
142     if (!stralloc_cats(&out, " ")) return;
143     if (flagtcp) {
144         if (!stralloc_cats(&out, "TCP ")) return;
145     }
146     else {
147         if (!stralloc_cats(&out, "UDP ")) return;
148     }
149     if (!stralloc_cats(&out, iptostr(0, d->servers + 16 * d->curserver))) return;
150     if (!stralloc_cats(&out, " ")) return;
151     if (!stralloc_cats(&out, porttostr(0, d->port))) return;
152     if (!stralloc_cats(&out, ": failed: ")) return;
153     if (!stralloc_cats(&out, e_str(errno))) return;
154     if (!stralloc_cats(&out, "\n")) return;
155     writeall(2, out.s, out.len);
156 }
157 
dns_verbosity_queryfailedtc(struct dns_transmit * d)158 void dns_verbosity_queryfailedtc(struct dns_transmit *d) {
159 
160     if (dns_verbosity_flag < 2) return;
161 
162     if (!stralloc_copys(&out, dns_verbosity_message)) return;
163     if (DNS_HASKEY(d)) {
164         if (!stralloc_cats(&out, "DNSCurve query: ")) return;
165     }
166     else {
167         if (!stralloc_cats(&out, "DNS query: ")) return;
168     }
169     if (!dns_domain_todot_cat(&out, d->name)) return;
170     if (!stralloc_cats(&out, " ")) return;
171     if (!stralloc_cats(&out, qtypetostr(d->qtype))) return;
172     if (!stralloc_cats(&out, " ")) return;
173     if (d->tcpstate > 0) {
174         if (!stralloc_cats(&out, "TCP ")) return;
175     }
176     else {
177         if (!stralloc_cats(&out, "UDP ")) return;
178     }
179     if (!stralloc_cats(&out, iptostr(0, d->servers + 16 * d->curserver))) return;
180     if (!stralloc_cats(&out, " ")) return;
181     if (!stralloc_cats(&out, porttostr(0, d->port))) return;
182     if (!stralloc_cats(&out, ": failed: truncated\n")) return;
183     writeall(2, out.s, out.len);
184 }
185 
dns_verbosity_queryok(struct dns_transmit * d)186 void dns_verbosity_queryok(struct dns_transmit *d) {
187 
188     if (dns_verbosity_flag < 3) return;
189 
190     if (!stralloc_copys(&out, dns_verbosity_message)) return;
191     if (DNS_HASKEY(d)) {
192         if (!stralloc_cats(&out, "DNSCurve query: ")) return;
193     }
194     else {
195         if (!stralloc_cats(&out, "DNS query: ")) return;
196     }
197     if (!dns_domain_todot_cat(&out, d->name)) return;
198     if (!stralloc_cats(&out, " ")) return;
199     if (!stralloc_cats(&out, qtypetostr(d->qtype))) return;
200     if (!stralloc_cats(&out, " ")) return;
201     if (d->tcpstate > 0) {
202         if (!stralloc_cats(&out, "TCP ")) return;
203     }
204     else {
205         if (!stralloc_cats(&out, "UDP ")) return;
206     }
207     if (!stralloc_cats(&out, iptostr(0, d->servers + 16 * d->curserver))) return;
208     if (!stralloc_cats(&out, " ")) return;
209     if (!stralloc_cats(&out, porttostr(0, d->port))) return;
210     if (!stralloc_cats(&out, ": received\n")) return;
211     writeall(2, out.s, out.len);
212 }
213