1 /* Copyright 1999-2004 The Apache Software Foundation
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * Tom Rathborne - tomr uunet.ca - http://www.uunet.ca/~tomr/
20 * UUNET Canada, April 16, 1995
22 * Rewritten by David Robinson. (drtr ast.cam.ac.uk)
24 * Usage: logresolve [-s filename] [-c] < access_log > new_log
27 * -s filename name of a file to record statistics
28 * -c check the DNS for a matching A record for the host.
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.
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).
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
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.
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.
70 #if APR_HAVE_NETINET_IN_H
71 #include <netinet/in.h>
76 #if APR_HAVE_SYS_SOCKET_H
77 #include <sys/socket.h>
79 #if APR_HAVE_ARPA_INET_H
80 #include <arpa/inet.h>
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);
88 #define NO_ADDRESS NO_DATA
92 /* maximum line length */
97 /* maximum length of a domain name */
102 /* number of buckets in cache hash apr_table_t */
106 * struct nsrec - record of nameservice for cache linked list
108 * ipnum - IP number hostname - hostname noname - nonzero if IP number has no
109 * hostname, i.e. hostname=IP number
113 struct in_addr ipnum;
120 * statistics - obvious
125 extern __declspec(dllimport) int h_errno;
127 extern int h_errno; /* some machines don't have this in their headers */
131 /* largest value for h_errno */
133 #define MAX_ERR (NO_ADDRESS)
134 #define UNKNOWN_ERR (MAX_ERR+1)
135 #define NO_REVERSE (MAX_ERR+2)
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];
145 * cgethost - gets hostname by IP address, caching, and adding unresolvable
146 * IP numbers with their IP number as hostname, setting noname flag
149 static void cgethost (struct in_addr ipnum, char *string, int check)
151 struct nsrec **current, *new;
152 struct hostent *hostdata;
155 current = &nscache[((ipnum.s_addr + (ipnum.s_addr >> 8) +
156 (ipnum.s_addr >> 16) + (ipnum.s_addr >> 24)) % BUCKETS)];
158 while (*current != NULL && ipnum.s_addr != (*current)->ipnum.s_addr)
159 current = &(*current)->next;
161 if (*current == NULL) {
163 new = (struct nsrec *) malloc(sizeof(struct nsrec));
166 fprintf(stderr, "Insufficient memory\n");
174 hostdata = gethostbyaddr((const char *) &ipnum, sizeof(struct in_addr),
176 if (hostdata == NULL) {
177 if (h_errno > MAX_ERR)
178 errors[UNKNOWN_ERR]++;
181 new->noname = h_errno;
182 name = strdup(inet_ntoa(ipnum));
186 name = strdup(hostdata->h_name);
190 fprintf(stderr, "Insufficient memory\n");
193 hostdata = gethostbyname(name);
194 if (hostdata != NULL) {
197 for (hptr = hostdata->h_addr_list; *hptr != NULL; hptr++)
198 if (((struct in_addr *) (*hptr))->s_addr == ipnum.s_addr)
203 if (hostdata == NULL) {
204 fprintf(stderr, "Bad host: %s != %s\n", name,
206 new->noname = NO_REVERSE;
208 name = strdup(inet_ntoa(ipnum));
209 errors[NO_REVERSE]++;
213 new->hostname = name;
214 if (new->hostname == NULL) {
216 fprintf(stderr, "Insufficient memory\n");
223 /* size of string == MAXDNAME +1 */
224 strncpy(string, (*current)->hostname, MAXDNAME);
225 string[MAXDNAME] = '\0';
229 * prints various statistics to output
232 static void stats (FILE *output)
236 struct nsrec *current;
237 char *errstring[MAX_ERR + 3];
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";
248 fprintf(output, "logresolve Statistics:\n");
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]);
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");
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,
274 if (current->noname > MAX_ERR + 2)
275 fprintf(output, " %3d %15s : Unknown error\n", i,
278 fprintf(output, " %3d %15s : %s\n", i, ipstring,
279 errstring[current->noname]);
286 * gets a line from stdin
289 static int get_line (char *s, int n)
293 if (!fgets(s, n, stdin))
295 cp = strchr(s, '\n');
301 int main (int argc, char *argv[])
303 struct in_addr ipnum;
304 char *bar, hoststring[MAXDNAME + 1], line[MAXLINE], *statfile;
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.
312 WSAStartup(MAKEWORD(2, 0), &wsaData);
317 for (i = 1; i < argc; i++) {
318 if (strcmp(argv[i], "-c") == 0)
320 else if (strcmp(argv[i], "-s") == 0) {
322 fprintf(stderr, "logresolve: missing filename to -s\n");
329 fprintf(stderr, "Usage: logresolve [-s statfile] [-c] < input > output\n");
334 for (i = 0; i < BUCKETS; i++)
336 for (i = 0; i < MAX_ERR + 2; i++)
339 while (get_line(line, MAXLINE)) {
343 if (!apr_isdigit(line[0])) { /* short cut */
348 bar = strchr(line, ' ');
351 ipnum.s_addr = inet_addr(line);
352 if (ipnum.s_addr == 0xffffffffu) {
362 cgethost(ipnum, hoststring, check);
364 printf("%s %s\n", hoststring, bar + 1);
369 #if defined(WIN32) || defined(NETWARE)
373 if (statfile != NULL) {
375 fp = fopen(statfile, "w");
377 fprintf(stderr, "logresolve: could not open statistics file '%s'\n"