From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/10139 Path: news.gmane.org!not-for-mail From: Natanael Copa Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH v2] refactor name_from_dns Date: Wed, 15 Jun 2016 20:27:46 +0200 Message-ID: <20160615182746.6728-1-ncopa@alpinelinux.org> References: <20160527044057.GB10893@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1466015299 17537 80.91.229.3 (15 Jun 2016 18:28:19 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 15 Jun 2016 18:28:19 +0000 (UTC) Cc: Natanael Copa To: musl@lists.openwall.com Original-X-From: musl-return-10152-gllmg-musl=m.gmane.org@lists.openwall.com Wed Jun 15 20:28:18 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1bDFY2-0004Ot-BE for gllmg-musl@m.gmane.org; Wed, 15 Jun 2016 20:28:18 +0200 Original-Received: (qmail 7251 invoked by uid 550); 15 Jun 2016 18:28:15 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 7181 invoked from network); 15 Jun 2016 18:28:07 -0000 X-Mailer: git-send-email 2.8.4 In-Reply-To: <20160527044057.GB10893@brightrain.aerifal.cx> Xref: news.gmane.org gmane.linux.lib.musl.general:10139 Archived-At: loop over an address family / resource record mapping to avoid repetitive code. --- Changes since first version: - use static const for the struct. As discussed in IRC gcc manages to unroll it and the size of the name_from_dns (.text) is slightly smaller. src/network/lookup_name.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/network/lookup_name.c b/src/network/lookup_name.c index d3d97b4..fb7303a 100644 --- a/src/network/lookup_name.c +++ b/src/network/lookup_name.c @@ -141,20 +141,19 @@ static int name_from_dns(struct address buf[static MAXADDRS], char canon[static int qlens[2], alens[2]; int i, nq = 0; struct dpc_ctx ctx = { .addrs = buf, .canon = canon }; - - if (family != AF_INET6) { - qlens[nq] = __res_mkquery(0, name, 1, RR_A, 0, 0, 0, - qbuf[nq], sizeof *qbuf); - if (qlens[nq] == -1) - return EAI_NONAME; - nq++; - } - if (family != AF_INET) { - qlens[nq] = __res_mkquery(0, name, 1, RR_AAAA, 0, 0, 0, - qbuf[nq], sizeof *qbuf); - if (qlens[nq] == -1) - return EAI_NONAME; - nq++; + static const struct { int af; int rr; } afrr[2] = { + { .af = AF_INET6, .rr = RR_A }, + { .af = AF_INET, .rr = RR_AAAA }, + }; + + for (i=0; i<2; i++) { + if (family != afrr[i].af) { + qlens[nq] = __res_mkquery(0, name, 1, afrr[i].rr, + 0, 0, 0, qbuf[nq], sizeof *qbuf); + if (qlens[nq] == -1) + return EAI_NONAME; + nq++; + } } if (__res_msend_rc(nq, qp, qlens, ap, alens, sizeof *abuf, conf) < 0) -- 2.8.4