--- webalizer.h.orig Wed Jun 30 19:03:36 1999 +++ webalizer.h Wed Jun 30 21:21:59 1999 @@ -250,6 +250,9 @@ void top_search_table(); /* top n search strs */ static char *save_opt(char *); /* save conf option */ u_long ctry_idx(char *); /* index domain name */ + +void lookup_host_name(char* host, int max_host_size); /* inverse lookup of IPs */ +void lookup_sigalrm(); /* need this to time out long DNS lookups */ u_long tot_visit(HNODEPTR *); /* calc total visits */ int ispage(char *); /* check for HTML type */ void update_entry(char *); /* update entry total */ --- webalizer.c.orig Wed Jun 30 20:58:39 1999 +++ webalizer.c Wed Jun 30 21:25:35 1999 @@ -39,6 +39,16 @@ #include #include +/* needed for inverse lookup */ + +#include +#include +#include +#include +#include +#include +#include + /* ensure getopt */ #ifdef HAVE_GETOPT_H #include @@ -209,6 +219,11 @@ NLISTPTR html_end = NULL; /* after everything else */ NLISTPTR page_type = NULL; /* page view types */ +int reverse_lookup = 0; /* enable reverse lookup of IP's */ +int dns_cache = 8192; /* size of the DNS hash table*/ +int dns_timeout = 2; /* DNS timout in seconds */ +jmp_buf jumper; /* need this to jump out the signal hanlder */ + /*********************************************/ /* MAIN - start here */ /*********************************************/ @@ -297,6 +312,11 @@ } } + if(reverse_lookup) + { + hcreate(dns_cache); + } + if (argc - optind != 0) log_fname = argv[optind]; /* setup our internal variables */ @@ -567,6 +587,9 @@ /* DO SOME PRE-PROCESS FORMATTING */ /*********************************************/ + if (reverse_lookup) + lookup_host_name(log_rec.hostname, sizeof(log_rec.hostname)); + /* lowercase hostname */ cp1 = log_rec.hostname; while (*cp1 != '\0') @@ -1168,6 +1191,92 @@ } } +void lookup_host_name(char* host, int max_host_size) +{ + struct in_addr in; + struct hostent *h; + static ENTRY e, *ep; + char* e_ip; + char* e_host = ""; + int ip_len; + int host_len; + + e.key = host; + ep = hsearch(e, FIND); + + if(debug_mode) + fprintf(stderr, "Looking up %s\n", e.key); + + if(ep) + { + if(debug_mode) + fprintf(stderr, "Found %s for %s in the DNS cache\n", ep->data, e.key); + + strncpy(host, ep->data, max_host_size); + return; + } + + inet_aton(host, &in); + + if(!setjmp(jumper)) + { + signal(SIGALRM, lookup_sigalrm); + alarm(dns_timeout); + h = gethostbyaddr((char*)&in, sizeof(struct in_addr), AF_INET); + alarm(0); + } + else + { + if(verbose) + fprintf(stderr, "DNS request timed out for %s\n", e.key); + h = NULL; + } + + e_ip = (char*)malloc(ip_len = strlen(host) + 1); + if(!e_ip) + { + if (verbose) + fprintf(stderr, "Warning: malloc() failed in lookup_host_name()\n"); + return; + } + + memcpy(e_ip, host, ip_len); + + if(h) + { + e_host = (char*)malloc(host_len = strlen(h->h_name) + 1); + if(!e_host) + { + if (verbose) + fprintf(stderr, "Warning: malloc() failed in lookup_host_name()\n"); + return; + } + + memcpy(e_host, h->h_name, host_len ); + memcpy(host, h->h_name, (max_host_size < host_len) ? + max_host_size : (host_len + 1)); + } + else + { + e_host = (char*)malloc(host_len = strlen(host) + 1); + if(!e_host) + { + if (verbose) + fprintf(stderr, "Warning: malloc() failed in lookup_host_name()\n"); + return; + } + memcpy(e_host, host, host_len); + } + + e.key = e_ip; + e.data = e_host; + if(!hsearch(e, ENTER)) + { + free(e_ip); + free(e_host); + } +} + /*********************************************/ /* PARSE_RECORD - uhhh, you know... */ /*********************************************/ @@ -1402,6 +1511,11 @@ return 1; /* maybe a valid record, return with TRUE */ } +void lookup_sigalrm() +{ + longjmp(jumper, 1); +} + /*********************************************/ /* CLEAR_MONTH - initalize monthly stuff */ /*********************************************/ @@ -1493,7 +1607,10 @@ "TopEntry", /* Top Entry Pages 57 */ "TopExit", /* Top Exit Pages 58 */ "TopSearch", /* Top Search Strings 59 */ - "LogType" /* Log Type (clf or ftp) 60 */ + "LogType", /* Log Type (clf or ftp) 60 */ + "ReverseLookup", /* Do DNS lookup (yes/no) 61 */ + "DNSCache", /* Cache size (8K default) 62 */ + "DNSTimeout" /* Timeout (default 2 sec) 63 */ }; FILE *fp; @@ -1603,6 +1720,9 @@ case 58: ntop_exit = atoi(value); break; /* Top Exit pages */ case 59: ntop_search = atoi(value); break; /* Top Search pgs */ case 60: log_type=(value[0]=='f')?1:0; break; /* LogType (1=ftp)*/ + case 61: reverse_lookup=(value[0]=='y')?1:0; break; /* Reverse DNS */ + case 62: dns_cache=atoi(value); break; /* DNS Cache size */ + case 63: dns_timeout=atoi(value); break; /* DNS Timeout */ } } fclose(fp);