3 * - a replacement for the Apache logresolve program using adns
7 * Copyright (C) 1999 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 Ian Jackson <ian@davenant.greenend.org.uk>
12 * Copyright (C) 1999 Tony Finch <dot@dotat.at>
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)
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.
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.
28 * This version was originally supplied by Tony Finch, but has been
29 * modified by Ian Jackson as it was incorporated into adns and
33 static const char * const cvsid
=
36 #include <sys/types.h>
50 /* maximum number of concurrent DNS queries */
51 #define MAXMAXPENDING 64000
52 #define DEFMAXPENDING 2000
54 /* maximum length of a line */
61 static const char *progname
;
63 #define guard_null(str) ((str) ? (str) : "")
65 #define sensible_ctype(type,ch) (type((unsigned char)(ch)))
66 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
68 static void msg(const char *fmt
, ...) {
71 fprintf(stderr
, "%s: ", progname
);
73 vfprintf(stderr
, fmt
, al
);
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
);
86 * Parse the IP address and convert to a reverse domain name.
88 static char *ipaddr2domain(char *start
, char **addr
, char **rest
) {
89 static char buf
[30]; /* "123.123.123.123.in-addr.arpa.\0" */
95 while (!sensible_ctype(isdigit
,*ptrs
[0]))
97 strcpy(buf
, "invalid.");
101 for (i
= 1; i
< 5; i
++) {
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)) {
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]);
121 static void printline(FILE *outf
, char *start
, char *addr
, char *rest
, char *domain
) {
123 fprintf(outf
, "%.*s%s%s", addr
- start
, start
, domain
, rest
);
126 if (ferror(outf
)) aargh("write output");
129 typedef struct logline
{
130 struct logline
*next
;
131 char *start
, *addr
, *rest
;
135 static logline
*readline(FILE *inf
, adns_state adns
, int opts
) {
136 static char buf
[MAXLINE
];
140 if (fgets(buf
, MAXLINE
, inf
)) {
141 str
= malloc(sizeof(*line
) + strlen(buf
) + 1);
142 if (!str
) aargh("malloc");
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
,
153 aargh("adns_submit");
161 static void proclog(FILE *inf
, FILE *outf
, int maxpending
, int opts
) {
165 logline
*head
, *tail
, *line
;
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
);
173 if (opts
& OPT_DEBUG
)
174 msg("%d in queue; checking %.*s", len
,
175 head
->rest
-head
->addr
, guard_null(head
->addr
));
176 if (eof
|| len
> maxpending
) {
178 err
= adns_wait_poll(adns
, &head
->query
, &answer
, NULL
);
180 err
= adns_wait(adns
, &head
->query
, &answer
, NULL
);
182 err
= adns_check(adns
, &head
->query
, &answer
, NULL
);
184 if (err
== EAGAIN
) break;
185 printline(outf
, head
->start
, head
->addr
, head
->rest
,
186 answer
->status
== adns_s_ok ?
*answer
->rrs
.str
: NULL
);
187 line
= head
; head
= head
->next
;
188 free(line
); free(answer
);
192 line
= readline(inf
, adns
, opts
);
194 if (!head
) head
= line
;
195 else tail
->next
= line
;
205 static void usage(void) {
206 fprintf(stderr
, "usage: %s [-d] [-p] [-c concurrency] [logfile]\n", progname
);
210 int main(int argc
, char *argv
[]) {
211 int c
, opts
, maxpending
;
215 progname
= strrchr(*argv
, '/');
221 maxpending
= DEFMAXPENDING
;
223 while ((c
= getopt(argc
, argv
, "c:dp")) != -1)
226 maxpending
= atoi(optarg
);
227 if (maxpending
< 1 || maxpending
> MAXMAXPENDING
) {
228 fprintf(stderr
, "%s: unfeasible concurrency %d\n", progname
, maxpending
);
249 inf
= fopen(*argv
, "r");
254 aargh("couldn't open input");
256 proclog(inf
, stdout
, maxpending
, opts
);
259 aargh("fclose input");
261 aargh("fclose output");