4 * Tom Rathborne - tomr@uunet.ca - http://www.uunet.ca/~tomr/
5 * UUNET Canada, April 16, 1995
7 * Rewritten by David Robinson. (drtr@ast.cam.ac.uk)
9 * Usage: logresolve [-s filename] [-c] < access_log > new_log
12 * -s filename name of a file to record statistics
13 * -c check the DNS for a matching A record for the host.
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.
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).
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
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.
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.
55 #if APR_HAVE_NETINET_IN_H
56 #include <netinet/in.h>
61 #if APR_HAVE_SYS_SOCKET_H
62 #include <sys/socket.h>
64 #if APR_HAVE_ARPA_INET_H
65 #include <arpa/inet.h>
68 static void cgethost(struct in_addr ipnum, char *string, int check);
69 static int getline(char *s, int n);
70 static void stats(FILE *output);
73 #define NO_ADDRESS NO_DATA
77 /* maximum line length */
80 /* maximum length of a domain name */
85 /* number of buckets in cache hash apr_table_t */
89 char *strdup (const char *str)
93 if (!(dup = (char *) malloc(strlen(str) + 1)))
95 dup = strcpy(dup, str);
102 * struct nsrec - record of nameservice for cache linked list
104 * ipnum - IP number hostname - hostname noname - nonzero if IP number has no
105 * hostname, i.e. hostname=IP number
109 struct in_addr ipnum;
116 * statistics - obvious
120 extern int h_errno; /* some machines don't have this in their headers */
123 /* largest value for h_errno */
125 #define MAX_ERR (NO_ADDRESS)
126 #define UNKNOWN_ERR (MAX_ERR+1)
127 #define NO_REVERSE (MAX_ERR+2)
129 static int cachehits = 0;
130 static int cachesize = 0;
131 static int entries = 0;
132 static int resolves = 0;
133 static int withname = 0;
134 static int errors[MAX_ERR + 3];
137 * cgethost - gets hostname by IP address, caching, and adding unresolvable
138 * IP numbers with their IP number as hostname, setting noname flag
141 static void cgethost (struct in_addr ipnum, char *string, int check)
143 struct nsrec **current, *new;
144 struct hostent *hostdata;
147 current = &nscache[((ipnum.s_addr + (ipnum.s_addr >> 8) +
148 (ipnum.s_addr >> 16) + (ipnum.s_addr >> 24)) % BUCKETS)];
150 while (*current != NULL && ipnum.s_addr != (*current)->ipnum.s_addr)
151 current = &(*current)->next;
153 if (*current == NULL) {
155 new = (struct nsrec *) malloc(sizeof(struct nsrec));
158 fprintf(stderr, "Insufficient memory\n");
166 hostdata = gethostbyaddr((const char *) &ipnum, sizeof(struct in_addr),
168 if (hostdata == NULL) {
169 if (h_errno > MAX_ERR)
170 errors[UNKNOWN_ERR]++;
173 new->noname = h_errno;
174 name = strdup(inet_ntoa(ipnum));
178 name = strdup(hostdata->h_name);
182 fprintf(stderr, "Insufficient memory\n");
185 hostdata = gethostbyname(name);
186 if (hostdata != NULL) {
189 for (hptr = hostdata->h_addr_list; *hptr != NULL; hptr++)
190 if (((struct in_addr *) (*hptr))->s_addr == ipnum.s_addr)
195 if (hostdata == NULL) {
196 fprintf(stderr, "Bad host: %s != %s\n", name,
198 new->noname = NO_REVERSE;
200 name = strdup(inet_ntoa(ipnum));
201 errors[NO_REVERSE]++;
205 new->hostname = name;
206 if (new->hostname == NULL) {
208 fprintf(stderr, "Insufficient memory\n");
215 /* size of string == MAXDNAME +1 */
216 strncpy(string, (*current)->hostname, MAXDNAME);
217 string[MAXDNAME] = '\0';
221 * prints various statistics to output
224 static void stats (FILE *output)
228 struct nsrec *current;
229 char *errstring[MAX_ERR + 3];
231 for (i = 0; i < MAX_ERR + 3; i++)
232 errstring[i] = "Unknown error";
233 errstring[HOST_NOT_FOUND] = "Host not found";
234 errstring[TRY_AGAIN] = "Try again";
235 errstring[NO_RECOVERY] = "Non recoverable error";
236 errstring[NO_DATA] = "No data record";
237 errstring[NO_ADDRESS] = "No address";
238 errstring[NO_REVERSE] = "No reverse entry";
240 fprintf(output, "logresolve Statistics:\n");
242 fprintf(output, "Entries: %d\n", entries);
243 fprintf(output, " With name : %d\n", withname);
244 fprintf(output, " Resolves : %d\n", resolves);
245 if (errors[HOST_NOT_FOUND])
246 fprintf(output, " - Not found : %d\n", errors[HOST_NOT_FOUND]);
247 if (errors[TRY_AGAIN])
248 fprintf(output, " - Try again : %d\n", errors[TRY_AGAIN]);
250 fprintf(output, " - No data : %d\n", errors[NO_DATA]);
251 if (errors[NO_ADDRESS])
252 fprintf(output, " - No address: %d\n", errors[NO_ADDRESS]);
253 if (errors[NO_REVERSE])
254 fprintf(output, " - No reverse: %d\n", errors[NO_REVERSE]);
255 fprintf(output, "Cache hits : %d\n", cachehits);
256 fprintf(output, "Cache size : %d\n", cachesize);
257 fprintf(output, "Cache buckets : IP number * hostname\n");
259 for (i = 0; i < BUCKETS; i++)
260 for (current = nscache[i]; current != NULL; current = current->next) {
261 ipstring = inet_ntoa(current->ipnum);
262 if (current->noname == 0)
263 fprintf(output, " %3d %15s - %s\n", i, ipstring,
266 if (current->noname > MAX_ERR + 2)
267 fprintf(output, " %3d %15s : Unknown error\n", i,
270 fprintf(output, " %3d %15s : %s\n", i, ipstring,
271 errstring[current->noname]);
278 * gets a line from stdin
281 static int getline (char *s, int n)
285 if (!fgets(s, n, stdin))
287 cp = strchr(s, '\n');
293 int main (int argc, char *argv[])
295 struct in_addr ipnum;
296 char *bar, hoststring[MAXDNAME + 1], line[MAXLINE], *statfile;
300 /* If we apr'ify this code, apr_pool_create/apr_pool_destroy
301 * should perform the WSAStartup/WSACleanup for us.
304 WSAStartup(0x101, &wsaData);
309 for (i = 1; i < argc; i++) {
310 if (strcmp(argv[i], "-c") == 0)
312 else if (strcmp(argv[i], "-s") == 0) {
314 fprintf(stderr, "logresolve: missing filename to -s\n");
321 fprintf(stderr, "Usage: logresolve [-s statfile] [-c] < input > output\n");
326 for (i = 0; i < BUCKETS; i++)
328 for (i = 0; i < MAX_ERR + 2; i++)
331 while (getline(line, MAXLINE)) {
335 if (!apr_isdigit(line[0])) { /* short cut */
340 bar = strchr(line, ' ');
343 ipnum.s_addr = inet_addr(line);
344 if (ipnum.s_addr == 0xffffffffu) {
354 cgethost(ipnum, hoststring, check);
356 printf("%s %s\n", hoststring, bar + 1);
365 if (statfile != NULL) {
367 fp = fopen(statfile, "w");
369 fprintf(stderr, "logresolve: could not open statistics file '%s'\n"