3 * - a replacement for the Apache logresolve program using adns
6 * This file is Copyright (C) 1999 Tony Finch <fanf@demon.net> <dot@dotat.at>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 static const char * const cvsid
=
26 #include <sys/types.h>
37 /* maximum number of concurrent DNS queries */
38 #define MAXPENDING 1000
40 /* maximum length of a line */
43 static const char *progname
;
45 static void aargh(const char *msg
) {
46 fprintf(stderr
, "%s: %s: %s (%d)\n", progname
, msg
,
47 strerror(errno
) ?
strerror(errno
) : "Unknown error", errno
);
52 * Parse the IP address and convert to a reverse domain name.
54 static void ipaddr2domain(char *start
, char **addr
, char **rest
, char **domain
) {
55 static char buf
[30]; /* "123.123.123.123.in-addr.arpa.\0" */
59 for (ptrs
[0]= start
; !isdigit(*ptrs
[0]); ptrs
[0]++)
62 for (i
= 1; i
< 5; i
++) {
63 ptrs
[i
]= strchr(ptrs
[i
-1], (i
== 4) ?
' ' : '.');
64 if (!ptrs
[i
] || ptrs
[i
]-ptrs
[i
-1] > 3)
69 sprintf(buf
, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
70 ptrs
[4]-ptrs
[3]-1, ptrs
[3],
71 ptrs
[3]-ptrs
[2]-1, ptrs
[2],
72 ptrs
[2]-ptrs
[1]-1, ptrs
[1],
73 ptrs
[1]-ptrs
[0]-1, ptrs
[0]);
79 strcpy(buf
, "invalid.");
85 static void printline(char *start
, char *addr
, char *rest
, char *domain
) {
87 printf("%.*s%s%s", addr
- start
, start
, domain
, rest
);
90 /* XXX: error checking */
93 typedef struct logline
{
95 char *start
, *addr
, *rest
;
99 static logline
*readline(adns_state adns
) {
100 static char buf
[MAXLINE
];
104 if (fgets(buf
, MAXLINE
, stdin
)) {
105 str
= malloc(sizeof(*line
) + strlen(buf
) + 1);
106 if (!str
) aargh("malloc");
109 line
->start
= str
+sizeof(logline
);
110 strcpy(line
->start
, buf
);
111 ipaddr2domain(line
->start
, &line
->addr
, &line
->rest
, &str
);
112 if (adns_submit(adns
, str
, adns_r_ptr
,
113 adns_qf_quoteok_cname
|adns_qf_cname_loose
,
115 aargh("adns_submit");
123 static void proclog(void) {
127 logline
*head
, *tail
, *line
;
129 errno
= adns_init(&adns
, 0, 0);
130 if (errno
) aargh("adns_init");
131 head
= tail
= readline(adns
);
134 if (eof
|| len
> MAXPENDING
)
135 err
= adns_wait(adns
, &head
->query
, &answer
, NULL
);
137 err
= adns_check(adns
, &head
->query
, &answer
, NULL
);
138 if (err
!= EWOULDBLOCK
) {
139 printline(head
->start
, head
->addr
, head
->rest
,
140 answer
->status
== adns_s_ok ?
*answer
->rrs
.str
: NULL
);
141 line
= head
; head
= head
->next
;
142 free(line
); free(answer
);
146 line
= readline(adns
);
162 int main(int argc
, char *argv
[])