1 /*
2 * collectd - src/utils_dns.c
3 * Modifications Copyright (C) 2006 Florian octo Forster
4 * Copyright (C) 2002 The Measurement Factory, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 * 3. Neither the name of The Measurement Factory nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Authors:
32 * The Measurement Factory, Inc. <http://www.measurement-factory.com/>
33 * Florian octo Forster <octo at verplant.org>
34 */
36 #define _BSD_SOURCE
38 #include "collectd.h"
39 #include "plugin.h"
40 #include "common.h"
42 #if HAVE_NETINET_IN_SYSTM_H
43 # include <netinet/in_systm.h>
44 #endif
45 #if HAVE_NETINET_IN_H
46 # include <netinet/in.h>
47 #endif
48 #if HAVE_ARPA_INET_H
49 # include <arpa/inet.h>
50 #endif
51 #if HAVE_SYS_SOCKET_H
52 # include <sys/socket.h>
53 #endif
55 #if HAVE_ARPA_NAMESER_H
56 # include <arpa/nameser.h>
57 #endif
58 #if HAVE_ARPA_NAMESER_COMPAT_H
59 # include <arpa/nameser_compat.h>
60 #endif
62 #if HAVE_NET_IF_ARP_H
63 # include <net/if_arp.h>
64 #endif
65 #if HAVE_NET_IF_H
66 # include <net/if.h>
67 #endif
68 #if HAVE_NETINET_IF_ETHER_H
69 # include <netinet/if_ether.h>
70 #endif
71 #if HAVE_NET_PPP_DEFS_H
72 # include <net/ppp_defs.h>
73 #endif
74 #if HAVE_NET_IF_PPP_H
75 # include <net/if_ppp.h>
76 #endif
78 #if HAVE_NETDB_H
79 # include <netdb.h>
80 #endif
82 #if HAVE_NETINET_IP_H
83 # include <netinet/ip.h>
84 #endif
85 #ifdef HAVE_NETINET_IP_VAR_H
86 # include <netinet/ip_var.h>
87 #endif
88 #if HAVE_NETINET_IP6_H
89 # include <netinet/ip6.h>
90 #endif
91 #if HAVE_NETINET_UDP_H
92 # include <netinet/udp.h>
93 #endif
95 #if HAVE_PCAP_H
96 # include <pcap.h>
97 #endif
99 #define PCAP_SNAPLEN 1460
100 #ifndef ETHER_HDR_LEN
101 #define ETHER_ADDR_LEN 6
102 #define ETHER_TYPE_LEN 2
103 #define ETHER_HDR_LEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
104 #endif
105 #ifndef ETHERTYPE_8021Q
106 # define ETHERTYPE_8021Q 0x8100
107 #endif
108 #ifndef ETHERTYPE_IPV6
109 # define ETHERTYPE_IPV6 0x86DD
110 #endif
112 #ifndef PPP_ADDRESS_VAL
113 # define PPP_ADDRESS_VAL 0xff /* The address byte value */
114 #endif
115 #ifndef PPP_CONTROL_VAL
116 # define PPP_CONTROL_VAL 0x03 /* The control byte value */
117 #endif
119 #if HAVE_STRUCT_UDPHDR_UH_DPORT && HAVE_STRUCT_UDPHDR_UH_SPORT
120 # define UDP_DEST uh_dport
121 # define UDP_SRC uh_dport
122 #elif HAVE_STRUCT_UDPHDR_DEST && HAVE_STRUCT_UDPHDR_SOURCE
123 # define UDP_DEST dest
124 # define UDP_SRC source
125 #else
126 # error "`struct udphdr' is unusable."
127 #endif
129 #include "utils_dns.h"
131 /*
132 * Type definitions
133 */
134 struct ip_list_s
135 {
136 struct in6_addr addr;
137 void *data;
138 struct ip_list_s *next;
139 };
140 typedef struct ip_list_s ip_list_t;
142 typedef int (printer)(const char *, ...);
144 /*
145 * flags/features for non-interactive mode
146 */
148 #ifndef T_A6
149 #define T_A6 38
150 #endif
151 #ifndef T_SRV
152 #define T_SRV 33
153 #endif
155 /*
156 * Global variables
157 */
158 int qtype_counts[T_MAX];
159 int opcode_counts[OP_MAX];
160 int qclass_counts[C_MAX];
162 #if HAVE_PCAP_H
163 static pcap_t *pcap_obj = NULL;
164 #endif
166 static ip_list_t *IgnoreList = NULL;
168 #if HAVE_PCAP_H
169 static void (*Callback) (const rfc1035_header_t *) = NULL;
171 static int query_count_intvl = 0;
172 static int query_count_total = 0;
173 # ifdef __OpenBSD__
174 static struct bpf_timeval last_ts;
175 # else
176 static struct timeval last_ts;
177 # endif /* __OpenBSD__ */
178 #endif /* HAVE_PCAP_H */
180 static int cmp_in6_addr (const struct in6_addr *a,
181 const struct in6_addr *b)
182 {
183 int i;
185 assert (sizeof (struct in6_addr) == 16);
187 for (i = 0; i < 16; i++)
188 if (a->s6_addr[i] != b->s6_addr[i])
189 break;
191 if (i >= 16)
192 return (0);
194 return (a->s6_addr[i] > b->s6_addr[i] ? 1 : -1);
195 } /* int cmp_addrinfo */
197 static inline int ignore_list_match (const struct in6_addr *addr)
198 {
199 ip_list_t *ptr;
201 for (ptr = IgnoreList; ptr != NULL; ptr = ptr->next)
202 if (cmp_in6_addr (addr, &ptr->addr) == 0)
203 return (1);
204 return (0);
205 } /* int ignore_list_match */
207 static void ignore_list_add (const struct in6_addr *addr)
208 {
209 ip_list_t *new;
211 if (ignore_list_match (addr) != 0)
212 return;
214 new = malloc (sizeof (ip_list_t));
215 if (new == NULL)
216 {
217 perror ("malloc");
218 return;
219 }
221 memcpy (&new->addr, addr, sizeof (struct in6_addr));
222 new->next = IgnoreList;
224 IgnoreList = new;
225 } /* void ignore_list_add */
227 void ignore_list_add_name (const char *name)
228 {
229 struct addrinfo *ai_list;
230 struct addrinfo *ai_ptr;
231 struct in6_addr addr;
232 int status;
234 status = getaddrinfo (name, NULL, NULL, &ai_list);
235 if (status != 0)
236 return;
238 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
239 {
240 if (ai_ptr->ai_family == AF_INET)
241 {
242 memset (&addr, '\0', sizeof (addr));
243 addr.s6_addr[10] = 0xFF;
244 addr.s6_addr[11] = 0xFF;
245 memcpy (addr.s6_addr + 12, &((struct sockaddr_in *) ai_ptr->ai_addr)->sin_addr, 4);
247 ignore_list_add (&addr);
248 }
249 else
250 {
251 ignore_list_add (&((struct sockaddr_in6 *) ai_ptr->ai_addr)->sin6_addr);
252 }
253 } /* for */
255 freeaddrinfo (ai_list);
256 }
258 #if HAVE_PCAP_H
259 static void in6_addr_from_buffer (struct in6_addr *ia,
260 const void *buf, size_t buf_len,
261 int family)
262 {
263 memset (ia, 0, sizeof (struct in6_addr));
264 if ((AF_INET == family) && (sizeof (uint32_t) == buf_len))
265 {
266 ia->s6_addr[10] = 0xFF;
267 ia->s6_addr[11] = 0xFF;
268 memcpy (ia->s6_addr + 12, buf, buf_len);
269 }
270 else if ((AF_INET6 == family) && (sizeof (struct in6_addr) == buf_len))
271 {
272 memcpy (ia, buf, buf_len);
273 }
274 } /* void in6_addr_from_buffer */
276 void dnstop_set_pcap_obj (pcap_t *po)
277 {
278 pcap_obj = po;
279 }
281 void dnstop_set_callback (void (*cb) (const rfc1035_header_t *))
282 {
283 Callback = cb;
284 }
286 #define RFC1035_MAXLABELSZ 63
287 static int
288 rfc1035NameUnpack(const char *buf, size_t sz, off_t * off, char *name, size_t ns
289 )
290 {
291 off_t no = 0;
292 unsigned char c;
293 size_t len;
294 assert(ns > 0);
295 do {
296 if ((*off) >= sz)
297 break;
298 c = *(buf + (*off));
299 if (c > 191) {
300 /* blasted compression */
301 unsigned short s;
302 off_t ptr;
303 memcpy(&s, buf + (*off), sizeof(s));
304 s = ntohs(s);
305 (*off) += sizeof(s);
306 /* Sanity check */
307 if ((*off) >= sz)
308 return 1;
309 ptr = s & 0x3FFF;
310 /* Make sure the pointer is inside this message */
311 if (ptr >= sz)
312 return 2;
313 return rfc1035NameUnpack(buf, sz, &ptr, name + no, ns - no);
314 } else if (c > RFC1035_MAXLABELSZ) {
315 /*
316 * "(The 10 and 01 combinations are reserved for future use.)"
317 */
318 break;
319 return 3;
320 } else {
321 (*off)++;
322 len = (size_t) c;
323 if (len == 0)
324 break;
325 if (len > (ns - 1))
326 len = ns - 1;
327 if ((*off) + len > sz) /* message is too short */
328 return 4;
329 memcpy(name + no, buf + (*off), len);
330 (*off) += len;
331 no += len;
332 *(name + (no++)) = '.';
333 }
334 } while (c > 0);
335 *(name + no - 1) = '\0';
336 /* make sure we didn't allow someone to overflow the name buffer */
337 assert(no <= ns);
338 return 0;
339 }
341 static int
342 handle_dns(const char *buf, int len)
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 {
417 char buf[PCAP_SNAPLEN];
418 if ((ntohs (udp->UDP_DEST) != 53)
419 && (ntohs (udp->UDP_SRC) != 53))
420 return 0;
421 memcpy(buf, udp + 1, len - sizeof(*udp));
422 if (0 == handle_dns(buf, len - sizeof(*udp)))
423 return 0;
424 return 1;
425 }
427 static int
428 handle_ipv6 (struct ip6_hdr *ipv6, int len)
429 {
430 char buf[PCAP_SNAPLEN];
431 unsigned int offset;
432 int nexthdr;
434 struct in6_addr s_addr;
435 struct in6_addr d_addr;
436 uint16_t payload_len;
438 if (0 > len)
439 return (0);
441 offset = sizeof (struct ip6_hdr);
442 nexthdr = ipv6->ip6_nxt;
443 s_addr = ipv6->ip6_src;
444 d_addr = ipv6->ip6_dst;
445 payload_len = ntohs (ipv6->ip6_plen);
447 if (ignore_list_match (&s_addr))
448 return (0);
450 /* Parse extension headers. This only handles the standard headers, as
451 * defined in RFC 2460, correctly. Fragments are discarded. */
452 while ((IPPROTO_ROUTING == nexthdr) /* routing header */
453 || (IPPROTO_HOPOPTS == nexthdr) /* Hop-by-Hop options. */
454 || (IPPROTO_FRAGMENT == nexthdr) /* fragmentation header. */
455 || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
456 || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
457 || (IPPROTO_AH == nexthdr) /* destination options. */
458 || (IPPROTO_ESP == nexthdr)) /* encapsulating security payload. */
459 {
460 struct ip6_ext ext_hdr;
461 uint16_t ext_hdr_len;
463 /* Catch broken packets */
464 if ((offset + sizeof (struct ip6_ext)) > (unsigned int)len)
465 return (0);
467 /* Cannot handle fragments. */
468 if (IPPROTO_FRAGMENT == nexthdr)
469 return (0);
471 memcpy (&ext_hdr, (char *) ipv6 + offset, sizeof (struct ip6_ext));
472 nexthdr = ext_hdr.ip6e_nxt;
473 ext_hdr_len = (8 * (ntohs (ext_hdr.ip6e_len) + 1));
475 /* This header is longer than the packets payload.. WTF? */
476 if (ext_hdr_len > payload_len)
477 return (0);
479 offset += ext_hdr_len;
480 payload_len -= ext_hdr_len;
481 } /* while */
483 /* Catch broken and empty packets */
484 if (((offset + payload_len) > (unsigned int)len)
485 || (payload_len == 0)
486 || (payload_len > PCAP_SNAPLEN))
487 return (0);
489 if (IPPROTO_UDP != nexthdr)
490 return (0);
492 memcpy (buf, (char *) ipv6 + offset, payload_len);
493 if (handle_udp ((struct udphdr *) buf, payload_len) == 0)
494 return (0);
496 return (1); /* Success */
497 } /* int handle_ipv6 */
499 static int
500 handle_ip(const struct ip *ip, int len)
501 {
502 char buf[PCAP_SNAPLEN];
503 int offset = ip->ip_hl << 2;
504 struct in6_addr s_addr;
505 struct in6_addr d_addr;
507 if (ip->ip_v == 6)
508 return (handle_ipv6 ((struct ip6_hdr *) ip, len));
510 in6_addr_from_buffer (&s_addr, &ip->ip_src.s_addr, sizeof (ip->ip_src.s_addr), AF_INET);
511 in6_addr_from_buffer (&d_addr, &ip->ip_dst.s_addr, sizeof (ip->ip_dst.s_addr), AF_INET);
512 if (ignore_list_match (&s_addr))
513 return (0);
514 if (IPPROTO_UDP != ip->ip_p)
515 return 0;
516 memcpy(buf, (void *) ip + offset, len - offset);
517 if (0 == handle_udp((struct udphdr *) buf, len - offset))
518 return 0;
519 return 1;
520 }
522 #if HAVE_NET_IF_PPP_H
523 static int
524 handle_ppp(const u_char * pkt, int len)
525 {
526 char buf[PCAP_SNAPLEN];
527 unsigned short us;
528 unsigned short proto;
529 if (len < 2)
530 return 0;
531 if (*pkt == PPP_ADDRESS_VAL && *(pkt + 1) == PPP_CONTROL_VAL) {
532 pkt += 2; /* ACFC not used */
533 len -= 2;
534 }
535 if (len < 2)
536 return 0;
537 if (*pkt % 2) {
538 proto = *pkt; /* PFC is used */
539 pkt++;
540 len--;
541 } else {
542 memcpy(&us, pkt, sizeof(us));
543 proto = ntohs(us);
544 pkt += 2;
545 len -= 2;
546 }
547 if (ETHERTYPE_IP != proto && PPP_IP != proto)
548 return 0;
549 memcpy(buf, pkt, len);
550 return handle_ip((struct ip *) buf, len);
551 }
552 #endif /* HAVE_NET_IF_PPP_H */
554 static int
555 handle_null(const u_char * pkt, int len)
556 {
557 unsigned int family;
558 memcpy(&family, pkt, sizeof(family));
559 if (AF_INET != family)
560 return 0;
561 return handle_ip((struct ip *) (pkt + 4), len - 4);
562 }
564 #ifdef DLT_LOOP
565 static int
566 handle_loop(const u_char * pkt, int len)
567 {
568 unsigned int family;
569 memcpy(&family, pkt, sizeof(family));
570 if (AF_INET != ntohl(family))
571 return 0;
572 return handle_ip((struct ip *) (pkt + 4), len - 4);
573 }
575 #endif
577 #ifdef DLT_RAW
578 static int
579 handle_raw(const u_char * pkt, int len)
580 {
581 return handle_ip((struct ip *) pkt, len);
582 }
584 #endif
586 static int
587 handle_ether(const u_char * pkt, int len)
588 {
589 char buf[PCAP_SNAPLEN];
590 struct ether_header *e = (void *) pkt;
591 unsigned short etype = ntohs(e->ether_type);
592 if (len < ETHER_HDR_LEN)
593 return 0;
594 pkt += ETHER_HDR_LEN;
595 len -= ETHER_HDR_LEN;
596 if (ETHERTYPE_8021Q == etype) {
597 etype = ntohs(*(unsigned short *) (pkt + 2));
598 pkt += 4;
599 len -= 4;
600 }
601 if ((ETHERTYPE_IP != etype)
602 && (ETHERTYPE_IPV6 != etype))
603 return 0;
604 memcpy(buf, pkt, len);
605 if (ETHERTYPE_IPV6 == etype)
606 return (handle_ipv6 ((struct ip6_hdr *) buf, len));
607 else
608 return handle_ip((struct ip *) buf, len);
609 }
611 #ifdef DLT_LINUX_SLL
612 static int
613 handle_linux_sll (const u_char *pkt, int len)
614 {
615 struct sll_header
616 {
617 uint16_t pkt_type;
618 uint16_t dev_type;
619 uint16_t addr_len;
620 uint8_t addr[8];
621 uint16_t proto_type;
622 } *hdr;
623 uint16_t etype;
625 if ((0 > len) || ((unsigned int)len < sizeof (struct sll_header)))
626 return (0);
628 hdr = (struct sll_header *) pkt;
629 pkt = (u_char *) (hdr + 1);
630 len -= sizeof (struct sll_header);
632 etype = ntohs (hdr->proto_type);
634 if ((ETHERTYPE_IP != etype)
635 && (ETHERTYPE_IPV6 != etype))
636 return 0;
638 if (ETHERTYPE_IPV6 == etype)
639 return (handle_ipv6 ((struct ip6_hdr *) pkt, len));
640 else
641 return handle_ip((struct ip *) pkt, len);
642 }
643 #endif /* DLT_LINUX_SLL */
645 /* public function */
646 void handle_pcap(u_char *udata, const struct pcap_pkthdr *hdr, const u_char *pkt)
647 {
648 int status;
650 DEBUG ("handle_pcap (udata = %p, hdr = %p, pkt = %p): hdr->caplen = %i\n",
651 (void *) udata, (void *) hdr, (void *) pkt,
652 hdr->caplen);
654 if (hdr->caplen < ETHER_HDR_LEN)
655 return;
657 switch (pcap_datalink (pcap_obj))
658 {
659 case DLT_EN10MB:
660 status = handle_ether (pkt, hdr->caplen);
661 break;
662 #if HAVE_NET_IF_PPP_H
663 case DLT_PPP:
664 status = handle_ppp (pkt, hdr->caplen);
665 break;
666 #endif
667 #ifdef DLT_LOOP
668 case DLT_LOOP:
669 status = handle_loop (pkt, hdr->caplen);
670 break;
671 #endif
672 #ifdef DLT_RAW
673 case DLT_RAW:
674 status = handle_raw (pkt, hdr->caplen);
675 break;
676 #endif
677 #ifdef DLT_LINUX_SLL
678 case DLT_LINUX_SLL:
679 status = handle_linux_sll (pkt, hdr->caplen);
680 break;
681 #endif
682 case DLT_NULL:
683 status = handle_null (pkt, hdr->caplen);
684 break;
686 default:
687 ERROR ("handle_pcap: unsupported data link type %d\n",
688 pcap_datalink(pcap_obj));
689 status = 0;
690 break;
691 } /* switch (pcap_datalink(pcap_obj)) */
693 if (0 == status)
694 return;
696 query_count_intvl++;
697 query_count_total++;
698 last_ts = hdr->ts;
699 }
700 #endif /* HAVE_PCAP_H */
702 const char *qtype_str(int t)
703 {
704 static char buf[32];
705 switch (t) {
706 #if (defined (__NAMESER)) && (__NAMESER >= 19991001)
707 case ns_t_a: return ("A");
708 case ns_t_ns: return ("NS");
709 case ns_t_md: return ("MD");
710 case ns_t_mf: return ("MF");
711 case ns_t_cname: return ("CNAME");
712 case ns_t_soa: return ("SOA");
713 case ns_t_mb: return ("MB");
714 case ns_t_mg: return ("MG");
715 case ns_t_mr: return ("MR");
716 case ns_t_null: return ("NULL");
717 case ns_t_wks: return ("WKS");
718 case ns_t_ptr: return ("PTR");
719 case ns_t_hinfo: return ("HINFO");
720 case ns_t_minfo: return ("MINFO");
721 case ns_t_mx: return ("MX");
722 case ns_t_txt: return ("TXT");
723 case ns_t_rp: return ("RP");
724 case ns_t_afsdb: return ("AFSDB");
725 case ns_t_x25: return ("X25");
726 case ns_t_isdn: return ("ISDN");
727 case ns_t_rt: return ("RT");
728 case ns_t_nsap: return ("NSAP");
729 case ns_t_nsap_ptr: return ("NSAP-PTR");
730 case ns_t_sig: return ("SIG");
731 case ns_t_key: return ("KEY");
732 case ns_t_px: return ("PX");
733 case ns_t_gpos: return ("GPOS");
734 case ns_t_aaaa: return ("AAAA");
735 case ns_t_loc: return ("LOC");
736 case ns_t_nxt: return ("NXT");
737 case ns_t_eid: return ("EID");
738 case ns_t_nimloc: return ("NIMLOC");
739 case ns_t_srv: return ("SRV");
740 case ns_t_atma: return ("ATMA");
741 case ns_t_naptr: return ("NAPTR");
742 case ns_t_kx: return ("KX");
743 case ns_t_cert: return ("CERT");
744 case ns_t_a6: return ("A6");
745 case ns_t_dname: return ("DNAME");
746 case ns_t_sink: return ("SINK");
747 case ns_t_opt: return ("OPT");
748 # if __NAMESER >= 19991006
749 case ns_t_tsig: return ("TSIG");
750 # endif
751 case ns_t_ixfr: return ("IXFR");
752 case ns_t_axfr: return ("AXFR");
753 case ns_t_mailb: return ("MAILB");
754 case ns_t_maila: return ("MAILA");
755 case ns_t_any: return ("ANY");
756 case ns_t_zxfr: return ("ZXFR");
757 /* #endif __NAMESER >= 19991006 */
758 #elif (defined (__BIND)) && (__BIND >= 19950621)
759 case T_A: return ("A"); /* 1 ... */
760 case T_NS: return ("NS");
761 case T_MD: return ("MD");
762 case T_MF: return ("MF");
763 case T_CNAME: return ("CNAME");
764 case T_SOA: return ("SOA");
765 case T_MB: return ("MB");
766 case T_MG: return ("MG");
767 case T_MR: return ("MR");
768 case T_NULL: return ("NULL");
769 case T_WKS: return ("WKS");
770 case T_PTR: return ("PTR");
771 case T_HINFO: return ("HINFO");
772 case T_MINFO: return ("MINFO");
773 case T_MX: return ("MX");
774 case T_TXT: return ("TXT");
775 case T_RP: return ("RP");
776 case T_AFSDB: return ("AFSDB");
777 case T_X25: return ("X25");
778 case T_ISDN: return ("ISDN");
779 case T_RT: return ("RT");
780 case T_NSAP: return ("NSAP");
781 case T_NSAP_PTR: return ("NSAP_PTR");
782 case T_SIG: return ("SIG");
783 case T_KEY: return ("KEY");
784 case T_PX: return ("PX");
785 case T_GPOS: return ("GPOS");
786 case T_AAAA: return ("AAAA");
787 case T_LOC: return ("LOC");
788 case T_NXT: return ("NXT");
789 case T_EID: return ("EID");
790 case T_NIMLOC: return ("NIMLOC");
791 case T_SRV: return ("SRV");
792 case T_ATMA: return ("ATMA");
793 case T_NAPTR: return ("NAPTR"); /* ... 35 */
794 #if (__BIND >= 19960801)
795 case T_KX: return ("KX"); /* 36 ... */
796 case T_CERT: return ("CERT");
797 case T_A6: return ("A6");
798 case T_DNAME: return ("DNAME");
799 case T_SINK: return ("SINK");
800 case T_OPT: return ("OPT");
801 case T_APL: return ("APL");
802 case T_DS: return ("DS");
803 case T_SSHFP: return ("SSHFP");
804 case T_RRSIG: return ("RRSIG");
805 case T_NSEC: return ("NSEC");
806 case T_DNSKEY: return ("DNSKEY"); /* ... 48 */
807 case T_TKEY: return ("TKEY"); /* 249 */
808 #endif /* __BIND >= 19960801 */
809 case T_TSIG: return ("TSIG"); /* 250 ... */
810 case T_IXFR: return ("IXFR");
811 case T_AXFR: return ("AXFR");
812 case T_MAILB: return ("MAILB");
813 case T_MAILA: return ("MAILA");
814 case T_ANY: return ("ANY"); /* ... 255 */
815 #endif /* __BIND >= 19950621 */
816 default:
817 ssnprintf (buf, sizeof (buf), "#%i", t);
818 return (buf);
819 }; /* switch (t) */
820 /* NOTREACHED */
821 return (NULL);
822 }
824 const char *opcode_str (int o)
825 {
826 static char buf[30];
827 switch (o) {
828 case 0:
829 return "Query";
830 break;
831 case 1:
832 return "Iquery";
833 break;
834 case 2:
835 return "Status";
836 break;
837 case 4:
838 return "Notify";
839 break;
840 case 5:
841 return "Update";
842 break;
843 default:
844 ssnprintf(buf, sizeof (buf), "Opcode%d", o);
845 return buf;
846 }
847 /* NOTREACHED */
848 }
850 const char *rcode_str (int rcode)
851 {
852 static char buf[32];
853 switch (rcode)
854 {
855 #if (defined (__NAMESER)) && (__NAMESER >= 19991006)
856 case ns_r_noerror: return ("NOERROR");
857 case ns_r_formerr: return ("FORMERR");
858 case ns_r_servfail: return ("SERVFAIL");
859 case ns_r_nxdomain: return ("NXDOMAIN");
860 case ns_r_notimpl: return ("NOTIMPL");
861 case ns_r_refused: return ("REFUSED");
862 case ns_r_yxdomain: return ("YXDOMAIN");
863 case ns_r_yxrrset: return ("YXRRSET");
864 case ns_r_nxrrset: return ("NXRRSET");
865 case ns_r_notauth: return ("NOTAUTH");
866 case ns_r_notzone: return ("NOTZONE");
867 case ns_r_max: return ("MAX");
868 case ns_r_badsig: return ("BADSIG");
869 case ns_r_badkey: return ("BADKEY");
870 case ns_r_badtime: return ("BADTIME");
871 /* #endif __NAMESER >= 19991006 */
872 #elif (defined (__BIND)) && (__BIND >= 19950621)
873 case NOERROR: return ("NOERROR");
874 case FORMERR: return ("FORMERR");
875 case SERVFAIL: return ("SERVFAIL");
876 case NXDOMAIN: return ("NXDOMAIN");
877 case NOTIMP: return ("NOTIMP");
878 case REFUSED: return ("REFUSED");
879 #if defined (YXDOMAIN) && defined (NXRRSET)
880 case YXDOMAIN: return ("YXDOMAIN");
881 case YXRRSET: return ("YXRRSET");
882 case NXRRSET: return ("NXRRSET");
883 case NOTAUTH: return ("NOTAUTH");
884 case NOTZONE: return ("NOTZONE");
885 #endif /* RFC2136 rcodes */
886 #endif /* __BIND >= 19950621 */
887 default:
888 ssnprintf (buf, sizeof (buf), "RCode%i", rcode);
889 return (buf);
890 }
891 /* Never reached */
892 return (NULL);
893 } /* const char *rcode_str (int rcode) */
895 #if 0
896 static int
897 main(int argc, char *argv[])
898 {
899 char errbuf[PCAP_ERRBUF_SIZE];
900 int x;
901 struct stat sb;
902 int readfile_state = 0;
903 struct bpf_program fp;
905 port53 = htons(53);
906 SubReport = Sources_report;
907 ignore_addr.s_addr = 0;
908 progname = strdup(strrchr(argv[0], '/') ? strchr(argv[0], '/') + 1 : argv[0]);
909 srandom(time(NULL));
910 ResetCounters();
912 while ((x = getopt(argc, argv, "ab:f:i:pst")) != -1) {
913 switch (x) {
914 case 'a':
915 anon_flag = 1;
916 break;
917 case 's':
918 sld_flag = 1;
919 break;
920 case 't':
921 nld_flag = 1;
922 break;
923 case 'p':
924 promisc_flag = 0;
925 break;
926 case 'b':
927 bpf_program_str = strdup(optarg);
928 break;
929 case 'i':
930 ignore_addr.s_addr = inet_addr(optarg);
931 break;
932 case 'f':
933 set_filter(optarg);
934 break;
935 default:
936 usage();
937 break;
938 }
939 }
940 argc -= optind;
941 argv += optind;
943 if (argc < 1)
944 usage();
945 device = strdup(argv[0]);
947 if (0 == stat(device, &sb))
948 readfile_state = 1;
949 if (readfile_state) {
950 pcap_obj = pcap_open_offline(device, errbuf);
951 } else {
952 pcap_obj = pcap_open_live(device, PCAP_SNAPLEN, promisc_flag, 1000, errbuf);
953 }
954 if (NULL == pcap_obj) {
955 fprintf(stderr, "pcap_open_*: %s\n", errbuf);
956 exit(1);
957 }
959 if (0 == isatty(1)) {
960 if (0 == readfile_state) {
961 fprintf(stderr, "Non-interactive mode requires savefile argument\n");
962 exit(1);
963 }
964 interactive = 0;
965 print_func = printf;
966 }
968 memset(&fp, '\0', sizeof(fp));
969 x = pcap_compile(pcap_obj, &fp, bpf_program_str, 1, 0);
970 if (x < 0) {
971 fprintf(stderr, "pcap_compile failed\n");
972 exit(1);
973 }
974 x = pcap_setfilter(pcap_obj, &fp);
975 if (x < 0) {
976 fprintf(stderr, "pcap_setfilter failed\n");
977 exit(1);
978 }
980 /*
981 * non-blocking call added for Mac OS X bugfix. Sent by Max Horn.
982 * ref http://www.tcpdump.org/lists/workers/2002/09/msg00033.html
983 */
984 x = pcap_setnonblock(pcap_obj, 1, errbuf);
985 if (x < 0) {
986 fprintf(stderr, "pcap_setnonblock failed: %s\n", errbuf);
987 exit(1);
988 }
990 switch (pcap_datalink(pcap_obj)) {
991 case DLT_EN10MB:
992 handle_datalink = handle_ether;
993 break;
994 #if HAVE_NET_IF_PPP_H
995 case DLT_PPP:
996 handle_datalink = handle_ppp;
997 break;
998 #endif
999 #ifdef DLT_LOOP
1000 case DLT_LOOP:
1001 handle_datalink = handle_loop;
1002 break;
1003 #endif
1004 #ifdef DLT_RAW
1005 case DLT_RAW:
1006 handle_datalink = handle_raw;
1007 break;
1008 #endif
1009 case DLT_NULL:
1010 handle_datalink = handle_null;
1011 break;
1012 default:
1013 fprintf(stderr, "unsupported data link type %d\n",
1014 pcap_datalink(pcap_obj));
1015 return 1;
1016 break;
1017 }
1018 if (interactive) {
1019 init_curses();
1020 while (0 == Quit) {
1021 if (readfile_state < 2) {
1022 /*
1023 * On some OSes select() might return 0 even when
1024 * there are packets to process. Thus, we always
1025 * ignore its return value and just call pcap_dispatch()
1026 * anyway.
1027 */
1028 if (0 == readfile_state) /* interactive */
1029 pcap_select(pcap_obj, 1, 0);
1030 x = pcap_dispatch(pcap_obj, 50, handle_pcap, NULL);
1031 }
1032 if (0 == x && 1 == readfile_state) {
1033 /* block on keyboard until user quits */
1034 readfile_state++;
1035 nodelay(w, 0);
1036 }
1037 keyboard();
1038 cron_pre();
1039 report();
1040 cron_post();
1041 }
1042 endwin(); /* klin, Thu Nov 28 08:56:51 2002 */
1043 } else {
1044 while (pcap_dispatch(pcap_obj, 50, handle_pcap, NULL))
1045 (void) 0;
1046 cron_pre();
1047 Sources_report(); print_func("\n");
1048 Destinatioreport(); print_func("\n");
1049 Qtypes_report(); print_func("\n");
1050 Opcodes_report(); print_func("\n");
1051 Tld_report(); print_func("\n");
1052 Sld_report(); print_func("\n");
1053 Nld_report(); print_func("\n");
1054 SldBySource_report();
1055 }
1057 pcap_close(pcap_obj);
1058 return 0;
1059 } /* static int main(int argc, char *argv[]) */
1060 #endif
1061 /*
1062 * vim:shiftwidth=4:tabstop=8:softtabstop=4
1063 */