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