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