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