~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to support/logresolve.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Licensed to the Apache Software Foundation (ASF) under one or more
 
2
 * contributor license agreements.  See the NOTICE file distributed with
 
3
 * this work for additional information regarding copyright ownership.
 
4
 * The ASF licenses this file to You under the Apache License, Version 2.0
 
5
 * (the "License"); you may not use this file except in compliance with
 
6
 * the License.  You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
/*
 
18
 * logresolve 1.1
 
19
 *
 
20
 * Tom Rathborne - tomr uunet.ca - http://www.uunet.ca/~tomr/
 
21
 * UUNET Canada, April 16, 1995
 
22
 *
 
23
 * Rewritten by David Robinson. (drtr ast.cam.ac.uk)
 
24
 *
 
25
 * Usage: logresolve [-s filename] [-c] < access_log > new_log
 
26
 *
 
27
 * Arguments:
 
28
 *    -s filename     name of a file to record statistics
 
29
 *    -c              check the DNS for a matching A record for the host.
 
30
 *
 
31
 * Notes:
 
32
 *
 
33
 * To generate meaningful statistics from an HTTPD log file, it's good
 
34
 * to have the domain name of each machine that accessed your site, but
 
35
 * doing this on the fly can slow HTTPD down.
 
36
 *
 
37
 * Compiling NCSA HTTPD with the -DMINIMAL_DNS flag turns IP#->hostname
 
38
 * resolution off. Before running your stats program, just run your log
 
39
 * file through this program (logresolve) and all of your IP numbers will
 
40
 * be resolved into hostnames (where possible).
 
41
 *
 
42
 * logresolve takes an HTTPD access log (in the COMMON log file format,
 
43
 * or any other format that has the IP number/domain name as the first
 
44
 * field for that matter), and outputs the same file with all of the
 
45
 * domain names looked up. Where no domain name can be found, the IP
 
46
 * number is left in.
 
47
 *
 
48
 * To minimize impact on your nameserver, logresolve has its very own
 
49
 * internal hash-table cache. This means that each IP number will only
 
50
 * be looked up the first time it is found in the log file.
 
51
 *
 
52
 * The -c option causes logresolve to apply the same check as httpd
 
53
 * compiled with -DMAXIMUM_DNS; after finding the hostname from the IP
 
54
 * address, it looks up the IP addresses for the hostname and checks
 
55
 * that one of these matches the original address.
 
56
 */
 
57
 
 
58
#include "apr_lib.h"
 
59
#if APR_HAVE_STDIO_H
 
60
#include <stdio.h>
 
61
#endif
 
62
#if APR_HAVE_STDLIB_H
 
63
#include <stdlib.h>
 
64
#endif
 
65
#if APR_HAVE_CTYPE_H
 
66
#include <ctype.h>
 
67
#endif
 
68
#if APR_HAVE_NETDB_H
 
69
#include <netdb.h>
 
70
#endif
 
71
#if APR_HAVE_NETINET_IN_H
 
72
#include <netinet/in.h>
 
73
#endif
 
74
#if APR_HAVE_STRING_H
 
75
#include <string.h>
 
76
#endif
 
77
#if APR_HAVE_SYS_SOCKET_H
 
78
#include <sys/socket.h>
 
79
#endif
 
80
#if APR_HAVE_ARPA_INET_H
 
81
#include <arpa/inet.h>
 
82
#endif
 
83
 
 
84
static void cgethost(struct in_addr ipnum, char *string, int check);
 
85
static int get_line(char *s, int n);
 
86
static void stats(FILE *output);
 
87
 
 
88
#ifdef BEOS
 
89
#define NO_ADDRESS NO_DATA
 
90
#endif
 
91
 
 
92
 
 
93
/* maximum line length */
 
94
#ifndef MAXLINE
 
95
#define MAXLINE 1024
 
96
#endif
 
97
 
 
98
/* maximum length of a domain name */
 
99
#ifndef MAXDNAME
 
