chiark / gitweb /
Changes as from fanf in his message of 11th May 2000.
[adns.git] / client / adnslogres.c
1 /*
2  * adnslogres.c
3  * - a replacement for the Apache logresolve program using adns
4  */
5 /*
6  *  This file is
7  *   Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
8  *   Copyright (C) 1999-2000 Ian Jackson <ian@davenant.greenend.org.uk>
9  *
10  *  It is part of adns, which is
11  *    Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
12  *    Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
13  *  
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)
17  *  any later version.
18  *  
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.
23  *  
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.
27  *
28  *  This version was originally supplied by Tony Finch, but has been
29  *  modified by Ian Jackson as it was incorporated into adns and
30  *  subsequently.
31  */
32
33 static const char * const cvsid =
34         "$Id$";
35
36 #include <sys/types.h>
37 #include <sys/time.h>
38
39 #include <unistd.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <ctype.h>
44 #include <errno.h>
45 #include <stdarg.h>
46
47 #include "config.h"
48 #include "adns.h"
49
50 /* maximum number of concurrent DNS queries */
51 #define MAXPENDING 64000
52 #define MAXPENDING 64000
53
54 /* maximum length of a line */
55 #define MAXLINE 1024
56
57 /* option flags */
58 #define OPT_DEBUG 1
59 #define OPT_POLL 2
60
61 static const char *progname;
62
63 #define guard_null(str) ((str) ? (str) : "")
64
65 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
66   /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
67
68 static void msg(const char *fmt, ...) {
69   va_list al;
70
71   fprintf(stderr, "%s: ", progname);
72   va_start(al,fmt);
73   vfprintf(stderr, fmt, al);
74   va_end(al);
75   fputc('\n',stderr);
76 }
77
78 static void aargh(const char *cause) {
79   const char *why = strerror(errno);
80   if (!why) why = "Unknown error";
81   msg("%s: %s (%d)", cause, why, errno);
82   exit(1);
83 }
84
85 /*
86  * Parse the IP address and convert to a reverse domain name.
87  */
88 static char *ipaddr2domain(char *start, char **addr, char **rest) {
89   static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
90   char *ptrs[5];
91   int i;
92
93   ptrs[0]= start;
94 retry:
95   while (!sensible_ctype(isdigit,*ptrs[0]))
96     if (!*ptrs[0]++) {
97       strcpy(buf, "invalid.");
98       *addr= *rest= NULL;
99       return buf;
100     }
101   for (i= 1; i < 5; i++) {
102     ptrs[i]= ptrs[i-1];
103     while (sensible_ctype(isdigit,*ptrs[i]++));
104     if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
105         (i != 4 && ptrs[i][-1] != '.') ||
106         (ptrs[i]-ptrs[i-1] > 4)) {
107       ptrs[0]= ptrs[i]-1;
108       goto retry;
109     }
110   }
111   sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
112           ptrs[4]-ptrs[3]-1, ptrs[3],
113           ptrs[3]-ptrs[2]-1, ptrs[2],
114           ptrs[2]-ptrs[1]-1, ptrs[1],
115           ptrs[1]-ptrs[0]-1, ptrs[0]);
116   *addr= ptrs[0];
117   *rest= ptrs[4]-1;
118   return buf;
119 }
120
121 static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
122   if (domain)
123     fprintf(outf, "%.*s%s%s", addr - start, start, domain, rest);
124   else
125     fputs(start, outf);
126   if (ferror(outf)) aargh("write output");
127 }
128
129 typedef struct logline {
130   struct logline *next;
131   char *start, *addr, *rest;
132   adns_query query;
133 } logline;
134
135 static logline *readline(FILE *inf, adns_state adns, int opts) {
136   static char buf[MAXLINE];
137   char *str;
138   logline *line;
139
140   if (fgets(buf, MAXLINE, inf)) {
141     str= malloc(sizeof(*line) + strlen(buf) + 1);
142     if (!str) aargh("malloc");
143     line= (logline*)str;
144     line->next= NULL;
145     line->start= str+sizeof(logline);
146     strcpy(line->start, buf);
147     str= ipaddr2domain(line->start, &line->addr, &line->rest);
148     if (opts & OPT_DEBUG)
149       msg("submitting %.*s -> %s", line->rest-line->addr, guard_null(line->addr), str);
150     if (adns_submit(adns, str, adns_r_ptr,
151                     adns_qf_quoteok_cname|adns_qf_cname_loose,
152                     NULL, &line->query))
153       aargh("adns_submit");
154     return line;
155   }
156   if (!feof(inf))
157     aargh("fgets");
158   return NULL;
159 }
160         
161 static void proclog(FILE *inf, FILE *outf, int opts) {
162   int eof, err, len;
163   adns_state adns;
164   adns_answer *answer;
165   logline *head, *tail, *line;
166
167   errno= adns_init(&adns, (opts & OPT_DEBUG) ? adns_if_debug : 0, 0);
168   if (errno) aargh("adns_init");
169   head= tail= readline(inf, adns, opts);
170   len= 1; eof= 0;
171   while (head) {
172     if (opts & OPT_DEBUG)
173       msg("%d in queue; checking %.*s", len,
174           head->rest-head->addr, guard_null(head->addr));
175     if (eof || len > MAXPENDING) {
176       if (opts & OPT_POLL)
177         err= adns_wait_poll(adns, &head->query, &answer, NULL);
178       else
179         err= adns_wait(adns, &head->query, &answer, NULL);
180     } else {
181       err= adns_check(adns, &head->query, &answer, NULL);
182     }
183     if (err != EAGAIN) {
184       printline(outf, head->start, head->addr, head->rest,
185                 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
186       line= head; head= head->next;
187       free(line); free(answer);
188       len--;
189     }
190     if (!eof) {
191       line= readline(inf, adns, opts);
192       if (!line)
193         eof= 1;
194       else {
195         if (!head)
196           head= line;
197         else
198           tail->next= line;
199         tail= line;
200         len++;
201       }
202     }
203   }
204   adns_finish(adns);
205 }
206
207 static void usage(void) {
208   fprintf(stderr, "usage: %s [-d] [-p] [-c concurrency] [logfile]\n", progname);
209   exit(1);
210 }
211
212 int main(int argc, char *argv[]) {
213   int c, opts, maxpending;
214   extern char *optarg;
215   FILE *inf;
216
217   progname= strrchr(*argv, '/');
218   if (progname)
219     progname++;
220   else
221     progname= *argv;
222
223   maxpending= MAXPENDING;
224   opts= 0;
225   while ((c= getopt(argc, argv, "c:dp")) != -1)
226     switch (c) {
227     case 'c':
228       maxpending= atoi(optarg);
229       if (maxpending < 1 || maxpending > MAXPENDING) {
230        fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending);
231        exit(1);
232       }
233       break;
234     case 'd':
235       opts|= OPT_DEBUG;
236       break;
237     case 'p':
238       opts|= OPT_POLL;
239       break;
240     default:
241       usage();
242     }
243
244   argc-= optind;
245   argv+= optind;
246
247   inf= NULL;
248   if (argc == 0)
249     inf= stdin;
250   else if (argc == 1)
251     inf= fopen(*argv, "r");
252   else
253     usage();
254
255   if (!inf)
256     aargh("couldn't open input");
257
258   proclog(inf, stdout, maxpending, opts);
259
260   if (fclose(inf))
261     aargh("fclose input");
262   if (fclose(stdout))
263     aargh("fclose output");
264
265   return 0;
266 }