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