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