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