3 * - a replacement for the Apache logresolve program using adns
7 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
8 * Copyright (C) 1999-2000 Ian Jackson <ian@davenant.greenend.org.uk>
10 * It is part of adns, which is
11 * Copyright (C) 1997-2000,2003,2006 Ian Jackson
12 * Copyright (C) 1999-2000,2003,2006 Tony Finch
13 * Copyright (C) 1991 Massachusetts Institute of Technology
14 * (See the file INSTALL for full details.)
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3, or (at your option)
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation.
29 * This version was originally supplied by Tony Finch, but has been
30 * modified by Ian Jackson as it was incorporated into adns and
34 static const char * const cvsid =
37 #include <sys/types.h>
52 #ifdef ADNS_REGRESS_TEST
53 # include "hredirect.h"
56 /* maximum number of concurrent DNS queries */
57 #define MAXMAXPENDING 64000
58 #define DEFMAXPENDING 2000
60 /* maximum length of a line */
67 static const char *const progname= "adnslogres";
68 static const char *config_text;
70 #define guard_null(str) ((str) ? (str) : "")
72 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
73 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
75 static void msg(const char *fmt, ...) {
78 fprintf(stderr, "%s: ", progname);
80 vfprintf(stderr, fmt, al);
85 static void aargh(const char *cause) {
86 const char *why = strerror(errno);
87 if (!why) why = "Unknown error";
88 msg("%s: %s (%d)", cause, why, errno);
93 * Parse the IP address and convert to a reverse domain name.
95 static char *ipaddr2domain(char *start, char **addr, char **rest) {
96 static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
102 while (!sensible_ctype(isdigit,*ptrs[0]))
104 strcpy(buf, "invalid.");
108 for (i= 1; i < 5; i++) {
110 while (sensible_ctype(isdigit,*ptrs[i]++));
111 if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
112 (i != 4 && ptrs[i][-1] != '.') ||
113 (ptrs[i]-ptrs[i-1] > 4)) {
118 sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
119 (int)(ptrs[4]-ptrs[3]-1), ptrs[3],
120 (int)(ptrs[3]-ptrs[2]-1), ptrs[2],
121 (int)(ptrs[2]-ptrs[1]-1), ptrs[1],
122 (int)(ptrs[1]-ptrs[0]-1), ptrs[0]);
128 static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
130 fprintf(outf, "%.*s%s%s", (int)(addr - start), start, domain, rest);
133 if (ferror(outf)) aargh("write output");
136 typedef struct logline {
137 struct logline *next;
138 char *start, *addr, *rest;
142 static logline *readline(FILE *inf, adns_state adns, int opts) {
143 static char buf[MAXLINE];
147 if (fgets(buf, MAXLINE, inf)) {
148 str= malloc(sizeof(*line) + strlen(buf) + 1);
149 if (!str) aargh("malloc");
152 line->start= str+sizeof(logline);
153 strcpy(line->start, buf);
154 str= ipaddr2domain(line->start, &line->addr, &line->rest);
155 if (opts & OPT_DEBUG)
156 msg("submitting %.*s -> %s", (int)(line->rest-line->addr), guard_null(line->addr), str);
157 if (adns_submit(adns, str, adns_r_ptr,
158 adns_qf_quoteok_cname|adns_qf_cname_loose,
160 aargh("adns_submit");
168 static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
172 logline *head, *tail, *line;
173 adns_initflags initflags;
175 initflags= (opts & OPT_DEBUG) ? adns_if_debug : 0;
177 errno= adns_init_strcfg(&adns, initflags, stderr, config_text);
179 errno= adns_init(&adns, initflags, 0);
181 if (errno) aargh("adns_init");
182 head= tail= readline(inf, adns, opts);
186 if (opts & OPT_DEBUG)
187 msg("%d in queue; checking %.*s", len,
188 (int)(head->rest-head->addr), guard_null(head->addr));
189 if (eof || len >= maxpending) {
191 err= adns_wait_poll(adns, &head->query, &answer, NULL);
193 err= adns_wait(adns, &head->query, &answer, NULL);
195 err= adns_check(adns, &head->query, &answer, NULL);
197 if (err == EAGAIN) break;
199 fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err));
202 printline(outf, head->start, head->addr, head->rest,
203 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
204 line= head; head= head->next;
210 line= readline(inf, adns, opts);
212 if (!head) head= line;
213 else tail->next= line;
223 static void printhelp(FILE *file) {
224 fputs("usage: adnslogres [<options>] [<logfile>]\n"
225 " adnslogres --version|--help\n"
226 "options: -c <concurrency> set max number of outstanding queries\n"
227 " -p use poll(2) instead of select(2)\n"
228 " -d turn on debugging\n"
229 " -C <config> use instead of contents of resolv.conf\n",
233 static void usage(void) {
238 int main(int argc, char *argv[]) {
239 int c, opts, maxpending;
243 if (argv[1] && !strncmp(argv[1],"--",2)) {
244 if (!strcmp(argv[1],"--help")) {
246 } else if (!strcmp(argv[1],"--version")) {
247 fputs(VERSION_MESSAGE("adnslogres"),stdout);
251 if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); }
255 maxpending= DEFMAXPENDING;
257 while ((c= getopt(argc, argv, "c:C:dp")) != -1)
260 maxpending= atoi(optarg);
261 if (maxpending < 1 || maxpending > MAXMAXPENDING) {
262 fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending);
286 inf= fopen(*argv, "r");
291 aargh("couldn't open input");
293 proclog(inf, stdout, maxpending, opts);
296 aargh("fclose input");
298 aargh("fclose output");