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