chiark / gitweb /
Remove spurious semicolon.
[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 Tony Finch <dot@dotat.at>
8  *   Copyright (C) 1999 Ian Jackson <ian@davenant.greenend.org.uk>
9  *
10  *  It is part of adns, which is
11  *    Copyright (C) 1997-1999 Ian Jackson <ian@davenant.greenend.org.uk>
12  *    Copyright (C) 1999 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.
30  */
31
32 static const char * const cvsid =
33         "$Id$";
34
35 #include <sys/types.h>
36 #include <sys/time.h>
37
38 #include <unistd.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <ctype.h>
43 #include <errno.h>
44
45 #include "adns.h"
46
47 /* maximum number of concurrent DNS queries */
48 #define MAXPENDING 1000
49
50 /* maximum length of a line */
51 #define MAXLINE 1024
52
53 /* option flags */
54 #define OPT_DEBUG 1
55 #define OPT_POLL 2
56
57 static const char *progname;
58
59 static void aargh(const char *msg) {
60   fprintf(stderr, "%s: %s: %s (%d)\n", progname, msg,
61           strerror(errno) ? strerror(errno) : "Unknown error", errno);
62   exit(1);
63 }
64
65 /*
66  * Parse the IP address and convert to a reverse domain name.
67  */
68 static char *ipaddr2domain(char *start, char **addr, char **rest) {
69   static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
70   char *ptrs[5];
71   int i;
72
73   ptrs[0]= start;
74 retry:
75   while (!isdigit(*ptrs[0]))
76     if (!*ptrs[0]++) {
77       strcpy(buf, "invalid.");
78       *addr= *rest= NULL;
79       return buf;
80     }
81   for (i= 1; i < 5; i ++) {
82     ptrs[i]= strchr(ptrs[i-1], (i == 4) ? ' ' : '.');
83     if (!ptrs[i] || ptrs[i]-ptrs[i-1] > 3) {
84       ptrs[0]++;
85       goto retry;
86     } else
87       ptrs[i]++;
88   }
89   sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
90           ptrs[4]-ptrs[3]-1, ptrs[3],
91           ptrs[3]-ptrs[2]-1, ptrs[2],
92           ptrs[2]-ptrs[1]-1, ptrs[1],
93           ptrs[1]-ptrs[0]-1, ptrs[0]);
94   *addr= ptrs[0];
95   *rest= ptrs[4]-1;
96   return buf;
97 }
98
99 static void printline(char *start, char *addr, char *rest, char *domain) {
100   if (domain)
101     printf("%.*s%s%s", addr - start, start, domain, rest);
102   else
103     fputs(start, stdout);
104   if (ferror(stdout)) aargh("write output");
105 }
106
107 typedef struct logline {
108   struct logline *next;
109   char *start, *addr, *rest;
110   adns_query query;
111 } logline;
112
113 static logline *readline(adns_state adns, int opts) {
114   static char buf[MAXLINE];
115   char *str;
116   logline *line;
117
118   if (fgets(buf, MAXLINE, stdin)) {
119     str= malloc(sizeof(*line) + strlen(buf) + 1);
120     if (!str) aargh("malloc");
121     line= (logline*)str;
122     line->next= NULL;
123     line->start= str+sizeof(logline);
124     strcpy(line->start, buf);
125     str = ipaddr2domain(line->start, &line->addr, &line->rest);
126     if (opts & OPT_DEBUG)
127         fprintf(stderr, "%s: adns_submit %s\n", progname, str);
128     if (adns_submit(adns, str, adns_r_ptr,
129                     adns_qf_quoteok_cname|adns_qf_cname_loose,
130                     NULL, &line->query))
131       aargh("adns_submit");
132     return line;
133   }
134   if (!feof(stdin))
135     aargh("fgets");
136   return NULL;
137 }
138         
139 static void proclog(int opts) {
140   int eof, err, len;
141   adns_state adns;
142   adns_answer *answer;
143   logline *head, *tail, *line;
144
145   errno= adns_init(&adns, (opts & OPT_DEBUG) ? adns_if_debug : 0, 0);
146   if (errno) aargh("adns_init");
147   head= tail= readline(adns, opts);
148   len= 1; eof= 0;
149   while (head) {
150     if (eof || len > MAXPENDING)
151       if (opts & OPT_POLL)
152         err= adns_wait_poll(adns, &head->query, &answer, NULL);
153       else
154         err= adns_wait(adns, &head->query, &answer, NULL);
155     else
156       err= adns_check(adns, &head->query, &answer, NULL);
157     if (err != EAGAIN) {
158         printline(head->start, head->addr, head->rest,
159                   answer->status == adns_s_ok ? *answer->rrs.str : NULL);
160         line= head; head= head->next;
161         free(line); free(answer);
162         len--;
163     }
164     if (!eof) {
165       line= readline(adns, opts);
166       if (!line)
167         eof= 1;
168       else {
169         if (!head)
170           head = line;
171         else
172           tail->next = line;
173         tail = line;
174         len++;
175       }
176     }
177   }
178   adns_finish(adns);
179 }
180
181 int main(int argc, char *argv[]) {
182   int c, opts;
183
184   progname= *argv;
185   opts= 0;
186
187   while ((c= getopt(argc, argv, "dp")) != -1) {
188     switch (c) {
189     case 'd':
190       opts |= OPT_DEBUG;
191       break;
192     case 'p':
193       opts |= OPT_POLL;
194       break;
195     default:
196       fprintf(stderr, "usage: %s [-d] < logfile\n", progname);
197       exit(1);
198     }
199     argc-= optind;
200     argv+= optind;
201   }
202
203   proclog(opts);
204
205   if (fclose(stdout)) aargh("finish writing output");
206   return 0;
207 }