1 #include "buffer.h"
2 #include "uint32_unpack_big.h"
3 #include "uint16_unpack_big.h"
4 #include "e.h"
5 #include "byte.h"
6 #include "iptostr.h"
7 #include "numtostr.h"
8 #include "log.h"
9
10 /* work around gcc 2.95.2 bug */
11 #define number(x) ( (u64 = (x)), u64_print() )
12 static crypto_uint64 u64;
u64_print(void)13 static void u64_print(void) {
14 char buf[20];
15 long long pos;
16
17 pos = sizeof buf;
18 do {
19 if (pos <= 0) break;
20 buf[--pos] = '0' + (u64 % 10);
21 u64 /= 10;
22 } while(u64);
23
24 buffer_put(buffer_2, buf + pos, sizeof buf - pos);
25 }
26
hex(unsigned char c)27 static void hex(unsigned char c) {
28 buffer_put(buffer_2, "0123456789abcdef" + (c >> 4), 1);
29 buffer_put(buffer_2, "0123456789abcdef" + (c & 15), 1);
30 }
31
string(const char * s)32 static void string(const char *s) {
33 buffer_puts(buffer_2, s);
34 }
35
line(void)36 static void line(void) {
37 string("\n");
38 buffer_flush(buffer_2);
39 }
40
space(void)41 static void space(void) {
42 string(" ");
43 }
44
ip(const unsigned char i[16])45 static void ip(const unsigned char i[16]) {
46 #if 1
47 string(iptostr(0, i));
48 #else
49 hex(i[0]); hex(i[1]); hex(i[2]); hex(i[3]);
50 hex(i[4]); hex(i[5]); hex(i[6]); hex(i[7]);
51 hex(i[8]); hex(i[9]); hex(i[10]); hex(i[11]);
52 hex(i[12]); hex(i[13]); hex(i[14]); hex(i[15]);
53 #endif
54 }
55
dctype(unsigned char x)56 static void dctype(unsigned char x) {
57
58 switch(x) {
59 case 1: string("S"); break;
60 case 2: string("T"); break;
61 default: string("R"); break;
62 }
63 }
64
logid(const unsigned char id[2])65 static void logid(const unsigned char id[2]) {
66 #if 1
67 string(numtostr(0, uint16_unpack_big(id)));
68 #else
69 hex(id[0]);
70 hex(id[1]);
71 #endif
72 }
73
logtype(const unsigned char type[2])74 static void logtype(const unsigned char type[2]) {
75
76 crypto_uint16 u;
77
78 u = uint16_unpack_big(type);
79 number(u);
80 }
81
name(const unsigned char * q)82 static void name(const unsigned char *q) {
83
84 unsigned char ch;
85 int state;
86
87 if (!*q) {
88 string(".");
89 return;
90 }
91
92 while(state = *q++) {
93 while (state) {
94 ch = *q++;
95 --state;
96 if ((ch <= 32) || (ch > 126)) ch = '?';
97 if ((ch >= 'A') && (ch <= 'Z')) ch += 32;
98 buffer_put(buffer_2, (char *)&ch, 1);
99 }
100 string(".");
101 }
102 }
103
log_startup(void)104 void log_startup(void) {
105 string("starting");
106 line();
107 }
108
log_dnscurvekey(const unsigned char * key)109 void log_dnscurvekey(const unsigned char *key) {
110
111 long long i;
112 string("dnscurve public-key ");
113 for(i = 0; i < 32; ++i) {
114 hex(key[i]);
115 }
116 line();
117 }
118
log_query(crypto_uint64 * qnum,const unsigned char client[16],unsigned char port[2],const unsigned char id[2],const unsigned char * q,const unsigned char qtype[2])119 void log_query(crypto_uint64 *qnum, const unsigned char client[16], unsigned char port[2], const unsigned char id[2], const unsigned char *q, const unsigned char qtype[2]) {
120
121 string("query "); number(*qnum); space();
122 ip(client); string(":"); string(numtostr(0, uint16_unpack_big(port)));
123 string(":"); logid(id); space();
124 logtype(qtype); space(); name(q);
125 line();
126 }
127
128
log_queryreject(const unsigned char * client,unsigned char * port,const unsigned char * id,const unsigned char * q,const unsigned char * qtype,const char * x)129 void log_queryreject(const unsigned char *client, unsigned char *port, const unsigned char *id, const unsigned char *q, const unsigned char *qtype, const char *x) {
130
131 string("reject ");
132 ip(client); string(":"); string(numtostr(0, uint16_unpack_big(port)));
133 string(":");
134
135 if (id) {
136 logid(id);
137 }
138 else {
139 string("?");
140 }
141 space();
142
143 if (qtype) {
144 logtype(qtype);
145 }
146 else {
147 string("?");
148 }
149 space();
150
151 if (q) {
152 name(q);
153 }
154 else {
155 string("?");
156 }
157 space();
158 string(x);
159 line();
160 }
161
log_querydone(crypto_uint64 * qnum,long long len)162 void log_querydone(crypto_uint64 *qnum, long long len) {
163 string("sent "); number(*qnum); space();
164 number(len);
165 line();
166 }
167
log_querydrop(crypto_uint64 * qnum)168 void log_querydrop(crypto_uint64 *qnum) {
169
170 const char *x = e_str(errno);
171
172 string("drop "); number(*qnum); space();
173 string(x);
174 line();
175 }
176
log_tcpopen(const unsigned char client[16],unsigned char port[2])177 void log_tcpopen(const unsigned char client[16], unsigned char port[2]) {
178 string("tcpopen ");
179 ip(client); string(":"); hex(port[0]); hex(port[1]);
180 line();
181 }
182
log_tcpclose(const unsigned char client[16],unsigned char port[2])183 void log_tcpclose(const unsigned char client[16],unsigned char port[2]) {
184
185 const char *x = e_str(errno);
186 string("tcpclose ");
187 ip(client); string(":"); hex(port[0]); hex(port[1]); space();
188 string(x);
189 line();
190 }
191
192 /* XXX */
log_tx(const unsigned char * q,const unsigned char qtype[2],const unsigned char * control,const unsigned char servers[256],const unsigned char keys[528],int flaghavekeys,unsigned int gluelessness)193 void log_tx(const unsigned char *q, const unsigned char qtype[2], const unsigned char *control, const unsigned char servers[256], const unsigned char keys[528], int flaghavekeys, unsigned int gluelessness) {
194
195 long long i, j;
196 const unsigned char *k;
197
198 string("tx "); number(gluelessness); space();
199 logtype(qtype); space(); name(q); space();
200 name(control);
201 string(flaghavekeys ? " +" : " -");
202 for (i = 0; i < 256; i += 16) {
203 j = i >> 4;
204 if (!byte_isequal(servers + i, 16, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0")) {
205 k = keys + 33 * j;
206 space();
207 dctype(k[0]);
208 ip(servers + i);
209 }
210 }
211 line();
212 }
213
log_cachedanswer(const unsigned char * q,const unsigned char type[2])214 void log_cachedanswer(const unsigned char *q,const unsigned char type[2])
215 {
216 string("cached "); logtype(type); space();
217 name(q);
218 line();
219 }
220
log_cachedcname(const unsigned char * dn,const unsigned char * dn2)221 void log_cachedcname(const unsigned char *dn,const unsigned char *dn2)
222 {
223 string("cached cname "); name(dn); space(); name(dn2);
224 line();
225 }
226
log_cachedns(const unsigned char * control,const unsigned char * ns)227 void log_cachedns(const unsigned char *control,const unsigned char *ns)
228 {
229 string("cached ns "); name(control); space(); name(ns);
230 line();
231 }
232
log_cachednxdomain(const unsigned char * dn)233 void log_cachednxdomain(const unsigned char *dn)
234 {
235 string("cached nxdomain "); name(dn);
236 line();
237 }
238
log_nxdomain(const unsigned char * server,const unsigned char * q,unsigned int ttl)239 void log_nxdomain(const unsigned char *server,const unsigned char *q,unsigned int ttl)
240 {
241 string("nxdomain "); ip(server); space(); number(ttl); space();
242 name(q);
243 line();
244 }
245
log_nodata(const unsigned char * server,const unsigned char * q,const unsigned char qtype[2],unsigned int ttl)246 void log_nodata(const unsigned char *server,const unsigned char *q,const unsigned char qtype[2],unsigned int ttl)
247 {
248 string("nodata "); ip(server); space(); number(ttl); space();
249 logtype(qtype); space(); name(q);
250 line();
251 }
252
log_lame(const unsigned char * server,const unsigned char * control,const unsigned char * referral)253 void log_lame(const unsigned char *server,const unsigned char *control,const unsigned char *referral)
254 {
255 string("lame "); ip(server); space();
256 name(control); space(); name(referral);
257 line();
258 }
259
log_ignore_referral(const unsigned char * server,const unsigned char * control,const unsigned char * referral)260 void log_ignore_referral(const unsigned char *server, const unsigned char *control, const unsigned char *referral)
261 {
262 string("ignored referral "); ip(server); space();
263 name(control); space(); name(referral);
264 line();
265 }
266
log_servfail(const unsigned char * dn)267 void log_servfail(const unsigned char *dn)
268 {
269 const char *x = e_str(errno);
270
271 string("servfail "); name(dn); space();
272 string(x);
273 line();
274 }
275
log_cachedservfail(const unsigned char * dn,const unsigned char * dt)276 void log_cachedservfail(const unsigned char *dn, const unsigned char *dt)
277 {
278 string("cached servfail "); name(dn); space();
279 logtype(dt);
280 line();
281 }
282
log_rr(const unsigned char * server,const unsigned char * q,const unsigned char type[2],const unsigned char * buf,unsigned int len,unsigned int ttl,unsigned char flagkey)283 void log_rr(const unsigned char *server,const unsigned char *q,const unsigned char type[2],const unsigned char *buf,unsigned int len,unsigned int ttl, unsigned char flagkey)
284 {
285 int i;
286
287 string("rr "); dctype(flagkey); ip(server); space(); number(ttl); space();
288 logtype(type); space(); name(q); space();
289
290 for (i = 0;i < len;++i) {
291 hex(buf[i]);
292 if (i > 30) {
293 string("...");
294 break;
295 }
296 }
297 line();
298 }
299
log_rra(const unsigned char * server,const unsigned char * q,const unsigned char * data,unsigned int ttl,unsigned char flagkey)300 void log_rra(const unsigned char *server,const unsigned char *q,const unsigned char *data,unsigned int ttl, unsigned char flagkey)
301 {
302 unsigned char i[16];
303 byte_copy(i, 12, "\0\0\0\0\0\0\0\0\0\0\377\377");
304 byte_copy(i + 12, 4, data);
305 string("rr "); dctype(flagkey); ip(server); space(); number(ttl);
306 string(" a "); name(q); space();
307 string(iptostr(0, i));
308 line();
309 }
310
log_rraaaa(const unsigned char * server,const unsigned char * q,const unsigned char * data,unsigned int ttl,unsigned char flagkey)311 void log_rraaaa(const unsigned char *server,const unsigned char *q,const unsigned char *data,unsigned int ttl, unsigned char flagkey)
312 {
313 string("rr "); dctype(flagkey); ip(server); space(); number(ttl);
314 string(" aaaa "); name(q); space();
315 string(iptostr(0, data));
316 line();
317 }
318
log_rrns(const unsigned char * server,const unsigned char * q,const unsigned char * data,unsigned int ttl,unsigned char flagkey)319 void log_rrns(const unsigned char *server,const unsigned char *q,const unsigned char *data,unsigned int ttl, unsigned char flagkey)
320 {
321 string("rr "); dctype(flagkey); ip(server); space(); number(ttl);
322 string(" ns "); name(q); space();
323 name(data);
324 line();
325 }
326
log_rrcname(const unsigned char * server,const unsigned char * q,const unsigned char * data,unsigned int ttl,unsigned char flagkey)327 void log_rrcname(const unsigned char *server,const unsigned char *q,const unsigned char *data,unsigned int ttl, unsigned char flagkey)
328 {
329 string("rr "); dctype(flagkey); ip(server); space(); number(ttl);
330 string(" cname "); name(q); space();
331 name(data);
332 line();
333 }
334
log_rrptr(const unsigned char * server,const unsigned char * q,const unsigned char * data,unsigned int ttl,unsigned char flagkey)335 void log_rrptr(const unsigned char *server,const unsigned char *q,const unsigned char *data,unsigned int ttl, unsigned char flagkey)
336 {
337 string("rr "); dctype(flagkey); ip(server); space(); number(ttl);
338 string(" ptr "); name(q); space();
339 name(data);
340 line();
341 }
342
log_rrmx(const unsigned char * server,const unsigned char * q,const unsigned char * mx,const unsigned char pref[2],unsigned int ttl,unsigned char flagkey)343 void log_rrmx(const unsigned char *server,const unsigned char *q,const unsigned char *mx,const unsigned char pref[2],unsigned int ttl, unsigned char flagkey)
344 {
345 crypto_uint16 u;
346
347 string("rr "); dctype(flagkey); ip(server); space(); number(ttl);
348 string(" mx "); name(q); space();
349 u = uint16_unpack_big(pref);
350 number(u); space(); name(mx);
351 line();
352 }
353
log_rrsoa(const unsigned char * server,const unsigned char * q,const unsigned char * n1,const unsigned char * n2,const unsigned char misc[20],unsigned int ttl,unsigned char flagkey)354 void log_rrsoa(const unsigned char *server,const unsigned char *q,const unsigned char *n1,const unsigned char *n2,const unsigned char misc[20],unsigned int ttl,unsigned char flagkey)
355 {
356 crypto_uint32 u;
357 int i;
358
359 string("rr "); dctype(flagkey); ip(server); space(); number(ttl);
360 string(" soa "); name(q); space();
361 name(n1); space(); name(n2);
362 for (i = 0;i < 20;i += 4) {
363 u = uint32_unpack_big(misc + i);
364 space(); number(u);
365 }
366 line();
367 }
368
369
log_stats(void)370 void log_stats(void)
371 {
372 extern crypto_uint64 numqueries;
373 extern crypto_uint64 cache_motion;
374 extern crypto_uint64 cache_hit;
375 extern crypto_uint64 cache_miss;
376 extern crypto_uint64 tx4;
377 extern crypto_uint64 tx6;
378 extern int uactive;
379 extern int tactive;
380
381 string("stats ");
382 number(numqueries); space();
383 number(cache_motion); space();
384 number(uactive); space();
385 number(tactive); space();
386 number(cache_hit); space();
387 number(cache_miss); space();
388 number(tx4); space();
389 number(tx6);
390 line();
391 }
392