100
#define MAXDNAME 256
 
101
#endif
 
102
 
 
103
/* number of buckets in cache hash apr_table_t */
 
104
#define BUCKETS 256
 
105
 
 
106
/*
 
107
 * struct nsrec - record of nameservice for cache linked list
 
108
 *
 
109
 * ipnum - IP number hostname - hostname noname - nonzero if IP number has no
 
110
 * hostname, i.e. hostname=IP number
 
111
 */
 
112
 
 
113
struct nsrec {
 
114
    struct in_addr ipnum;
 
115
    char *hostname;
 
116
    int noname;
 
117
    struct nsrec *next;
 
118
}    *nscache[BUCKETS];
 
119
 
 
120
/*
 
121
 * statistics - obvious
 
122
 */
 
123
 
 
124
#ifndef h_errno
 
125
#ifdef __CYGWIN__
 
126
extern __declspec(dllimport) int h_errno;
 
127
#else
 
128
extern int h_errno; /* some machines don't have this in their headers */
 
129
#endif
 
130
#endif
 
131
 
 
132
/* largest value for h_errno */
 
133
 
 
134
#define MAX_ERR (NO_ADDRESS)
 
135
#define UNKNOWN_ERR (MAX_ERR+1)
 
136
#define NO_REVERSE  (MAX_ERR+2)
 
137
 
 
138
static int cachehits = 0;
 
139
static int cachesize = 0;
 
140
static int entries = 0;
 
141
static int resolves = 0;
 
142
static int withname = 0;
 
143
static int errors[MAX_ERR + 3];
 
144
 
 
145
/*
 
146
 * cgethost - gets hostname by IP address, caching, and adding unresolvable
 
147
 * IP numbers with their IP number as hostname, setting noname flag
 
148
 */
 
149
 
 
150
static void cgethost (struct in_addr ipnum, char *string, int check)
 
151
{
 
152
    struct nsrec **current, *new;
 
153
    struct hostent *hostdata;
 
154
    char *name;
 
155
 
 
156
    current = &nscache[((ipnum.s_addr + (ipnum.s_addr >> 8) +
 
157
                         (ipnum.s_addr >> 16) + (ipnum.s_addr >> 24)) % BUCKETS)];
 
158
 
 
159
    while (*current != NULL && ipnum.s_addr != (*current)->ipnum.s_addr)
 
160
        current = &(*current)->next;
 
161
 
 
162
    if (*current == NULL) {
 
163
        cachesize++;
 
164
        new = (struct nsrec *) malloc(sizeof(struct nsrec));
 
165
        if (new == NULL) {
 
166
            perror("malloc");
 
167
            fprintf(stderr, "Insufficient memory\n");
 
168
            exit(1);
 
169
        }
 
170
        *current = new;
 
171
        new->next = NULL;
 
172
 
 
173
        new->ipnum = ipnum;
 
174
 
 
175
        hostdata = gethostbyaddr((const char *) &ipnum, sizeof(struct in_addr),
 
176
                                 AF_INET);
 
177
        if (hostdata == NULL) {
 
178
            if (h_errno > MAX_ERR)
 
179
                errors[UNKNOWN_ERR]++;
 
180
            else
 
181
                errors[h_errno]++;
 
182
            new->noname = h_errno;
 
183
            name = strdup(inet_ntoa(ipnum));
 
184
        }
 
185
        else {
 
186
            new->noname = 0;
 
187
            name = strdup(hostdata->h_name);
 
188
            if (check) {
 
189
                if (name == NULL) {
 
190
                    perror("strdup");
 
191
                    fprintf(stderr, "Insufficient memory\n");
 
192
                    exit(1);
 
193
                }
 
194
                hostdata = gethostbyname(name);
 
195
                if (hostdata != NULL) {
 
196
                    char **hptr;
 
197
 
 
198
                    for (hptr = hostdata->h_addr_list; *hptr != NULL; hptr++)
 
199
                        if (((struct in_addr *) (*hptr))->s_addr == ipnum.s_addr)
 
200
                            break;
 
201
                    if (*hptr == NULL)
 
202
                        hostdata = NULL;
 
203
                }
 
204
                if (hostdata == NULL) {
 
205
                    fprintf(stderr, "Bad host: %s != %s\n", name,
 
206
                            inet_ntoa(ipnum));
 
207
                    new->noname = NO_REVERSE;
 
208
                    free(name);
 
209
                    name = strdup(inet_ntoa(ipnum));
 
210
                    errors[NO_REVERSE]++;
 
211
                }
 
212
            }
 
213
        }
 
214
        new->hostname = name;
 
215
        if (new->hostname == NULL) {
 
216
            perror("strdup");
 
217
            fprintf(stderr, "Insufficient memory\n");
 
218
            exit(1);
 
219
        }
 
220
    }
 
221
    else
 
222
        cachehits++;
 
223
 
 
224
    /* size of string == MAXDNAME +1 */
 
225
    strncpy(string, (*current)->hostname, MAXDNAME);
 
226
    string[MAXDNAME] = '\0';
 
227
}
 
