]> granicus.if.org Git - apache/blob - support/logresolve.c
73d5a4cfdcc2ef9b5555d24f89fdc96e8ae98893
[apache] / support / logresolve.c
1 /* Copyright 1999-2004 The Apache Software Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 /*
17  * logresolve 1.1
18  *
19  * Tom Rathborne - tomr uunet.ca - http://www.uunet.ca/~tomr/
20  * UUNET Canada, April 16, 1995
21  *
22  * Rewritten by David Robinson. (drtr ast.cam.ac.uk)
23  *
24  * Usage: logresolve [-s filename] [-c] < access_log > new_log
25  *
26  * Arguments:
27  *    -s filename     name of a file to record statistics
28  *    -c              check the DNS for a matching A record for the host.
29  *
30  * Notes:
31  *
32  * To generate meaningful statistics from an HTTPD log file, it's good
33  * to have the domain name of each machine that accessed your site, but
34  * doing this on the fly can slow HTTPD down.
35  *
36  * Compiling NCSA HTTPD with the -DMINIMAL_DNS flag turns IP#->hostname
37  * resolution off. Before running your stats program, just run your log
38  * file through this program (logresolve) and all of your IP numbers will
39  * be resolved into hostnames (where possible).
40  *
41  * logresolve takes an HTTPD access log (in the COMMON log file format,
42  * or any other format that has the IP number/domain name as the first
43  * field for that matter), and outputs the same file with all of the
44  * domain names looked up. Where no domain name can be found, the IP
45  * number is left in.
46  *
47  * To minimize impact on your nameserver, logresolve has its very own
48  * internal hash-table cache. This means that each IP number will only
49  * be looked up the first time it is found in the log file.
50  *
51  * The -c option causes logresolve to apply the same check as httpd
52  * compiled with -DMAXIMUM_DNS; after finding the hostname from the IP
53  * address, it looks up the IP addresses for the hostname and checks
54  * that one of these matches the original address.
55  */
56
57 #include "apr_lib.h"
58 #if APR_HAVE_STDIO_H
59 #include <stdio.h>
60 #endif
61 #if APR_HAVE_STDLIB_H
62 #include <stdlib.h>
63 #endif
64 #if APR_HAVE_CTYPE_H
65 #include <ctype.h>
66 #endif
67 #if APR_HAVE_NETDB_H
68 #include <netdb.h>
69 #endif
70 #if APR_HAVE_NETINET_IN_H
71 #include <netinet/in.h>
72 #endif
73 #if APR_HAVE_STRING_H
74 #include <string.h>
75 #endif
76 #if APR_HAVE_SYS_SOCKET_H
77 #include <sys/socket.h>
78 #endif
79 #if APR_HAVE_ARPA_INET_H
80 #include <arpa/inet.h>
81 #endif
82
83 static void cgethost(struct in_addr ipnum, char *string, int check);
84 static int get_line(char *s, int n);
85 static void stats(FILE *output);
86
87 #ifdef BEOS
88 #define NO_ADDRESS NO_DATA
89 #endif
90
91
92 /* maximum line length */
93 #define MAXLINE 1024
94
95 /* maximum length of a domain name */
96 #ifndef MAXDNAME
97 #define MAXDNAME 256
98 #endif
99
100 /* number of buckets in cache hash apr_table_t */
101 #define BUCKETS 256
102
103 /*
104  * struct nsrec - record of nameservice for cache linked list
105  * 
106  * ipnum - IP number hostname - hostname noname - nonzero if IP number has no
107  * hostname, i.e. hostname=IP number
108  */
109
110 struct nsrec {
111     struct in_addr ipnum;
112     char *hostname;
113     int noname;
114     struct nsrec *next;
115 }    *nscache[BUCKETS];
116
117 /*
118  * statistics - obvious
119  */
120
121 #ifndef h_errno
122 #ifdef __CYGWIN__
123 extern __declspec(dllimport) int h_errno;
124 #else
125 extern int h_errno; /* some machines don't have this in their headers */
126 #endif
127 #endif
128
129 /* largest value for h_errno */
130
131 #define MAX_ERR (NO_ADDRESS)
132 #define UNKNOWN_ERR (MAX_ERR+1)
133 #define NO_REVERSE  (MAX_ERR+2)
134
135 static int cachehits = 0;
136 static int cachesize = 0;
137 static int entries = 0;
138 static int resolves = 0;
139 static int withname = 0;
140 static int errors[MAX_ERR + 3];
141
142 /*
143  * cgethost - gets hostname by IP address, caching, and adding unresolvable
144  * IP numbers with their IP number as hostname, setting noname flag
145  */
146
147 static void cgethost (struct in_addr ipnum, char *string, int check)
148 {
149     struct nsrec **current, *new;
150     struct hostent *hostdata;
151     char *name;
152
153     current = &nscache[((ipnum.s_addr + (ipnum.s_addr >> 8) +
154                          (ipnum.s_addr >> 16) + (ipnum.s_addr >> 24)) % BUCKETS)];
155
156     while (*current != NULL && ipnum.s_addr != (*current)->ipnum.s_addr)
157         current = &(*current)->next;
158
159     if (*current == NULL) {
160         cachesize++;
161         new = (struct nsrec *) malloc(sizeof(struct nsrec));
162         if (new == NULL) {
163             perror("malloc");
164             fprintf(stderr, "Insufficient memory\n");
165             exit(1);
166         }
167         *current = new;
168         new->next = NULL;
169
170         new->ipnum = ipnum;
171
172         hostdata = gethostbyaddr((const char *) &ipnum, sizeof(struct in_addr),
173                                  AF_INET);
174         if (hostdata == NULL) {
175             if (h_errno > MAX_ERR)
176                 errors[UNKNOWN_ERR]++;
177             else
178                 errors[h_errno]++;
179             new->noname = h_errno;
180             name = strdup(inet_ntoa(ipnum));
181         }
182         else {
183             new->noname = 0;
184             name = strdup(hostdata->h_name);
185             if (check) {
186                 if (name == NULL) {
187                     perror("strdup");
188                     fprintf(stderr, "Insufficient memory\n");
189                     exit(1);
190                 }
191                 hostdata = gethostbyname(name);
192                 if (hostdata != NULL) {
193                     char **hptr;
194
195                     for (hptr = hostdata->h_addr_list; *hptr != NULL; hptr++)
196                         if (((struct in_addr *) (*hptr))->s_addr == ipnum.s_addr)
197                             break;
198                     if (*hptr == NULL)
199                         hostdata = NULL;
200                 }
201                 if (hostdata == NULL) {
202                     fprintf(stderr, "Bad host: %s != %s\n", name,
203                             inet_ntoa(ipnum));
204                     new->noname = NO_REVERSE;
205                     free(name);
206                     name = strdup(inet_ntoa(ipnum));
207                     errors[NO_REVERSE]++;
208                 }
209             }
210         }
211         new->hostname = name;
212         if (new->hostname == NULL) {
213             perror("strdup");
214             fprintf(stderr, "Insufficient memory\n");
215             exit(1);
216         }
217     }
218     else
219         cachehits++;
220
221     /* size of string == MAXDNAME +1 */
222     strncpy(string, (*current)->hostname, MAXDNAME);
223     string[MAXDNAME] = '\0';
224 }
225
226 /*
227  * prints various statistics to output
228  */
229
230 static void stats (FILE *output)
231 {
232     int i;
233     char *ipstring;
234     struct nsrec *current;
235     char *errstring[MAX_ERR + 3];
236
237     for (i = 0; i < MAX_ERR + 3; i++)
238         errstring[i] = "Unknown error";
239     errstring[HOST_NOT_FOUND] = "Host not found";
240     errstring[TRY_AGAIN] = "Try again";
241     errstring[NO_RECOVERY] = "Non recoverable error";
242     errstring[NO_DATA] = "No data record";
243     errstring[NO_ADDRESS] = "No address";
244     errstring[NO_REVERSE] = "No reverse entry";
245
246     fprintf(output, "logresolve Statistics:\n");
247
248     fprintf(output, "Entries: %d\n", entries);
249     fprintf(output, "    With name   : %d\n", withname);
250     fprintf(output, "    Resolves    : %d\n", resolves);
251     if (errors[HOST_NOT_FOUND])
252         fprintf(output, "    - Not found : %d\n", errors[HOST_NOT_FOUND]);
253     if (errors[TRY_AGAIN])
254         fprintf(output, "    - Try again : %d\n", errors[TRY_AGAIN]);
255     if (errors[NO_DATA])
256         fprintf(output, "    - No data   : %d\n", errors[NO_DATA]);
257     if (errors[NO_ADDRESS])
258         fprintf(output, "    - No address: %d\n", errors[NO_ADDRESS]);
259     if (errors[NO_REVERSE])
260         fprintf(output, "    - No reverse: %d\n", errors[NO_REVERSE]);
261     fprintf(output, "Cache hits      : %d\n", cachehits);
262     fprintf(output, "Cache size      : %d\n", cachesize);
263     fprintf(output, "Cache buckets   :     IP number * hostname\n");
264
265     for (i = 0; i < BUCKETS; i++)
266         for (current = nscache[i]; current != NULL; current = current->next) {
267             ipstring = inet_ntoa(current->ipnum);
268             if (current->noname == 0)
269                 fprintf(output, "  %3d  %15s - %s\n", i, ipstring,
270                         current->hostname);
271             else {
272                 if (current->noname > MAX_ERR + 2)
273                     fprintf(output, "  %3d  %15s : Unknown error\n", i,
274                             ipstring);
275                 else
276                     fprintf(output, "  %3d  %15s : %s\n", i, ipstring,
277                             errstring[current->noname]);
278             }
279         }
280 }
281
282
283 /*
284  * gets a line from stdin
285  */
286
287 static int get_line (char *s, int n)
288 {
289     char *cp;
290
291     if (!fgets(s, n, stdin))
292         return (0);
293     cp = strchr(s, '\n');
294     if (cp)
295         *cp = '\0';
296     return (1);
297 }
298
299 int main (int argc, char *argv[])
300 {
301     struct in_addr ipnum;
302     char *bar, hoststring[MAXDNAME + 1], line[MAXLINE], *statfile;
303     int i, check;
304
305 #if defined(WIN32) || defined(NETWARE)
306     /*  If we apr'ify this code, apr_pool_create/apr_pool_destroy
307      *  should perform the WSAStartup/WSACleanup for us. 
308      */
309     WSADATA wsaData;
310     WSAStartup(MAKEWORD(2, 0), &wsaData);
311 #endif
312
313     check = 0;
314     statfile = NULL;
315     for (i = 1; i < argc; i++) {
316         if (strcmp(argv[i], "-c") == 0)
317             check = 1;
318         else if (strcmp(argv[i], "-s") == 0) {
319             if (i == argc - 1) {
320                 fprintf(stderr, "logresolve: missing filename to -s\n");
321                 exit(1);
322             }
323             i++;
324             statfile = argv[i];
325         }
326         else {
327             fprintf(stderr, "Usage: logresolve [-s statfile] [-c] < input > output\n");
328             exit(0);
329         }
330     }
331
332     for (i = 0; i < BUCKETS; i++)
333         nscache[i] = NULL;
334     for (i = 0; i < MAX_ERR + 2; i++)
335         errors[i] = 0;
336
337     while (get_line(line, MAXLINE)) {
338         if (line[0] == '\0')
339             continue;
340         entries++;
341         if (!apr_isdigit(line[0])) {    /* short cut */
342             puts(line);
343             withname++;
344             continue;
345         }
346         bar = strchr(line, ' ');
347         if (bar != NULL)
348             *bar = '\0';
349         ipnum.s_addr = inet_addr(line);
350         if (ipnum.s_addr == 0xffffffffu) {
351             if (bar != NULL)
352                 *bar = ' ';
353             puts(line);
354             withname++;
355             continue;
356         }
357
358         resolves++;
359
360         cgethost(ipnum, hoststring, check);
361         if (bar != NULL)
362             printf("%s %s\n", hoststring, bar + 1);
363         else
364             puts(hoststring);
365     }
366
367 #if defined(WIN32) || defined(NETWARE)
368      WSACleanup();
369 #endif
370
371     if (statfile != NULL) {
372         FILE *fp;
373         fp = fopen(statfile, "w");
374         if (fp == NULL) {
375             fprintf(stderr, "logresolve: could not open statistics file '%s'\n"
376                     ,statfile);
377             exit(1);
378         }
379         stats(fp);
380         fclose(fp);
381     }
382
383     return (0);
384 }