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