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 */
95 /* maximum length of a domain name */
100 /* number of buckets in cache hash apr_table_t */
104 * struct nsrec - record of nameservice for cache linked list
106 * ipnum - IP number hostname - hostname noname - nonzero if IP number has no
107 * hostname, i.e. hostname=IP number
111 struct in_addr ipnum;
118 * statistics - obvious
123 extern __declspec(dllimport) int h_errno;
125 extern int h_errno; /* some machines don't have this in their headers */
129 /* largest value for h_errno */
131 #define MAX_ERR (NO_ADDRESS)
132 #define UNKNOWN_ERR (MAX_ERR+1)
133 #define NO_REVERSE (MAX_ERR+2)
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];
143 * cgethost - gets hostname by IP address, caching, and adding unresolvable
144 * IP numbers with their IP number as hostname, setting noname flag
147 static void cgethost (struct in_addr ipnum, char *string, int check)
149 struct nsrec **current, *new;
150 struct hostent *hostdata;
153 current = &nscache[((ipnum.s_addr + (ipnum.s_addr >> 8) +
154 (ipnum.s_addr >> 16) + (ipnum.s_addr >> 24)) % BUCKETS)];
156 while (*current != NULL && ipnum.s_addr != (*current)->ipnum.s_addr)
157 current = &(*current)->next;
159 if (*current == NULL) {
161 new = (struct nsrec *) malloc(sizeof(struct nsrec));
164 fprintf(stderr, "Insufficient memory\n");
172 hostdata = gethostbyaddr((const char *) &ipnum, sizeof(struct in_addr),
174 if (hostdata == NULL) {
175 if (h_errno > MAX_ERR)
176 errors[UNKNOWN_ERR]++;
179 new->noname = h_errno;
180 name = strdup(inet_ntoa(ipnum));
184 name = strdup(hostdata->h_name);
188 fprintf(stderr, "Insufficient memory\n");
191 hostdata = gethostbyname(name);
192 if (hostdata != NULL) {
195 for (hptr = hostdata->h_addr_list; *hptr != NULL; hptr++)
196 if (((struct in_addr *) (*hptr))->s_addr == ipnum.s_addr)
201 if (hostdata == NULL) {
202 fprintf(stderr, "Bad host: %s != %s\n", name,
204 new->noname = NO_REVERSE;
206 name = strdup(inet_ntoa(ipnum));
207 errors[NO_REVERSE]++;
211 new->hostname = name;
212 if (new->hostname == NULL) {
214 fprintf(stderr, "Insufficient memory\n");
221 /* size of string == MAXDNAME +1 */
222 strncpy(string, (*current)->hostname, MAXDNAME);
223 string[MAXDNAME] = '\0';
227 * prints various statistics to output
230 static void stats (FILE *output)
234 struct nsrec *current;
235 char *errstring[MAX_ERR + 3];
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";
246 fprintf(output, "logresolve Statistics:\n");
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]);
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");
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,
272 if (current->noname > MAX_ERR + 2)
273 fprintf(output, " %3d %15s : Unknown error\n", i,
276 fprintf(output, " %3d %15s : %s\n", i, ipstring,
277 errstring[current->noname]);
284 * gets a line from stdin
287 static int get_line (char *s, int n)
291 if (!fgets(s, n, stdin))
293 cp = strchr(s, '\n');
299 int main (int argc, char *argv[])
301 struct in_addr ipnum;
302 char *bar, hoststring[MAXDNAME + 1], line[MAXLINE], *statfile;
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.
310 WSAStartup(MAKEWORD(2, 0), &wsaData);
315 for (i = 1; i < argc; i++) {
316 if (strcmp(argv[i], "-c") == 0)
318 else if (strcmp(argv[i], "-s") == 0) {
320 fprintf(stderr, "logresolve: missing filename to -s\n");
327 fprintf(stderr, "Usage: logresolve [-s statfile] [-c] < input > output\n");
332 for (i = 0; i < BUCKETS; i++)
334 for (i = 0; i < MAX_ERR + 2; i++)
337 while (get_line(line, MAXLINE)) {
341 if (!apr_isdigit(line[0])) { /* short cut */
346 bar = strchr(line, ' ');
349 ipnum.s_addr = inet_addr(line);
350 if (ipnum.s_addr == 0xffffffffu) {
360 cgethost(ipnum, hoststring, check);
362 printf("%s %s\n", hoststring, bar + 1);
367 #if defined(WIN32) || defined(NETWARE)
371 if (statfile != NULL) {
373 fp = fopen(statfile, "w");
375 fprintf(stderr, "logresolve: could not open statistics file '%s'\n"