3 * - a replacement for the Apache logresolve program using adns
7 * Copyright (C) 1999-2000 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,2003,2006 Ian Jackson
12 * Copyright (C) 1999-2000,2003,2006 Tony Finch
13 * Copyright (C) 1991 Massachusetts Institute of Technology
14 * (See the file INSTALL for full details.)
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2, or (at your option)
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software Foundation,
28 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 * This version was originally supplied by Tony Finch, but has been
31 * modified by Ian Jackson as it was incorporated into adns and
35 static const char * const cvsid
=
36 "$Id: adnslogres.c,v 1.23 2006/05/09 19:37:58 ian Exp $";
38 #include <sys/types.h>
53 #ifdef ADNS_REGRESS_TEST
54 # include "hredirect.h"
57 /* maximum number of concurrent DNS queries */
58 #define MAXMAXPENDING 64000
59 #define DEFMAXPENDING 2000
61 /* maximum length of a line */
68 static const char *const progname
= "adnslogres";
69 static const char *config_text
;
71 #define guard_null(str) ((str) ? (str) : "")
73 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
74 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
76 static void msg(const char *fmt
, ...) {
79 fprintf(stderr
, "%s: ", progname
);
81 vfprintf(stderr
, fmt
, al
);
86 static void aargh(const char *cause
) {
87 const char *why
= strerror(errno
);
88 if (!why
) why
= "Unknown error";
89 msg("%s: %s (%d)", cause
, why
, errno
);
94 * Parse the IP address and convert to a reverse domain name.
96 static char *ipaddr2domain(char *start
, char **addr
, char **rest
) {
97 static char buf
[30]; /* "123.123.123.123.in-addr.arpa.\0" */
103 while (!sensible_ctype(isdigit
,*ptrs
[0]))
105 strcpy(buf
, "invalid.");
109 for (i
= 1; i
< 5; i
++) {
111 while (sensible_ctype(isdigit
,*ptrs
[i
]++));
112 if ((i
== 4 && !sensible_ctype(isspace
,ptrs
[i
][-1])) ||
113 (i
!= 4 && ptrs
[i
][-1] != '.') ||
114 (ptrs
[i
]-ptrs
[i
-1] > 4)) {
119 sprintf(buf
, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
120 (int)(ptrs
[4]-ptrs
[3]-1), ptrs
[3],
121 (int)(ptrs
[3]-ptrs
[2]-1), ptrs
[2],
122 (int)(ptrs
[2]-ptrs
[1]-1), ptrs
[1],
123 (int)(ptrs
[1]-ptrs
[0]-1), ptrs
[0]);
129 static void printline(FILE *outf
, char *start
, char *addr
, char *rest
, char *domain
) {
131 fprintf(outf
, "%.*s%s%s", (int)(addr
- start
), start
, domain
, rest
);
134 if (ferror(outf
)) aargh("write output");
137 typedef struct logline
{
138 struct logline
*next
;
139 char *start
, *addr
, *rest
;
143 static logline
*readline(FILE *inf
, adns_state adns
, int opts
) {
144 static char buf
[MAXLINE
];
148 if (fgets(buf
, MAXLINE
, inf
)) {
149 str
= malloc(sizeof(*line
) + strlen(buf
) + 1);
150 if (!str
) aargh("malloc");
153 line
->start
= str
+sizeof(logline
);
154 strcpy(line
->start
, buf
);
155 str
= ipaddr2domain(line
->start
, &line
->addr
, &line
->rest
);
156 if (opts
& OPT_DEBUG
)
157 msg("submitting %.*s -> %s", (int)(line
->rest
-line
->addr
), guard_null(line
->addr
), str
);
158 if (adns_submit(adns
, str
, adns_r_ptr
,
159 adns_qf_quoteok_cname
|adns_qf_cname_loose
,
161 aargh("adns_submit");
169 static void proclog(FILE *inf
, FILE *outf
, int maxpending
, int opts
) {
173 logline
*head
, *tail
, *line
;
174 adns_initflags initflags
;
176 initflags
= (opts
& OPT_DEBUG
) ? adns_if_debug
: 0;
178 errno
= adns_init_strcfg(&adns
, initflags
, stderr
, config_text
);
180 errno
= adns_init(&adns
, initflags
, 0);
182 if (errno
) aargh("adns_init");
183 head
= tail
= readline(inf
, adns
, opts
);
187 if (opts
& OPT_DEBUG
)
188 msg("%d in queue; checking %.*s", len
,
189 (int)(head
->rest
-head
->addr
), guard_null(head
->addr
));
190 if (eof
|| len
>= maxpending
) {
192 err
= adns_wait_poll(adns
, &head
->query
, &answer
, NULL
);
194 err
= adns_wait(adns
, &head
->query
, &answer
, NULL
);
196 err
= adns_check(adns
, &head
->query
, &answer
, NULL
);
198 if (err
== EAGAIN
) break;
200 fprintf(stderr
, "%s: adns_wait/check: %s", progname
, strerror(err
));
203 printline(outf
, head
->start
, head
->addr
, head
->rest
,
204 answer
->status
== adns_s_ok ?
*answer
->rrs
.str
: NULL
);
205 line
= head
; head
= head
->next
;
211 line
= readline(inf
, adns
, opts
);
213 if (!head
) head
= line
;
214 else tail
->next
= line
;
224 static void printhelp(FILE *file
) {
225 fputs("usage: adnslogres [<options>] [<logfile>]\n"
226 " adnslogres --version|--help\n"
227 "options: -c <concurrency> set max number of outstanding queries\n"
228 " -p use poll(2) instead of select(2)\n"
229 " -d turn on debugging\n"
230 " -C <config> use instead of contents of resolv.conf\n",
234 static void usage(void) {
239 int main(int argc
, char *argv
[]) {
240 int c
, opts
, maxpending
;
244 if (argv
[1] && !strncmp(argv
[1],"--",2)) {
245 if (!strcmp(argv
[1],"--help")) {
247 } else if (!strcmp(argv
[1],"--version")) {
248 fputs(VERSION_MESSAGE("adnslogres"),stdout
);
252 if (ferror(stdout
) || fclose(stdout
)) { perror("stdout"); exit(1); }
256 maxpending
= DEFMAXPENDING
;
258 while ((c
= getopt(argc
, argv
, "c:C:dp")) != -1)
261 maxpending
= atoi(optarg
);
262 if (maxpending
< 1 || maxpending
> MAXMAXPENDING
) {
263 fprintf(stderr
, "%s: unfeasible concurrency %d\n", progname
, maxpending
);
287 inf
= fopen(*argv
, "r");
292 aargh("couldn't open input");
294 proclog(inf
, stdout
, maxpending
, opts
);
297 aargh("fclose input");
299 aargh("fclose output");