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 Ian Jackson <ian@davenant.greenend.org.uk>
12 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 * This version was originally supplied by Tony Finch, but has been
29 * modified by Ian Jackson as it was incorporated into adns and
33 static const char * const cvsid =
36 #include <sys/types.h>
51 #ifdef ADNS_REGRESS_TEST
52 # include "hredirect.h"
55 /* maximum number of concurrent DNS queries */
56 #define MAXMAXPENDING 64000
57 #define DEFMAXPENDING 2000
59 /* maximum length of a line */
66 static const char *const progname= "adnslogres";
67 static const char *config_text;
69 #define guard_null(str) ((str) ? (str) : "")
71 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
72 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
74 static void msg(const char *fmt, ...) {
77 fprintf(stderr, "%s: ", progname);
79 vfprintf(stderr, fmt, al);
84 static void aargh(const char *cause) {
85 const char *why = strerror(errno);
86 if (!why) why = "Unknown error";
87 msg("%s: %s (%d)", cause, why, errno);
92 * Parse the IP address and convert to a reverse domain name.
94 static char *ipaddr2domain(char *start, char **addr, char **rest) {
95 static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
101 while (!sensible_ctype(isdigit,*ptrs[0]))
103 strcpy(buf, "invalid.");
107 for (i= 1; i < 5; i++) {
109 while (sensible_ctype(isdigit,*ptrs[i]++));
110 if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
111 (i != 4 && ptrs[i][-1] != '.') ||
112 (ptrs[i]-ptrs[i-1] > 4)) {
117 sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
118 ptrs[4]-ptrs[3]-1, ptrs[3],
119 ptrs[3]-ptrs[2]-1, ptrs[2],
120 ptrs[2]-ptrs[1]-1, ptrs[1],
121 ptrs[1]-ptrs[0]-1, ptrs[0]);
127 static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
129 fprintf(outf, "%.*s%s%s", addr - start, start, domain, rest);
132 if (ferror(outf)) aargh("write output");
135 typedef struct logline {
136 struct logline *next;
137 char *start, *addr, *rest;
141 static logline *readline(FILE *inf, adns_state adns, int opts) {
142 static char buf[MAXLINE];
146 if (fgets(buf, MAXLINE, inf)) {
147 str= malloc(sizeof(*line) + strlen(buf) + 1);
148 if (!str) aargh("malloc");
151 line->start= str+sizeof(logline);
152 strcpy(line->start, buf);
153 str= ipaddr2domain(line->start, &line->addr, &line->rest);
154 if (opts & OPT_DEBUG)
155 msg("submitting %.*s -> %s", line->rest-line->addr, guard_null(line->addr), str);
156 if (adns_submit(adns, str, adns_r_ptr,
157 adns_qf_quoteok_cname|adns_qf_cname_loose,
159 aargh("adns_submit");
167 static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
171 logline *head, *tail, *line;
172 adns_initflags initflags;
174 initflags= (opts & OPT_DEBUG) ? adns_if_debug : 0;
176 errno= adns_init_strcfg(&adns, initflags, stderr, config_text);
178 errno= adns_init(&adns, initflags, 0);
180 if (errno) aargh("adns_init");
181 head= tail= readline(inf, adns, opts);
185 if (opts & OPT_DEBUG)
186 msg("%d in queue; checking %.*s", len,
187 head->rest-head->addr, guard_null(head->addr));
188 if (eof || len >= maxpending) {
190 err= adns_wait_poll(adns, &head->query, &answer, NULL);
192 err= adns_wait(adns, &head->query, &answer, NULL);
194 err= adns_check(adns, &head->query, &answer, NULL);
196 if (err == EAGAIN) break;
198 fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err));
201 printline(outf, head->start, head->addr, head->rest,
202 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
203 line= head; head= head->next;
209 line= readline(inf, adns, opts);
211 if (!head) head= line;
212 else tail->next= line;
222 static void printhelp(FILE *file) {
223 fputs("usage: adnslogres [<options>] [<logfile>]\n"
224 " adnslogres --version|--help\n"
225 "options: -c <concurrency> set max number of outstanding queries\n"
226 " -p use poll(2) instead of select(2)\n"
227 " -d turn on debugging\n"
228 " -C <config> use instead of contents of resolv.conf\n",
232 static void usage(void) {
237 int main(int argc, char *argv[]) {
238 int c, opts, maxpending;
242 if (argv[1] && !strncmp(argv[1],"--",2)) {
243 if (!strcmp(argv[1],"--help")) {
245 } else if (!strcmp(argv[1],"--version")) {
246 fputs(VERSION_MESSAGE("adnslogres"),stdout);
250 if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); }
254 maxpending= DEFMAXPENDING;
256 while ((c= getopt(argc, argv, "c:C:dp")) != -1)
259 maxpending= atoi(optarg);
260 if (maxpending < 1 || maxpending > MAXMAXPENDING) {
261 fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending);
285 inf= fopen(*argv, "r");
290 aargh("couldn't open input");
292 proclog(inf, stdout, maxpending, opts);
295 aargh("fclose input");
297 aargh("fclose output");