4 * Tom Rathborne - tomr@uunet.ca - http://www.uunet.ca/~tomr/
5 * UUNET Canada, April 16, 1995
7 * Rewritten by David Robinson. (drtr@ast.cam.ac.uk)
9 * Usage: logresolve [-s filename] [-c] < access_log > new_log
12 * -s filename name of a file to record statistics
13 * -c check the DNS for a matching A record for the host.
17 * To generate meaningful statistics from an HTTPD log file, it's good
18 * to have the domain name of each machine that accessed your site, but
19 * doing this on the fly can slow HTTPD down.
21 * Compiling NCSA HTTPD with the -DMINIMAL_DNS flag turns IP#->hostname
22 * resolution off. Before running your stats program, just run your log
23 * file through this program (logresolve) and all of your IP numbers will
24 * be resolved into hostnames (where possible).
26 * logresolve takes an HTTPD access log (in the COMMON log file format,
27 * or any other format that has the IP number/domain name as the first
28 * field for that matter), and outputs the same file with all of the
29 * domain names looked up. Where no domain name can be found, the IP
32 * To minimize impact on your nameserver, logresolve has its very own
33 * internal hash-table cache. This means that each IP number will only
34 * be looked up the first time it is found in the log file.
36 * The -c option causes logresolve to apply the same check as httpd
37 * compiled with -DMAXIMUM_DNS; after finding the hostname from the IP
38 * address, it looks up the IP addresses for the hostname and checks
39 * that one of these matches the original address.
42 #include "ap_config.h"
50 #ifdef HAVE_NETINET_IN_H
51 #include <netinet/in.h>
53 #ifdef HAVE_SYS_SOCKET_H
54 #include <sys/socket.h>
56 #ifdef HAVE_ARPA_INET_H
57 #include <arpa/inet.h>
60 static void cgethost(struct in_addr ipnum, char *string, int check);
61 static int getline(char *s, int n);
62 static void stats(FILE *output);
65 #define NO_ADDRESS NO_DATA
69 /* maximum line length */
72 /* maximum length of a domain name */
77 /* number of buckets in cache hash apr_table_t */
80 #if defined(NEED_STRDUP)
81 char *strdup (const char *str)
85 if (!(dup = (char *) malloc(strlen(str) + 1)))
87 dup = strcpy(dup, str);
94 * struct nsrec - record of nameservice for cache linked list
96 * ipnum - IP number hostname - hostname noname - nonzero if IP number has no
97 * hostname, i.e. hostname=IP number
101 struct in_addr ipnum;
108 * statistics - obvious
112 extern int h_errno; /* some machines don't have this in their headers */
115 /* largest value for h_errno */
117 #define MAX_ERR (NO_ADDRESS)
118 #define UNKNOWN_ERR (MAX_ERR+1)
119 #define NO_REVERSE (MAX_ERR+2)
121 static int cachehits = 0;
122 static int cachesize = 0;
123 static int entries = 0;
124 static int resolves = 0;
125 static int withname = 0;
126 static int errors[MAX_ERR + 3];
129 * cgethost - gets hostname by IP address, caching, and adding unresolvable
130 * IP numbers with their IP number as hostname, setting noname flag
133 static void cgethost (struct in_addr ipnum, char *string, int check)
135 struct nsrec **current, *new;
136 struct hostent *hostdata;
139 current = &nscache[((ipnum.s_addr + (ipnum.s_addr >> 8) +
140 (ipnum.s_addr >> 16) + (ipnum.s_addr >> 24)) % BUCKETS)];
142 while (*current != NULL && ipnum.s_addr != (*current)->ipnum.s_addr)
143 current = &(*current)->next;
145 if (*current == NULL) {
147 new = (struct nsrec *) malloc(sizeof(struct nsrec));
150 fprintf(stderr, "Insufficient memory\n");
158 hostdata = gethostbyaddr((const char *) &ipnum, sizeof(struct in_addr),
160 if (hostdata == NULL) {
161 if (h_errno > MAX_ERR)
162 errors[UNKNOWN_ERR]++;
165 new->noname = h_errno;
166 name = strdup(inet_ntoa(ipnum));
170 name = strdup(hostdata->h_name);
174 fprintf(stderr, "Insufficient memory\n");
177 hostdata = gethostbyname(name);
178 if (hostdata != NULL) {
181 for (hptr = hostdata->h_addr_list; *hptr != NULL; hptr++)
182 if (((struct in_addr *) (*hptr))->s_addr == ipnum.s_addr)
187 if (hostdata == NULL) {
188 fprintf(stderr, "Bad host: %s != %s\n", name,
190 new->noname = NO_REVERSE;
192 name = strdup(inet_ntoa(ipnum));
193 errors[NO_REVERSE]++;
197 new->hostname = name;
198 if (new->hostname == NULL) {
200 fprintf(stderr, "Insufficient memory\n");
207 /* size of string == MAXDNAME +1 */
208 strncpy(string, (*current)->hostname, MAXDNAME);
209 string[MAXDNAME] = '\0';
213 * prints various statistics to output
216 static void stats (FILE *output)
220 struct nsrec *current;
221 char *errstring[MAX_ERR + 3];
223 for (i = 0; i < MAX_ERR + 3; i++)
224 errstring[i] = "Unknown error";
225 errstring[HOST_NOT_FOUND] = "Host not found";
226 errstring[TRY_AGAIN] = "Try again";
227 errstring[NO_RECOVERY] = "Non recoverable error";
228 errstring[NO_DATA] = "No data record";
229 errstring[NO_ADDRESS] = "No address";
230 errstring[NO_REVERSE] = "No reverse entry";
232 fprintf(output, "logresolve Statistics:\n");
234 fprintf(output, "Entries: %d\n", entries);
235 fprintf(output, " With name : %d\n", withname);
236 fprintf(output, " Resolves : %d\n", resolves);
237 if (errors[HOST_NOT_FOUND])
238 fprintf(output, " - Not found : %d\n", errors[HOST_NOT_FOUND]);
239 if (errors[TRY_AGAIN])
240 fprintf(output, " - Try again : %d\n", errors[TRY_AGAIN]);
242 fprintf(output, " - No data : %d\n", errors[NO_DATA]);
243 if (errors[NO_ADDRESS])
244 fprintf(output, " - No address: %d\n", errors[NO_ADDRESS]);
245 if (errors[NO_REVERSE])
246 fprintf(output, " - No reverse: %d\n", errors[NO_REVERSE]);
247 fprintf(output, "Cache hits : %d\n", cachehits);
248 fprintf(output, "Cache size : %d\n", cachesize);
249 fprintf(output, "Cache buckets : IP number * hostname\n");
251 for (i = 0; i < BUCKETS; i++)
252 for (current = nscache[i]; current != NULL; current = current->next) {
253 ipstring = inet_ntoa(current->ipnum);
254 if (current->noname == 0)
255 fprintf(output, " %3d %15s - %s\n", i, ipstring,
258 if (current->noname > MAX_ERR + 2)
259 fprintf(output, " %3d %15s : Unknown error\n", i,
262 fprintf(output, " %3d %15s : %s\n", i, ipstring,
263 errstring[current->noname]);
270 * gets a line from stdin
273 static int getline (char *s, int n)
277 if (!fgets(s, n, stdin))
279 cp = strchr(s, '\n');
285 int main (int argc, char *argv[])
287 struct in_addr ipnum;
288 char *bar, hoststring[MAXDNAME + 1], line[MAXLINE], *statfile;
292 /* If we apr'ify this code, apr_create_pool/apr_destroy_pool
293 * should perform the WSAStartup/WSACleanup for us.
296 WSAStartup(0x101, &wsaData);
301 for (i = 1; i < argc; i++) {
302 if (strcmp(argv[i], "-c") == 0)
304 else if (strcmp(argv[i], "-s") == 0) {
306 fprintf(stderr, "logresolve: missing filename to -s\n");
313 fprintf(stderr, "Usage: logresolve [-s statfile] [-c] < input > output\n");
318 for (i = 0; i < BUCKETS; i++)
320 for (i = 0; i < MAX_ERR + 2; i++)
323 while (getline(line, MAXLINE)) {
327 if (!isdigit(line[0])) { /* short cut */
332 bar = strchr(line, ' ');
335 ipnum.s_addr = inet_addr(line);
336 if (ipnum.s_addr == 0xffffffffu) {
346 cgethost(ipnum, hoststring, check);
348 printf("%s %s\n", hoststring, bar + 1);
357 if (statfile != NULL) {
359 fp = fopen(statfile, "w");
361 fprintf(stderr, "logresolve: could not open statistics file '%s'\n"