1 #include "buffer.h"
2 #include "uint32.h"
3 #include "uint16.h"
4 #include "error.h"
5 #include "byte.h"
6 #include "log.h"
7
8 /* work around gcc 2.95.2 bug */
9 #define number(x) ( (u64 = (x)), u64_print() )
10 static uint64 u64;
u64_print(void)11 static void u64_print(void)
12 {
13 char buf[20];
14 unsigned int pos;
15
16 pos = sizeof buf;
17 do {
18 if (!pos) break;
19 buf[--pos] = '0' + (u64 % 10);
20 u64 /= 10;
21 } while(u64);
22
23 buffer_put(buffer_2,buf + pos,sizeof buf - pos);
24 }
25
hex(unsigned char c)26 static void hex(unsigned char c)
27 {
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 {
34 buffer_puts(buffer_2,s);
35 }
36
line(void)37 static void line(void)
38 {
39 string("\n");
40 buffer_flush(buffer_2);
41 }
42
space(void)43 static void space(void)
44 {
45 string(" ");
46 }
47
ip(const char i[4])48 static void ip(const char i[4])
49 {
50 hex(i[0]);
51 hex(i[1]);
52 hex(i[2]);
53 hex(i[3]);
54 }
55
logid(const char id[2])56 static void logid(const char id[2])
57 {
58 hex(id[0]);
59 hex(id[1]);
60 }
61
logtype(const char type[2])62 static void logtype(const char type[2])
63 {
64 uint16 u;
65
66 uint16_unpack_big(type,&u);
67 number(u);
68 }
69
name(const char * q)70 static void name(const char *q)
71 {
72 char ch;
73 int state;
74
75 if (!*q) {
76 string(".");
77 return;
78 }
79 while (state = *q++) {
80 while (state) {
81 ch = *q++;
82 --state;
83 if ((ch <= 32) || (ch > 126)) ch = '?';
84 if ((ch >= 'A') && (ch <= 'Z')) ch += 32;
85 buffer_put(buffer_2,&ch,1);
86 }
87 string(".");
88 }
89 }
90
log_startup(void)91 void log_startup(void)
92 {
93 string("starting");
94 line();
95 }
96
log_query(uint64 * qnum,const char client[4],unsigned int port,const char id[2],const char * q,const char qtype[2])97 void log_query(uint64 *qnum,const char client[4],unsigned int port,const char id[2],const char *q,const char qtype[2])
98 {
99 string("query "); number(*qnum); space();
100 ip(client); string(":"); hex(port >> 8); hex(port & 255);
101 string(":"); logid(id); space();
102 logtype(qtype); space(); name(q);
103 line();
104 }
105
log_querydone(uint64 * qnum,unsigned int len)106 void log_querydone(uint64 *qnum,unsigned int len)
107 {
108 string("sent "); number(*qnum); space();
109 number(len);
110 line();
111 }
112
log_querydrop(uint64 * qnum)113 void log_querydrop(uint64 *qnum)
114 {
115 const char *x = error_str(errno);
116
117 string("drop "); number(*qnum); space();
118 string(x);
119 line();
120 }
121
log_tcpopen(const char client[4],unsigned int port)122 void log_tcpopen(const char client[4],unsigned int port)
123 {
124 string("tcpopen ");
125 ip(client); string(":"); hex(port >> 8); hex(port & 255);
126 line();
127 }
128
log_tcpclose(const char client[4],unsigned int port)129 void log_tcpclose(const char client[4],unsigned int port)
130 {
131 const char *x = error_str(errno);
132 string("tcpclose ");
133 ip(client); string(":"); hex(port >> 8); hex(port & 255); space();
134 string(x);
135 line();
136 }
137
log_tx(const char * q,const char qtype[2],const char * control,const char servers[64],unsigned int gluelessness)138 void log_tx(const char *q,const char qtype[2],const char *control,const char servers[64],unsigned int gluelessness)
139 {
140 int i;
141
142 string("tx "); number(gluelessness); space();
143 logtype(qtype); space(); name(q); space();
144 name(control);
145 for (i = 0;i < 64;i += 4)
146 if (byte_diff(servers + i,4,"\0\0\0\0")) {
147 space();
148 ip(servers + i);
149 }
150 line();
151 }
152
log_cachedanswer(const char * q,const char type[2])153 void log_cachedanswer(const char *q,const char type[2])
154 {
155 string("cached "); logtype(type); space();
156 name(q);
157 line();
158 }
159
log_cachedcname(const char * dn,const char * dn2)160 void log_cachedcname(const char *dn,const char *dn2)
161 {
162 string("cached cname "); name(dn); space(); name(dn2);
163 line();
164 }
165
log_cachedns(const char * control,const char * ns)166 void log_cachedns(const char *control,const char *ns)
167 {
168 string("cached ns "); name(control); space(); name(ns);
169 line();
170 }
171
log_cachednxdomain(const char * dn)172 void log_cachednxdomain(const char *dn)
173 {
174 string("cached nxdomain "); name(dn);
175 line();
176 }
177
log_nxdomain(const char server[4],const char * q,unsigned int ttl)178 void log_nxdomain(const char server[4],const char *q,unsigned int ttl)
179 {
180 string("nxdomain "); ip(server); space(); number(ttl); space();
181 name(q);
182 line();
183 }
184
log_nodata(const char server[4],const char * q,const char qtype[2],unsigned int ttl)185 void log_nodata(const char server[4],const char *q,const char qtype[2],unsigned int ttl)
186 {
187 string("nodata "); ip(server); space(); number(ttl); space();
188 logtype(qtype); space(); name(q);
189 line();
190 }
191
log_lame(const char server[4],const char * control,const char * referral)192 void log_lame(const char server[4],const char *control,const char *referral)
193 {
194 string("lame "); ip(server); space();
195 name(control); space(); name(referral);
196 line();
197 }
198
log_servfail(const char * dn)199 void log_servfail(const char *dn)
200 {
201 const char *x = error_str(errno);
202
203 string("servfail "); name(dn); space();
204 string(x);
205 line();
206 }
207
log_rr(const char server[4],const char * q,const char type[2],const char * buf,unsigned int len,unsigned int ttl)208 void log_rr(const char server[4],const char *q,const char type[2],const char *buf,unsigned int len,unsigned int ttl)
209 {
210 int i;
211
212 string("rr "); ip(server); space(); number(ttl); space();
213 logtype(type); space(); name(q); space();
214
215 for (i = 0;i < len;++i) {
216 hex(buf[i]);
217 if (i > 30) {
218 string("...");
219 break;
220 }
221 }
222 line();
223 }
224
log_rrns(const char server[4],const char * q,const char * data,unsigned int ttl)225 void log_rrns(const char server[4],const char *q,const char *data,unsigned int ttl)
226 {
227 string("rr "); ip(server); space(); number(ttl);
228 string(" ns "); name(q); space();
229 name(data);
230 line();
231 }
232
log_rrcname(const char server[4],const char * q,const char * data,unsigned int ttl)233 void log_rrcname(const char server[4],const char *q,const char *data,unsigned int ttl)
234 {
235 string("rr "); ip(server); space(); number(ttl);
236 string(" cname "); name(q); space();
237 name(data);
238 line();
239 }
240
log_rrptr(const char server[4],const char * q,const char * data,unsigned int ttl)241 void log_rrptr(const char server[4],const char *q,const char *data,unsigned int ttl)
242 {
243 string("rr "); ip(server); space(); number(ttl);
244 string(" ptr "); name(q); space();
245 name(data);
246 line();
247 }
248
log_rrmx(const char server[4],const char * q,const char * mx,const char pref[2],unsigned int ttl)249 void log_rrmx(const char server[4],const char *q,const char *mx,const char pref[2],unsigned int ttl)
250 {
251 uint16 u;
252
253 string("rr "); ip(server); space(); number(ttl);
254 string(" mx "); name(q); space();
255 uint16_unpack_big(pref,&u);
256 number(u); space(); name(mx);
257 line();
258 }
259
log_rrsoa(const char server[4],const char * q,const char * n1,const char * n2,const char misc[20],unsigned int ttl)260 void log_rrsoa(const char server[4],const char *q,const char *n1,const char *n2,const char misc[20],unsigned int ttl)
261 {
262 uint32 u;
263 int i;
264
265 string("rr "); ip(server); space(); number(ttl);
266 string(" soa "); name(q); space();
267 name(n1); space(); name(n2);
268 for (i = 0;i < 20;i += 4) {
269 uint32_unpack_big(misc + i,&u);
270 space(); number(u);
271 }
272 line();
273 }
274
log_stats(void)275 void log_stats(void)
276 {
277 extern uint64 numqueries;
278 extern uint64 cache_motion;
279 extern int uactive;
280 extern int tactive;
281
282 string("stats ");
283 number(numqueries); space();
284 number(cache_motion); space();
285 number(uactive); space();
286 number(tactive);
287 line();
288 }
289