3 * - a replacement for the Apache logresolve program using adns
7 * Copyright (C) 1999 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 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>
48 /* maximum number of concurrent DNS queries */
49 #define MAXPENDING 1000
51 /* maximum length of a line */
58 static const char *progname;
60 #define msg(fmt, args...) fprintf(stderr, "%s: " fmt "\n", progname, ##args)
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 aargh(const char *cause) {
67 const char *why = strerror(errno);
68 if (!why) why = "Unknown error";
69 msg("%s: %s (%d)", cause, why, errno);
74 * Parse the IP address and convert to a reverse domain name.
76 static char *ipaddr2domain(char *start, char **addr, char **rest) {
77 static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
83 while (!sensible_ctype(isdigit,*ptrs[0]))
85 strcpy(buf, "invalid.");
89 for (i= 1; i < 5; i++) {
91 while (sensible_ctype(isdigit,*ptrs[i]++));
92 if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
93 (i != 4 && ptrs[i][-1] != '.') ||
94 (ptrs[i]-ptrs[i-1] > 4)) {
99 sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
100 ptrs[4]-ptrs[3]-1, ptrs[3],
101 ptrs[3]-ptrs[2]-1, ptrs[2],
102 ptrs[2]-ptrs[1]-1, ptrs[1],
103 ptrs[1]-ptrs[0]-1, ptrs[0]);
109 static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
111 fprintf(outf, "%.*s%s%s", addr - start, start, domain, rest);
114 if (ferror(outf)) aargh("write output");
117 typedef struct logline {
118 struct logline *next;
119 char *start, *addr, *rest;
123 static logline *readline(FILE *inf, adns_state adns, int opts) {
124 static char buf[MAXLINE];
128 if (fgets(buf, MAXLINE, inf)) {
129 str= malloc(sizeof(*line) + strlen(buf) + 1);
130 if (!str) aargh("malloc");
133 line->start= str+sizeof(logline);
134 strcpy(line->start, buf);
135 str= ipaddr2domain(line->start, &line->addr, &line->rest);
136 if (opts & OPT_DEBUG)
137 msg("submitting %.*s -> %s", line->rest-line->addr, guard_null(line->addr), str);
138 if (adns_submit(adns, str, adns_r_ptr,
139 adns_qf_quoteok_cname|adns_qf_cname_loose,
141 aargh("adns_submit");
149 static void proclog(FILE *inf, FILE *outf, int opts) {
153 logline *head, *tail, *line;
155 errno= adns_init(&adns, (opts & OPT_DEBUG) ? adns_if_debug : 0, 0);
156 if (errno) aargh("adns_init");
157 head= tail= readline(inf, adns, opts);
160 if (opts & OPT_DEBUG)
161 msg("%d in queue; checking %.*s", len,
162 head->rest-head->addr, guard_null(head->addr));
163 if (eof || len > MAXPENDING)
165 err= adns_wait_poll(adns, &head->query, &answer, NULL);
167 err= adns_wait(adns, &head->query, &answer, NULL);
169 err= adns_check(adns, &head->query, &answer, NULL);
171 printline(outf, head->start, head->addr, head->rest,
172 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
173 line= head; head= head->next;
174 free(line); free(answer);
178 line= readline(inf, adns, opts);
194 static void usage(void) {
195 fprintf(stderr, "usage: %s [-d] [-p] [logfile]\n", progname);
199 int main(int argc, char *argv[]) {
203 progname= strrchr(*argv, '/');
210 while ((c= getopt(argc, argv, "dp")) != -1)
229 inf= fopen(*argv, "r");
234 aargh("couldn't open input");
236 proclog(inf, stdout, opts);
239 aargh("fclose input");
241 aargh("fclose output");