228
 
 
229
/*
 
230
 * prints various statistics to output
 
231
 */
 
232
 
 
233
static void stats (FILE *output)
 
234
{
 
235
    int i;
 
236
    char *ipstring;
 
237
    struct nsrec *current;
 
238
    char *errstring[MAX_ERR + 3];
 
239
 
 
240
    for (i = 0; i < MAX_ERR + 3; i++)
 
241
        errstring[i] = "Unknown error";
 
242
    errstring[HOST_NOT_FOUND] = "Host not found";
 
243
    errstring[TRY_AGAIN] = "Try again";
 
244
    errstring[NO_RECOVERY] = "Non recoverable error";
 
245
    errstring[NO_DATA] = "No data record";
 
246
    errstring[NO_ADDRESS] = "No address";
 
247
    errstring[NO_REVERSE] = "No reverse entry";
 
248
 
 
249
    fprintf(output, "logresolve Statistics:\n");
 
250
 
 
251
    fprintf(output, "Entries: %d\n", entries);
 
252
    fprintf(output, "    With name   : %d\n", withname);
 
253
    fprintf(output, "    Resolves    : %d\n", resolves);
 
254
    if (errors[HOST_NOT_FOUND])
 
255
        fprintf(output, "    - Not found : %d\n", errors[HOST_NOT_FOUND]);
 
256
    if (errors[TRY_AGAIN])
 
257
        fprintf(output, "    - Try again : %d\n", errors[TRY_AGAIN]);
 
258
    if (errors[NO_DATA])
 
259
        fprintf(output, "    - No data   : %d\n", errors[NO_DATA]);
 
260
    if (errors[NO_ADDRESS])
 
261
        fprintf(output, "    - No address: %d\n", errors[NO_ADDRESS]);
 
262
    if (errors[NO_REVERSE])
 
263
        fprintf(output, "    - No reverse: %d\n", errors[NO_REVERSE]);
 
264
    fprintf(output, "Cache hits      : %d\n", cachehits);
 
265
    fprintf(output, "Cache size      : %d\n", cachesize);
 
266
    fprintf(output, "Cache buckets   :     IP number * hostname\n");
 
267
 
 
268
    for (i = 0; i < BUCKETS; i++)
 
269
        for (current = nscache[i]; current != NULL; current = current->next) {
 
270
            ipstring = inet_ntoa(current->ipnum);
 
271
            if (current->noname == 0)
 
272
                fprintf(output, "  %3d  %15s - %s\n", i, ipstring,
 
273
                        current->hostname);
 
274
            else {
 
275
                if (current->noname > MAX_ERR + 2)
 
276
                    fprintf(output, "  %3d  %15s : Unknown error\n", i,
 
277
                            ipstring);
 
278
                else
 
279
                    fprintf(output, "  %3d  %15s : %s\n", i, ipstring,
 
280
                            errstring[current->noname]);
 
281
            }
 
282
        }
 
283
}
 
