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>
39 # include <netinet/in.h>
45 # include <arpa/inet.h>
48 #if HAVE_ARPA_NAMESER_H
49 # include <arpa/nameser.h>
50 #elif HAVE_ARPA_NAMESER_COMPAT_H
51 # include <arpa/nameser_compat.h>
55 # include <net/if_arp.h>
60 #if HAVE_NETINET_IF_ETHER_H
61 # include <netinet/if_ether.h>
64 # include <net/if_ppp.h>
68 # include <sys/socket.h>
74 #if HAVE_NETINET_IN_SYSTM_H
75 # include <netinet/in_systm.h>
78 # include <netinet/in.h>
81 # include <netinet/ip.h>
83 #ifdef HAVE_NETINET_IP_VAR_H
84 # include <netinet/ip_var.h>
86 #if HAVE_NETINET_IP6_H
87 # include <netinet/ip6.h>
89 #if HAVE_NETINET_UDP_H
90 # include <netinet/udp.h>
93 #define PCAP_SNAPLEN 1460
95 #define ETHER_ADDR_LEN 6
96 #define ETHER_TYPE_LEN 2
97 #define ETHER_HDR_LEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
99 #ifndef ETHERTYPE_8021Q
100 # define ETHERTYPE_8021Q 0x8100
102 #ifndef ETHERTYPE_IPV6
103 # define ETHERTYPE_IPV6 0x86DD
106 #ifndef PPP_ADDRESS_VAL
107 # define PPP_ADDRESS_VAL 0xff /* The address byte value */
109 #ifndef PPP_CONTROL_VAL
110 # define PPP_CONTROL_VAL 0x03 /* The control byte value */
114 #define uh_sport source
115 #define uh_dport dest
118 #include "utils_dns.h"
125 struct in6_addr addr;
127 struct ip_list_s *next;
129 typedef struct ip_list_s ip_list_t;
131 typedef int (printer)(const char *, ...);
134 * flags/features for non-interactive mode
147 int qtype_counts[T_MAX];
148 int opcode_counts[OP_MAX];
149 int qclass_counts[C_MAX];
152 static pcap_t *pcap_obj = NULL;
155 static ip_list_t *IgnoreList = NULL;
158 static void (*Callback) (const rfc1035_header_t *) = NULL;
160 static int query_count_intvl = 0;
161 static int query_count_total = 0;
163 static struct bpf_timeval last_ts;
165 static struct timeval last_ts;
166 # endif /* __OpenBSD__ */
167 #endif /* HAVE_PCAP_H */
169 static int cmp_in6_addr (const struct in6_addr *a,
170 const struct in6_addr *b)
174 assert (sizeof (struct in6_addr) == 16);
176 for (i = 0; i < 16; i++)
177 if (a->s6_addr[i] != b->s6_addr[i])
183 return (a->s6_addr[i] > b->s6_addr[i] ? 1 : -1);
184 } /* int cmp_addrinfo */
186 static inline int ignore_list_match (const struct in6_addr *addr)
190 for (ptr = IgnoreList; ptr != NULL; ptr = ptr->next)
191 if (cmp_in6_addr (addr, &ptr->addr) == 0)
194 } /* int ignore_list_match */
196 static void ignore_list_add (const struct in6_addr *addr)
200 if (ignore_list_match (addr) != 0)
203 new = malloc (sizeof (ip_list_t));
210 memcpy (&new->addr, addr, sizeof (struct in6_addr));
211 new->next = IgnoreList;
214 } /* void ignore_list_add */
216 void ignore_list_add_name (const char *name)
218 struct addrinfo *ai_list;
219 struct addrinfo *ai_ptr;
220 struct in6_addr addr;
223 status = getaddrinfo (name, NULL, NULL, &ai_list);
227 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
229 if (ai_ptr->ai_family == AF_INET)
231 memset (&addr, '\0', sizeof (addr));
232 addr.s6_addr[10] = 0xFF;
233 addr.s6_addr[11] = 0xFF;
234 memcpy (addr.s6_addr + 12, &((struct sockaddr_in *) ai_ptr->ai_addr)->sin_addr, 4);
236 ignore_list_add (&addr);
240 ignore_list_add (&((struct sockaddr_in6 *) ai_ptr->ai_addr)->sin6_addr);
244 freeaddrinfo (ai_list);
248 static void in6_addr_from_buffer (struct in6_addr *ia,
249 const void *buf, size_t buf_len,
252 memset (ia, 0, sizeof (struct in6_addr));
253 if ((AF_INET == family) && (sizeof (uint32_t) == buf_len))
255 ia->s6_addr[10] = 0xFF;
256 ia->s6_addr[11] = 0xFF;
257 memcpy (ia->s6_addr + 12, buf, buf_len);
259 else if ((AF_INET6 == family) && (sizeof (struct in6_addr) == buf_len))
261 memcpy (ia, buf, buf_len);
263 } /* void in6_addr_from_buffer */
265 void dnstop_set_pcap_obj (pcap_t *po)
270 void dnstop_set_callback (void (*cb) (const rfc1035_header_t *))
275 #define RFC1035_MAXLABELSZ 63
277 rfc1035NameUnpack(const char *buf, size_t sz, off_t * off, char *name, size_t ns
289 /* blasted compression */
292 memcpy(&s, buf + (*off), sizeof(s));
299 /* Make sure the pointer is inside this message */
302 return rfc1035NameUnpack(buf, sz, &ptr, name + no, ns - no);
303 } else if (c > RFC1035_MAXLABELSZ) {
305 * "(The 10 and 01 combinations are reserved for future use.)"
316 if ((*off) + len > sz) /* message is too short */
318 memcpy(name + no, buf + (*off), len);
321 *(name + (no++)) = '.';
324 *(name + no - 1) = '\0';
325 /* make sure we didn't allow someone to overflow the name buffer */
331 handle_dns(const char *buf, int len,
332 const struct in6_addr *s_addr,
333 const struct in6_addr *d_addr)
341 /* The DNS header is 12 bytes long */
345 memcpy(&us, buf + 0, 2);
348 memcpy(&us, buf + 2, 2);
350 fprintf (stderr, "Bytes 0, 1: 0x%04hx\n", us);
351 qh.qr = (us >> 15) & 0x01;
352 qh.opcode = (us >> 11) & 0x0F;
353 qh.aa = (us >> 10) & 0x01;
354 qh.tc = (us >> 9) & 0x01;
355 qh.rd = (us >> 8) & 0x01;
356 qh.ra = (us >> 7) & 0x01;
357 qh.z = (us >> 6) & 0x01;
358 qh.ad = (us >> 5) & 0x01;
359 qh.cd = (us >> 4) & 0x01;
360 qh.rcode = us & 0x0F;
362 memcpy(&us, buf + 4, 2);
363 qh.qdcount = ntohs(us);
365 memcpy(&us, buf + 6, 2);
366 qh.ancount = ntohs(us);
368 memcpy(&us, buf + 8, 2);
369 qh.nscount = ntohs(us);
371 memcpy(&us, buf + 10, 2);
372 qh.arcount = ntohs(us);
375 memset(qh.qname, '\0', MAX_QNAME_SZ);
376 x = rfc1035NameUnpack(buf, len, &offset, qh.qname, MAX_QNAME_SZ);
379 if ('\0' == qh.qname[0])
380 strcpy(qh.qname, ".");
381 while ((t = strchr(qh.qname, '\n')))
383 while ((t = strchr(qh.qname, '\r')))
385 for (t = qh.qname; *t; t++)
388 memcpy(&us, buf + offset, 2);
389 qh.qtype = ntohs(us);
390 memcpy(&us, buf + offset + 2, 2);
391 qh.qclass = ntohs(us);
393 qh.length = (uint16_t) len;
396 qtype_counts[qh.qtype]++;
397 qclass_counts[qh.qclass]++;
398 opcode_counts[qh.opcode]++;
400 if (Callback != NULL)
407 handle_udp(const struct udphdr *udp, int len,
408 const struct in6_addr *s_addr,
409 const struct in6_addr *d_addr)
411 char buf[PCAP_SNAPLEN];
412 if ((ntohs (udp->uh_dport) != 53)
413 && (ntohs (udp->uh_sport) != 53))
415 memcpy(buf, udp + 1, len - sizeof(*udp));
416 if (0 == handle_dns(buf, len - sizeof(*udp), s_addr, d_addr))
422 handle_ipv6 (struct ip6_hdr *ipv6, int len)
424 char buf[PCAP_SNAPLEN];
428 struct in6_addr s_addr;
429 struct in6_addr d_addr;
430 uint16_t payload_len;
432 offset = sizeof (struct ip6_hdr);
433 nexthdr = ipv6->ip6_nxt;
434 s_addr = ipv6->ip6_src;
435 d_addr = ipv6->ip6_dst;
436 payload_len = ntohs (ipv6->ip6_plen);
438 if (ignore_list_match (&s_addr))
441 /* Parse extension headers. This only handles the standard headers, as
442 * defined in RFC 2460, correctly. Fragments are discarded. */
443 while ((IPPROTO_ROUTING == nexthdr) /* routing header */
444 || (IPPROTO_HOPOPTS == nexthdr) /* Hop-by-Hop options. */
445 || (IPPROTO_FRAGMENT == nexthdr) /* fragmentation header. */
446 || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
447 || (IPPROTO_DSTOPTS == nexthdr) /* destination options. */
448 || (IPPROTO_AH == nexthdr) /* destination options. */
449 || (IPPROTO_ESP == nexthdr)) /* encapsulating security payload. */
451 struct ip6_ext ext_hdr;
452 uint16_t ext_hdr_len;
454 /* Catch broken packets */
455 if ((offset + sizeof (struct ip6_ext)) > len)
458 /* Cannot handle fragments. */
459 if (IPPROTO_FRAGMENT == nexthdr)
462 memcpy (&ext_hdr, (char *) ipv6 + offset, sizeof (struct ip6_ext));
463 nexthdr = ext_hdr.ip6e_nxt;
464 ext_hdr_len = (8 * (ntohs (ext_hdr.ip6e_len) + 1));
466 /* This header is longer than the packets payload.. WTF? */
467 if (ext_hdr_len > payload_len)
470 offset += ext_hdr_len;
471 payload_len -= ext_hdr_len;
474 /* Catch broken and empty packets */
475 if (((offset + payload_len) > len)
476 || (payload_len == 0)
477 || (payload_len > PCAP_SNAPLEN))
480 if (IPPROTO_UDP != nexthdr)
483 memcpy (buf, (char *) ipv6 + offset, payload_len);
484 if (handle_udp ((struct udphdr *) buf, payload_len, &s_addr, &d_addr) == 0)
487 return (1); /* Success */
488 } /* int handle_ipv6 */
491 handle_ip(const struct ip *ip, int len)
493 char buf[PCAP_SNAPLEN];
494 int offset = ip->ip_hl << 2;
495 struct in6_addr s_addr;
496 struct in6_addr d_addr;
499 return (handle_ipv6 ((struct ip6_hdr *) ip, len));
501 in6_addr_from_buffer (&s_addr, &ip->ip_src.s_addr, sizeof (ip->ip_src.s_addr), AF_INET);
502 in6_addr_from_buffer (&d_addr, &ip->ip_dst.s_addr, sizeof (ip->ip_dst.s_addr), AF_INET);
503 if (ignore_list_match (&s_addr))
505 if (IPPROTO_UDP != ip->ip_p)
507 memcpy(buf, (void *) ip + offset, len - offset);
508 if (0 == handle_udp((struct udphdr *) buf, len - offset, &s_addr, &d_addr))
513 #if HAVE_NET_IF_PPP_H
515 handle_ppp(const u_char * pkt, int len)
517 char buf[PCAP_SNAPLEN];
519 unsigned short proto;
522 if (*pkt == PPP_ADDRESS_VAL && *(pkt + 1) == PPP_CONTROL_VAL) {
523 pkt += 2; /* ACFC not used */
529 proto = *pkt; /* PFC is used */
533 memcpy(&us, pkt, sizeof(us));
538 if (ETHERTYPE_IP != proto && PPP_IP != proto)
540 memcpy(buf, pkt, len);
541 return handle_ip((struct ip *) buf, len);
543 #endif /* HAVE_NET_IF_PPP_H */
546 handle_null(const u_char * pkt, int len)
549 memcpy(&family, pkt, sizeof(family));
550 if (AF_INET != family)
552 return handle_ip((struct ip *) (pkt + 4), len - 4);
557 handle_loop(const u_char * pkt, int len)
560 memcpy(&family, pkt, sizeof(family));
561 if (AF_INET != ntohl(family))
563 return handle_ip((struct ip *) (pkt + 4), len - 4);
570 handle_raw(const u_char * pkt, int len)
572 return handle_ip((struct ip *) pkt, len);
578 handle_ether(const u_char * pkt, int len)
580 char buf[PCAP_SNAPLEN];
581 struct ether_header *e = (void *) pkt;
582 unsigned short etype = ntohs(e->ether_type);
583 if (len < ETHER_HDR_LEN)
585 pkt += ETHER_HDR_LEN;
586 len -= ETHER_HDR_LEN;
587 if (ETHERTYPE_8021Q == etype) {
588 etype = ntohs(*(unsigned short *) (pkt + 2));
592 if ((ETHERTYPE_IP != etype)
593 && (ETHERTYPE_IPV6 != etype))
595 memcpy(buf, pkt, len);
596 if (ETHERTYPE_IPV6 == etype)
597 return (handle_ipv6 ((struct ip6_hdr *) buf, len));
599 return handle_ip((struct ip *) buf, len);
602 /* public function */
603 void handle_pcap(u_char *udata, const struct pcap_pkthdr *hdr, const u_char *pkt)
607 fprintf (stderr, "handle_pcap (udata = %p, hdr = %p, pkt = %p): hdr->caplen = %i\n",
608 (void *) udata, (void *) hdr, (void *) pkt,
611 if (hdr->caplen < ETHER_HDR_LEN)
614 switch (pcap_datalink (pcap_obj))
617 status = handle_ether (pkt, hdr->caplen);
619 #if HAVE_NET_IF_PPP_H
621 status = handle_ppp (pkt, hdr->caplen);
626 status = handle_loop (pkt, hdr->caplen);
631 status = handle_raw (pkt, hdr->caplen);
635 status = handle_null (pkt, hdr->caplen);
639 fprintf (stderr, "unsupported data link type %d\n",
640 pcap_datalink(pcap_obj));
643 } /* switch (pcap_datalink(pcap_obj)) */
652 #endif /* HAVE_PCAP_H */
654 const char *qtype_str(int t)
658 case ns_t_a: return ("A");
659 case ns_t_ns: return ("NS");
660 case ns_t_md: return ("MD");
661 case ns_t_mf: return ("MF");
662 case ns_t_cname: return ("CNAME");
663 case ns_t_soa: return ("SOA");
664 case ns_t_mb: return ("MB");
665 case ns_t_mg: return ("MG");
666 case ns_t_mr: return ("MR");
667 case ns_t_null: return ("NULL");
668 case ns_t_wks: return ("WKS");
669 case ns_t_ptr: return ("PTR");
670 case ns_t_hinfo: return ("HINFO");
671 case ns_t_minfo: return ("MINFO");
672 case ns_t_mx: return ("MX");
673 case ns_t_txt: return ("TXT");
674 case ns_t_rp: return ("RP");
675 case ns_t_afsdb: return ("AFSDB");
676 case ns_t_x25: return ("X25");
677 case ns_t_isdn: return ("ISDN");
678 case ns_t_rt: return ("RT");
679 case ns_t_nsap: return ("NSAP");
680 case ns_t_nsap_ptr: return ("NSAP-PTR");
681 case ns_t_sig: return ("SIG");
682 case ns_t_key: return ("KEY");
683 case ns_t_px: return ("PX");
684 case ns_t_gpos: return ("GPOS");
685 case ns_t_aaaa: return ("AAAA");
686 case ns_t_loc: return ("LOC");
687 case ns_t_nxt: return ("NXT");
688 case ns_t_eid: return ("EID");
689 case ns_t_nimloc: return ("NIMLOC");
690 case ns_t_srv: return ("SRV");
691 case ns_t_atma: return ("ATMA");
692 case ns_t_naptr: return ("NAPTR");
693 case ns_t_kx: return ("KX");
694 case ns_t_cert: return ("CERT");
695 case ns_t_a6: return ("A6");
696 case ns_t_dname: return ("DNAME");
697 case ns_t_sink: return ("SINK");
698 case ns_t_opt: return ("OPT");
699 case ns_t_tsig: return ("TSIG");
700 case ns_t_ixfr: return ("IXFR");
701 case ns_t_axfr: return ("AXFR");
702 case ns_t_mailb: return ("MAILB");
703 case ns_t_maila: return ("MAILA");
704 case ns_t_any: return ("ANY");
705 case ns_t_zxfr: return ("ZXFR");
707 snprintf (buf, 32, "#%i", t);
715 const char *opcode_str (int o)
735 snprintf(buf, 30, "Opcode%d", o);
741 const char *rcode_str (int rcode)
746 case ns_r_noerror: return ("NOERROR");
747 case ns_r_formerr: return ("FORMERR");
748 case ns_r_servfail: return ("SERVFAIL");
749 case ns_r_nxdomain: return ("NXDOMAIN");
750 case ns_r_notimpl: return ("NOTIMPL");
751 case ns_r_refused: return ("REFUSED");
752 case ns_r_yxdomain: return ("YXDOMAIN");
753 case ns_r_yxrrset: return ("YXRRSET");
754 case ns_r_nxrrset: return ("NXRRSET");
755 case ns_r_notauth: return ("NOTAUTH");
756 case ns_r_notzone: return ("NOTZONE");
757 case ns_r_max: return ("MAX");
758 case ns_r_badsig: return ("BADSIG");
759 case ns_r_badkey: return ("BADKEY");
760 case ns_r_badtime: return ("BADTIME");
762 snprintf (buf, 32, "RCode%i", rcode);
768 } /* const char *rcode_str (int rcode) */
772 main(int argc, char *argv[])
774 char errbuf[PCAP_ERRBUF_SIZE];
777 int readfile_state = 0;
778 struct bpf_program fp;
781 SubReport = Sources_report;
782 ignore_addr.s_addr = 0;
783 progname = strdup(strrchr(argv[0], '/') ? strchr(argv[0], '/') + 1 : argv[0]);
787 while ((x = getopt(argc, argv, "ab:f:i:pst")) != -1) {
802 bpf_program_str = strdup(optarg);
805 ignore_addr.s_addr = inet_addr(optarg);
820 device = strdup(argv[0]);
822 if (0 == stat(device, &sb))
824 if (readfile_state) {
825 pcap_obj = pcap_open_offline(device, errbuf);
827 pcap_obj = pcap_open_live(device, PCAP_SNAPLEN, promisc_flag, 1000, errbuf);
829 if (NULL == pcap_obj) {
830 fprintf(stderr, "pcap_open_*: %s\n", errbuf);
834 if (0 == isatty(1)) {
835 if (0 == readfile_state) {
836 fprintf(stderr, "Non-interactive mode requires savefile argument\n");
843 memset(&fp, '\0', sizeof(fp));
844 x = pcap_compile(pcap_obj, &fp, bpf_program_str, 1, 0);
846 fprintf(stderr, "pcap_compile failed\n");
849 x = pcap_setfilter(pcap_obj, &fp);
851 fprintf(stderr, "pcap_setfilter failed\n");
856 * non-blocking call added for Mac OS X bugfix. Sent by Max Horn.
857 * ref http://www.tcpdump.org/lists/workers/2002/09/msg00033.html
859 x = pcap_setnonblock(pcap_obj, 1, errbuf);
861 fprintf(stderr, "pcap_setnonblock failed: %s\n", errbuf);
865 switch (pcap_datalink(pcap_obj)) {
867 handle_datalink = handle_ether;
869 #if HAVE_NET_IF_PPP_H
871 handle_datalink = handle_ppp;
876 handle_datalink = handle_loop;
881 handle_datalink = handle_raw;
885 handle_datalink = handle_null;
888 fprintf(stderr, "unsupported data link type %d\n",
889 pcap_datalink(pcap_obj));
896 if (readfile_state < 2) {
898 * On some OSes select() might return 0 even when
899 * there are packets to process. Thus, we always
900 * ignore its return value and just call pcap_dispatch()
903 if (0 == readfile_state) /* interactive */
904 pcap_select(pcap_obj, 1, 0);
905 x = pcap_dispatch(pcap_obj, 50, handle_pcap, NULL);
907 if (0 == x && 1 == readfile_state) {
908 /* block on keyboard until user quits */
917 endwin(); /* klin, Thu Nov 28 08:56:51 2002 */
919 while (pcap_dispatch(pcap_obj, 50, handle_pcap, NULL))
922 Sources_report(); print_func("\n");
923 Destinatioreport(); print_func("\n");
924 Qtypes_report(); print_func("\n");
925 Opcodes_report(); print_func("\n");
926 Tld_report(); print_func("\n");
927 Sld_report(); print_func("\n");
928 Nld_report(); print_func("\n");
929 SldBySource_report();
932 pcap_close(pcap_obj);
934 } /* static int main(int argc, char *argv[]) */
937 * vim:shiftwidth=4:tabstop=8:softtabstop=4