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,2014-2016 Ian Jackson
12 * Copyright (C) 2014 Mark Wooding
13 * Copyright (C) 1999-2000,2003,2006 Tony Finch
14 * Copyright (C) 1991 Massachusetts Institute of Technology
15 * (See the file INSTALL for full details.)
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3, or (at your option)
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software Foundation.
30 * This version was originally supplied by Tony Finch, but has been
31 * modified by Ian Jackson as it was incorporated into adns and
35 #include <sys/types.h>
50 #ifdef ADNS_REGRESS_TEST
51 # include "hredirect.h"
54 /* maximum number of concurrent DNS queries */
55 #define MAXMAXPENDING 64000
56 #define DEFMAXPENDING 2000
58 /* maximum length of a line */
65 static const char *const progname= "adnslogres";
66 static const char *config_text;
68 #define guard_null(str) ((str) ? (str) : "")
70 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
71 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
73 static void msg(const char *fmt, ...) {
76 fprintf(stderr, "%s: ", progname);
78 vfprintf(stderr, fmt, al);
83 static void aargh(const char *cause) {
84 const char *why = strerror(errno);
85 if (!why) why = "Unknown error";
86 msg("%s: %s (%d)", cause, why, errno);
91 * Parse the IP address and convert to a reverse domain name.
93 static char *ipaddr2domain(char *start, char **addr, char **rest) {
94 static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
100 while (!sensible_ctype(isdigit,*ptrs[0]))
102 strcpy(buf, "invalid.");
106 for (i= 1; i < 5; i++) {
108 while (sensible_ctype(isdigit,*ptrs[i]++));
109 if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
110 (i != 4 && ptrs[i][-1] != '.') ||
111 (ptrs[i]-ptrs[i-1] > 4)) {
116 sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
117 (int)(ptrs[4]-ptrs[3]-1), ptrs[3],
118 (int)(ptrs[3]-ptrs[2]-1), ptrs[2],
119 (int)(ptrs[2]-ptrs[1]-1), ptrs[1],
120 (int)(ptrs[1]-ptrs[0]-1), ptrs[0]);
126 static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
128 fprintf(outf, "%.*s%s%s", (int)(addr - start), start, domain, rest);
131 if (ferror(outf)) aargh("write output");
134 typedef struct logline {
135 struct logline *next;
136 char *start, *addr, *rest;
140 static logline *readline(FILE *inf, adns_state adns, int opts) {
141 static char buf[MAXLINE];
145 if (fgets(buf, MAXLINE, inf)) {
146 str= malloc(sizeof(*line) + strlen(buf) + 1);
147 if (!str) aargh("malloc");
150 line->start= str+sizeof(logline);
151 strcpy(line->start, buf);
152 str= ipaddr2domain(line->start, &line->addr, &line->rest);
153 if (opts & OPT_DEBUG)
154 msg("submitting %.*s -> %s", (int)(line->rest-line->addr), guard_null(line->addr), str);
155 if (adns_submit(adns, str, adns_r_ptr,
156 adns_qf_quoteok_cname|adns_qf_cname_loose,
158 aargh("adns_submit");
166 static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
170 logline *head, *tail, *line;
171 adns_initflags initflags;
173 initflags= (opts & OPT_DEBUG) ? adns_if_debug : 0;
175 errno= adns_init_strcfg(&adns, initflags, stderr, config_text);
177 errno= adns_init(&adns, initflags, 0);
179 if (errno) aargh("adns_init");
180 head= tail= readline(inf, adns, opts);
184 if (opts & OPT_DEBUG)
185 msg("%d in queue; checking %.*s", len,
186 (int)(head->rest-head->addr), guard_null(head->addr));
187 if (eof || len >= maxpending) {
189 err= adns_wait_poll(adns, &head->query, &answer, NULL);
191 err= adns_wait(adns, &head->query, &answer, NULL);
193 err= adns_check(adns, &head->query, &answer, NULL);
195 if (err == EAGAIN) break;
197 fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err));
200 printline(outf, head->start, head->addr, head->rest,
201 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
202 line= head; head= head->next;
208 line= readline(inf, adns, opts);
210 if (!head) head= line;
211 else tail->next= line;
221 static void printhelp(FILE *file) {
222 fputs("usage: adnslogres [<options>] [<logfile>]\n"
223 " adnslogres --version|--help\n"
224 "options: -c <concurrency> set max number of outstanding queries\n"
225 " -p use poll(2) instead of select(2)\n"
226 " -d turn on debugging\n"
227 " -C <config> use instead of contents of resolv.conf\n",
231 static void usage(void) {
236 int main(int argc, char *argv[]) {
237 int c, opts, maxpending;
241 if (argv[1] && !strncmp(argv[1],"--",2)) {
242 if (!strcmp(argv[1],"--help")) {
244 } else if (!strcmp(argv[1],"--version")) {
245 fputs(VERSION_MESSAGE("adnslogres"),stdout);
249 if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); }
253 maxpending= DEFMAXPENDING;
255 while ((c= getopt(argc, argv, "c:C:dp")) != -1)
258 maxpending= atoi(optarg);
259 if (maxpending < 1 || maxpending > MAXMAXPENDING) {
260 fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending);
284 inf= fopen(*argv, "r");
289 aargh("couldn't open input");
291 proclog(inf, stdout, maxpending, opts);
294 aargh("fclose input");
296 aargh("fclose output");