--- webalizer.c.old Mon Jun 7 20:43:06 1999 +++ webalizer.c Wed Jun 16 11:42:53 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 @@ -184,6 +194,11 @@ NLISTPTR html_tail = NULL; /* tail HTML code */ NLISTPTR html_end = NULL; /* after everything else */ +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 */ /*********************************************/ @@ -261,6 +276,11 @@ } } + if(reverse_lookup) + { + hcreate(dns_cache); + } + if (argc - optind != 0) log_fname = argv[optind]; /* setup our internal variables */ @@ -1069,6 +1089,89 @@ } } +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) + { + 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) + { + 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) + { + 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... */ /*********************************************/ @@ -1210,9 +1313,17 @@ while ( (*cp1 != '\0') && (cp1 != eos) ) *cp2++ = *cp1++; *cp2 = '\0'; + if(reverse_lookup) + lookup_host_name(log_rec.hostname, sizeof(log_rec.hostname)); + return 1; /* maybe a valid record, return with TRUE */ } +void lookup_sigalrm() + { + longjmp(jumper, 1); + } + void clear_month() { int i; @@ -1284,8 +1395,11 @@ "IncludeSite", /* Sites to always include 45 */ "IncludeURL", /* URL's to always include 46 */ "IncludeReferrer", /* Referrers to include 47 */ - "IncludeAgent" /* User Agents to include 48 */ - }; + "IncludeAgent", /* User Agents to include 48 */ + "ReverseLookup", /* Reverse Lookup of IPs 49 */ + "DNSCache", /* DNS cache size 50 */ + "DNSTimeout" /* DNS lookup timeout 51 */ + }; FILE *fp; @@ -1381,6 +1495,9 @@ case 46: add_nlist(value,&include_urls); break; /* IncludeURL */ case 47: add_nlist(value,&include_refs); break;/*IncludeReferrer*/ case 48: add_nlist(value,&include_agents); break; /* IncludeAgent */ + case 49: reverse_lookup = (value[0]=='y')?1:0; break; /* ReverseLookup */ + case 50: dns_cache = atoi(value); break; /* ReverseLookup */ + case 51: dns_timeout = atoi(value); break; /* ReverseLookup */ } } fclose(fp); --- webalizer.h.old Mon Jun 7 21:10:10 1999 +++ webalizer.h Wed Jun 16 11:42:57 1999 @@ -221,3 +221,6 @@ void top_ctry_table(); /* top n countries "" */ 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 */