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