diff options
author | Eric Faurot <eric@cvs.openbsd.org> | 2012-04-14 13:31:47 +0000 |
---|---|---|
committer | Eric Faurot <eric@cvs.openbsd.org> | 2012-04-14 13:31:47 +0000 |
commit | 845a8137ea15fc15a5385cd31af385cb7a6647fd (patch) | |
tree | 490578aaa3b3869af065ad92b130ea3f9527b6d9 /usr.sbin | |
parent | 1c463255c935d12494d54d6d5071600206ead964 (diff) |
improve readability
ok gilles@
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/smtpd/dns.c | 149 |
1 files changed, 74 insertions, 75 deletions
diff --git a/usr.sbin/smtpd/dns.c b/usr.sbin/smtpd/dns.c index eb2be76d9cf..85e530f0822 100644 --- a/usr.sbin/smtpd/dns.c +++ b/usr.sbin/smtpd/dns.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dns.c,v 1.47 2012/01/11 21:22:26 eric Exp $ */ +/* $OpenBSD: dns.c,v 1.48 2012/04/14 13:31:46 eric Exp $ */ /* * Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org> @@ -126,7 +126,7 @@ dns_query_ptr(struct sockaddr_storage *ss, u_int64_t id) void dns_async(struct imsgev *asker, int type, struct dns *query) { - struct dnssession *dnssession; + struct dnssession *s; if (asr == NULL && (asr = asr_resolver(NULL)) == NULL) { log_warnx("dns_async: cannot create resolver"); @@ -135,7 +135,7 @@ dns_async(struct imsgev *asker, int type, struct dns *query) query->type = type; query->asker = asker; - dnssession = dnssession_init(query); + s = dnssession_init(query); switch (type) { case IMSG_DNS_HOST: @@ -146,32 +146,32 @@ dns_async(struct imsgev *asker, int type, struct dns *query) query->error = DNS_OK; dns_reply(query, IMSG_DNS_HOST); dns_reply(query, IMSG_DNS_HOST_END); - dnssession_destroy(dnssession); + dnssession_destroy(s); return; } - dnssession_mx_insert(dnssession, query->host, 0); + dnssession_mx_insert(s, query->host, 0); stat_increment(STATS_LKA_SESSION_HOST); - dns_asr_dispatch_host(dnssession); + dns_asr_dispatch_host(s); return; case IMSG_DNS_PTR: - dnssession->aq = asr_query_cname(asr, - (struct sockaddr*)&query->ss, query->ss.ss_len); + s->aq = asr_query_cname(asr, (struct sockaddr*)&query->ss, + query->ss.ss_len); stat_increment(STATS_LKA_SESSION_CNAME); - if (dnssession->aq == NULL) { + if (s->aq == NULL) { log_debug("dns_async: asr_query_cname error"); break; } - dns_asr_dispatch_cname(dnssession); + dns_asr_dispatch_cname(s); return; case IMSG_DNS_MX: log_debug("dns: lookup mx \"%s\"", query->host); - dnssession->aq = asr_query_dns(asr, T_MX, C_IN, query->host, 0); + s->aq = asr_query_dns(asr, T_MX, C_IN, query->host, 0); stat_increment(STATS_LKA_SESSION_MX); - if (dnssession->aq == NULL) { + if (s->aq == NULL) { log_debug("dns_async: asr_query_dns error"); break; } - dns_asr_dispatch_mx(dnssession); + dns_asr_dispatch_mx(s); return; default: log_debug("dns_async: bad request"); @@ -179,7 +179,7 @@ dns_async(struct imsgev *asker, int type, struct dns *query) } stat_increment(STATS_LKA_FAILURE); - dnssession_destroy(dnssession); + dnssession_destroy(s); noasr: query->error = DNS_RETRY; dns_reply(query, type != IMSG_DNS_PTR ? IMSG_DNS_HOST_END : type); @@ -192,31 +192,30 @@ dns_reply(struct dns *query, int type) } static void -dns_asr_event_set(struct dnssession *dnssession, struct asr_result *ar) +dns_asr_event_set(struct dnssession *s, struct asr_result *ar) { struct timeval tv = { 0, 0 }; tv.tv_usec = ar->ar_timeout * 1000; - 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); + event_set(&s->ev, ar->ar_fd, + ar->ar_cond == ASR_READ ? EV_READ : EV_WRITE, dns_asr_handler, s); + event_add(&s->ev, &tv); } static void dns_asr_handler(int fd, short event, void *arg) { - struct dnssession *dnssession = arg; + struct dnssession *s = arg; - switch(dnssession->query.type) { + switch(s->query.type) { case IMSG_DNS_HOST: - dns_asr_dispatch_host(dnssession); + dns_asr_dispatch_host(s); break; case IMSG_DNS_PTR: - dns_asr_dispatch_cname(dnssession); + dns_asr_dispatch_cname(s); break; case IMSG_DNS_MX: - dns_asr_dispatch_mx(dnssession); + dns_asr_dispatch_mx(s); break; default: fatalx("bad query type"); @@ -239,9 +238,9 @@ dns_asr_error(int ar_err) } static void -dns_asr_dispatch_mx(struct dnssession *dnssession) +dns_asr_dispatch_mx(struct dnssession *s) { - struct dns *query = &dnssession->query; + struct dns *query = &s->query; struct asr_result ar; struct packed pack; struct header h; @@ -249,15 +248,15 @@ dns_asr_dispatch_mx(struct dnssession *dnssession) struct rr rr; char buf[512]; - if (asr_run(dnssession->aq, &ar) == ASR_COND) { - dns_asr_event_set(dnssession, &ar); + if (asr_run(s->aq, &ar) == ASR_COND) { + dns_asr_event_set(s, &ar); return; } if (ar.ar_err) { query->error = dns_asr_error(ar.ar_err); dns_reply(query, IMSG_DNS_HOST_END); - dnssession_destroy(dnssession); + dnssession_destroy(s); return; } @@ -270,19 +269,19 @@ dns_asr_dispatch_mx(struct dnssession *dnssession) if (RCODE(h.flags) == ERR_NAME) { query->error = DNS_ENONAME; dns_reply(query, IMSG_DNS_HOST_END); - dnssession_destroy(dnssession); + dnssession_destroy(s); return; } if (h.ancount == 0) /* fallback to host if no MX is found. */ - dnssession_mx_insert(dnssession, query->host, 0); + dnssession_mx_insert(s, query->host, 0); for (; h.ancount; h.ancount--) { unpack_rr(&pack, &rr); print_dname(rr.rr.mx.exchange, buf, sizeof(buf)); buf[strlen(buf) - 1] = '\0'; - dnssession_mx_insert(dnssession, buf, rr.rr.mx.preference); + dnssession_mx_insert(s, buf, rr.rr.mx.preference); } free(ar.ar_data); @@ -291,71 +290,71 @@ dns_asr_dispatch_mx(struct dnssession *dnssession) /* Now we have a sorted list of MX to resolve. Simply "turn" this * MX session into a regular host session. */ - dnssession->aq = NULL; - dnssession->query.type = IMSG_DNS_HOST; - dns_asr_dispatch_host(dnssession); + s->aq = NULL; + s->query.type = IMSG_DNS_HOST; + dns_asr_dispatch_host(s); } static void -dns_asr_dispatch_host(struct dnssession *dnssession) +dns_asr_dispatch_host(struct dnssession *s) { - struct dns *query = &dnssession->query; + struct dns *query = &s->query; struct mx *mx; struct asr_result ar; int ret; /* default to notfound, override with retry or ok later */ - if (dnssession->mxcurrent == 0) + if (s->mxcurrent == 0) query->error = DNS_ENOTFOUND; next: /* query all listed hosts in turn */ - while (dnssession->aq == NULL) { - if (dnssession->mxcurrent == dnssession->mxarraysz) { - if (dnssession->mxfound) + while (s->aq == NULL) { + if (s->mxcurrent == s->mxarraysz) { + if (s->mxfound) query->error = DNS_OK; dns_reply(query, IMSG_DNS_HOST_END); - dnssession_destroy(dnssession); + dnssession_destroy(s); return; } - mx = dnssession->mxarray + dnssession->mxcurrent++; - dnssession->aq = asr_query_host(asr, mx->host, AF_UNSPEC); + mx = s->mxarray + s->mxcurrent++; + s->aq = asr_query_host(asr, mx->host, AF_UNSPEC); } - while ((ret = asr_run(dnssession->aq, &ar)) == ASR_YIELD) { + while ((ret = asr_run(s->aq, &ar)) == ASR_YIELD) { free(ar.ar_cname); memcpy(&query->ss, &ar.ar_sa.sa, ar.ar_sa.sa.sa_len); dns_reply(query, IMSG_DNS_HOST); - dnssession->mxfound++; + s->mxfound++; } if (ret == ASR_COND) { - dns_asr_event_set(dnssession, &ar); + dns_asr_event_set(s, &ar); return; } if (dns_asr_error(ar.ar_err) == DNS_RETRY) query->error = DNS_RETRY; - dnssession->aq = NULL; + s->aq = NULL; goto next; } static void -dns_asr_dispatch_cname(struct dnssession *dnssession) +dns_asr_dispatch_cname(struct dnssession *s) { - struct dns *query = &dnssession->query; + struct dns *query = &s->query; struct asr_result ar; - switch (asr_run(dnssession->aq, &ar)) { + switch (asr_run(s->aq, &ar)) { case ASR_COND: - dns_asr_event_set(dnssession, &ar); + dns_asr_event_set(s, &ar); return; case ASR_YIELD: /* Only return the first answer */ query->error = DNS_OK; strlcpy(query->host, ar.ar_cname, sizeof (query->host)); - asr_abort(dnssession->aq); + asr_abort(s->aq); free(ar.ar_cname); break; case ASR_DONE: @@ -363,56 +362,56 @@ dns_asr_dispatch_cname(struct dnssession *dnssession) break; } dns_reply(query, IMSG_DNS_PTR); - dnssession_destroy(dnssession); + dnssession_destroy(s); } static struct dnssession * dnssession_init(struct dns *query) { - struct dnssession *dnssession; + struct dnssession *s; - dnssession = calloc(1, sizeof(struct dnssession)); - if (dnssession == NULL) + s = calloc(1, sizeof(struct dnssession)); + if (s == NULL) fatal("dnssession_init: calloc"); stat_increment(STATS_LKA_SESSION); - dnssession->id = query->id; - dnssession->query = *query; - SPLAY_INSERT(dnstree, &dns_sessions, dnssession); - return dnssession; + s->id = query->id; + s->query = *query; + SPLAY_INSERT(dnstree, &dns_sessions, s); + return (s); } static void -dnssession_destroy(struct dnssession *dnssession) +dnssession_destroy(struct dnssession *s) { stat_decrement(STATS_LKA_SESSION); - SPLAY_REMOVE(dnstree, &dns_sessions, dnssession); - event_del(&dnssession->ev); - free(dnssession); + SPLAY_REMOVE(dnstree, &dns_sessions, s); + event_del(&s->ev); + free(s); } static void -dnssession_mx_insert(struct dnssession *dnssession, const char *host, int prio) +dnssession_mx_insert(struct dnssession *s, const char *host, int prio) { size_t i, j; - for (i = 0; i < dnssession->mxarraysz; i++) - if (prio < dnssession->mxarray[i].prio) + for (i = 0; i < s->mxarraysz; i++) + if (prio < s->mxarray[i].prio) break; if (i == MAX_MX_COUNT) return; - if (dnssession->mxarraysz < MAX_MX_COUNT) - dnssession->mxarraysz++; + if (s->mxarraysz < MAX_MX_COUNT) + s->mxarraysz++; - for (j = dnssession->mxarraysz - 1; j > i; j--) - dnssession->mxarray[j] = dnssession->mxarray[j - 1]; + for (j = s->mxarraysz - 1; j > i; j--) + s->mxarray[j] = s->mxarray[j - 1]; - dnssession->mxarray[i].prio = prio; - strlcpy(dnssession->mxarray[i].host, host, - sizeof (dnssession->mxarray[i].host)); + s->mxarray[i].prio = prio; + strlcpy(s->mxarray[i].host, host, + sizeof (s->mxarray[i].host)); } static int |