]> granicus.if.org Git - apache/blob - support/logresolve.c
Update exports file for AIX.
[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 "ap_config.h"
43 #include <stdio.h>
44 #ifdef HAVE_CTYPE_H
45 #include <ctype.h>
46 #endif
47 #ifdef HAVE_NETDB_H
48 #include <netdb.h>
49 #endif
50 #ifdef HAVE_NETINET_IN_H
51 #include <netinet/in.h>
52 #endif
53 #ifdef HAVE_SYS_SOCKET_H
54 #include <sys/socket.h>
55 #endif
56 #ifdef HAVE_ARPA_INET_H
57 #include <arpa/inet.h>
58 #endif
59
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);
63
64 #ifdef BEOS
65 #define NO_ADDRESS NO_DATA
66 #endif
67
68
69 /* maximum line length */
70 #define MAXLINE 1024
71
72 /* maximum length of a domain name */
73 #ifndef MAXDNAME
74 #define MAXDNAME 256
75 #endif
76
77 /* number of buckets in cache hash apr_table_t */
78 #define BUCKETS 256
79
80 #if defined(NEED_STRDUP)
81 char *strdup (const char *str)
82 {
83     char *dup;
84
85     if (!(dup = (char *) malloc(strlen(str) + 1)))
86         return NULL;
87     dup = strcpy(dup, str);
88
89     return dup;
90 }
91 #endif
92
93 /*
94  * struct nsrec - record of nameservice for cache linked list
95  * 
96  * ipnum - IP number hostname - hostname noname - nonzero if IP number has no
97  * hostname, i.e. hostname=IP number
98  */
99
100 struct nsrec {
101     struct in_addr ipnum;
102     char *hostname;
103     int noname;
104     struct nsrec *next;
105 }    *nscache[BUCKETS];
106
107 /*
108  * statistics - obvious
109  */
110
111 #ifndef h_errno
112 extern int h_errno; /* some machines don't have this in their headers */
113 #endif
114
115 /* largest value for h_errno */
116
117 #define MAX_ERR (NO_ADDRESS)
118 #define UNKNOWN_ERR (MAX_ERR+1)
119 #define NO_REVERSE  (MAX_ERR+2)
120
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];
127
128 /*
129  * cgethost - gets hostname by IP address, caching, and adding unresolvable
130  * IP numbers with their IP number as hostname, setting noname flag
131  */
132
133 static void cgethost (struct in_addr ipnum, char *string, int check)
134 {
135     struct nsrec **current, *new;
136     struct hostent *hostdata;
137     char *name;
138
139     current = &nscache[((ipnum.s_addr + (ipnum.s_addr >> 8) +
140                          (ipnum.s_addr >> 16) + (ipnum.s_addr >> 24)) % BUCKETS)];
141
142     while (*current != NULL && ipnum.s_addr != (*current)->ipnum.s_addr)
143         current = &(*current)->next;
144
145     if (*current == NULL) {
146         cachesize++;
147         new = (struct nsrec *) malloc(sizeof(struct nsrec));
148         if (new == NULL) {
149             perror("malloc");
150             fprintf(stderr, "Insufficient memory\n");
151             exit(1);
152         }
153         *current = new;
154         new->next = NULL;
155
156         new->ipnum = ipnum;
157
158         hostdata = gethostbyaddr((const char *) &ipnum, sizeof(struct in_addr),
159                                  AF_INET);
160         if (hostdata == NULL) {
161             if (h_errno > MAX_ERR)
162                 errors[UNKNOWN_ERR]++;
163             else
164                 errors[h_errno]++;
165             new->noname = h_errno;
166             name = strdup(inet_ntoa(ipnum));
167         }
168         else {
169             new->noname = 0;
170             name = strdup(hostdata->h_name);
171             if (check) {
172                 if (name == NULL) {
173                     perror("strdup");
174                     fprintf(stderr, "Insufficient memory\n");
175                     exit(1);
176                 }
177                 hostdata = gethostbyname(name);
178                 if (hostdata != NULL) {
179                     char **hptr;
180
181                     for (hptr = hostdata->h_addr_list; *hptr != NULL; hptr++)
182                         if (((struct in_addr *) (*hptr))->s_addr == ipnum.s_addr)
183                             break;
184                     if (*hptr == NULL)
185                         hostdata = NULL;
186                 }
187                 if (hostdata == NULL) {
188                     fprintf(stderr, "Bad host: %s != %s\n", name,
189                             inet_ntoa(ipnum));
190                     new->noname = NO_REVERSE;
191                     free(name);
192                     name = strdup(inet_ntoa(ipnum));
193                     errors[NO_REVERSE]++;
194                 }
195             }
196         }
197         new->hostname = name;
198         if (new->hostname == NULL) {
199             perror("strdup");
200             fprintf(stderr, "Insufficient memory\n");
201             exit(1);
202         }
203     }
204     else
205         cachehits++;
206
207     /* size of string == MAXDNAME +1 */
208     strncpy(string, (*current)->hostname, MAXDNAME);
209     string[MAXDNAME] = '\0';
210 }
211
212 /*
213  * prints various statistics to output
214  */
215
216 static void stats (FILE *output)
217 {
218     int i;
219     char *ipstring;
220     struct nsrec *current;
221     char *errstring[MAX_ERR + 3];
222
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";
231
232     fprintf(output, "logresolve Statistics:\n");
233
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]);
241     if (errors[NO_DATA])
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");
250
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,
256                         current->hostname);
257             else {
258                 if (current->noname > MAX_ERR + 2)
259                     fprintf(output, "  %3d  %15s : Unknown error\n", i,
260                             ipstring);
261                 else
262                     fprintf(output, "  %3d  %15s : %s\n", i, ipstring,
263                             errstring[current->noname]);
264             }
265         }
266 }
267
268
269 /*
270  * gets a line from stdin
271  */
272
273 static int getline (char *s, int n)
274 {
275     char *cp;
276
277     if (!fgets(s, n, stdin))
278         return (0);
279     cp = strchr(s, '\n');
280     if (cp)
281         *cp = '\0';
282     return (1);
283 }
284
285 int main (int argc, char *argv[])
286 {
287     struct in_addr ipnum;
288     char *bar, hoststring[MAXDNAME + 1], line[MAXLINE], *statfile;
289     int i, check;
290
291 #ifdef WIN32
292     /*  If we apr'ify this code, apr_create_pool/apr_destroy_pool
293      *  should perform the WSAStartup/WSACleanup for us. 
294      */
295     WSADATA wsaData;
296     WSAStartup(0x101, &wsaData);
297 #endif
298
299     check = 0;
300     statfile = NULL;
301     for (i = 1; i < argc; i++) {
302         if (strcmp(argv[i], "-c") == 0)
303             check = 1;
304         else if (strcmp(argv[i], "-s") == 0) {
305             if (i == argc - 1) {
306                 fprintf(stderr, "logresolve: missing filename to -s\n");
307                 exit(1);
308             }
309             i++;
310             statfile = argv[i];
311         }
312         else {
313             fprintf(stderr, "Usage: logresolve [-s statfile] [-c] < input > output\n");
314             exit(0);
315         }
316     }
317
318     for (i = 0; i < BUCKETS; i++)
319         nscache[i] = NULL;
320     for (i = 0; i < MAX_ERR + 2; i++)
321         errors[i] = 0;
322
323     while (getline(line, MAXLINE)) {
324         if (line[0] == '\0')
325             continue;
326         entries++;
327         if (!isdigit(line[0])) {        /* short cut */
328             puts(line);
329             withname++;
330             continue;
331         }
332         bar = strchr(line, ' ');
333         if (bar != NULL)
334             *bar = '\0';
335         ipnum.s_addr = inet_addr(line);
336         if (ipnum.s_addr == 0xffffffffu) {
337             if (bar != NULL)
338                 *bar = ' ';
339             puts(line);
340             withname++;
341             continue;
342         }
343
344         resolves++;
345
346         cgethost(ipnum, hoststring, check);
347         if (bar != NULL)
348             printf("%s %s\n", hoststring, bar + 1);
349         else
350             puts(hoststring);
351     }
352
353 #ifdef WIN32
354      WSACleanup();
355 #endif
356
357     if (statfile != NULL) {
358         FILE *fp;
359         fp = fopen(statfile, "w");
360         if (fp == NULL) {
361             fprintf(stderr, "logresolve: could not open statistics file '%s'\n"
362                     ,statfile);
363             exit(1);
364         }
365         stats(fp);
366         fclose(fp);
367     }
368
369     return (0);
370 }