2 * collectd - src/utils_dns.c
3 * Modifications Copyright (C) 2006 Florian octo Forster
4 * Copyright (C) 2002 The Measurement Factory, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
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.
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.
32 * The Measurement Factory, Inc. <http://www.measurement-factory.com/>
33 * Florian octo Forster <octo at verplant.org>
36 #include <sys/types.h>
40 #include <netinet/in.h>
52 #include <arpa/inet.h>
53 #include <arpa/nameser.h>
55 #include <arpa/nameser_compat.h>
58 #include <sys/socket.h>
59 #include <net/if_arp.h>
61 #include <netinet/if_ether.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/ip6.h>
66 #include <netinet/udp.h>
70 #define PCAP_SNAPLEN 1460
72 #define ETHER_ADDR_LEN 6
73 #define ETHER_TYPE_LEN 2
74 #define ETHER_HDR_LEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
76 #ifndef ETHERTYPE_8021Q
77 # define ETHERTYPE_8021Q 0x8100
79 #ifndef ETHERTYPE_IPV6
80 # define ETHERTYPE_IPV6 0x86DD
83 #include <net/if_ppp.h>
84 #ifndef PPP_ADDRESS_VAL
85 # define PPP_ADDRESS_VAL 0xff /* The address byte value */
87 #ifndef PPP_CONTROL_VAL
88 # define PPP_CONTROL_VAL 0x03 /* The control byte value */
92 #define uh_sport source
96 #include "utils_dns.h"
103 struct in6_addr addr;
105 struct ip_list_s *next;
107 typedef struct ip_list_s ip_list_t;
109 typedef int (printer)(const char *, ...);
112 * flags/features for non-interactive mode
125 int qtype_counts[T_MAX];
126 int opcode_counts[OP_MAX];
127 int qclass_counts[C_MAX];
129 static pcap_t *pcap_obj = NULL;
131 static ip_list_t *IgnoreList = NULL;
133 static void (*Callback) (const rfc1035_header_t *) = NULL;
135 static int query_count_intvl = 0;
136 static int query_count_total = 0;
138 static struct bpf_timeval last_ts;
140 static struct timeval last_ts;
143 static int cmp_in6_addr (const struct in6_addr *a,
144 const struct in6_addr *b)
148 for (i = 0; i < 4; i++)
149 if (a->s6_addr32[i] != b->s6_addr32[i])
155 return (a->s6_addr32[i] > b->s6_addr32[i] ? 1 : -1);
156 } /* int cmp_addrinfo */
158 static inline int ignore_list_match (const struct in6_addr *addr)
162 for (ptr = IgnoreList; ptr != NULL; ptr = ptr->next)
163 if (cmp_in6_addr (addr, &ptr->addr) == 0)
166 } /* int ignore_list_match */
168 static void ignore_list_add (const struct in6_addr *addr)
172 if (ignore_list_match (addr) != 0)
175 new = malloc (sizeof (ip_list_t));
182 memcpy (&new->addr, addr, sizeof (struct in6_addr));
183 new->next = IgnoreList;
186 } /* void ignore_list_add */
188 void ignore_list_add_name (const char *name)
190 struct addrinfo *ai_list;
191 struct addrinfo *ai_ptr;
192 struct in6_addr addr;
195 status = getaddrinfo (name, NULL, NULL, &ai_list);
199 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
201 if (ai_ptr->ai_family == AF_INET)
203 addr.s6_addr32[0] = 0;
204 addr.s6_addr32[1] = 0;
205 addr.s6_addr32[2] = htonl (0x0000FFFF);
206 addr.s6_addr32[3] = ((struct sockaddr_in *) ai_ptr->ai_addr)->sin_addr.s_addr;
208 ignore_list_add (&addr);
212 ignore_list_add (&((struct sockaddr_in6 *) ai_ptr->ai_addr)->sin6_addr);
216 freeaddrinfo (ai_list);
219 static void in6_addr_from_buffer (struct in6_addr *ia,
220 const void *buf, size_t buf_len,
223 memset (ia, 0, sizeof (struct in6_addr));
224 if ((AF_INET == family) && (sizeof (uint32_t) == buf_len))
226 ia->s6_addr32[2] = htonl (0x0000FFFF);
227 ia->s6_addr32[3] = *((uint32_t *) buf);
229 else if ((AF_INET6 == family) && (sizeof (struct in6_addr) == buf_len))
231 memcpy (ia, buf, buf_len);
233 } /* void in6_addr_from_buffer */
235 void dnstop_set_pcap_obj (pcap_t *po)
240 void dnstop_set_callback (void (*cb) (const rfc1035_header_t *))
245 #define RFC1035_MAXLABELSZ 63
247 rfc1035NameUnpack(const char *buf, size_t sz, off_t * off, char *name, size_t ns
259 /* blasted compression */
262 memcpy(&s, buf + (*off), sizeof(s));
269 /* Make sure the pointer is inside this message */
272 return rfc1035NameUnpack(buf, sz, &ptr, name + no, ns - no);
273 } else if (c > RFC1035_MAXLABELSZ) {
275 * "(The 10 and 01 combinations are reserved for future use.)"
286 if ((*off) + len > sz) /* message is too short */
288 memcpy(name + no, buf + (*off), len);
291 *(name + (no++)) = '.';
294 *(name + no - 1) = '\0';
295 /* make sure we didn't allow someone to overflow the name buffer */
301 handle_dns(const char *buf, int len,
302 const struct in6_addr *s_addr,
303 const struct in6_addr *d_addr)
311 /* The DNS header is 12 bytes long */
315 memcpy(&us, buf + 0, 2);
318 memcpy(&us, buf + 2, 2);
320 fprintf (stderr, "Bytes 0, 1: 0x%04hx\n", us);
321 qh.qr = (us >> 15) & 0x01;
322 qh.opcode = (us >> 11) & 0x0F;
323 qh.aa = (us >> 10) & 0x01;
324 qh.tc = (us >> 9) & 0x01;
325 qh.rd = (us >> 8) & 0x01;
326 qh.ra = (us >> 7) & 0x01;
327 qh.z = (us >> 6) & 0x01;
328 qh.ad = (us >> 5) & 0x01;
329 qh.cd = (us >> 4) & 0x01;
330 qh.rcode = us & 0x0F;
332 memcpy(&us, buf + 4, 2);
333 qh.qdcount = ntohs(us);
335 memcpy(&us, buf + 6, 2);
336 qh.ancount = ntohs(us);
338 memcpy(&us, buf + 8, 2);
339 qh.nscount = ntohs(us);
341 memcpy(&us, buf + 10, 2);
342 qh.arcount = ntohs(us);
345 memset(qh.qname, '\0', MAX_QNAME_SZ);
346 x = rfc1035NameUnpack(buf, len, &offset, qh.qname, MAX_QNAME_SZ);
349 if ('\0' == qh.qname[0])
350 strcpy(qh.qname, ".");
351 while ((t = strchr(qh.qname, '\n')))
353 while ((t = strchr(qh.qname, '\r')))
355 for (t = qh.qname; *t; t++)
358 memcpy(&us, buf + offset, 2);
359 qh.qtype = ntohs(us);
360 memcpy(&us, buf + offset + 2, 2);
361 qh.qclass = ntohs(us);
363 qh.length = (uint16_t) len;
366 qtype_counts[qh.qtype]++;
367 qclass_counts[qh.qclass]++;
368 opcode_counts[qh.opcode]++;
370 if (Callback != NULL)
377 handle_udp(const struct udphdr *udp, int len,
378 const struct in6_addr *s_addr,
379 const struct in6_addr *d_addr)
381 char buf[PCAP_SNAPLEN];
382 if ((ntohs (udp->uh_dport) != 53)
383 && (ntohs (udp->uh_sport) != 53))
385 memcpy(buf, udp + 1, len - sizeof(*udp));
386 if (0 == handle_dns(buf, len - sizeof(*udp), s_addr, d_addr))
392 handle_ipv6 (struct ip6_hdr *ipv6, int len)
394 char buf[PCAP_SNAPLEN];
398 struct in6_addr s_addr;
399 struct in6_addr d_addr;
400 uint16_t payload_len;
402 offset = sizeof (struct ip6_hdr);
403 nexthdr = ipv6->ip6_nxt;
404 s_addr = ipv6->ip6_src;
405 d_addr = ipv6->ip6_dst;
406 payload_len = ntohs (ipv6->ip6_plen);
408 if (ignore_list_match (&s_addr))
411 /* Parse extension headers. This only handles the standard headers, as
412 * defined in RFC 2460, correctly. Fragments are discarded. */
413 while ((IPPROTO_ROUTING == nexthdr) /* routing header */
414 || (IPPROTO_HOPOPTS == nexthdr) /* Hop-by-Hop options. */
415 || (IPPROTO_FRAGMENT == nexthdr) /* fragmentation header. */
416 || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
417 || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
418 || (IPPROTO_AH == nexthdr) /* destination options. */
419 || (IPPROTO_ESP == nexthdr)) /* encapsulating security payload. */
421 struct ip6_ext ext_hdr;
422 uint16_t ext_hdr_len;
424 /* Catch broken packets */
425 if ((offset + sizeof (struct ip6_ext)) > len)
428 /* Cannot handle fragments. */
429 if (IPPROTO_FRAGMENT == nexthdr)
432 memcpy (&ext_hdr, (char *) ipv6 + offset, sizeof (struct ip6_ext));
433 nexthdr = ext_hdr.ip6e_nxt;
434 ext_hdr_len = (8 * (ntohs (ext_hdr.ip6e_len) + 1));
436 /* This header is longer than the packets payload.. WTF? */
437 if (ext_hdr_len > payload_len)
440 offset += ext_hdr_len;
441 payload_len -= ext_hdr_len;
444 /* Catch broken and empty packets */
445 if (((offset + payload_len) > len)
446 || (payload_len == 0)
447 || (payload_len > PCAP_SNAPLEN))
450 if (IPPROTO_UDP != nexthdr)
453 memcpy (buf, (char *) ipv6 + offset, payload_len);
454 if (handle_udp ((struct udphdr *) buf, payload_len, &s_addr, &d_addr) == 0)
457 return (1); /* Success */
458 } /* int handle_ipv6 */
461 handle_ip(const struct ip *ip, int len)
463 char buf[PCAP_SNAPLEN];
464 int offset = ip->ip_hl << 2;
465 struct in6_addr s_addr;
466 struct in6_addr d_addr;
469 return (handle_ipv6 ((struct ip6_hdr *) ip, len));
471 in6_addr_from_buffer (&s_addr, &ip->ip_src.s_addr, sizeof (ip->ip_src.s_addr), AF_INET);
472 in6_addr_from_buffer (&d_addr, &ip->ip_dst.s_addr, sizeof (ip->ip_dst.s_addr), AF_INET);
473 if (ignore_list_match (&s_addr))
475 if (IPPROTO_UDP != ip->ip_p)
477 memcpy(buf, (void *) ip + offset, len - offset);
478 if (0 == handle_udp((struct udphdr *) buf, len - offset, &s_addr, &d_addr))
484 handle_ppp(const u_char * pkt, int len)
486 char buf[PCAP_SNAPLEN];
488 unsigned short proto;
491 if (*pkt == PPP_ADDRESS_VAL && *(pkt + 1) == PPP_CONTROL_VAL) {
492 pkt += 2; /* ACFC not used */
498 proto = *pkt; /* PFC is used */
502 memcpy(&us, pkt, sizeof(us));
507 if (ETHERTYPE_IP != proto && PPP_IP != proto)
509 memcpy(buf, pkt, len);
510 return handle_ip((struct ip *) buf, len);
514 handle_null(const u_char * pkt, int len)
517 memcpy(&family, pkt, sizeof(family));
518 if (AF_INET != family)
520 return handle_ip((struct ip *) (pkt + 4), len - 4);
525 handle_loop(const u_char * pkt, int len)
528 memcpy(&family, pkt, sizeof(family));
529 if (AF_INET != ntohl(family))
531 return handle_ip((struct ip *) (pkt + 4), len - 4);
538 handle_raw(const u_char * pkt, int len)
540 return handle_ip((struct ip *) pkt, len);
546 handle_ether(const u_char * pkt, int len)
548 char buf[PCAP_SNAPLEN];
549 struct ether_header *e = (void *) pkt;
550 unsigned short etype = ntohs(e->ether_type);
551 if (len < ETHER_HDR_LEN)
553 pkt += ETHER_HDR_LEN;
554 len -= ETHER_HDR_LEN;
555 if (ETHERTYPE_8021Q == etype) {
556 etype = ntohs(*(unsigned short *) (pkt + 2));
560 if ((ETHERTYPE_IP != etype)
561 && (ETHERTYPE_IPV6 != etype))
563 memcpy(buf, pkt, len);
564 if (ETHERTYPE_IPV6 == etype)
565 return (handle_ipv6 ((struct ip6_hdr *) buf, len));
567 return handle_ip((struct ip *) buf, len);
570 /* public function */
571 void handle_pcap(u_char *udata, const struct pcap_pkthdr *hdr, const u_char *pkt)
575 fprintf (stderr, "handle_pcap (udata = %p, hdr = %p, pkt = %p): hdr->caplen = %i\n",
576 (void *) udata, (void *) hdr, (void *) pkt,
579 if (hdr->caplen < ETHER_HDR_LEN)
582 switch (pcap_datalink (pcap_obj))
585 status = handle_ether (pkt, hdr->caplen);
588 status = handle_ppp (pkt, hdr->caplen);
592 status = handle_loop (pkt, hdr->caplen);
597 status = handle_raw (pkt, hdr->caplen);
601 status = handle_null (pkt, hdr->caplen);
605 fprintf (stderr, "unsupported data link type %d\n",
606 pcap_datalink(pcap_obj));
609 } /* switch (pcap_datalink(pcap_obj)) */
619 const char *qtype_str(int t)
623 case ns_t_a: return ("A");
624 case ns_t_ns: return ("NS");
625 case ns_t_md: return ("MD");
626 case ns_t_mf: return ("MF");
627 case ns_t_cname: return ("CNAME");
628 case ns_t_soa: return ("SOA");
629 case ns_t_mb: return ("MB");
630 case ns_t_mg: return ("MG");
631 case ns_t_mr: return ("MR");
632 case ns_t_null: return ("NULL");
633 case ns_t_wks: return ("WKS");
634 case ns_t_ptr: return ("PTR");
635 case ns_t_hinfo: return ("HINFO");
636 case ns_t_minfo: return ("MINFO");
637 case ns_t_mx: return ("MX");
638 case ns_t_txt: return ("TXT");
639 case ns_t_rp: return ("RP");
640 case ns_t_afsdb: return ("AFSDB");
641 case ns_t_x25: return ("X25");
642 case ns_t_isdn: return ("ISDN");
643 case ns_t_rt: return ("RT");
644 case ns_t_nsap: return ("NSAP");
645 case ns_t_nsap_ptr: return ("NSAP-PTR");
646 case ns_t_sig: return ("SIG");
647 case ns_t_key: return ("KEY");
648 case ns_t_px: return ("PX");
649 case ns_t_gpos: return ("GPOS");
650 case ns_t_aaaa: return ("AAAA");
651 case ns_t_loc: return ("LOC");
652 case ns_t_nxt: return ("NXT");
653 case ns_t_eid: return ("EID");
654 case ns_t_nimloc: return ("NIMLOC");
655 case ns_t_srv: return ("SRV");
656 case ns_t_atma: return ("ATMA");
657 case ns_t_naptr: return ("NAPTR");
658 case ns_t_kx: return ("KX");
659 case ns_t_cert: return ("CERT");
660 case ns_t_a6: return ("A6");
661 case ns_t_dname: return ("DNAME");
662 case ns_t_sink: return ("SINK");
663 case ns_t_opt: return ("OPT");
664 case ns_t_tsig: return ("TSIG");
665 case ns_t_ixfr: return ("IXFR");
666 case ns_t_axfr: return ("AXFR");
667 case ns_t_mailb: return ("MAILB");
668 case ns_t_maila: return ("MAILA");
669 case ns_t_any: return ("ANY");
670 case ns_t_zxfr: return ("ZXFR");
672 snprintf (buf, 32, "#%i", t);
680 const char *opcode_str (int o)
700 snprintf(buf, 30, "Opcode%d", o);
706 const char *rcode_str (int rcode)
711 case ns_r_noerror: return ("NOERROR");
712 case ns_r_formerr: return ("FORMERR");
713 case ns_r_servfail: return ("SERVFAIL");
714 case ns_r_nxdomain: return ("NXDOMAIN");
715 case ns_r_notimpl: return ("NOTIMPL");
716 case ns_r_refused: return ("REFUSED");
717 case ns_r_yxdomain: return ("YXDOMAIN");
718 case ns_r_yxrrset: return ("YXRRSET");
719 case ns_r_nxrrset: return ("NXRRSET");
720 case ns_r_notauth: return ("NOTAUTH");
721 case ns_r_notzone: return ("NOTZONE");
722 case ns_r_max: return ("MAX");
723 case ns_r_badsig: return ("BADSIG");
724 case ns_r_badkey: return ("BADKEY");
725 case ns_r_badtime: return ("BADTIME");
727 snprintf (buf, 32, "RCode%i", rcode);
733 } /* const char *rcode_str (int rcode) */
737 main(int argc, char *argv[])
739 char errbuf[PCAP_ERRBUF_SIZE];
742 int readfile_state = 0;
743 struct bpf_program fp;
746 SubReport = Sources_report;
747 ignore_addr.s_addr = 0;
748 progname = strdup(strrchr(argv[0], '/') ? strchr(argv[0], '/') + 1 : argv[0]);
752 while ((x = getopt(argc, argv, "ab:f:i:pst")) != -1) {
767 bpf_program_str = strdup(optarg);
770 ignore_addr.s_addr = inet_addr(optarg);
785 device = strdup(argv[0]);
787 if (0 == stat(device, &sb))
789 if (readfile_state) {
790 pcap_obj = pcap_open_offline(device, errbuf);
792 pcap_obj = pcap_open_live(device, PCAP_SNAPLEN, promisc_flag, 1000, errbuf);
794 if (NULL == pcap_obj) {
795 fprintf(stderr, "pcap_open_*: %s\n", errbuf);
799 if (0 == isatty(1)) {
800 if (0 == readfile_state) {
801 fprintf(stderr, "Non-interactive mode requires savefile argument\n");
808 memset(&fp, '\0', sizeof(fp));
809 x = pcap_compile(pcap_obj, &fp, bpf_program_str, 1, 0);
811 fprintf(stderr, "pcap_compile failed\n");
814 x = pcap_setfilter(pcap_obj, &fp);
816 fprintf(stderr, "pcap_setfilter failed\n");
821 * non-blocking call added for Mac OS X bugfix. Sent by Max Horn.
822 * ref http://www.tcpdump.org/lists/workers/2002/09/msg00033.html
824 x = pcap_setnonblock(pcap_obj, 1, errbuf);
826 fprintf(stderr, "pcap_setnonblock failed: %s\n", errbuf);
830 switch (pcap_datalink(pcap_obj)) {
832 handle_datalink = handle_ether;
836 handle_datalink = handle_ppp;
841 handle_datalink = handle_loop;
846 handle_datalink = handle_raw;
850 handle_datalink = handle_null;
853 fprintf(stderr, "unsupported data link type %d\n",
854 pcap_datalink(pcap_obj));
861 if (readfile_state < 2) {
863 * On some OSes select() might return 0 even when
864 * there are packets to process. Thus, we always
865 * ignore its return value and just call pcap_dispatch()
868 if (0 == readfile_state) /* interactive */
869 pcap_select(pcap_obj, 1, 0);
870 x = pcap_dispatch(pcap_obj, 50, handle_pcap, NULL);
872 if (0 == x && 1 == readfile_state) {
873 /* block on keyboard until user quits */
882 endwin(); /* klin, Thu Nov 28 08:56:51 2002 */
884 while (pcap_dispatch(pcap_obj, 50, handle_pcap, NULL))
887 Sources_report(); print_func("\n");
888 Destinatioreport(); print_func("\n");
889 Qtypes_report(); print_func("\n");
890 Opcodes_report(); print_func("\n");
891 Tld_report(); print_func("\n");
892 Sld_report(); print_func("\n");
893 Nld_report(); print_func("\n");
894 SldBySource_report();
897 pcap_close(pcap_obj);
899 } /* static int main(int argc, char *argv[]) */