1 |
--- a/server.c |
2 |
+++ b/server.c |
3 |
@@ -25,12 +25,63 @@ static int len; |
4 |
|
5 |
static char *q; |
6 |
|
7 |
+static uint64 stats_numq; |
8 |
+static uint64 stats_plus; |
9 |
+static uint64 stats_minus; |
10 |
+static uint64 stats_nx; |
11 |
+static uint64 stats_notimp; |
12 |
+static uint64 stats_weird; |
13 |
+static uint64 stats_noq; |
14 |
+ |
15 |
+/* work around gcc 2.95.2 bug */ |
16 |
+#define number(x) ( (u64 = (x)), u64_print() ) |
17 |
+static uint64 u64; |
18 |
+static void u64_print(void) |
19 |
+{ |
20 |
+ char ubuf[20]; |
21 |
+ unsigned int pos; |
22 |
+ |
23 |
+ pos = sizeof ubuf; |
24 |
+ do { |
25 |
+ if (!pos) break; |
26 |
+ ubuf[--pos] = '0' + (u64 % 10); |
27 |
+ u64 /= 10; |
28 |
+ } while(u64); |
29 |
+ |
30 |
+ buffer_put(buffer_2,ubuf + pos,sizeof ubuf - pos); |
31 |
+} |
32 |
+ |
33 |
+static void string(const char *s) |
34 |
+{ |
35 |
+ buffer_puts(buffer_2,s); |
36 |
+} |
37 |
+ |
38 |
+static void line(void) |
39 |
+{ |
40 |
+ string("\n"); |
41 |
+ buffer_flush(buffer_2); |
42 |
+} |
43 |
+ |
44 |
+static void log_stats(void) |
45 |
+{ |
46 |
+ string("stats "); |
47 |
+ number(stats_numq); string(" "); |
48 |
+ number(stats_plus); string(" "); |
49 |
+ number(stats_minus); string(" "); |
50 |
+ number(stats_nx); string(" "); |
51 |
+ number(stats_notimp); string(" "); |
52 |
+ number(stats_weird); string(" "); |
53 |
+ number(stats_noq); |
54 |
+ line(); |
55 |
+} |
56 |
+ |
57 |
static int doit(void) |
58 |
{ |
59 |
unsigned int pos; |
60 |
char header[12]; |
61 |
char qtype[2]; |
62 |
char qclass[2]; |
63 |
+ stats_numq++; |
64 |
|
65 |
if (len >= sizeof buf) goto NOQ; |
66 |
pos = dns_packet_copy(buf,len,0,header,12); if (!pos) goto NOQ; |
67 |
@@ -56,25 +107,37 @@ static int doit(void) |
68 |
|
69 |
case_lowerb(q,dns_domain_length(q)); |
70 |
if (!respond(q,qtype,ip)) { |
71 |
+ stats_minus++; |
72 |
qlog(ip,port,header,q,qtype," - "); |
73 |
return 0; |
74 |
} |
75 |
- qlog(ip,port,header,q,qtype," + "); |
76 |
+ |
77 |
+ if ((response[2] & 4) && (response[3] & 3)) { |
78 |
+ stats_nx++; |
79 |
+ qlog(ip,port,header,q,qtype," N "); |
80 |
+ } |
81 |
+ else { |
82 |
+ stats_plus++; |
83 |
+ qlog(ip,port,header,q,qtype," + "); |
84 |
+ } |
85 |
return 1; |
86 |
|
87 |
NOTIMP: |
88 |
+ stats_notimp++; |
89 |
response[3] &= ~15; |
90 |
response[3] |= 4; |
91 |
qlog(ip,port,header,q,qtype," I "); |
92 |
return 1; |
93 |
|
94 |
WEIRDCLASS: |
95 |
+ stats_weird++; |
96 |
response[3] &= ~15; |
97 |
response[3] |= 1; |
98 |
qlog(ip,port,header,q,qtype," C "); |
99 |
return 1; |
100 |
|
101 |
NOQ: |
102 |
+ stats_noq++; |
103 |
qlog(ip,port,"\0\0","","\0\0"," / "); |
104 |
return 0; |
105 |
} |
106 |
@@ -83,6 +146,7 @@ int main() |
107 |
{ |
108 |
char *x; |
109 |
int udp53; |
110 |
+ unsigned char flag=0; |
111 |
|
112 |
x = env_get("IP"); |
113 |
if (!x) |
114 |
@@ -106,6 +170,8 @@ int main() |
115 |
buffer_putsflush(buffer_2,starting); |
116 |
|
117 |
for (;;) { |
118 |
+ if ((flag++)%32==1) |
119 |
+ log_stats(); |
120 |
len = socket_recv4(udp53,buf,sizeof buf,ip,&port); |
121 |
if (len < 0) continue; |
122 |
if (!doit()) continue; |