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