release
[hcoop/zz_old/debian/djbdns.git] / qlog.c
1 #include "buffer.h"
2 #include "qlog.h"
3
4 static void put(char c)
5 {
6 buffer_put(buffer_2,&c,1);
7 }
8
9 static void hex(unsigned char c)
10 {
11 put("0123456789abcdef"[(c >> 4) & 15]);
12 put("0123456789abcdef"[c & 15]);
13 }
14
15 static void octal(unsigned char c)
16 {
17 put('\\');
18 put('0' + ((c >> 6) & 7));
19 put('0' + ((c >> 3) & 7));
20 put('0' + (c & 7));
21 }
22
23 void qlog(const char ip[4],uint16 port,const char id[2],const char *q,const char qtype[2],const char *result)
24 {
25 char ch;
26 char ch2;
27
28 hex(ip[0]);
29 hex(ip[1]);
30 hex(ip[2]);
31 hex(ip[3]);
32 put(':');
33 hex(port >> 8);
34 hex(port & 255);
35 put(':');
36 hex(id[0]);
37 hex(id[1]);
38 buffer_puts(buffer_2,result);
39 hex(qtype[0]);
40 hex(qtype[1]);
41 put(' ');
42
43 if (!*q)
44 put('.');
45 else
46 for (;;) {
47 ch = *q++;
48 while (ch--) {
49 ch2 = *q++;
50 if ((ch2 >= 'A') && (ch2 <= 'Z'))
51 ch2 += 32;
52 if (((ch2 >= 'a') && (ch2 <= 'z')) || ((ch2 >= '0') && (ch2 <= '9')) || (ch2 == '-') || (ch2 == '_'))
53 put(ch2);
54 else
55 octal(ch2);
56 }
57 if (!*q) break;
58 put('.');
59 }
60
61 put('\n');
62 buffer_flush(buffer_2);
63 }