From patchwork Wed Feb 6 16:52:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 31332 Received: (qmail 23447 invoked by alias); 6 Feb 2019 16:52:33 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 23437 invoked by uid 89); 6 Feb 2019 16:52:33 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Three, family X-HELO: mx1.redhat.com From: Florian Weimer To: Adhemerval Zanella Cc: libc-alpha@sourceware.org Subject: Re: [PATCH] nss: getent: Print IPv6 scope ID for ahosts/ahostsv6 if available References: <875zu4d7x0.fsf@oldenburg2.str.redhat.com> <8798d68d-412e-a9d8-28bd-3f7bfd675c8c@linaro.org> <87r2cr8nwj.fsf@oldenburg2.str.redhat.com> <6029afa1-96e7-d780-c51e-d27d6cc475d3@linaro.org> Date: Wed, 06 Feb 2019 17:52:28 +0100 Message-ID: <87lg2s506r.fsf@oldenburg2.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 * Adhemerval Zanella: >> @@ -393,15 +394,30 @@ ahosts_keys_int (int af, int xflags, int number, char *key[]) >> sockstr = sockbuf; >> } >> >> + char scope[3 * sizeof (uint32_t) + 2]; > > I would prefer something more explicit as: > > /* It requires to hold a uint32_t plus a '%'. */ > cat scope[((CHAR_BIT * sizeof(uint32_t) / 3) + 3) + 1]; > > But I don't have a strong opinion here. “3 digits per byte” is sufficiently universal that I find your longer version not particularly illuminating. 8-/ I have added a commented about the %. New patch below. >> + int pad = 15 - strlen (buf) - strlen (scope); >> + if (pad < 0) >> + pad = 0; > > Not fun of the magic constant, but it came from original code anyway. Right. >> + >> + printf ("%s%-*s %-6s %s\n", >> + buf, pad, scope, sockstr, runp->ai_canonname ?: ""); > > Do we need to print the scope for invalid address as well? No, I think the invalid address can only happen (theoretically) for an unexpected address family from getaddrinfo. Thanks, Florian nss: getent: Print IPv6 scope ID for ahosts/ahostsv6 if available 2019-02-06 Florian Weimer * nss/getent.c (ahosts_keys_int): Include IPv6 scope ID in output. diff --git a/nss/getent.c b/nss/getent.c index f25de8f1fc..df73d8fb29 100644 --- a/nss/getent.c +++ b/nss/getent.c @@ -40,6 +40,7 @@ #include #include #include +#include /* Get libc version number. */ #include @@ -393,15 +394,31 @@ ahosts_keys_int (int af, int xflags, int number, char *key[]) sockstr = sockbuf; } + /* Three digits per byte, plus '%' and null terminator. */ + char scope[3 * sizeof (uint32_t) + 2]; + struct sockaddr_in6 *addr6 + = (struct sockaddr_in6 *) runp->ai_addr; + if (runp->ai_family != AF_INET6 || addr6->sin6_scope_id == 0) + /* No scope ID present. */ + scope[0] = '\0'; + else + snprintf (scope, sizeof (scope), "%%%" PRIu32, + addr6->sin6_scope_id); + char buf[INET6_ADDRSTRLEN]; - printf ("%-15s %-6s %s\n", - inet_ntop (runp->ai_family, - runp->ai_family == AF_INET - ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr - : (void *) &((struct sockaddr_in6 *) runp->ai_addr)->sin6_addr, - buf, sizeof (buf)), - sockstr, - runp->ai_canonname ?: ""); + if (inet_ntop (runp->ai_family, + runp->ai_family == AF_INET + ? (void *) &((struct sockaddr_in *) runp->ai_addr)->sin_addr + : &addr6->sin6_addr, + buf, sizeof (buf)) == NULL) + strcpy (buf, ""); + + int pad = 15 - strlen (buf) - strlen (scope); + if (pad < 0) + pad = 0; + + printf ("%s%-*s %-6s %s\n", + buf, pad, scope, sockstr, runp->ai_canonname ?: ""); runp = runp->ai_next; }