284
 
 
285
 
 
286
/*
 
287
 * gets a line from stdin
 
288
 */
 
289
 
 
290
static int get_line (char *s, int n)
 
291
{
 
292
    char *cp;
 
293
 
 
294
    if (!fgets(s, n, stdin))
 
295
        return (0);
 
296
    cp = strchr(s, '\n');
 
297
    if (cp)
 
298
        *cp = '\0';
 
299
    return (1);
 
300
}
 
301
 
 
302
int main (int argc, char *argv[])
 
303
{
 
304
    struct in_addr ipnum;
 
305
    char *bar, hoststring[MAXDNAME + 1], line[MAXLINE], *statfile;
 
306
    int i, check;
 
307
 
 
308
#if defined(WIN32) || (defined(NETWARE) && defined(USE_WINSOCK))
 
309
    /*  If we apr'ify this code, apr_pool_create/apr_pool_destroy
 
310
     *  should perform the WSAStartup/WSACleanup for us.
 
311
     */
 
312
    WSADATA wsaData;
 
313
    WSAStartup(MAKEWORD(2, 0), &wsaData);
 
314
#endif
 
315
 
 
316
    check = 0;
 
317
    statfile = NULL;
 
318
    for (i = 1; i < argc; i++) {
 
319
        if (strcmp(argv[i], "-c") == 0)
 
320
            check = 1;
 
321
        else if (strcmp(argv[i], "-s") == 0) {
 
322
            if (i == argc - 1) {
 
323
                fprintf(stderr, "logresolve: missing filename to -s\n");
 
324
                exit(1);
 
325
            }
 
326
            i++;
 
327
            statfile = argv[i];
 
328
        }
 
329
        else {
 
330
            fprintf(stderr, "Usage: logresolve [-s statfile] [-c] < input > output\n");
 
331
            exit(0);
 
332
        }
 
333
    }
 
334
 
 
335
    for (i = 0; i < BUCKETS; i++)
 
336
        nscache[i] = NULL;
 
337
    for (i = 0; i < MAX_ERR + 2; i++)
 
338
        errors[i] = 0;
 
339
 
 
340
    while (get_line(line, MAXLINE)) {
 
341
        if (line[0] == '\0')
 
342
            continue;
 
343
        entries++;
 
344
        if (!apr_isdigit(line[0])) {    /* short cut */
 
345
            puts(line);
 
346
            withname++;
 
347
            continue;
 
348
        }
 
349
        bar = strchr(line, ' ');
 
350
        if (bar != NULL)
 
351
            *bar = '\0';
 
352
        ipnum.s_addr = inet_addr(line);
 
353
        if (ipnum.s_addr == 0xffffffffu) {
 
354
            if (bar != NULL)
 
355
                *bar = ' ';
 
356
            puts(line);
 
357
            withname++;
 
358
            continue;
 
359
        }
 
360
 
 
361
        resolves++;
 
362
 
 
363
        cgethost(ipnum, hoststring, check);
 
364
        if (bar != NULL)
 
365
            printf("%s %s\n", hoststring, bar + 1);
 
366
        else
 
367
            puts(hoststring);
 
368
    }
 
369
 
 
370
#if defined(WIN32) || (defined(NETWARE) && defined(USE_WINSOCK))
 
371
     WSACleanup();
 
372
#endif
 
373
 
 
374
    if (statfile != NULL) {
 
375
        FILE *fp;
 
376
        fp = fopen(statfile, "w");
 
377
        if (fp == NULL) {
 
378
            fprintf(stderr, "logresolve: could not open statistics file '%s'\n"
 
379
                    ,statfile);
 
380
            exit(1);
 
381
        }
 
382
        stats(fp);
 
383
        fclose(fp);
 
384
    }
 
385
 
 
386
    return (0);
 
387
}