3 * - a replacement for the Apache logresolve program using adns
7 * Copyright (C) 1999 Tony Finch <fanf@demon.net> <dot@dotat.at>
8 * Copyright (C) 1999 Ian Jackson <ian@davenant.greenend.org.uk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * This version was originally supplied by Tony Finch, but has been
25 * modified by Ian Jackson as it was incorporated into adns.
28 static const char * const cvsid =
29 "$Id: adnslogres.c,v 1.3 1999/09/26 18:59:34 ian Exp $";
31 #include <sys/types.h>
43 /* maximum number of concurrent DNS queries */
44 #define MAXPENDING 1000
46 /* maximum length of a line */
53 static const char *progname;
55 static void aargh(const char *msg) {
56 fprintf(stderr, "%s: %s: %s (%d)\n", progname, msg,
57 strerror(errno) ? strerror(errno) : "Unknown error", errno);
62 * Parse the IP address and convert to a reverse domain name.
64 static char *ipaddr2domain(char *start, char **addr, char **rest) {
65 static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
71 while (!isdigit(*ptrs[0]))
73 strcpy(buf, "invalid.");
77 for (i= 1; i < 5; i ++) {
78 ptrs[i]= strchr(ptrs[i-1], (i == 4) ? ' ' : '.');
79 if (!ptrs[i] || ptrs[i]-ptrs[i-1] > 3) {
85 sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
86 ptrs[4]-ptrs[3]-1, ptrs[3],
87 ptrs[3]-ptrs[2]-1, ptrs[2],
88 ptrs[2]-ptrs[1]-1, ptrs[1],
89 ptrs[1]-ptrs[0]-1, ptrs[0]);
95 static void printline(char *start, char *addr, char *rest, char *domain) {
97 printf("%.*s%s%s", addr - start, start, domain, rest);
100 if (ferror(stdout)) aargh("write output");
103 typedef struct logline {
104 struct logline *next;
105 char *start, *addr, *rest;
109 static logline *readline(adns_state adns, int opts) {
110 static char buf[MAXLINE];
114 if (fgets(buf, MAXLINE, stdin)) {
115 str= malloc(sizeof(*line) + strlen(buf) + 1);
116 if (!str) aargh("malloc");
119 line->start= str+sizeof(logline);
120 strcpy(line->start, buf);
121 str = ipaddr2domain(line->start, &line->addr, &line->rest);
122 if (opts & OPT_DEBUG)
123 fprintf(stderr, "%s: adns_submit %s\n", progname, str);
124 if (adns_submit(adns, str, adns_r_ptr,
125 adns_qf_quoteok_cname|adns_qf_cname_loose,
127 aargh("adns_submit");
135 static void proclog(int opts) {
139 logline *head, *tail, *line;
141 errno= adns_init(&adns, (opts & OPT_DEBUG) ? adns_if_debug : 0, 0);
142 if (errno) aargh("adns_init");
143 head= tail= readline(adns, opts);
146 if (eof || len > MAXPENDING)
148 err= adns_wait_poll(adns, &head->query, &answer, NULL);
150 err= adns_wait(adns, &head->query, &answer, NULL);
152 err= adns_check(adns, &head->query, &answer, NULL);
153 if (err != EWOULDBLOCK) {
154 printline(head->start, head->addr, head->rest,
155 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
156 line= head; head= head->next;
157 free(line); free(answer);
161 line= readline(adns, opts);
177 int main(int argc, char *argv[]) {
183 while ((c= getopt(argc, argv, "dp")) != -1) {
192 fprintf(stderr, "usage: %s [-d] < logfile\n", progname);
201 if (fclose(stdout)) aargh("finish writing output");