]> granicus.if.org Git - apache/blob - support/logresolve.c
197181449a5b0f21309a1381d04bfa6e0a6eafb8
[apache] / support / logresolve.c
1 /*
2  * logresolve 1.1
3  *
4  * Tom Rathborne - tomr@uunet.ca - http://www.uunet.ca/~tomr/
5  * UUNET Canada, April 16, 1995
6  *
7  * Rewritten by David Robinson. (drtr@ast.cam.ac.uk)
8  *
9  * Usage: logresolve [-s filename] [-c] < access_log > new_log
10  *
11  * Arguments:
12  *    -s filename     name of a file to record statistics
13  *    -c              check the DNS for a matching A record for the host.
14  *
15  * Notes:
16  *
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.
20  *
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).
25  *
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
30  * number is left in.
31  *
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.
35  *
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.
40  */
41
42 #include "apr_lib.h"
43 #if APR_HAVE_STDIO_H
44 #include <stdio.h>
45 #endif
46 #if APR_HAVE_STDLIB_H
47 #include <stdlib.h>
48 #endif
49 #if APR_HAVE_CTYPE_H
50 #include <ctype.h>
51 #endif
52 #if APR_HAVE_NETDB_H
53 #include <netdb.h>
54 #endif
55 #if APR_HAVE_NETINET_IN_H
56 #include <netinet/in.h>
57 #endif
58 #if APR_HAVE_STRING_H
59 #include <string.h>
60 #endif
61 #if APR_HAVE_SYS_SOCKET_H
62 #include <sys/socket.h>
63 #endif
64 #if APR_HAVE_ARPA_INET_H
65 #include <arpa/inet.h>
66 #endif
67
68 static void cgethost(struct in_addr ipnum, char *string, int check);
69 static int getline(char *s, int n);
70 static void stats(FILE *output);
71
72 #ifdef BEOS
73 #define NO_ADDRESS NO_DATA
74 #endif
75
76
77 /* maximum line length */
78 #define MAXLINE 1024
79
80 /* maximum length of a domain name */
81 #ifndef MAXDNAME
82 #define MAXDNAME 256
83 #endif
84
85 /* number of buckets in cache hash apr_table_t */
86 #define BUCKETS 256
87
88 #if !APR_HAVE_STRDUP
89 char *strdup (const char *str)
90 {
91     char *dup;
92
93     if (!(dup = (char *) malloc(strlen(str) + 1)))
94         return NULL;
95     dup = strcpy(dup, str);
96
97     return dup;
98 }
99 #endif
100
101 /*
102  * struct nsrec - record of nameservice for cache linked list
103  * 
104  * ipnum - IP number hostname - hostname noname - nonzero if IP number has no
105  * hostname, i.e. hostname=IP number
106  */
107
108 struct nsrec {
109     struct in_addr ipnum;
110     char *hostname;
111     int noname;
112     struct nsrec *next;
113 }    *nscache[BUCKETS];
114
115 /*
116  * statistics - obvious
117  */
118
119 #ifndef h_errno
120 #ifdef __CYGWIN__
121 extern __declspec(dllimport) int h_errno;
122 #else
123 extern int h_errno; /* some machines don't have this in their headers */
124 #endif
125 #endif
126
127 /* largest value for h_errno */
128
129 #define MAX_ERR (NO_ADDRESS)
130 #define UNKNOWN_ERR (MAX_ERR+1)
131 #define NO_REVERSE  (MAX_ERR+2)
132
133 static int cachehits = 0;
134 static int cachesize = 0;
135 static int entries = 0;
136 static int resolves = 0;
137 static int withname = 0;
138 static int errors[MAX_ERR + 3];
139
140 /*
141  * cgethost - gets hostname by IP address, caching, and adding unresolvable
142  * IP numbers with their IP number as hostname, setting noname flag
143  */
144
145 static void cgethost (struct in_addr ipnum, char *string, int check)
146 {
147     struct nsrec **current, *new;
148     struct hostent *hostdata;
149     char *name;
150
151     current = &nscache[((ipnum.s_addr + (ipnum.s_addr >> 8) +
152                          (ipnum.s_addr >> 16) + (ipnum.s_addr >> 24)) % BUCKETS)];
153
154     while (*current != NULL && ipnum.s_addr != (*current)->ipnum.s_addr)
155         current = &(*current)->next;
156
157     if (*current == NULL) {
158         cachesize++;
159         new = (struct nsrec *) malloc(sizeof(struct nsrec));
160         if (new == NULL) {
161             perror("malloc");
162             fprintf(stderr, "Insufficient memory\n");
163             exit(1);
164         }
165         *current = new;
166         new->next = NULL;
167
168         new->ipnum = ipnum;
169
170         hostdata = gethostbyaddr((const char *) &ipnum, sizeof(struct in_addr),
171                                  AF_INET);
172         if (hostdata == NULL) {
173             if (h_errno > MAX_ERR)
174                 errors[UNKNOWN_ERR]++;
175             else
176                 errors[h_errno]++;
177             new->noname = h_errno;
178             name = strdup(inet_ntoa(ipnum));
179         }
180         else {
181             new->noname = 0;
182             name = strdup(hostdata->h_name);
183             if (check) {
184                 if (name == NULL) {
185                     perror("strdup");
186                     fprintf(stderr, "Insufficient memory\n");
187                     exit(1);
188                 }
189                 hostdata = gethostbyname(name);
190                 if (hostdata != NULL) {
191                     char **hptr;
192
193                     for (hptr = hostdata->h_addr_list; *hptr != NULL; hptr++)
194                         if (((struct in_addr *) (*hptr))->s_addr == ipnum.s_addr)
195                             break;
196                     if (*hptr == NULL)
197                         hostdata = NULL;
198                 }
199                 if (hostdata == NULL) {
200                     fprintf(stderr, "Bad host: %s != %s\n", name,
201                             inet_ntoa(ipnum));
202                     new->noname = NO_REVERSE;
203                     free(name);
204                     name = strdup(inet_ntoa(ipnum));
205                     errors[NO_REVERSE]++;
206                 }
207             }
208         }
209         new->hostname = name;
210         if (new->hostname == NULL) {
211             perror("strdup");
212             fprintf(stderr, "Insufficient memory\n");
213             exit(1);
214         }
215     }
216     else
217         cachehits++;
218
219     /* size of string == MAXDNAME +1 */
220     strncpy(string, (*current)->hostname, MAXDNAME);
221     string[MAXDNAME] = '\0';
222 }
223
224 /*
225  * prints various statistics to output
226  */
227
228 static void stats (FILE *output)
229 {
230     int i;
231     char *ipstring;
232     struct nsrec *current;
233     char *errstring[MAX_ERR + 3];
234
235     for (i = 0; i < MAX_ERR + 3; i++)
236         errstring[i] = "Unknown error";
237     errstring[HOST_NOT_FOUND] = "Host not found";
238     errstring[TRY_AGAIN] = "Try again";
239     errstring[NO_RECOVERY] = "Non recoverable error";
240     errstring[NO_DATA] = "No data record";
241     errstring[NO_ADDRESS] = "No address";
242     errstring[NO_REVERSE] = "No reverse entry";
243
244     fprintf(output, "logresolve Statistics:\n");
245
246     fprintf(output, "Entries: %d\n", entries);
247     fprintf(output, "    With name   : %d\n", withname);
248     fprintf(output, "    Resolves    : %d\n", resolves);
249     if (errors[HOST_NOT_FOUND])
250         fprintf(output, "    - Not found : %d\n", errors[HOST_NOT_FOUND]);
251     if (errors[TRY_AGAIN])
252         fprintf(output, "    - Try again : %d\n", errors[TRY_AGAIN]);
253     if (errors[NO_DATA])
254         fprintf(output, "    - No data   : %d\n", errors[NO_DATA]);
255     if (errors[NO_ADDRESS])
256         fprintf(output, "    - No address: %d\n", errors[NO_ADDRESS]);
257     if (errors[NO_REVERSE])
258         fprintf(output, "    - No reverse: %d\n", errors[NO_REVERSE]);
259     fprintf(output, "Cache hits      : %d\n", cachehits);
260     fprintf(output, "Cache size      : %d\n", cachesize);
261     fprintf(output, "Cache buckets   :     IP number * hostname\n");
262
263     for (i = 0; i < BUCKETS; i++)
264         for (current = nscache[i]; current != NULL; current = current->next) {
265             ipstring = inet_ntoa(current->ipnum);
266             if (current->noname == 0)
267                 fprintf(output, "  %3d  %15s - %s\n", i, ipstring,
268                         current->hostname);
269             else {
270                 if (current->noname > MAX_ERR + 2)
271                     fprintf(output, "  %3d  %15s : Unknown error\n", i,
272                             ipstring);
273                 else
274                     fprintf(output, "  %3d  %15s : %s\n", i, ipstring,
275                             errstring[current->noname]);
276             }
277         }
278 }
279
280
281 /*
282  * gets a line from stdin
283  */
284
285 static int getline (char *s, int n)
286 {
287     char *cp;
288
289     if (!fgets(s, n, stdin))
290         return (0);
291     cp = strchr(s, '\n');
292     if (cp)
293         *cp = '\0';
294     return (1);
295 }
296
297 int main (int argc, char *argv[])
298 {
299     struct in_addr ipnum;
300     char *bar, hoststring[MAXDNAME + 1], line[MAXLINE], *statfile;
301     int i, check;
302
303 #ifdef WIN32
304     /*  If we apr'ify this code, apr_pool_create/apr_pool_destroy
305      *  should perform the WSAStartup/WSACleanup for us. 
306      */
307     WSADATA wsaData;
308     WSAStartup(0x101, &wsaData);
309 #endif
310
311     check = 0;
312     statfile = NULL;
313     for (i = 1; i < argc; i++) {
314         if (strcmp(argv[i], "-c") == 0)
315             check = 1;
316         else if (strcmp(argv[i], "-s") == 0) {
317             if (i == argc - 1) {
318                 fprintf(stderr, "logresolve: missing filename to -s\n");
319                 exit(1);
320             }
321             i++;
322             statfile = argv[i];
323         }
324         else {
325             fprintf(stderr, "Usage: logresolve [-s statfile] [-c] < input > output\n");
326             exit(0);
327         }
328     }
329
330     for (i = 0; i < BUCKETS; i++)
331         nscache[i] = NULL;
332     for (i = 0; i < MAX_ERR + 2; i++)
333         errors[i] = 0;
334
335     while (getline(line, MAXLINE)) {
336         if (line[0] == '\0')
337             continue;
338         entries++;
339         if (!apr_isdigit(line[0])) {    /* short cut */
340             puts(line);
341             withname++;
342             continue;
343         }
344         bar = strchr(line, ' ');
345         if (bar != NULL)
346             *bar = '\0';
347         ipnum.s_addr = inet_addr(line);
348         if (ipnum.s_addr == 0xffffffffu) {
349             if (bar != NULL)
350                 *bar = ' ';
351             puts(line);
352             withname++;
353             continue;
354         }
355
356         resolves++;
357
358         cgethost(ipnum, hoststring, check);
359         if (bar != NULL)
360             printf("%s %s\n", hoststring, bar + 1);
361         else
362             puts(hoststring);
363     }
364
365 #ifdef WIN32
366      WSACleanup();
367 #endif
368
369     if (statfile != NULL) {
370         FILE *fp;
371         fp = fopen(statfile, "w");
372         if (fp == NULL) {
373             fprintf(stderr, "logresolve: could not open statistics file '%s'\n"
374                     ,statfile);
375             exit(1);
376         }
377         stats(fp);
378         fclose(fp);
379     }
380
381     return (0);
382 }