842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * http://www.apache.org/licenses/LICENSE-2.0
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * limitations under the License.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd */
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm * logresolve 2.0
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Tom Rathborne - tomr uunet.ca - http://www.uunet.ca/~tomr/
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * UUNET Canada, April 16, 1995
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Rewritten by David Robinson. (drtr ast.cam.ac.uk)
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm * Rewritten again, and ported to APR by Colm MacCarthaigh
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Usage: logresolve [-s filename] [-c] < access_log > new_log
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Arguments:
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * -s filename name of a file to record statistics
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * -c check the DNS for a matching A record for the host.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm * Notes: (For historical interest)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * To generate meaningful statistics from an HTTPD log file, it's good
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * to have the domain name of each machine that accessed your site, but
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * doing this on the fly can slow HTTPD down.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * Compiling NCSA HTTPD with the -DMINIMAL_DNS flag turns IP#->hostname
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * resolution off. Before running your stats program, just run your log
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * file through this program (logresolve) and all of your IP numbers will
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * be resolved into hostnames (where possible).
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * logresolve takes an HTTPD access log (in the COMMON log file format,
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * or any other format that has the IP number/domain name as the first
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * field for that matter), and outputs the same file with all of the
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * domain names looked up. Where no domain name can be found, the IP
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * number is left in.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * To minimize impact on your nameserver, logresolve has its very own
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * internal hash-table cache. This means that each IP number will only
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * be looked up the first time it is found in the log file.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding *
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * The -c option causes logresolve to apply the same check as httpd
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * compiled with -DMAXIMUM_DNS; after finding the hostname from the IP
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * address, it looks up the IP addresses for the hostname and checks
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * that one of these matches the original address.
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm#include "apr.h"
11f2b9c5d2411bb84925addb4c2bf66e3a375383wrowe#include "apr_lib.h"
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm#include "apr_hash.h"
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm#include "apr_getopt.h"
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm#include "apr_strings.h"
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm#include "apr_file_io.h"
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm#include "apr_network_io.h"
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm
1860b2b5f1de31f8cf9d95f1b394fe98c8dbfab7rbb#if APR_HAVE_STDLIB_H
1860b2b5f1de31f8cf9d95f1b394fe98c8dbfab7rbb#include <stdlib.h>
1860b2b5f1de31f8cf9d95f1b394fe98c8dbfab7rbb#endif
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
42575bb15a3ed6084cd1e225aa9251829dbdd258sf#define READ_BUF_SIZE 128*1024
42575bb15a3ed6084cd1e225aa9251829dbdd258sf#define WRITE_BUF_SIZE 128*1024
42575bb15a3ed6084cd1e225aa9251829dbdd258sf#define LINE_BUF_SIZE 128*1024
68fca23366bbf48e2ce03db1c23815cf9f2c80f3takashi
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colmstatic apr_file_t *errfile;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colmstatic const char *shortname = "logresolve";
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colmstatic apr_hash_t *cache;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm/* Statistics */
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int cachehits = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int cachesize = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int entries = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int resolves = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fieldingstatic int withname = 0;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colmstatic int doublefailed = 0;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colmstatic int noreverse = 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding * prints various statistics to output
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm#define NL APR_EOL_STR
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colmstatic void print_statistics (apr_file_t *output)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_printf(output, "logresolve Statistics:" NL);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_printf(output, "Entries: %d" NL, entries);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_printf(output, " With name : %d" NL, withname);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_printf(output, " Resolves : %d" NL, resolves);
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (noreverse) {
17dc8282ea6b3ad1bbc661b498de9ec2e9987edejim apr_file_printf(output, " - No reverse : %d" NL,
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm noreverse);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (doublefailed) {
17dc8282ea6b3ad1bbc661b498de9ec2e9987edejim apr_file_printf(output, " - Double lookup failed : %d" NL,
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm doublefailed);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_printf(output, "Cache hits : %d" NL, cachehits);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_printf(output, "Cache size : %d" NL, cachesize);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding/*
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm * usage info
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding */
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colmstatic void usage(void)
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_printf(errfile,
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm "%s -- Resolve IP-addresses to hostnames in Apache log files." NL
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm "Usage: %s [-s STATFILE] [-c]" NL
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm NL
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm "Options:" NL
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm " -s Record statistics to STATFILE when finished." NL
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm NL
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm " -c Perform double lookups when resolving IP addresses." NL,
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm shortname, shortname);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm exit(1);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm#undef NL
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colmint main(int argc, const char * const argv[])
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding{
2b672ae3a6d190fb62d04f4f47bbdc0a2bde151fcolm apr_file_t * outfile;
2b672ae3a6d190fb62d04f4f47bbdc0a2bde151fcolm apr_file_t * infile;
2b672ae3a6d190fb62d04f4f47bbdc0a2bde151fcolm apr_getopt_t * o;
2b672ae3a6d190fb62d04f4f47bbdc0a2bde151fcolm apr_pool_t * pool;
b76764362f1f69ced135b0f2e1481a26fef04347takashi apr_pool_t *pline;
2b672ae3a6d190fb62d04f4f47bbdc0a2bde151fcolm apr_status_t status;
2b672ae3a6d190fb62d04f4f47bbdc0a2bde151fcolm const char * arg;
2b672ae3a6d190fb62d04f4f47bbdc0a2bde151fcolm char * stats = NULL;
2b672ae3a6d190fb62d04f4f47bbdc0a2bde151fcolm char * inbuffer;
2b672ae3a6d190fb62d04f4f47bbdc0a2bde151fcolm char * outbuffer;
42575bb15a3ed6084cd1e225aa9251829dbdd258sf char * line;
2b672ae3a6d190fb62d04f4f47bbdc0a2bde151fcolm int doublelookups = 0;
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (apr_app_initialize(&argc, &argv, NULL) != APR_SUCCESS) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm return 1;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm atexit(apr_terminate);
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (argc) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm shortname = apr_filepath_name_get(argv[0]);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm return 1;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_open_stderr(&errfile, pool);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_getopt_init(&o, pool, argc, argv);
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm while (1) {
181f74b98ff31aba729c25b66809b9ff28647a3btakashi char opt;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm status = apr_getopt(o, "s:c", &opt, &arg);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (status == APR_EOF) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm break;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm else if (status != APR_SUCCESS) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm usage();
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm else {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm switch (opt) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm case 'c':
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (doublelookups) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm usage();
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm doublelookups = 1;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm break;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm case 's':
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (stats) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm usage();
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm stats = apr_pstrdup(pool, arg);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm break;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm } /* switch */
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm } /* else */
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm } /* while */
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_open_stdout(&outfile, pool);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_open_stdin(&infile, pool);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Allocate two new 10k file buffers */
42575bb15a3ed6084cd1e225aa9251829dbdd258sf if ( (outbuffer = apr_palloc(pool, WRITE_BUF_SIZE)) == NULL
42575bb15a3ed6084cd1e225aa9251829dbdd258sf || (inbuffer = apr_palloc(pool, READ_BUF_SIZE)) == NULL
42575bb15a3ed6084cd1e225aa9251829dbdd258sf || (line = apr_palloc(pool, LINE_BUF_SIZE)) == NULL) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm return 1;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Set the buffers */
e18cfc773d01b1c7e93fabc09f94bd068a53dabetakashi apr_file_buffer_set(infile, inbuffer, READ_BUF_SIZE);
e18cfc773d01b1c7e93fabc09f94bd068a53dabetakashi apr_file_buffer_set(outfile, outbuffer, WRITE_BUF_SIZE);
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm cache = apr_hash_make(pool);
1fa95e3b3bba5cdeec20418683de952dbaf3396dtakashi if(apr_pool_create(&pline, pool) != APR_SUCCESS){
b76764362f1f69ced135b0f2e1481a26fef04347takashi return 1;
b76764362f1f69ced135b0f2e1481a26fef04347takashi }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm
42575bb15a3ed6084cd1e225aa9251829dbdd258sf while (apr_file_gets(line, LINE_BUF_SIZE, infile) == APR_SUCCESS) {
181f74b98ff31aba729c25b66809b9ff28647a3btakashi char *hostname;
181f74b98ff31aba729c25b66809b9ff28647a3btakashi char *space;
181f74b98ff31aba729c25b66809b9ff28647a3btakashi apr_sockaddr_t *ip;
181f74b98ff31aba729c25b66809b9ff28647a3btakashi apr_sockaddr_t *ipdouble;
02c8e5e329cb0652f9444b492df79cca886bb762jorton char dummy[] = " " APR_EOL_STR;
02c8e5e329cb0652f9444b492df79cca886bb762jorton
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (line[0] == '\0') {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm continue;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Count our log entries */
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm entries++;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Check if this could even be an IP address */
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (!apr_isxdigit(line[0]) && line[0] != ':') {
43c3e6a4b559b76b750c245ee95e2782c15b4296jim withname++;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_puts(line, outfile);
e8f95a682820a599fe41b22977010636be5c2717jim continue;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Terminate the line at the next space */
2b672ae3a6d190fb62d04f4f47bbdc0a2bde151fcolm if ((space = strchr(line, ' ')) != NULL) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm *space = '\0';
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
02c8e5e329cb0652f9444b492df79cca886bb762jorton else {
02c8e5e329cb0652f9444b492df79cca886bb762jorton space = dummy;
02c8e5e329cb0652f9444b492df79cca886bb762jorton }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* See if we have it in our cache */
2b672ae3a6d190fb62d04f4f47bbdc0a2bde151fcolm hostname = (char *) apr_hash_get(cache, line, APR_HASH_KEY_STRING);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (hostname) {
02c8e5e329cb0652f9444b492df79cca886bb762jorton apr_file_printf(outfile, "%s %s", hostname, space + 1);
76afc31650e2621ed0882c45bbb900e550f9c96acolm cachehits++;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm continue;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Parse the IP address */
b76764362f1f69ced135b0f2e1481a26fef04347takashi status = apr_sockaddr_info_get(&ip, line, APR_UNSPEC, 0, 0, pline);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (status != APR_SUCCESS) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Not an IP address */
76afc31650e2621ed0882c45bbb900e550f9c96acolm withname++;
02c8e5e329cb0652f9444b492df79cca886bb762jorton *space = ' ';
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_puts(line, outfile);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm continue;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* This does not make much sense, but historically "resolves" means
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm * "parsed as an IP address". It does not mean we actually resolved
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm * the IP address into a hostname.
e8f95a682820a599fe41b22977010636be5c2717jim */
76afc31650e2621ed0882c45bbb900e550f9c96acolm resolves++;
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* From here on our we cache each result, even if it was not
e8f95a682820a599fe41b22977010636be5c2717jim * succesful
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm */
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm cachesize++;
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Try and perform a reverse lookup */
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm status = apr_getnameinfo(&hostname, ip, 0) != APR_SUCCESS;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (status || hostname == NULL) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Could not perform a reverse lookup */
02c8e5e329cb0652f9444b492df79cca886bb762jorton *space = ' ';
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_puts(line, outfile);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm noreverse++;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Add to cache */
02c8e5e329cb0652f9444b492df79cca886bb762jorton *space = '\0';
e8f95a682820a599fe41b22977010636be5c2717jim apr_hash_set(cache, line, APR_HASH_KEY_STRING,
f3b944f742679424d2233dbcfb573c21abed184ftakashi apr_pstrdup(apr_hash_pool_get(cache), line));
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm continue;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Perform a double lookup */
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (doublelookups) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Do a forward lookup on our hostname, and see if that matches our
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm * original IP address.
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm */
e8f95a682820a599fe41b22977010636be5c2717jim status = apr_sockaddr_info_get(&ipdouble, hostname, ip->family, 0,
b76764362f1f69ced135b0f2e1481a26fef04347takashi 0, pline);
e8f95a682820a599fe41b22977010636be5c2717jim if (status == APR_SUCCESS ||
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm memcmp(ipdouble->ipaddr_ptr, ip->ipaddr_ptr, ip->ipaddr_len)) {
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Double-lookup failed */
02c8e5e329cb0652f9444b492df79cca886bb762jorton *space = ' ';
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_puts(line, outfile);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm doublefailed++;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Add to cache */
02c8e5e329cb0652f9444b492df79cca886bb762jorton *space = '\0';
e8f95a682820a599fe41b22977010636be5c2717jim apr_hash_set(cache, line, APR_HASH_KEY_STRING,
f3b944f742679424d2233dbcfb573c21abed184ftakashi apr_pstrdup(apr_hash_pool_get(cache), line));
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm continue;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Outout the resolved name */
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_printf(outfile, "%s %s", hostname, space + 1);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Store it in the cache */
e8f95a682820a599fe41b22977010636be5c2717jim apr_hash_set(cache, line, APR_HASH_KEY_STRING,
f3b944f742679424d2233dbcfb573c21abed184ftakashi apr_pstrdup(apr_hash_pool_get(cache), hostname));
b76764362f1f69ced135b0f2e1481a26fef04347takashi
b76764362f1f69ced135b0f2e1481a26fef04347takashi apr_pool_clear(pline);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
db848a70422b56cf0f15a47c37e17ebe05e2ce04stoddard
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm /* Flush any remaining output */
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_flush(outfile);
e8f95a682820a599fe41b22977010636be5c2717jim
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm if (stats) {
181f74b98ff31aba729c25b66809b9ff28647a3btakashi apr_file_t *statsfile;
e8f95a682820a599fe41b22977010636be5c2717jim if (apr_file_open(&statsfile, stats,
e8f95a682820a599fe41b22977010636be5c2717jim APR_FOPEN_WRITE | APR_FOPEN_CREATE | APR_FOPEN_TRUNCATE,
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm APR_OS_DEFAULT, pool) != APR_SUCCESS) {
e8f95a682820a599fe41b22977010636be5c2717jim apr_file_printf(errfile, "%s: Could not open %s for writing.",
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm shortname, stats);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm return 1;
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm }
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm print_statistics(statsfile);
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm apr_file_close(statsfile);
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding }
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding
0b4b04d8621478ba59f0a6ba2950ddc02ab92b58colm return 0;
09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1fielding}