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