]> granicus.if.org Git - apache/blob - support/logresolve.c
renaming various functions for consistency sake
[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 extern int h_errno; /* some machines don't have this in their headers */
121 #endif
122
123 /* largest value for h_errno */
124
125 #define MAX_ERR (NO_ADDRESS)
126 #define UNKNOWN_ERR (MAX_ERR+1)
127 #define NO_REVERSE  (MAX_ERR+2)
128
129 static int cachehits = 0;
130 static int cachesize = 0;
131 static int entries = 0;
132 static int resolves = 0;
133 static int withname = 0;
134 static int errors[MAX_ERR + 3];
135
136 /*
137  * cgethost - gets hostname by IP address, caching, and adding unresolvable
138  * IP numbers with their IP number as hostname, setting noname flag
139  */
140
141 static void cgethost (struct in_addr ipnum, char *string, int check)
142 {
143     struct nsrec **current, *new;
144     struct hostent *hostdata;
145     char *name;
146
147     current = &nscache[((ipnum.s_addr + (ipnum.s_addr >> 8) +
148                          (ipnum.s_addr >> 16) + (ipnum.s_addr >> 24)) % BUCKETS)];
149
150     while (*current != NULL && ipnum.s_addr != (*current)->ipnum.s_addr)
151         current = &(*current)->next;
152
153     if (*current == NULL) {
154         cachesize++;
155         new = (struct nsrec *) malloc(sizeof(struct nsrec));
156         if (new == NULL) {
157             perror("malloc");
158             fprintf(stderr, "Insufficient memory\n");
159             exit(1);
160         }
161         *current = new;
162         new->next = NULL;
163
164         new->ipnum = ipnum;
165
166         hostdata = gethostbyaddr((const char *) &ipnum, sizeof(struct in_addr),
167                                  AF_INET);
168         if (hostdata == NULL) {
169             if (h_errno > MAX_ERR)
170                 errors[UNKNOWN_ERR]++;
171             else
172                 errors[h_errno]++;
173             new->noname = h_errno;
174             name = strdup(inet_ntoa(ipnum));
175         }
176         else {
177             new->noname = 0;
178             name = strdup(hostdata->h_name);
179             if (check) {
180                 if (name == NULL) {
181                     perror("strdup");
182                     fprintf(stderr, "Insufficient memory\n");
183                     exit(1);
184                 }
185                 hostdata = gethostbyname(name);
186                 if (hostdata != NULL) {
187                     char **hptr;
188
189                     for (hptr = hostdata->h_addr_list; *hptr != NULL; hptr++)
190                         if (((struct in_addr *) (*hptr))->s_addr == ipnum.s_addr)
191                             break;
192                     if (*hptr == NULL)
193                         hostdata = NULL;
194                 }
195                 if (hostdata == NULL) {
196                     fprintf(stderr, "Bad host: %s != %s\n", name,
197                             inet_ntoa(ipnum));
198                     new->noname = NO_REVERSE;
199                     free(name);
200                     name = strdup(inet_ntoa(ipnum));
201                     errors[NO_REVERSE]++;
202                 }
203             }
204         }
205         new->hostname = name;
206         if (new->hostname == NULL) {
207             perror("strdup");
208             fprintf(stderr, "Insufficient memory\n");
209             exit(1);
210         }
211     }
212     else
213         cachehits++;
214
215     /* size of string == MAXDNAME +1 */
216     strncpy(string, (*current)->hostname, MAXDNAME);
217     string[MAXDNAME] = '\0';
218 }
219
220 /*
221  * prints various statistics to output
222  */
223
224 static void stats (FILE *output)
225 {
226     int i;
227     char *ipstring;
228     struct nsrec *current;
229     char *errstring[MAX_ERR + 3];
230
231     for (i = 0; i < MAX_ERR + 3; i++)
232         errstring[i] = "Unknown error";
233     errstring[HOST_NOT_FOUND] = "Host not found";
234     errstring[TRY_AGAIN] = "Try again";
235     errstring[NO_RECOVERY] = "Non recoverable error";
236     errstring[NO_DATA] = "No data record";
237     errstring[NO_ADDRESS] = "No address";
238     errstring[NO_REVERSE] = "No reverse entry";
239
240     fprintf(output, "logresolve Statistics:\n");
241
242     fprintf(output, "Entries: %d\n", entries);
243     fprintf(output, "    With name   : %d\n", withname);
244     fprintf(output, "    Resolves    : %d\n", resolves);
245     if (errors[HOST_NOT_FOUND])
246         fprintf(output, "    - Not found : %d\n", errors[HOST_NOT_FOUND]);
247     if (errors[TRY_AGAIN])
248         fprintf(output, "    - Try again : %d\n", errors[TRY_AGAIN]);
249     if (errors[NO_DATA])
250         fprintf(output, "    - No data   : %d\n", errors[NO_DATA]);
251     if (errors[NO_ADDRESS])
252         fprintf(output, "    - No address: %d\n", errors[NO_ADDRESS]);
253     if (errors[NO_REVERSE])
254         fprintf(output, "    - No reverse: %d\n", errors[NO_REVERSE]);
255     fprintf(output, "Cache hits      : %d\n", cachehits);
256     fprintf(output, "Cache size      : %d\n", cachesize);
257     fprintf(output, "Cache buckets   :     IP number * hostname\n");
258
259     for (i = 0; i < BUCKETS; i++)
260         for (current = nscache[i]; current != NULL; current = current->next) {
261             ipstring = inet_ntoa(current->ipnum);
262             if (current->noname == 0)
263                 fprintf(output, "  %3d  %15s - %s\n", i, ipstring,
264                         current->hostname);
265             else {
266                 if (current->noname > MAX_ERR + 2)
267                     fprintf(output, "  %3d  %15s : Unknown error\n", i,
268                             ipstring);
269                 else
270                     fprintf(output, "  %3d  %15s : %s\n", i, ipstring,
271                             errstring[current->noname]);
272             }
273         }
274 }
275
276
277 /*
278  * gets a line from stdin
279  */
280
281 static int getline (char *s, int n)
282 {
283     char *cp;
284
285     if (!fgets(s, n, stdin))
286         return (0);
287     cp = strchr(s, '\n');
288     if (cp)
289         *cp = '\0';
290     return (1);
291 }
292
293 int main (int argc, char *argv[])
294 {
295     struct in_addr ipnum;
296     char *bar, hoststring[MAXDNAME + 1], line[MAXLINE], *statfile;
297     int i, check;
298
299 #ifdef WIN32
300     /*  If we apr'ify this code, apr_pool_create/apr_pool_destroy
301      *  should perform the WSAStartup/WSACleanup for us. 
302      */
303     WSADATA wsaData;
304     WSAStartup(0x101, &wsaData);
305 #endif
306
307     check = 0;
308     statfile = NULL;
309     for (i = 1; i < argc; i++) {
310         if (strcmp(argv[i], "-c") == 0)
311             check = 1;
312         else if (strcmp(argv[i], "-s") == 0) {
313             if (i == argc - 1) {
314                 fprintf(stderr, "logresolve: missing filename to -s\n");
315                 exit(1);
316             }
317             i++;
318             statfile = argv[i];
319         }
320         else {
321             fprintf(stderr, "Usage: logresolve [-s statfile] [-c] < input > output\n");
322             exit(0);
323         }
324     }
325
326     for (i = 0; i < BUCKETS; i++)
327         nscache[i] = NULL;
328     for (i = 0; i < MAX_ERR + 2; i++)
329         errors[i] = 0;
330
331     while (getline(line, MAXLINE)) {
332         if (line[0] == '\0')
333             continue;
334         entries++;
335         if (!apr_isdigit(line[0])) {    /* short cut */
336             puts(line);
337             withname++;
338             continue;
339         }
340         bar = strchr(line, ' ');
341         if (bar != NULL)
342             *bar = '\0';
343         ipnum.s_addr = inet_addr(line);
344         if (ipnum.s_addr == 0xffffffffu) {
345             if (bar != NULL)
346                 *bar = ' ';
347             puts(line);
348             withname++;
349             continue;
350         }
351
352         resolves++;
353
354         cgethost(ipnum, hoststring, check);
355         if (bar != NULL)
356             printf("%s %s\n", hoststring, bar + 1);
357         else
358             puts(hoststring);
359     }
360
361 #ifdef WIN32
362      WSACleanup();
363 #endif
364
365     if (statfile != NULL) {
366         FILE *fp;
367         fp = fopen(statfile, "w");
368         if (fp == NULL) {
369             fprintf(stderr, "logresolve: could not open statistics file '%s'\n"
370                     ,statfile);
371             exit(1);
372         }
373         stats(fp);
374         fclose(fp);
375     }
376
377     return (0);
378 }