3 * - a replacement for the Apache logresolve program using adns
6 * This file is part of adns, which is Copyright Ian Jackson
7 * and contributors (see the file INSTALL for full details).
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation.
22 * This version was originally supplied by Tony Finch, but has been
23 * modified by Ian Jackson as it was incorporated into adns and
27 #include <sys/types.h>
42 #ifdef ADNS_REGRESS_TEST
43 # include "hredirect.h"
46 /* maximum number of concurrent DNS queries */
47 #define MAXMAXPENDING 64000
48 #define DEFMAXPENDING 2000
50 /* maximum length of a line */
58 static const char *const progname= "adnslogres";
59 static const char *config_text;
61 #define guard_null(str) ((str) ? (str) : "")
63 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
64 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
66 static void msg(const char *fmt, ...) {
69 fprintf(stderr, "%s: ", progname);
71 vfprintf(stderr, fmt, al);
76 static void aargh(const char *cause) {
77 const char *why = strerror(errno);
78 if (!why) why = "Unknown error";
79 msg("%s: %s (%d)", cause, why, errno);
84 * Parse the IP address and convert to a reverse domain name.
86 static char *ipaddr2domain(char *start, char **addr, char **rest) {
87 static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
93 while (!sensible_ctype(isdigit,*ptrs[0]))
95 strcpy(buf, "invalid.");
99 for (i= 1; i < 5; i++) {
101 while (sensible_ctype(isdigit,*ptrs[i]++));
102 if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
103 (i != 4 && ptrs[i][-1] != '.') ||
104 (ptrs[i]-ptrs[i-1] > 4)) {
109 sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
110 (int)(ptrs[4]-ptrs[3]-1), ptrs[3],
111 (int)(ptrs[3]-ptrs[2]-1), ptrs[2],
112 (int)(ptrs[2]-ptrs[1]-1), ptrs[1],
113 (int)(ptrs[1]-ptrs[0]-1), ptrs[0]);
119 static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
121 fprintf(outf, "%.*s%s%s", (int)(addr - start), start, domain, rest);
124 if (ferror(outf)) aargh("write output");
127 typedef struct logline {
128 struct logline *next;
129 char *start, *addr, *rest;
133 static logline *readline(FILE *inf, adns_state adns, int opts) {
134 static char buf[MAXLINE];
138 if (fgets(buf, MAXLINE, inf)) {
139 str= malloc(sizeof(*line) + strlen(buf) + 1);
140 if (!str) aargh("malloc");
143 line->start= str+sizeof(logline);
144 strcpy(line->start, buf);
145 str= ipaddr2domain(line->start, &line->addr, &line->rest);
146 if (opts & OPT_DEBUG)
147 msg("submitting %.*s -> %s", (int)(line->rest-line->addr), guard_null(line->addr), str);
148 if (adns_submit(adns, str, adns_r_ptr,
149 adns_qf_quoteok_cname|adns_qf_cname_loose,
151 aargh("adns_submit");
159 static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
163 logline *head, *tail, *line;
164 adns_initflags initflags;
166 initflags= (((opts & OPT_DEBUG) ? adns_if_debug : 0) |
167 ((opts & OPT_CHECKC) ? adns_if_checkc_entex : 0));
169 errno= adns_init_strcfg(&adns, initflags, stderr, config_text);
171 errno= adns_init(&adns, initflags, 0);
173 if (errno) aargh("adns_init");
174 head= tail= readline(inf, adns, opts);
178 if (opts & OPT_DEBUG)
179 msg("%d in queue; checking %.*s", len,
180 (int)(head->rest-head->addr), guard_null(head->addr));
181 if (eof || len >= maxpending) {
183 err= adns_wait_poll(adns, &head->query, &answer, NULL);
185 err= adns_wait(adns, &head->query, &answer, NULL);
187 err= adns_check(adns, &head->query, &answer, NULL);
189 if (err == EAGAIN) break;
191 fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err));
194 printline(outf, head->start, head->addr, head->rest,
195 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
196 line= head; head= head->next;
202 line= readline(inf, adns, opts);
204 if (!head) head= line;
205 else tail->next= line;
215 static void printhelp(FILE *file) {
216 fputs("usage: adnslogres [<options>] [<logfile>]\n"
217 " adnslogres --version|--help\n"
218 "options: -c <concurrency> set max number of outstanding queries\n"
219 " -p use poll(2) instead of select(2)\n"
220 " -d turn on debugging\n"
221 " -C <config> use instead of contents of resolv.conf\n",
225 static void usage(void) {
230 int main(int argc, char *argv[]) {
231 int c, opts, maxpending;
237 if (argv[1] && !strcmp(argv[1],"--checkc-freq")) {
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;
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");