221bac4bfdcb27c9022e75b20a406946e19bc842
[collectd.git] / src / utils_dns.c
1 /*
2  * collectd - src/utils_dns.c
3  * Modifications Copyright (C) 2006  Florian octo Forster
4  * Copyright (C) 2002  The Measurement Factory, Inc.
5  * All rights reserved.
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright notice,
11  *    this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  * 3. Neither the name of The Measurement Factory nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  * Authors:
32  *   The Measurement Factory, Inc. <http://www.measurement-factory.com/>
33  *   Florian octo Forster <octo at verplant.org>
34  */
35
36 #include "collectd.h"
37
38 #if HAVE_NETINET_IN_H
39 # include <netinet/in.h>
40 #endif
41 #if HAVE_PCAP_H
42 # include <pcap.h>
43 #endif
44 #if HAVE_ARPA_INET_H
45 # include <arpa/inet.h>
46 #endif
47
48 #if HAVE_ARPA_NAMESER_H
49 # include <arpa/nameser.h>
50 #elif HAVE_ARPA_NAMESER_COMPAT_H
51 # include <arpa/nameser_compat.h>
52 #endif
53
54 #if HAVE_NET_IF_ARP_H
55 # include <net/if_arp.h>
56 #endif
57 #if HAVE_NET_IF_H
58 # include <net/if.h>
59 #endif
60 #if HAVE_NETINET_IF_ETHER_H
61 # include <netinet/if_ether.h>
62 #endif
63 #if HAVE_NET_IF_PPP_H
64 # include <net/if_ppp.h>
65 #endif
66
67 #if HAVE_SYS_SOCKET_H
68 # include <sys/socket.h>
69 #endif
70 #if HAVE_NETDB_H
71 # include <netdb.h>
72 #endif
73
74 #if HAVE_NETINET_IN_SYSTM_H
75 # include <netinet/in_systm.h>
76 #endif
77 #if HAVE_NETINET_IN_H
78 # include <netinet/in.h>
79 #endif
80 #if HAVE_NETINET_IP_H
81 # include <netinet/ip.h>
82 #endif
83 #ifdef HAVE_NETINET_IP_VAR_H
84 # include <netinet/ip_var.h>
85 #endif
86 #if HAVE_NETINET_IP6_H
87 # include <netinet/ip6.h>
88 #endif
89 #if HAVE_NETINET_UDP_H
90 # include <netinet/udp.h>
91 #endif
92
93 #define PCAP_SNAPLEN 1460
94 #ifndef ETHER_HDR_LEN
95 #define ETHER_ADDR_LEN 6
96 #define ETHER_TYPE_LEN 2
97 #define ETHER_HDR_LEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
98 #endif
99 #ifndef ETHERTYPE_8021Q
100 # define ETHERTYPE_8021Q 0x8100
101 #endif
102 #ifndef ETHERTYPE_IPV6
103 # define ETHERTYPE_IPV6 0x86DD
104 #endif
105
106 #ifndef PPP_ADDRESS_VAL
107 # define PPP_ADDRESS_VAL 0xff   /* The address byte value */
108 #endif
109 #ifndef PPP_CONTROL_VAL
110 # define PPP_CONTROL_VAL 0x03   /* The control byte value */
111 #endif
112
113 #ifdef __linux__
114 #define uh_sport source
115 #define uh_dport dest
116 #endif
117
118 #include "utils_dns.h"
119
120 /*
121  * Type definitions
122  */
123 struct ip_list_s
124 {
125     struct in6_addr addr;
126     void *data;
127     struct ip_list_s *next;
128 };
129 typedef struct ip_list_s ip_list_t;
130
131 typedef int (printer)(const char *, ...);
132
133 /*
134  * flags/features for non-interactive mode
135  */
136
137 #ifndef T_A6
138 #define T_A6 38
139 #endif
140 #ifndef T_SRV
141 #define T_SRV 33
142 #endif
143
144 /*
145  * Global variables
146  */
147 int qtype_counts[T_MAX];
148 int opcode_counts[OP_MAX];
149 int qclass_counts[C_MAX];
150
151 #if HAVE_PCAP_H
152 static pcap_t *pcap_obj = NULL;
153 #endif
154
155 static ip_list_t *IgnoreList = NULL;
156
157 #if HAVE_PCAP_H
158 static void (*Callback) (const rfc1035_header_t *) = NULL;
159
160 static int query_count_intvl = 0;
161 static int query_count_total = 0;
162 # ifdef __OpenBSD__
163 static struct bpf_timeval last_ts;
164 # else
165 static struct timeval last_ts;
166 # endif /* __OpenBSD__ */
167 #endif /* HAVE_PCAP_H */
168
169 static int cmp_in6_addr (const struct in6_addr *a,
170         const struct in6_addr *b)
171 {
172     int i;
173
174     assert (sizeof (struct in6_addr) == 16);
175
176     for (i = 0; i < 16; i++)
177         if (a->s6_addr[i] != b->s6_addr[i])
178             break;
179
180     if (i >= 16)
181         return (0);
182
183     return (a->s6_addr[i] > b->s6_addr[i] ? 1 : -1);
184 } /* int cmp_addrinfo */
185
186 static inline int ignore_list_match (const struct in6_addr *addr)
187 {
188     ip_list_t *ptr;
189
190     for (ptr = IgnoreList; ptr != NULL; ptr = ptr->next)
191         if (cmp_in6_addr (addr, &ptr->addr) == 0)
192             return (1);
193     return (0);
194 } /* int ignore_list_match */
195
196 static void ignore_list_add (const struct in6_addr *addr)
197 {
198     ip_list_t *new;
199
200     if (ignore_list_match (addr) != 0)
201         return;
202
203     new = malloc (sizeof (ip_list_t));
204     if (new == NULL)
205     {
206         perror ("malloc");
207         return;
208     }
209
210     memcpy (&new->addr, addr, sizeof (struct in6_addr));
211     new->next = IgnoreList;
212
213     IgnoreList = new;
214 } /* void ignore_list_add */
215
216 void ignore_list_add_name (const char *name)
217 {
218     struct addrinfo *ai_list;
219     struct addrinfo *ai_ptr;
220     struct in6_addr  addr;
221     int status;
222
223     status = getaddrinfo (name, NULL, NULL, &ai_list);
224     if (status != 0)
225         return;
226
227     for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
228     {
229         if (ai_ptr->ai_family == AF_INET)
230         {
231             memset (&addr, '\0', sizeof (addr));
232             addr.s6_addr[10] = 0xFF;
233             addr.s6_addr[11] = 0xFF;
234             memcpy (addr.s6_addr + 12, &((struct sockaddr_in *) ai_ptr->ai_addr)->sin_addr, 4);
235
236             ignore_list_add (&addr);
237         }
238         else
239         {
240             ignore_list_add (&((struct sockaddr_in6 *) ai_ptr->ai_addr)->sin6_addr);
241         }
242     } /* for */
243
244     freeaddrinfo (ai_list);
245 }
246
247 #if HAVE_PCAP_H
248 static void in6_addr_from_buffer (struct in6_addr *ia,
249         const void *buf, size_t buf_len,
250         int family)
251 {
252     memset (ia, 0, sizeof (struct in6_addr));
253     if ((AF_INET == family) && (sizeof (uint32_t) == buf_len))
254     {
255         ia->s6_addr[10] = 0xFF;
256         ia->s6_addr[11] = 0xFF;
257         memcpy (ia->s6_addr + 12, buf, buf_len);
258     }
259     else if ((AF_INET6 == family) && (sizeof (struct in6_addr) == buf_len))
260     {
261         memcpy (ia, buf, buf_len);
262     }
263 } /* void in6_addr_from_buffer */
264
265 void dnstop_set_pcap_obj (pcap_t *po)
266 {
267         pcap_obj = po;
268 }
269
270 void dnstop_set_callback (void (*cb) (const rfc1035_header_t *))
271 {
272         Callback = cb;
273 }
274
275 #define RFC1035_MAXLABELSZ 63
276 static int
277 rfc1035NameUnpack(const char *buf, size_t sz, off_t * off, char *name, size_t ns
278 )
279 {
280     off_t no = 0;
281     unsigned char c;
282     size_t len;
283     assert(ns > 0);
284     do {
285         if ((*off) >= sz)
286             break;
287         c = *(buf + (*off));
288         if (c > 191) {
289             /* blasted compression */
290             unsigned short s;
291             off_t ptr;
292             memcpy(&s, buf + (*off), sizeof(s));
293             s = ntohs(s);
294             (*off) += sizeof(s);
295             /* Sanity check */
296             if ((*off) >= sz)
297                 return 1;
298             ptr = s & 0x3FFF;
299             /* Make sure the pointer is inside this message */
300             if (ptr >= sz)
301                 return 2;
302             return rfc1035NameUnpack(buf, sz, &ptr, name + no, ns - no);
303         } else if (c > RFC1035_MAXLABELSZ) {
304             /*
305              * "(The 10 and 01 combinations are reserved for future use.)"
306              */
307             break;
308             return 3;
309         } else {
310             (*off)++;
311             len = (size_t) c;
312             if (len == 0)
313                 break;
314             if (len > (ns - 1))
315                 len = ns - 1;
316             if ((*off) + len > sz)      /* message is too short */
317                 return 4;
318             memcpy(name + no, buf + (*off), len);
319             (*off) += len;
320             no += len;
321             *(name + (no++)) = '.';
322         }
323     } while (c > 0);
324     *(name + no - 1) = '\0';
325     /* make sure we didn't allow someone to overflow the name buffer */
326     assert(no <= ns);
327     return 0;
328 }
329
330 static int
331 handle_dns(const char *buf, int len,
332         const struct in6_addr *s_addr,
333         const struct in6_addr *d_addr)
334 {
335     rfc1035_header_t qh;
336     uint16_t us;
337     off_t offset;
338     char *t;
339     int x;
340
341     /* The DNS header is 12 bytes long */
342     if (len < 12)
343         return 0;
344
345     memcpy(&us, buf + 0, 2);
346     qh.id = ntohs(us);
347
348     memcpy(&us, buf + 2, 2);
349     us = ntohs(us);
350     fprintf (stderr, "Bytes 0, 1: 0x%04hx\n", us);
351     qh.qr = (us >> 15) & 0x01;
352     qh.opcode = (us >> 11) & 0x0F;
353     qh.aa = (us >> 10) & 0x01;
354     qh.tc = (us >> 9) & 0x01;
355     qh.rd = (us >> 8) & 0x01;
356     qh.ra = (us >> 7) & 0x01;
357     qh.z  = (us >> 6) & 0x01;
358     qh.ad = (us >> 5) & 0x01;
359     qh.cd = (us >> 4) & 0x01;
360     qh.rcode = us & 0x0F;
361
362     memcpy(&us, buf + 4, 2);
363     qh.qdcount = ntohs(us);
364
365     memcpy(&us, buf + 6, 2);
366     qh.ancount = ntohs(us);
367
368     memcpy(&us, buf + 8, 2);
369     qh.nscount = ntohs(us);
370
371     memcpy(&us, buf + 10, 2);
372     qh.arcount = ntohs(us);
373
374     offset = 12;
375     memset(qh.qname, '\0', MAX_QNAME_SZ);
376     x = rfc1035NameUnpack(buf, len, &offset, qh.qname, MAX_QNAME_SZ);
377     if (0 != x)
378         return 0;
379     if ('\0' == qh.qname[0])
380         strcpy(qh.qname, ".");
381     while ((t = strchr(qh.qname, '\n')))
382         *t = ' ';
383     while ((t = strchr(qh.qname, '\r')))
384         *t = ' ';
385     for (t = qh.qname; *t; t++)
386         *t = tolower(*t);
387
388     memcpy(&us, buf + offset, 2);
389     qh.qtype = ntohs(us);
390     memcpy(&us, buf + offset + 2, 2);
391     qh.qclass = ntohs(us);
392
393     qh.length = (uint16_t) len;
394
395     /* gather stats */
396     qtype_counts[qh.qtype]++;
397     qclass_counts[qh.qclass]++;
398     opcode_counts[qh.opcode]++;
399
400     if (Callback != NULL)
401             Callback (&qh);
402
403     return 1;
404 }
405
406 static int
407 handle_udp(const struct udphdr *udp, int len,
408         const struct in6_addr *s_addr,
409         const struct in6_addr *d_addr)
410 {
411     char buf[PCAP_SNAPLEN];
412     if ((ntohs (udp->uh_dport) != 53)
413                     && (ntohs (udp->uh_sport) != 53))
414         return 0;
415     memcpy(buf, udp + 1, len - sizeof(*udp));
416     if (0 == handle_dns(buf, len - sizeof(*udp), s_addr, d_addr))
417         return 0;
418     return 1;
419 }
420
421 static int
422 handle_ipv6 (struct ip6_hdr *ipv6, int len)
423 {
424     char buf[PCAP_SNAPLEN];
425     int offset;
426     int nexthdr;
427
428     struct in6_addr s_addr;
429     struct in6_addr d_addr;
430     uint16_t payload_len;
431
432     offset = sizeof (struct ip6_hdr);
433     nexthdr = ipv6->ip6_nxt;
434     s_addr = ipv6->ip6_src;
435     d_addr = ipv6->ip6_dst;
436     payload_len = ntohs (ipv6->ip6_plen);
437
438     if (ignore_list_match (&s_addr))
439             return (0);
440
441     /* Parse extension headers. This only handles the standard headers, as
442      * defined in RFC 2460, correctly. Fragments are discarded. */
443     while ((IPPROTO_ROUTING == nexthdr) /* routing header */
444             || (IPPROTO_HOPOPTS == nexthdr) /* Hop-by-Hop options. */
445             || (IPPROTO_FRAGMENT == nexthdr) /* fragmentation header. */
446             || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
447             || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
448             || (IPPROTO_AH == nexthdr) /* destination options. */
449             || (IPPROTO_ESP == nexthdr)) /* encapsulating security payload. */
450     {
451         struct ip6_ext ext_hdr;
452         uint16_t ext_hdr_len;
453
454         /* Catch broken packets */
455         if ((offset + sizeof (struct ip6_ext)) > len)
456             return (0);
457
458         /* Cannot handle fragments. */
459         if (IPPROTO_FRAGMENT == nexthdr)
460             return (0);
461
462         memcpy (&ext_hdr, (char *) ipv6 + offset, sizeof (struct ip6_ext));
463         nexthdr = ext_hdr.ip6e_nxt;
464         ext_hdr_len = (8 * (ntohs (ext_hdr.ip6e_len) + 1));
465
466         /* This header is longer than the packets payload.. WTF? */
467         if (ext_hdr_len > payload_len)
468             return (0);
469
470         offset += ext_hdr_len;
471         payload_len -= ext_hdr_len;
472     } /* while */
473
474     /* Catch broken and empty packets */
475     if (((offset + payload_len) > len)
476             || (payload_len == 0)
477             || (payload_len > PCAP_SNAPLEN))
478         return (0);
479
480     if (IPPROTO_UDP != nexthdr)
481         return (0);
482
483     memcpy (buf, (char *) ipv6 + offset, payload_len);
484     if (handle_udp ((struct udphdr *) buf, payload_len, &s_addr, &d_addr) == 0)
485         return (0);
486
487     return (1); /* Success */
488 } /* int handle_ipv6 */
489
490 static int
491 handle_ip(const struct ip *ip, int len)
492 {
493     char buf[PCAP_SNAPLEN];
494     int offset = ip->ip_hl << 2;
495     struct in6_addr s_addr;
496     struct in6_addr d_addr;
497
498     if (ip->ip_v == 6)
499         return (handle_ipv6 ((struct ip6_hdr *) ip, len));
500
501     in6_addr_from_buffer (&s_addr, &ip->ip_src.s_addr, sizeof (ip->ip_src.s_addr), AF_INET);
502     in6_addr_from_buffer (&d_addr, &ip->ip_dst.s_addr, sizeof (ip->ip_dst.s_addr), AF_INET);
503     if (ignore_list_match (&s_addr))
504             return (0);
505     if (IPPROTO_UDP != ip->ip_p)
506         return 0;
507     memcpy(buf, (void *) ip + offset, len - offset);
508     if (0 == handle_udp((struct udphdr *) buf, len - offset, &s_addr, &d_addr))
509         return 0;
510     return 1;
511 }
512
513 #if HAVE_NET_IF_PPP_H
514 static int
515 handle_ppp(const u_char * pkt, int len)
516 {
517     char buf[PCAP_SNAPLEN];
518     unsigned short us;
519     unsigned short proto;
520     if (len < 2)
521         return 0;
522     if (*pkt == PPP_ADDRESS_VAL && *(pkt + 1) == PPP_CONTROL_VAL) {
523         pkt += 2;               /* ACFC not used */
524         len -= 2;
525     }
526     if (len < 2)
527         return 0;
528     if (*pkt % 2) {
529         proto = *pkt;           /* PFC is used */
530         pkt++;
531         len--;
532     } else {
533         memcpy(&us, pkt, sizeof(us));
534         proto = ntohs(us);
535         pkt += 2;
536         len -= 2;
537     }
538     if (ETHERTYPE_IP != proto && PPP_IP != proto)
539         return 0;
540     memcpy(buf, pkt, len);
541     return handle_ip((struct ip *) buf, len);
542 }
543 #endif /* HAVE_NET_IF_PPP_H */
544
545 static int
546 handle_null(const u_char * pkt, int len)
547 {
548     unsigned int family;
549     memcpy(&family, pkt, sizeof(family));
550     if (AF_INET != family)
551         return 0;
552     return handle_ip((struct ip *) (pkt + 4), len - 4);
553 }
554
555 #ifdef DLT_LOOP
556 static int
557 handle_loop(const u_char * pkt, int len)
558 {
559     unsigned int family;
560     memcpy(&family, pkt, sizeof(family));
561     if (AF_INET != ntohl(family))
562         return 0;
563     return handle_ip((struct ip *) (pkt + 4), len - 4);
564 }
565
566 #endif
567
568 #ifdef DLT_RAW
569 static int
570 handle_raw(const u_char * pkt, int len)
571 {
572     return handle_ip((struct ip *) pkt, len);
573 }
574
575 #endif
576
577 static int
578 handle_ether(const u_char * pkt, int len)
579 {
580     char buf[PCAP_SNAPLEN];
581     struct ether_header *e = (void *) pkt;
582     unsigned short etype = ntohs(e->ether_type);
583     if (len < ETHER_HDR_LEN)
584         return 0;
585     pkt += ETHER_HDR_LEN;
586     len -= ETHER_HDR_LEN;
587     if (ETHERTYPE_8021Q == etype) {
588         etype = ntohs(*(unsigned short *) (pkt + 2));
589         pkt += 4;
590         len -= 4;
591     }
592     if ((ETHERTYPE_IP != etype)
593             && (ETHERTYPE_IPV6 != etype))
594         return 0;
595     memcpy(buf, pkt, len);
596     if (ETHERTYPE_IPV6 == etype)
597         return (handle_ipv6 ((struct ip6_hdr *) buf, len));
598     else
599         return handle_ip((struct ip *) buf, len);
600 }
601
602 /* public function */
603 void handle_pcap(u_char *udata, const struct pcap_pkthdr *hdr, const u_char *pkt)
604 {
605     int status;
606
607     fprintf (stderr, "handle_pcap (udata = %p, hdr = %p, pkt = %p): hdr->caplen = %i\n",
608                     (void *) udata, (void *) hdr, (void *) pkt,
609                     hdr->caplen);
610
611     if (hdr->caplen < ETHER_HDR_LEN)
612         return;
613
614     switch (pcap_datalink (pcap_obj))
615     {
616         case DLT_EN10MB:
617             status = handle_ether (pkt, hdr->caplen);
618             break;
619 #if HAVE_NET_IF_PPP_H
620         case DLT_PPP:
621             status = handle_ppp (pkt, hdr->caplen);
622             break;
623 #endif
624 #ifdef DLT_LOOP
625         case DLT_LOOP:
626             status = handle_loop (pkt, hdr->caplen);
627             break;
628 #endif
629 #ifdef DLT_RAW
630         case DLT_RAW:
631             status = handle_raw (pkt, hdr->caplen);
632             break;
633 #endif
634         case DLT_NULL:
635             status = handle_null (pkt, hdr->caplen);
636             break;
637
638         default:
639             fprintf (stderr, "unsupported data link type %d\n",
640                     pcap_datalink(pcap_obj));
641             status = 0;
642             break;
643     } /* switch (pcap_datalink(pcap_obj)) */
644
645     if (0 == status)
646         return;
647
648     query_count_intvl++;
649     query_count_total++;
650     last_ts = hdr->ts;
651 }
652 #endif /* HAVE_PCAP_H */
653
654 const char *qtype_str(int t)
655 {
656     static char buf[32];
657     switch (t) {
658             case ns_t_a:        return ("A");
659             case ns_t_ns:       return ("NS");
660             case ns_t_md:       return ("MD");
661             case ns_t_mf:       return ("MF");
662             case ns_t_cname:    return ("CNAME");
663             case ns_t_soa:      return ("SOA");
664             case ns_t_mb:       return ("MB");
665             case ns_t_mg:       return ("MG");
666             case ns_t_mr:       return ("MR");
667             case ns_t_null:     return ("NULL");
668             case ns_t_wks:      return ("WKS");
669             case ns_t_ptr:      return ("PTR");
670             case ns_t_hinfo:    return ("HINFO");
671             case ns_t_minfo:    return ("MINFO");
672             case ns_t_mx:       return ("MX");
673             case ns_t_txt:      return ("TXT");
674             case ns_t_rp:       return ("RP");
675             case ns_t_afsdb:    return ("AFSDB");
676             case ns_t_x25:      return ("X25");
677             case ns_t_isdn:     return ("ISDN");
678             case ns_t_rt:       return ("RT");
679             case ns_t_nsap:     return ("NSAP");
680             case ns_t_nsap_ptr: return ("NSAP-PTR");
681             case ns_t_sig:      return ("SIG");
682             case ns_t_key:      return ("KEY");
683             case ns_t_px:       return ("PX");
684             case ns_t_gpos:     return ("GPOS");
685             case ns_t_aaaa:     return ("AAAA");
686             case ns_t_loc:      return ("LOC");
687             case ns_t_nxt:      return ("NXT");
688             case ns_t_eid:      return ("EID");
689             case ns_t_nimloc:   return ("NIMLOC");
690             case ns_t_srv:      return ("SRV");
691             case ns_t_atma:     return ("ATMA");
692             case ns_t_naptr:    return ("NAPTR");
693             case ns_t_kx:       return ("KX");
694             case ns_t_cert:     return ("CERT");
695             case ns_t_a6:       return ("A6");
696             case ns_t_dname:    return ("DNAME");
697             case ns_t_sink:     return ("SINK");
698             case ns_t_opt:      return ("OPT");
699             case ns_t_tsig:     return ("TSIG");
700             case ns_t_ixfr:     return ("IXFR");
701             case ns_t_axfr:     return ("AXFR");
702             case ns_t_mailb:    return ("MAILB");
703             case ns_t_maila:    return ("MAILA");
704             case ns_t_any:      return ("ANY");
705             case ns_t_zxfr:     return ("ZXFR");
706             default:
707                     snprintf (buf, 32, "#%i", t);
708                     buf[31] = '\0';
709                     return (buf);
710     }; /* switch (t) */
711     /* NOTREACHED */
712     return (NULL);
713 }
714
715 const char *opcode_str (int o)
716 {
717     static char buf[30];
718     switch (o) {
719     case 0:
720         return "Query";
721         break;
722     case 1:
723         return "Iquery";
724         break;
725     case 2:
726         return "Status";
727         break;
728     case 4:
729         return "Notify";
730         break;
731     case 5:
732         return "Update";
733         break;
734     default:
735         snprintf(buf, 30, "Opcode%d", o);
736         return buf;
737     }
738     /* NOTREACHED */
739 }
740
741 const char *rcode_str (int rcode)
742 {
743         static char buf[32];
744         switch (rcode)
745         {
746                 case ns_r_noerror:  return ("NOERROR");
747                 case ns_r_formerr:  return ("FORMERR");
748                 case ns_r_servfail: return ("SERVFAIL");
749                 case ns_r_nxdomain: return ("NXDOMAIN");
750                 case ns_r_notimpl:  return ("NOTIMPL");
751                 case ns_r_refused:  return ("REFUSED");
752                 case ns_r_yxdomain: return ("YXDOMAIN");
753                 case ns_r_yxrrset:  return ("YXRRSET");
754                 case ns_r_nxrrset:  return ("NXRRSET");
755                 case ns_r_notauth:  return ("NOTAUTH");
756                 case ns_r_notzone:  return ("NOTZONE");
757                 case ns_r_max:      return ("MAX");
758                 case ns_r_badsig:   return ("BADSIG");
759                 case ns_r_badkey:   return ("BADKEY");
760                 case ns_r_badtime:  return ("BADTIME");
761                 default:
762                         snprintf (buf, 32, "RCode%i", rcode);
763                         buf[31] = '\0';
764                         return (buf);
765         }
766         /* Never reached */
767         return (NULL);
768 } /* const char *rcode_str (int rcode) */
769
770 #if 0
771 static int
772 main(int argc, char *argv[])
773 {
774     char errbuf[PCAP_ERRBUF_SIZE];
775     int x;
776     struct stat sb;
777     int readfile_state = 0;
778     struct bpf_program fp;
779
780     port53 = htons(53);
781     SubReport = Sources_report;
782     ignore_addr.s_addr = 0;
783     progname = strdup(strrchr(argv[0], '/') ? strchr(argv[0], '/') + 1 : argv[0]);
784     srandom(time(NULL));
785     ResetCounters();
786
787     while ((x = getopt(argc, argv, "ab:f:i:pst")) != -1) {
788         switch (x) {
789         case 'a':
790             anon_flag = 1;
791             break;
792         case 's':
793             sld_flag = 1;
794             break;
795         case 't':
796             nld_flag = 1;
797             break;
798         case 'p':
799             promisc_flag = 0;
800             break;
801         case 'b':
802             bpf_program_str = strdup(optarg);
803             break;
804         case 'i':
805             ignore_addr.s_addr = inet_addr(optarg);
806             break;
807         case 'f':
808             set_filter(optarg);
809             break;
810         default:
811             usage();
812             break;
813         }
814     }
815     argc -= optind;
816     argv += optind;
817
818     if (argc < 1)
819         usage();
820     device = strdup(argv[0]);
821
822     if (0 == stat(device, &sb))
823         readfile_state = 1;
824     if (readfile_state) {
825         pcap_obj = pcap_open_offline(device, errbuf);
826     } else {
827         pcap_obj = pcap_open_live(device, PCAP_SNAPLEN, promisc_flag, 1000, errbuf);
828     }
829     if (NULL == pcap_obj) {
830         fprintf(stderr, "pcap_open_*: %s\n", errbuf);
831         exit(1);
832     }
833
834     if (0 == isatty(1)) {
835         if (0 == readfile_state) {
836             fprintf(stderr, "Non-interactive mode requires savefile argument\n");
837             exit(1);
838         }
839         interactive = 0;
840         print_func = printf;
841     }
842
843     memset(&fp, '\0', sizeof(fp));
844     x = pcap_compile(pcap_obj, &fp, bpf_program_str, 1, 0);
845     if (x < 0) {
846         fprintf(stderr, "pcap_compile failed\n");
847         exit(1);
848     }
849     x = pcap_setfilter(pcap_obj, &fp);
850     if (x < 0) {
851         fprintf(stderr, "pcap_setfilter failed\n");
852         exit(1);
853     }
854
855     /*
856      * non-blocking call added for Mac OS X bugfix.  Sent by Max Horn.
857      * ref http://www.tcpdump.org/lists/workers/2002/09/msg00033.html
858      */
859     x = pcap_setnonblock(pcap_obj, 1, errbuf);
860     if (x < 0) {
861         fprintf(stderr, "pcap_setnonblock failed: %s\n", errbuf);
862         exit(1);
863     }
864
865     switch (pcap_datalink(pcap_obj)) {
866     case DLT_EN10MB:
867         handle_datalink = handle_ether;
868         break;
869 #if HAVE_NET_IF_PPP_H
870     case DLT_PPP:
871         handle_datalink = handle_ppp;
872         break;
873 #endif
874 #ifdef DLT_LOOP
875     case DLT_LOOP:
876         handle_datalink = handle_loop;
877         break;
878 #endif
879 #ifdef DLT_RAW
880     case DLT_RAW:
881         handle_datalink = handle_raw;
882         break;
883 #endif
884     case DLT_NULL:
885         handle_datalink = handle_null;
886         break;
887     default:
888         fprintf(stderr, "unsupported data link type %d\n",
889             pcap_datalink(pcap_obj));
890         return 1;
891         break;
892     }
893     if (interactive) {
894         init_curses();
895         while (0 == Quit) {
896             if (readfile_state < 2) {
897                 /*
898                  * On some OSes select() might return 0 even when
899                  * there are packets to process.  Thus, we always
900                  * ignore its return value and just call pcap_dispatch()
901                  * anyway.
902                  */
903                 if (0 == readfile_state)        /* interactive */
904                     pcap_select(pcap_obj, 1, 0);
905                 x = pcap_dispatch(pcap_obj, 50, handle_pcap, NULL);
906             }
907             if (0 == x && 1 == readfile_state) {
908                 /* block on keyboard until user quits */
909                 readfile_state++;
910                 nodelay(w, 0);
911             }
912             keyboard();
913             cron_pre();
914             report();
915             cron_post();
916         }
917         endwin();               /* klin, Thu Nov 28 08:56:51 2002 */
918     } else {
919         while (pcap_dispatch(pcap_obj, 50, handle_pcap, NULL))
920                 (void) 0;
921         cron_pre();
922         Sources_report(); print_func("\n");
923         Destinatioreport(); print_func("\n");
924         Qtypes_report(); print_func("\n");
925         Opcodes_report(); print_func("\n");
926         Tld_report(); print_func("\n");
927         Sld_report(); print_func("\n");
928         Nld_report(); print_func("\n");
929         SldBySource_report();
930     }
931
932     pcap_close(pcap_obj);
933     return 0;
934 } /* static int main(int argc, char *argv[]) */
935 #endif
936 /*
937  * vim:shiftwidth=4:tabstop=8:softtabstop=4
938  */