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