djbdnscurve6  38
djbdnscurve6
log.c
Go to the documentation of this file.
1 #include "buffer.h"
2 #include "uint_t.h"
3 #include "error.h"
4 #include "byte.h"
5 #include "ip.h"
6 #include "fmt.h"
7 #include "log.h"
8 
9 /* work around gcc 2.95.2 bug */
10 #define number(x) ( (u64 = (x)), u64_print() )
11 static uint64 u64;
12 static void u64_print(void)
13 {
14  char buf[20];
15  unsigned int pos;
16 
17  pos = sizeof(buf);
18  do {
19  if (!pos) 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 
27 static void hex(unsigned char c)
28 {
29  buffer_put(buffer_2,"0123456789abcdef" + (c >> 4),1);
30  buffer_put(buffer_2,"0123456789abcdef" + (c & 15),1);
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 space(void)
45 {
46  string(" ");
47 }
48 
49 static void ip(const char i[16])
50 {
51  int j;
52 
53  if (ip6_isv4mapped(i))
54  for (j = 12; j < 16; ++j) hex(i[j]);
55  else
56  for (j = 0; j < 16; ++j) hex(i[j]);
57 }
58 
59 static void logid(const char id[2])
60 {
61  hex(id[0]);
62  hex(id[1]);
63 }
64 
65 static void logtype(const char type[2])
66 {
67  uint16 u;
68 
69  uint16_unpack_big(type,&u);
70  number(u);
71 }
72 
73 static void name(const char *q)
74 {
75  char ch;
76  int state;
77 
78  if (!*q) {
79  string(".");
80  return;
81  }
82  while ((state = *q++)) {
83  while (state) {
84  ch = *q++;
85  --state;
86  if ((ch <= 32) || (ch > 126)) ch = '?';
87  if ((ch >= 'A') && (ch <= 'Z')) ch += 32;
88  buffer_put(buffer_2,&ch,1);
89  }
90  string(".");
91  }
92 }
93 
94 void log_startup(const char listip[16],uint32 scope,const char sendip[16],int size)
95 {
96  char strnum[FMT_ULONG];
97 
98  string("starting dnscache listening on ip ");
99  ip(listip);
100  string("%");
101  strnum[fmt_ulong(strnum,scope)] = 0;
102  string(strnum);
103  string(" sending queries from ip ");
104  ip(sendip);
105  strnum[fmt_ulong(strnum,size)] = 0;
106  string(" udp maxsize = "); string(strnum);
107  line();
108 }
109 
110 void log_query(uint64 *qnum,const char client[16],unsigned int port,const char id[2],const char *q,const char qtype[2],const char *p)
111 {
112  string(p); string(".query "); number(*qnum); space();
113  ip(client); string(":"); hex(port >> 8); hex(port & 255);
114  string(":"); logid(id); space();
115  logtype(qtype); space(); name(q);
116  line();
117 }
118 
119 void log_querydone(uint64 *qnum,unsigned int len,const char *p)
120 {
121  string(p); string(".sent "); number(*qnum); space();
122  number(len);
123  line();
124 }
125 
126 void log_querydrop(uint64 *qnum,const char *p)
127 {
128  const char *x = errstr(errno);
129 
130  string(p); string(".drop "); number(*qnum); space();
131  string(x);
132  line();
133 }
134 
135  void log_ignore_referral(const char server[16],const char * control, const char *referral)
136  {
137  string("ignored referral "); ip(server); space();
138  name(control); space(); name(referral);
139  line();
140  }
141 
142 void log_tcpopen(const char client[16],unsigned int port)
143 {
144  string("tcpopen ");
145  ip(client); string(":"); hex(port >> 8); hex(port & 255);
146  line();
147 }
148 
149 void log_tcpclose(const char client[16],unsigned int port)
150 {
151  const char *x = errstr(errno);
152  string("tcpclose ");
153  ip(client); string(":"); hex(port >> 8); hex(port & 255); space();
154  string(x);
155  line();
156 }
157 
158 void log_tx(const char *q,const char qtype[2],const char *control,const char servers[512],int flagkey,unsigned int gluelessness)
159 {
160  int i;
161 
162  string("tx "); number(gluelessness); space();
163  logtype(qtype); space();
164  name(q); space(); name(control);
165  switch (flagkey) {
166  case -1: string(" !"); break;
167  case 0: string(" -"); break;
168  case 1: string(" +"); break;
169  case 2: string(" *"); break;
170  }
171 
172  for (i = 0; i < 512; i += 16)
173  if (byte_diff(servers + i,16,V6localnet)) {
174  space();
175  ip(servers + i);
176  }
177  line();
178 }
179 
180 void log_cachedanswer(const char *q,const char type[2])
181 {
182  string("cached "); logtype(type); space();
183  name(q);
184  line();
185 }
186 
187 void log_cachedcname(const char *dn,const char *dn2)
188 {
189  string("cached cname "); name(dn); space(); name(dn2);
190  line();
191 }
192 
193 void log_cachedns(const char *control,const char *ns)
194 {
195  string("cached ns "); name(control); space(); name(ns);
196  line();
197 }
198 
199 void log_cachednxdomain(const char *dn)
200 {
201  string("cached nxdomain "); name(dn);
202  line();
203 }
204 
205 void log_nxdomain(const char server[16],const char *q,unsigned int ttl)
206 {
207  string("nxdomain "); ip(server); space(); number(ttl); space();
208  name(q);
209  line();
210 }
211 
212 void log_nodata(const char server[16],const char *q,const char qtype[2],unsigned int ttl)
213 {
214  string("nodata "); ip(server); space(); number(ttl); space();
215  logtype(qtype); space(); name(q);
216  line();
217 }
218 
219 void log_lame(const char server[16],const char *control,const char *referral)
220 {
221  string("lame "); ip(server); space();
222  name(control); space(); name(referral);
223  line();
224 }
225 
226 void log_servflag(const char server[16],int flag)
227 {
228  string("servflagged ");
229  if (flag == 1) string("% ");
230  if (flag == -1) string("- ");
231  ip(server);
232  line();
233 }
234 
235 void log_servfail(const char *dn)
236 {
237  const char *x = errstr(errno);
238 
239  string("servfail "); name(dn); space();
240  string(x);
241  line();
242 }
243 
244 void log_rr(const char server[16],const char *q,const char type[2],const char *buf,unsigned int len,unsigned int ttl)
245 {
246  int i;
247 
248  string("rr "); ip(server); space(); number(ttl); space();
249  logtype(type); space(); name(q); space();
250 
251  for (i = 0; i < len; ++i) {
252  hex(buf[i]);
253  if (i > 30) {
254  string("...");
255  break;
256  }
257  }
258  line();
259 }
260 
261 void log_rrns(const char server[16],const char *q,const char *data,unsigned int ttl)
262 {
263  string("rr "); ip(server); space(); number(ttl);
264  string(" ns "); name(q); space();
265  name(data); line();
266 }
267 
268 void log_rrcname(const char server[16],const char *q,const char *data,unsigned int ttl)
269 {
270  string("rr "); ip(server); space(); number(ttl);
271  string(" cname "); name(q); space();
272  name(data);
273  line();
274 }
275 
276 void log_rrptr(const char server[16],const char *q,const char *data,unsigned int ttl)
277 {
278  string("rr "); ip(server); space(); number(ttl);
279  string(" ptr "); name(q); space();
280  name(data);
281  line();
282 }
283 
284 void log_rrmx(const char server[16],const char *q,const char *mx,const char pref[2],unsigned int ttl)
285 {
286  uint16 u;
287 
288  string("rr "); ip(server); space(); number(ttl);
289  string(" mx "); name(q); space();
290  uint16_unpack_big(pref,&u);
291  number(u); space(); name(mx);
292  line();
293 }
294 
295 void log_rrsoa(const char server[16],const char *q,const char *n1,const char *n2,const char misc[20],unsigned int ttl)
296 {
297  uint32 u;
298  int i;
299 
300  string("rr "); ip(server); space(); number(ttl);
301  string(" soa "); name(q); space();
302  name(n1); space(); name(n2);
303  for (i = 0; i < 20; i += 4) {
304  uint32_unpack_big(misc + i,&u);
305  space(); number(u);
306  }
307  line();
308 }
309 
310 void log_stats(void)
311 {
312  extern uint64 numqueries;
313  extern uint64 cache_motion;
314  extern int uactive;
315  extern int eactive;
316  extern int tactive;
317 
318  string("stats ");
319  number(numqueries); space();
320  number(cache_motion); space();
321  number(uactive); space();
322  number(eactive); space();
323  number(tactive);
324  line();
325 }
stralloc line
Definition: axfr-get.c:126
char data[32767]
Definition: axfrdns.c:131
unsigned long port
Definition: axfrdns.c:127
char ip[16]
Definition: axfrdns.c:126
uint16 len
Definition: axfrdns.c:302
char buf[MSGSIZE]
Definition: axfrdns.c:301
uint64 cache_motion
Definition: cache.c:8
uint64 numqueries
Definition: dnscache.c:70
char * p
Definition: dnscache.c:38
int uactive
Definition: dnscache.c:84
int eactive
Definition: dnscache.c:85
int tactive
Definition: dnscache.c:179
char servers[QUERY_MAXIPLEN]
Definition: dnsfilter.c:48
struct line * x
char name[DNS_NAME6_DOMAIN]
Definition: dnsfilter.c:52
char strnum[FMT_ULONG]
Definition: dnsmx.c:22
char type[2]
Definition: dnsq.c:56
void log_rrmx(const char server[16], const char *q, const char *mx, const char pref[2], unsigned int ttl)
Definition: log.c:284
void log_tcpclose(const char client[16], unsigned int port)
Definition: log.c:149
void log_rrcname(const char server[16], const char *q, const char *data, unsigned int ttl)
Definition: log.c:268
void log_servflag(const char server[16], int flag)
Definition: log.c:226
void log_servfail(const char *dn)
Definition: log.c:235
void log_tcpopen(const char client[16], unsigned int port)
Definition: log.c:142
void log_tx(const char *q, const char qtype[2], const char *control, const char servers[512], int flagkey, unsigned int gluelessness)
Definition: log.c:158
void log_cachedanswer(const char *q, const char type[2])
Definition: log.c:180
void log_nxdomain(const char server[16], const char *q, unsigned int ttl)
Definition: log.c:205
void log_cachednxdomain(const char *dn)
Definition: log.c:199
void log_nodata(const char server[16], const char *q, const char qtype[2], unsigned int ttl)
Definition: log.c:212
void log_rrptr(const char server[16], const char *q, const char *data, unsigned int ttl)
Definition: log.c:276
void log_ignore_referral(const char server[16], const char *control, const char *referral)
Definition: log.c:135
void log_querydone(uint64 *qnum, unsigned int len, const char *p)
Definition: log.c:119
void log_startup(const char listip[16], uint32 scope, const char sendip[16], int size)
Definition: log.c:94
void log_lame(const char server[16], const char *control, const char *referral)
Definition: log.c:219
void log_query(uint64 *qnum, const char client[16], unsigned int port, const char id[2], const char *q, const char qtype[2], const char *p)
Definition: log.c:110
void log_stats(void)
Definition: log.c:310
void log_cachedcname(const char *dn, const char *dn2)
Definition: log.c:187
void log_rrsoa(const char server[16], const char *q, const char *n1, const char *n2, const char misc[20], unsigned int ttl)
Definition: log.c:295
void log_rrns(const char server[16], const char *q, const char *data, unsigned int ttl)
Definition: log.c:261
void log_cachedns(const char *control, const char *ns)
Definition: log.c:193
void log_rr(const char server[16], const char *q, const char type[2], const char *buf, unsigned int len, unsigned int ttl)
Definition: log.c:244
void log_querydrop(uint64 *qnum, const char *p)
Definition: log.c:126
#define number(x)
Definition: log.c:10
unsigned long u
Definition: utime.c:10