diff -rN -U20 dnstop-20121017.orig/dnstop.c dnstop-20121017/dnstop.c --- dnstop-20121017.orig/dnstop.c 2012-10-17 09:38:06.000000000 -0700 +++ dnstop-20121017/dnstop.c 2013-02-17 13:35:26.132786982 -0800 @@ -1,40 +1,42 @@ /* * $Id: dnstop.c,v 1.114 2012/10/17 16:37:23 wessels Exp $ * * http://dnstop.measurement-factory.com/ * * Copyright (c) 2002, The Measurement Factory, Inc. All rights reserved. See * the LICENSE file for details. */ static const char *Version = "20121017"; #include "config.h" #include <sys/types.h> #include <sys/time.h> #include <sys/stat.h> #include <fcntl.h> #include <netinet/in.h> +#include <err.h> +#include <errno.h> #include <pcap.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <time.h> #include <ctype.h> #include <curses.h> #include <assert.h> #include <arpa/inet.h> #include <arpa/nameser.h> #ifdef HAVE_ARPA_NAMESER_COMPAT_H #include <arpa/nameser_compat.h> #endif #include <sys/socket.h> #include <net/if_arp.h> #include <net/if.h> #include <netinet/if_ether.h> @@ -330,46 +332,50 @@ } if (-1 == entropy_fd) { entropy_fd = open("/dev/urandom", 0); if (-1 == entropy_fd) { entropy_fd = open("/dev/random", 0); if (-1 == entropy_fd) { fprintf(stderr, "failed to open /dev/urandom or /dev/random"); exit(1); } } } if (ptr == NULL) { char buf[16]; ptr = (ip_list_t *) malloc(sizeof(ip_list_t) + sizeof(inX_addr)); if (ptr == NULL) return; ptr->addr = *orig_addr; ptr->data = (void *)(ptr + 1); if (4 == inXaddr_version(orig_addr)) { - read(entropy_fd, buf, 4); + ssize_t rd = read(entropy_fd, buf, 4); + if (rd < 4) + err(errno, "read entropy"); inXaddr_assign_v4(ptr->data, (struct in_addr *)buf); } #if USE_IPV6 else { - read(entropy_fd, buf, 16); + ssize_t rd = read(entropy_fd, buf, 16); + if (rd < 16) + err(errno, "read entropy"); inXaddr_assign_v6(ptr->data, (struct in6_addr *)buf); } #endif ptr->next = list; list = ptr; } *anon_addr = *(inX_addr *) ptr->data; } const char * anon_inet_ntoa(const inX_addr * addr) { static char buffer[INET6_ADDRSTRLEN]; inX_addr anon_addr; if (anon_flag) { allocate_anonymous_address(&anon_addr, addr); addr = &anon_addr; } return inXaddr_ntop(addr, buffer, sizeof(buffer)); } @@ -1352,48 +1358,48 @@ } void Domain_report(void) { if (cur_level > max_level) { print_func("\tYou must start %s with -l %d\n", progname, cur_level); print_func("\tto collect this level of domain stats.\n", progname); return; } StringCounter_report(Domains[cur_level], "Query Name"); } const char * Qtype_col_fmt(const SortItem * si) { return si->ptr; } void -Simple_report(unsigned int a[], unsigned int max, const char *name, strify * to_str) +Simple_report(int a[], unsigned int max, const char *name, strify * to_str) { unsigned int i; unsigned int sum = 0; unsigned int sortsize = 0; SortItem *sortme = calloc(max, sizeof(SortItem)); for (i = 0; i < max; i++) { - if (0 == a[i]) + if (0 >= a[i]) continue; sum += a[i]; sortme[sortsize].cnt = a[i]; sortme[sortsize].ptr = to_str(i); sortsize++; } qsort(sortme, sortsize, sizeof(SortItem), SortItem_cmp); Table_report(sortme, sortsize, name, NULL, Qtype_col_fmt, NULL, sum); free(sortme); } void Qtypes_report(void) { Simple_report(qtype_counts, T_MAX, "Query Type", qtype_str); }