new adns_wait_poll function.
Thanks to Tony Finch for the program and the performance figure.
* New adnshost utility - currently only a usage message :-).
* New internal consistency checking with assert if right options set.
+ * New adns_wait_poll function like adns_wait but uses poll, not select.
Incompatible changes:
* RRs with mailboxes never rejected due to strange chars if _raw.
#include <sys/types.h>
#include <sys/time.h>
+#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
/* maximum length of a line */
#define MAXLINE 1024
+/* option flags */
+#define OPT_DEBUG 1
+#define OPT_POLL 2
+
static const char *progname;
static void aargh(const char *msg) {
/*
* Parse the IP address and convert to a reverse domain name.
*/
-static void ipaddr2domain(char *start, char **addr, char **rest, char **domain) {
+static char *ipaddr2domain(char *start, char **addr, char **rest) {
static char buf[30]; /* "123.123.123.123.in-addr.arpa.\0" */
char *ptrs[5];
int i;
- for (ptrs[0]= start; !isdigit(*ptrs[0]); ptrs[0]++)
- if (!*ptrs[0])
- goto invalid;
+ ptrs[0]= start;
+retry:
+ while (!isdigit(*ptrs[0]))
+ if (!*ptrs[0]++) {
+ strcpy(buf, "invalid.");
+ *addr= *rest= NULL;
+ return buf;
+ }
for (i= 1; i < 5; i ++) {
ptrs[i]= strchr(ptrs[i-1], (i == 4) ? ' ' : '.');
- if (!ptrs[i] || ptrs[i]-ptrs[i-1] > 3)
- goto invalid;
- else
+ if (!ptrs[i] || ptrs[i]-ptrs[i-1] > 3) {
+ ptrs[0]++;
+ goto retry;
+ } else
ptrs[i]++;
}
sprintf(buf, "%.*s.%.*s.%.*s.%.*s.in-addr.arpa.",
ptrs[1]-ptrs[0]-1, ptrs[0]);
*addr= ptrs[0];
*rest= ptrs[4]-1;
- *domain= buf;
- return;
-invalid:
- strcpy(buf, "invalid.");
- *addr= *rest= NULL;
- *domain= buf;
- return;
+ return buf;
}
static void printline(char *start, char *addr, char *rest, char *domain) {
adns_query query;
} logline;
-static logline *readline(adns_state adns) {
+static logline *readline(adns_state adns, int opts) {
static char buf[MAXLINE];
char *str;
logline *line;
line->next= NULL;
line->start= str+sizeof(logline);
strcpy(line->start, buf);
- ipaddr2domain(line->start, &line->addr, &line->rest, &str);
+ str = ipaddr2domain(line->start, &line->addr, &line->rest);
+ if (opts & OPT_DEBUG)
+ fprintf(stderr, "%s: adns_submit %s\n", progname, str);
if (adns_submit(adns, str, adns_r_ptr,
adns_qf_quoteok_cname|adns_qf_cname_loose,
NULL, &line->query))
return NULL;
}
-static void proclog(void) {
+static void proclog(int opts) {
int eof, err, len;
adns_state adns;
adns_answer *answer;
logline *head, *tail, *line;
- errno= adns_init(&adns, 0, 0);
+ errno= adns_init(&adns, (opts & OPT_DEBUG) ? adns_if_debug : 0, 0);
if (errno) aargh("adns_init");
- head= tail= readline(adns);
+ head= tail= readline(adns, opts);
len= 1; eof= 0;
while (head) {
if (eof || len > MAXPENDING)
- err= adns_wait(adns, &head->query, &answer, NULL);
+ if (opts & OPT_POLL)
+ err= adns_wait_poll(adns, &head->query, &answer, NULL);
+ else
+ err= adns_wait(adns, &head->query, &answer, NULL);
else
err= adns_check(adns, &head->query, &answer, NULL);
if (err != EWOULDBLOCK) {
len--;
}
if (!eof) {
- line= readline(adns);
+ line= readline(adns, opts);
if (!line)
eof= 1;
else {
}
int main(int argc, char *argv[]) {
+ int c, opts;
+
progname= *argv;
- proclog();
+ opts= 0;
+
+ while ((c= getopt(argc, argv, "dp")) != -1) {
+ switch (c) {
+ case 'd':
+ opts |= OPT_DEBUG;
+ break;
+ case 'p':
+ opts |= OPT_POLL;
+ break;
+ default:
+ fprintf(stderr, "usage: %s [-d] < logfile\n", progname);
+ exit(1);
+ }
+ argc-= optind;
+ argv+= optind;
+ }
+
+ proclog(opts);
+
if (fclose(stdout)) aargh("finish writing output");
return 0;
}
}
if (strchr(owninitflags,'p')) {
- for (;;) {
- r= adns_check(ads,&qu,&ans,&mcr);
- if (r != EWOULDBLOCK) break;
- for (;;) {
- npollfds= npollfdsavail;
- timeout= -1;
- r= adns_beforepoll(ads, pollfds, &npollfds, &timeout, 0);
- if (r != ERANGE) break;
- pollfds= realloc(pollfds,sizeof(*pollfds)*npollfds);
- if (!pollfds) failure_errno("realloc pollfds",errno);
- npollfdsavail= npollfds;
- }
- if (r) failure_errno("beforepoll",r);
- r= poll(pollfds,npollfds,timeout);
- if (r == -1) failure_errno("poll",errno);
- adns_afterpoll(ads,pollfds, r?npollfds:0, 0);
- }
+ r= adns_wait_poll(ads,&qu,&ans,&mcr);
} else {
r= adns_wait(ads,&qu,&ans,&mcr);
}
adns_answer **answer_r,
void **context_r);
+/* same as adns_wait but uses poll(2) internally */
+int adns_wait_poll(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer_r,
+ void **context_r);
+
void adns_cancel(adns_query query);
/* The adns_query you get back from _submit is valid (ie, can be
adns_processany(ads);
}
-static int internal_check(adns_state ads,
- adns_query *query_io,
- adns_answer **answer,
- void **context_r) {
+int adns__internal_check(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer,
+ void **context_r) {
adns_query qu;
qu= *query_io;
adns__consistency(ads,*query_io,cc_entex);
for (;;) {
- r= internal_check(ads,query_io,answer_r,context_r);
+ r= adns__internal_check(ads,query_io,answer_r,context_r);
if (r != EWOULDBLOCK) break;
maxfd= 0; tvp= 0;
FD_ZERO(&readfds); FD_ZERO(&writefds); FD_ZERO(&exceptfds);
r= gettimeofday(&now,0);
if (!r) adns__autosys(ads,now);
- r= internal_check(ads,query_io,answer_r,context_r);
+ r= adns__internal_check(ads,query_io,answer_r,context_r);
adns__consistency(ads,0,cc_entex);
return r;
}
int maxfd, const fd_set *readfds,
const fd_set *writefds, const fd_set *exceptfds,
struct timeval now, int *r_r);
+int adns__internal_check(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer,
+ void **context_r);
/* From check.c: */
*/
#include <limits.h>
+#include <string.h>
#include "internal.h"
adns__consistency(ads,0,cc_entex);
}
+int adns_wait_poll(adns_state ads,
+ adns_query *query_io,
+ adns_answer **answer_r,
+ void **context_r) {
+ int r, nfds, to;
+ struct pollfd fds[MAX_POLLFDS];
+
+ adns__consistency(ads,0,cc_entex);
+
+ for (;;) {
+ r= adns__internal_check(ads,query_io,answer_r,context_r);
+ if (r != EWOULDBLOCK) goto xit;
+ nfds= MAX_POLLFDS; to= -1;
+ adns_beforepoll(ads,fds,&nfds,&to,0);
+ r= poll(fds,nfds,to);
+ if (r == -1) {
+ if (errno == EINTR) {
+ if (ads->iflags & adns_if_eintr) { r= EINTR; goto xit; }
+ } else {
+ adns__diag(ads,-1,0,"poll failed in wait: %s",strerror(errno));
+ adns_globalsystemfailure(ads);
+ }
+ } else {
+ assert(r >= 0);
+ adns_afterpoll(ads,fds,nfds,0);
+ }
+ }
+
+ xit:
+ adns__consistency(ads,0,cc_entex);
+ return r;
+}
+
#endif