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