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