diff options
author | Eric Faurot <eric@cvs.openbsd.org> | 2011-03-26 14:38:15 +0000 |
---|---|---|
committer | Eric Faurot <eric@cvs.openbsd.org> | 2011-03-26 14:38:15 +0000 |
commit | 6bb7d0cf86ba4220bc81dd475077ecb6e652a8c9 (patch) | |
tree | 14a76a5c44cd5221ed73044679098036d1902314 /usr.sbin | |
parent | 1486fccbeeb5a88ef9e406739f3934b8e9084b71 (diff) |
Tweak the asr API to make things a bit smoother on the user side.
Then asr_run() call now returns ASR_COND when a condition on a FD is
expected. The exact condition (readable or writeable) is specified in
the asr_result structure, along with the fd and timeout.
ok gilles@
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/smtpd/asr.c | 53 | ||||
-rw-r--r-- | usr.sbin/smtpd/asr.h | 13 | ||||
-rw-r--r-- | usr.sbin/smtpd/dns.c | 26 |
3 files changed, 46 insertions, 46 deletions
diff --git a/usr.sbin/smtpd/asr.c b/usr.sbin/smtpd/asr.c index ae36032a3fb..c57aa5980db 100644 --- a/usr.sbin/smtpd/asr.c +++ b/usr.sbin/smtpd/asr.c @@ -318,8 +318,7 @@ struct kv kv_state[] = { }; struct kv kv_transition[] = { - { ASR_NEED_READ, "ASR_NEED_READ" }, - { ASR_NEED_WRITE, "ASR_NEED_WRITE" }, + { ASR_COND, "ASR_COND" }, { ASR_YIELD, "ASR_YIELD" }, { ASR_DONE, "ASR_DONE" }, { 0, NULL } @@ -452,12 +451,9 @@ asr_run_sync(struct asr_query *aq, struct asr_result *ar) struct pollfd fds[1]; int r; - for(;;) { - r = asr_run(aq, ar); - if (r == ASR_DONE || r == ASR_YIELD) - break; + while((r = asr_run(aq, ar)) == ASR_COND) { fds[0].fd = ar->ar_fd; - fds[0].events = (r == ASR_NEED_READ) ? POLLIN : POLLOUT; + fds[0].events = (ar->ar_cond == ASR_READ) ? POLLIN : POLLOUT; again: r = poll(fds, 1, ar->ar_timeout); if (r == -1 && errno == EINTR) @@ -466,7 +462,7 @@ asr_run_sync(struct asr_query *aq, struct asr_result *ar) err(1, "poll"); } - return r; + return (r); } void @@ -1451,9 +1447,10 @@ asr_run_dns(struct asr_query *aq, struct asr_result *ar) case ASR_STATE_UDP_SEND: if (asr_udp_send(aq) == 0) { aq->aq_state = ASR_STATE_UDP_RECV; + ar->ar_cond = ASR_READ; ar->ar_fd = aq->aq_fd; ar->ar_timeout = aq->aq_timeout; - return (ASR_NEED_READ); + return (ASR_COND); } aq->aq_state = ASR_STATE_NEXT_NS; break; @@ -1481,13 +1478,15 @@ asr_run_dns(struct asr_query *aq, struct asr_result *ar) break; case 0: aq->aq_state = ASR_STATE_TCP_READ; + ar->ar_cond = ASR_READ; ar->ar_fd = aq->aq_fd; ar->ar_timeout = aq->aq_timeout; - return (ASR_NEED_READ); + return (ASR_COND); case 1: + ar->ar_cond = ASR_WRITE; ar->ar_fd = aq->aq_fd; ar->ar_timeout = aq->aq_timeout; - return (ASR_NEED_WRITE); + return (ASR_COND); } break; @@ -1505,9 +1504,10 @@ asr_run_dns(struct asr_query *aq, struct asr_result *ar) aq->aq_state = ASR_STATE_PACKET; break; case 1: + ar->ar_cond = ASR_READ; ar->ar_fd = aq->aq_fd; ar->ar_timeout = aq->aq_timeout; - return (ASR_NEED_READ); + return (ASR_COND); } break; @@ -1727,9 +1727,10 @@ asr_run_host(struct asr_query *aq, struct asr_result *ar) case ASR_STATE_UDP_SEND: if (asr_udp_send(aq) == 0) { aq->aq_state = ASR_STATE_UDP_RECV; + ar->ar_cond = ASR_READ; ar->ar_fd = aq->aq_fd; ar->ar_timeout = aq->aq_timeout; - return (ASR_NEED_READ); + return (ASR_COND); } aq->aq_state = ASR_STATE_NEXT_NS; break; @@ -1757,13 +1758,15 @@ asr_run_host(struct asr_query *aq, struct asr_result *ar) break; case 0: aq->aq_state = ASR_STATE_TCP_READ; + ar->ar_cond = ASR_READ; ar->ar_fd = aq->aq_fd; ar->ar_timeout = aq->aq_timeout; - return (ASR_NEED_READ); + return (ASR_COND); case 1: + ar->ar_cond = ASR_WRITE; ar->ar_fd = aq->aq_fd; ar->ar_timeout = aq->aq_timeout; - return (ASR_NEED_WRITE); + return (ASR_COND); } break; @@ -1781,9 +1784,10 @@ asr_run_host(struct asr_query *aq, struct asr_result *ar) aq->aq_state = ASR_STATE_PACKET; break; case 1: + ar->ar_cond = ASR_READ; ar->ar_fd = aq->aq_fd; ar->ar_timeout = aq->aq_timeout; - return (ASR_NEED_READ); + return (ASR_COND); } break; @@ -2211,8 +2215,7 @@ asr_run_addrinfo(struct asr_query *aq, struct asr_result *ar) case ASR_STATE_SUBQUERY: switch ((r = asr_run(aq->aq_subq, ar))) { - case ASR_NEED_READ: - case ASR_NEED_WRITE: + case ASR_COND: return (r); case ASR_YIELD: if ((r = asr_add_sockaddr(aq, &ar->ar_sa.sa))) { @@ -2368,7 +2371,7 @@ asr_run_cname(struct asr_query *aq, struct asr_result *ar) aq->aq_state = ASR_STATE_HALT; break; case -1: - ar->ar_err = EASR_NAME; + ar->ar_err = EASR_NAME; /* XXX impossible */ aq->aq_state = ASR_STATE_HALT; break; default: @@ -2383,9 +2386,10 @@ asr_run_cname(struct asr_query *aq, struct asr_result *ar) case ASR_STATE_UDP_SEND: if (asr_udp_send(aq) == 0) { aq->aq_state = ASR_STATE_UDP_RECV; + ar->ar_cond = ASR_READ; ar->ar_fd = aq->aq_fd; ar->ar_timeout = aq->aq_timeout; - return (ASR_NEED_READ); + return (ASR_COND); } aq->aq_state = ASR_STATE_NEXT_NS; break; @@ -2413,13 +2417,15 @@ asr_run_cname(struct asr_query *aq, struct asr_result *ar) break; case 0: aq->aq_state = ASR_STATE_TCP_READ; + ar->ar_cond = ASR_READ; ar->ar_fd = aq->aq_fd; ar->ar_timeout = aq->aq_timeout; - return (ASR_NEED_READ); + return (ASR_COND); case 1: + ar->ar_cond = ASR_WRITE; ar->ar_fd = aq->aq_fd; ar->ar_timeout = aq->aq_timeout; - return (ASR_NEED_WRITE); + return (ASR_COND); } break; @@ -2437,9 +2443,10 @@ asr_run_cname(struct asr_query *aq, struct asr_result *ar) aq->aq_state = ASR_STATE_PACKET; break; case 1: + ar->ar_cond = ASR_READ; ar->ar_fd = aq->aq_fd; ar->ar_timeout = aq->aq_timeout; - return (ASR_NEED_READ); + return (ASR_COND); } break; diff --git a/usr.sbin/smtpd/asr.h b/usr.sbin/smtpd/asr.h index 142e8124cea..d46ad7f0344 100644 --- a/usr.sbin/smtpd/asr.h +++ b/usr.sbin/smtpd/asr.h @@ -21,12 +21,14 @@ #include <netinet/in.h> enum { - ASR_NEED_READ, - ASR_NEED_WRITE, + ASR_COND, ASR_YIELD, ASR_DONE }; +#define ASR_READ 0x01 +#define ASR_WRITE 0x02 + #define ASR_NOREC 0x01 enum { @@ -41,10 +43,13 @@ enum { }; struct asr_result { - int ar_fd; - int ar_timeout; int ar_err; const char *ar_errstr; + + int ar_cond; + int ar_fd; + int ar_timeout; + int ar_count; struct addrinfo *ar_ai; char *ar_cname; diff --git a/usr.sbin/smtpd/dns.c b/usr.sbin/smtpd/dns.c index 30de4495e85..38db61ff3df 100644 --- a/usr.sbin/smtpd/dns.c +++ b/usr.sbin/smtpd/dns.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dns.c,v 1.32 2011/03/26 10:54:22 eric Exp $ */ +/* $OpenBSD: dns.c,v 1.33 2011/03/26 14:38:14 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org> @@ -169,16 +169,10 @@ dns_asr_handler(int fd, short event, void *arg) bzero(&ar, sizeof (ar)); switch ((ret = asr_run(dnssession->aq, &ar))) { - case ASR_NEED_READ: + case ASR_COND: tv.tv_usec = ar.ar_timeout * 1000; - event_set(&dnssession->ev, ar.ar_fd, EV_READ, - dns_asr_handler, dnssession); - event_add(&dnssession->ev, &tv); - return; - - case ASR_NEED_WRITE: - tv.tv_usec = ar.ar_timeout * 1000; - event_set(&dnssession->ev, ar.ar_fd, EV_WRITE, + event_set(&dnssession->ev, ar.ar_fd, + ar.ar_cond == ASR_READ ? EV_READ : EV_WRITE, dns_asr_handler, dnssession); event_add(&dnssession->ev, &tv); return; @@ -318,16 +312,10 @@ dns_asr_mx_handler(int fd, short event, void *arg) int ret; switch ((ret = asr_run(dnssession->aq, &ar))) { - case ASR_NEED_READ: - tv.tv_usec = ar.ar_timeout * 1000; - event_set(&dnssession->ev, ar.ar_fd, EV_READ, - dns_asr_mx_handler, dnssession); - event_add(&dnssession->ev, &tv); - return; - - case ASR_NEED_WRITE: + case ASR_COND: tv.tv_usec = ar.ar_timeout * 1000; - event_set(&dnssession->ev, ar.ar_fd, EV_WRITE, + event_set(&dnssession->ev, ar.ar_fd, + ar.ar_cond == ASR_READ ? EV_READ : EV_WRITE, dns_asr_mx_handler, dnssession); event_add(&dnssession->ev, &tv); return; |