chiark / gitweb /
Correct type of various printf arguments: ptrdiff_t != int
[adns] / client / adnslogres.c
CommitLineData
ab397b6e 1/*
2 * adnslogres.c
3 * - a replacement for the Apache logresolve program using adns
4 */
5/*
2b4bbcac 6 * This file is
c6826df6 7 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
80a72950 8 * Copyright (C) 1999-2000 Ian Jackson <ian@davenant.greenend.org.uk>
a79ac5ba 9 *
10 * It is part of adns, which is
80a72950 11 * Copyright (C) 1997-2000 Ian Jackson <ian@davenant.greenend.org.uk>
c6826df6 12 * Copyright (C) 1999-2000 Tony Finch <dot@dotat.at>
ab397b6e 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,
2b4bbcac 26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 *
80a72950 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.
ab397b6e 31 */
32
33static const char * const cvsid =
c2180d3e 34 "$Id: adnslogres.c,v 1.21 2006/04/03 22:41:14 ian Exp $";
ab397b6e 35
36#include <sys/types.h>
37#include <sys/time.h>
38
ef20fccf 39#include <unistd.h>
ab397b6e 40#include <string.h>
41#include <stdlib.h>
42#include <stdio.h>
43#include <ctype.h>
44#include <errno.h>
cc0f95d0 45#include <stdarg.h>
ab397b6e 46
cc0f95d0 47#include "config.h"
ab397b6e 48#include "adns.h"
8a5e5147 49#include "client.h"
ab397b6e 50
d024926c 51#ifdef ADNS_REGRESS_TEST
52# include "hredirect.h"
53#endif
54
ab397b6e 55/* maximum number of concurrent DNS queries */
6fb17308 56#define MAXMAXPENDING 64000
57#define DEFMAXPENDING 2000
ab397b6e 58
59/* maximum length of a line */
60#define MAXLINE 1024
61
ef20fccf 62/* option flags */
63#define OPT_DEBUG 1
64#define OPT_POLL 2
65
d024926c 66static const char *const progname= "adnslogres";
f26a66d0 67static const char *config_text;
ab397b6e 68
3b94f31a 69#define guard_null(str) ((str) ? (str) : "")
1bd9afad 70
acff310d 71#define sensible_ctype(type,ch) (type((unsigned char)(ch)))
72 /* isfoo() functions from ctype.h can't safely be fed char - blech ! */
73
cc0f95d0 74static void msg(const char *fmt, ...) {
75 va_list al;
76
77 fprintf(stderr, "%s: ", progname);
78 va_start(al,fmt);
79 vfprintf(stderr, fmt, al);
80 va_end(al);
81 fputc('\n',stderr);
82}
83
1bd9afad 84static void aargh(const char *cause) {
85 const char *why = strerror(errno);
86 if (!why) why = "Unknown error";
87 msg("%s: %s (%d)", cause, why, errno);
ab397b6e 88 exit(1);
89}
90
91/*
92 * Parse the IP address and convert to a reverse domain name.
93 */
ef20fccf 94static char *ipaddr2domain(char *start, char **addr, char **rest) {
ab397b6e 95 static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
96 char *ptrs[5];
97 int i;
98
ef20fccf 99 ptrs[0]= start;
100retry:
acff310d 101 while (!sensible_ctype(isdigit,*ptrs[0]))
ef20fccf 102 if (!*ptrs[0]++) {
103 strcpy(buf, "invalid.");
104 *addr= *rest= NULL;
105 return buf;
106 }
80775c8e 107 for (i= 1; i < 5; i++) {
108 ptrs[i]= ptrs[i-1];
acff310d 109 while (sensible_ctype(isdigit,*ptrs[i]++));
110 if ((i == 4 && !sensible_ctype(isspace,ptrs[i][-1])) ||
80775c8e 111 (i != 4 && ptrs[i][-1] != '.') ||
112 (ptrs[i]-ptrs[i-1] > 4)) {
113 ptrs[0]= ptrs[i]-1;
ef20fccf 114 goto retry;
80775c8e 115 }
ab397b6e 116 }
117 sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
c2180d3e 118 (int)(ptrs[4]-ptrs[3]-1), ptrs[3],
119 (int)(ptrs[3]-ptrs[2]-1), ptrs[2],
120 (int)(ptrs[2]-ptrs[1]-1), ptrs[1],
121 (int)(ptrs[1]-ptrs[0]-1), ptrs[0]);
ab397b6e 122 *addr= ptrs[0];
123 *rest= ptrs[4]-1;
ef20fccf 124 return buf;
ab397b6e 125}
126
80775c8e 127static void printline(FILE *outf, char *start, char *addr, char *rest, char *domain) {
ab397b6e 128 if (domain)
c2180d3e 129 fprintf(outf, "%.*s%s%s", (int)(addr - start), start, domain, rest);
ab397b6e 130 else
80775c8e 131 fputs(start, outf);
132 if (ferror(outf)) aargh("write output");
ab397b6e 133}
134
135typedef struct logline {
136 struct logline *next;
137 char *start, *addr, *rest;
138 adns_query query;
139} logline;
140
80775c8e 141static logline *readline(FILE *inf, adns_state adns, int opts) {
ab397b6e 142 static char buf[MAXLINE];
143 char *str;
144 logline *line;
145
80775c8e 146 if (fgets(buf, MAXLINE, inf)) {
ab397b6e 147 str= malloc(sizeof(*line) + strlen(buf) + 1);
148 if (!str) aargh("malloc");
149 line= (logline*)str;
150 line->next= NULL;
151 line->start= str+sizeof(logline);
152 strcpy(line->start, buf);
80775c8e 153 str= ipaddr2domain(line->start, &line->addr, &line->rest);
ef20fccf 154 if (opts & OPT_DEBUG)
3b94f31a 155 msg("submitting %.*s -> %s", line->rest-line->addr, guard_null(line->addr), str);
ab397b6e 156 if (adns_submit(adns, str, adns_r_ptr,
157 adns_qf_quoteok_cname|adns_qf_cname_loose,
158 NULL, &line->query))
159 aargh("adns_submit");
160 return line;
161 }
80775c8e 162 if (!feof(inf))
ab397b6e 163 aargh("fgets");
164 return NULL;
165}
166
6fb17308 167static void proclog(FILE *inf, FILE *outf, int maxpending, int opts) {
ab397b6e 168 int eof, err, len;
169 adns_state adns;
170 adns_answer *answer;
171 logline *head, *tail, *line;
f26a66d0 172 adns_initflags initflags;
ab397b6e 173
f26a66d0 174 initflags= (opts & OPT_DEBUG) ? adns_if_debug : 0;
175 if (config_text) {
176 errno= adns_init_strcfg(&adns, initflags, stderr, config_text);
177 } else {
178 errno= adns_init(&adns, initflags, 0);
179 }
ab397b6e 180 if (errno) aargh("adns_init");
80775c8e 181 head= tail= readline(inf, adns, opts);
ab397b6e 182 len= 1; eof= 0;
183 while (head) {
6fb17308 184 while (head) {
185 if (opts & OPT_DEBUG)
186 msg("%d in queue; checking %.*s", len,
187 head->rest-head->addr, guard_null(head->addr));
d024926c 188 if (eof || len >= maxpending) {
6fb17308 189 if (opts & OPT_POLL)
190 err= adns_wait_poll(adns, &head->query, &answer, NULL);
191 else
192 err= adns_wait(adns, &head->query, &answer, NULL);
193 } else {
194 err= adns_check(adns, &head->query, &answer, NULL);
195 }
196 if (err == EAGAIN) break;
d024926c 197 if (err) {
198 fprintf(stderr, "%s: adns_wait/check: %s", progname, strerror(err));
199 exit(1);
200 }
80775c8e 201 printline(outf, head->start, head->addr, head->rest,
202 answer->status == adns_s_ok ? *answer->rrs.str : NULL);
203 line= head; head= head->next;
d024926c 204 free(line);
205 free(answer);
80775c8e 206 len--;
ab397b6e 207 }
208 if (!eof) {
80775c8e 209 line= readline(inf, adns, opts);
6fb17308 210 if (line) {
211 if (!head) head= line;
212 else tail->next= line;
213 tail= line; len++;
214 } else {
ab397b6e 215 eof= 1;
ab397b6e 216 }
217 }
218 }
219 adns_finish(adns);
220}
221
aa958273 222static void printhelp(FILE *file) {
8a5e5147 223 fputs("usage: adnslogres [<options>] [<logfile>]\n"
224 " adnslogres --version|--help\n"
225 "options: -c <concurrency> set max number of outstanding queries\n"
226 " -p use poll(2) instead of select(2)\n"
227 " -d turn on debugging\n"
228 " -C <config> use instead of contents of resolv.conf\n",
229 stdout);
aa958273 230}
231
232static void usage(void) {
233 printhelp(stderr);
80775c8e 234 exit(1);
235}
236
2b4bbcac 237int main(int argc, char *argv[]) {
6fb17308 238 int c, opts, maxpending;
239 extern char *optarg;
80775c8e 240 FILE *inf;
ef20fccf 241
8a5e5147 242 if (argv[1] && !strncmp(argv[1],"--",2)) {
243 if (!strcmp(argv[1],"--help")) {
244 printhelp(stdout);
245 } else if (!strcmp(argv[1],"--version")) {
246 fputs(VERSION_MESSAGE("adnslogres"),stdout);
247 } else {
248 usage();
249 }
aa958273 250 if (ferror(stdout) || fclose(stdout)) { perror("stdout"); exit(1); }
251 exit(0);
252 }
253
6fb17308 254 maxpending= DEFMAXPENDING;
255 opts= 0;
f26a66d0 256 while ((c= getopt(argc, argv, "c:C:dp")) != -1)
ef20fccf 257 switch (c) {
6fb17308 258 case 'c':
259 maxpending= atoi(optarg);
260 if (maxpending < 1 || maxpending > MAXMAXPENDING) {
261 fprintf(stderr, "%s: unfeasible concurrency %d\n", progname, maxpending);
262 exit(1);
263 }
264 break;
f26a66d0 265 case 'C':
266 config_text= optarg;
267 break;
ef20fccf 268 case 'd':
80775c8e 269 opts|= OPT_DEBUG;
ef20fccf 270 break;
271 case 'p':
80775c8e 272 opts|= OPT_POLL;
ef20fccf 273 break;
274 default:
80775c8e 275 usage();
ef20fccf 276 }
ef20fccf 277
80775c8e 278 argc-= optind;
279 argv+= optind;
280
281 inf= NULL;
282 if (argc == 0)
283 inf= stdin;
284 else if (argc == 1)
285 inf= fopen(*argv, "r");
286 else
287 usage();
288
289 if (!inf)
290 aargh("couldn't open input");
291
6fb17308 292 proclog(inf, stdout, maxpending, opts);
80775c8e 293
294 if (fclose(inf))
295 aargh("fclose input");
296 if (fclose(stdout))
297 aargh("fclose output");
ef20fccf 298
ab397b6e 299 return 0;
300}