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