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.2 1999/08/15 16:15:52 ian Exp $";
31 #include <sys/types.h>
42 /* maximum number of concurrent DNS queries */
43 #define MAXPENDING 1000
45 /* maximum length of a line */
48 static const char *progname;
50 static void aargh(const char *msg) {
51 fprintf(stderr, "%s: %s: %s (%d)\n", progname, msg,
52 strerror(errno) ? strerror(errno) : "Unknown error", errno);
57 * Parse the IP address and convert to a reverse domain name.
59 static void ipaddr2domain(char *start, char **addr, char **rest, char **domain) {
60 static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
64 for (ptrs[0]= start; !isdigit(*ptrs[0]); ptrs[0]++)
67 for (i= 1; i < 5; i ++) {
68 ptrs[i]= strchr(ptrs[i-1], (i == 4) ? ' ' : '.');
69 if (!ptrs[i] || ptrs[i]-ptrs[i-1] > 3)
74 sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
75 ptrs[4]-ptrs[3]-1, ptrs[3],
76 ptrs[3]-ptrs[2]-1, ptrs[2],
77 ptrs[2]-ptrs[1]-1, ptrs[1],
78 ptrs[1]-ptrs[0]-1, ptrs[0]);
84 strcpy(buf, "invalid.");
90 static void printline(char *start, char *addr, char *rest, char *domain) {
92 printf("%.*s%s%s", addr - start, start, domain, rest);
95 if (ferror(stdout)) aargh("write output");
98 typedef struct logline {
100 char *start, *addr, *rest;
104 static logline *readline(adns_state adns) {
105 static char buf[MAXLINE];
109 if (fgets(buf, MAXLINE, stdin)) {
110 str= malloc(sizeof(*line) + strlen(buf) + 1);
111 if (!str) aargh("malloc");
114 line->start= str+sizeof(logline);
115 strcpy(line->start, buf);
116 ipaddr2domain(line->start, &line->addr, &line->rest, &str);
117 if (adns_submit(adns, str, adns_r_ptr,
118 adns_qf_quoteok_cname|adns_qf_cname_loose,
120 aargh("adns_submit");
128 static void proclog(void) {
132 logline *head, *tail, *line;
134 errno= adns_init(&adns, 0, 0);
135 if (errno) aargh("adns_init");
136 head= tail= readline(adns);
139 if (eof || len > MAXPENDING)
140 err= adns_wait(adns, &head->query, &answer, NULL);
142 err= adns_check(adns, &head->query, &answer, NULL);
143 if (err != EWOULDBLOCK) {
144 printline(head->start, head->addr, head->rest,
145 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
146 line= head; head= head->next;
147 free(line); free(answer);
151 line= readline(adns);
167 int main(int argc, char *argv[]) {
170 if (fclose(stdout)) aargh("finish writing output");