git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ident.c: add support for IPv6
@ 2015-10-30 14:48 Elia Pinto
  2015-10-30 17:26 ` Torsten Bögershausen
  2015-10-30 18:29 ` Jeff King
  0 siblings, 2 replies; 5+ messages in thread
From: Elia Pinto @ 2015-10-30 14:48 UTC (permalink / raw)
  To: git; +Cc: peff, Elia Pinto

Add IPv6 support by implementing name resolution with the
protocol agnostic getaddrinfo(3) API. The old gethostbyname(3)
code is still available when git is compiled with NO_IPV6.

Signed-off-by: Elia Pinto <gitter.spiros@gmail.com>
---
 ident.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/ident.c b/ident.c
index 5ff1aad..86b62be 100644
--- a/ident.c
+++ b/ident.c
@@ -69,6 +69,34 @@ static int add_mailname_host(struct strbuf *buf)
 	fclose(mailname);
 	return 0;
 }
+#ifndef NO_IPV6
+
+static void add_domainname(struct strbuf *out)
+{
+	char buf[1024];
+	struct addrinfo hints, *ai;
+	int gai;
+
+	if (gethostname(buf, sizeof(buf))) {
+		warning("cannot get host name: %s", strerror(errno));
+		strbuf_addstr(out, "(none)");
+		return;
+	}
+	if (strchr(buf, '.'))
+		strbuf_addstr(out, buf);
+	else	{
+		memset (&hints, '\0', sizeof (hints));
+		hints.ai_flags = AI_CANONNAME;
+		if (!(gai = getaddrinfo(buf, NULL, &hints, &ai)) && ai && strchr(ai->ai_canonname, '.')) {
+			strbuf_addstr(out, ai->ai_canonname);
+			freeaddrinfo(ai);
+		}
+		else
+			strbuf_addf(out, "%s.(none)", buf);
+	}
+}
+#else /* NO_IPV6 */
+
 
 static void add_domainname(struct strbuf *out)
 {
@@ -88,6 +116,8 @@ static void add_domainname(struct strbuf *out)
 		strbuf_addf(out, "%s.(none)", buf);
 }
 
+#endif /* NO_IPV6 */
+
 static void copy_email(const struct passwd *pw, struct strbuf *email)
 {
 	/*
-- 
2.3.3.GIT

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] ident.c: add support for IPv6
  2015-10-30 14:48 [PATCH] ident.c: add support for IPv6 Elia Pinto
@ 2015-10-30 17:26 ` Torsten Bögershausen
  2015-10-30 18:13   ` Junio C Hamano
  2015-10-30 18:19   ` Eric Sunshine
  2015-10-30 18:29 ` Jeff King
  1 sibling, 2 replies; 5+ messages in thread
From: Torsten Bögershausen @ 2015-10-30 17:26 UTC (permalink / raw)
  To: Elia Pinto, git; +Cc: peff

On 2015-10-30 15.48, Elia Pinto wrote:
> Add IPv6 support by implementing name resolution with the
Minor question: How is this related to IPV6?
Could the header line be written something like

"ident.c: Use getaddrinfo() instead of gethostbyname() if available"

On which systems has the patch been tested ?
Linux ?
Mac OS X ?
Windows ?
BSD ?

The motivation on which platforms the usage of getaddrinfo() is preferred
over gethostbyname() could be helpful to motivate this patch:
System XYZ behaves bad when gethostbyname() is used.
Fix it by using getaddrinfo() instead.

A more defensive patch could call getaddrinfo() (If available, iow
when NO_IPV6 is false), and if that fails for whatever reason,
fall back to gethostbyname(), which should be available on all systems.


> protocol agnostic getaddrinfo(3) API. The old gethostbyname(3)
> code is still available when git is compiled with NO_IPV6.
> 
> Signed-off-by: Elia Pinto <gitter.spiros@gmail.com>
> ---
>  ident.c | 30 ++++++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
> 
> diff --git a/ident.c b/ident.c
> index 5ff1aad..86b62be 100644
> --- a/ident.c
> +++ b/ident.c
> @@ -69,6 +69,34 @@ static int add_mailname_host(struct strbuf *buf)
>  	fclose(mailname);
>  	return 0;
>  }
> +#ifndef NO_IPV6
> +
> +static void add_domainname(struct strbuf *out)
> +{
> +	char buf[1024];
> +	struct addrinfo hints, *ai;
> +	int gai;
The scope of these variables can be narrowed, by moving them into the "{" block,
where they are needed. (Before the memset())
> +
> +	if (gethostname(buf, sizeof(buf))) {
> +		warning("cannot get host name: %s", strerror(errno));
> +		strbuf_addstr(out, "(none)");
> +		return;
> +	}
> +	if (strchr(buf, '.'))
> +		strbuf_addstr(out, buf);
> +	else	{
Many ' ' between else and '{', one should be enough
> +		memset (&hints, '\0', sizeof (hints));
> +		hints.ai_flags = AI_CANONNAME;
> +		if (!(gai = getaddrinfo(buf, NULL, &hints, &ai)) && ai && strchr(ai->ai_canonname, '.')) {
> +			strbuf_addstr(out, ai->ai_canonname);
> +			freeaddrinfo(ai);
> +		}
> +		else
Colud be written in one line as "} else"
> +			strbuf_addf(out, "%s.(none)", buf);
> +	}
> +}
> +#else /* NO_IPV6 */

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ident.c: add support for IPv6
  2015-10-30 17:26 ` Torsten Bögershausen
@ 2015-10-30 18:13   ` Junio C Hamano
  2015-10-30 18:19   ` Eric Sunshine
  1 sibling, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2015-10-30 18:13 UTC (permalink / raw)
  To: Torsten Bögershausen; +Cc: Elia Pinto, git, peff

Torsten Bögershausen <tboegi@web.de> writes:

> On 2015-10-30 15.48, Elia Pinto wrote:
>> Add IPv6 support by implementing name resolution with the
> Minor question: How is this related to IPV6?
> Could the header line be written something like
>
> "ident.c: Use getaddrinfo() instead of gethostbyname() if available"
>
> On which systems has the patch been tested ?
> Linux ?
> Mac OS X ?
> Windows ?
> BSD ?
>
> The motivation on which platforms the usage of getaddrinfo() is preferred
> over gethostbyname() could be helpful to motivate this patch:
> System XYZ behaves bad when gethostbyname() is used.
> Fix it by using getaddrinfo() instead.

gethostbyname() fills a hostent that gives us the official name,
list of aliases, _one_ addrtype (either AF_INET or AF_INET6), and
list of addresses, so if we were asking for the physical addresses,
we may not be able to obtain all addresses for a host with both IPv4
and IPv6 addresses, which may be an issue.

But this function is about learning the official name of the host,
given the result of gethostname().  In that context, does that
"limited to single address family" issue of gethostbyname() still
matter?  I am guessing it doesn't, and I somehow doubt that the
value of this patch is about working around any platform bug.

I think the real reason to favour getaddrinfo() over gethostbyname()
is that the family of functions the latter belongs to is obsolete.
In other codepaths where we need to learn the inet address, we
already use getaddrinfo() if available (i.e. NO_IPV6 is not set),
and gethostbyname() is used only when compiling with NO_IPV6.

Converting this codepath to match that pattern incidentally allows
you to work around a platform bugs like "gethostbyname() is broken
on sysmte XYZ" (which you work around by not saying NO_IPV6), but I
view it as a side effect.

>> +static void add_domainname(struct strbuf *out)
>> +{
>> +	char buf[1024];
>> +	struct addrinfo hints, *ai;
>> +	int gai;
> The scope of these variables can be narrowed, by moving them into the "{" block,
> where they are needed. (Before the memset())
>> +
>> +	if (gethostname(buf, sizeof(buf))) {
>> +		warning("cannot get host name: %s", strerror(errno));
>> +		strbuf_addstr(out, "(none)");
>> +		return;
>> +	}
>> +	if (strchr(buf, '.'))
>> +		strbuf_addstr(out, buf);
>> +	else	{
> Many ' ' between else and '{', one should be enough
>> +		memset (&hints, '\0', sizeof (hints));
>> +		hints.ai_flags = AI_CANONNAME;
>> +		if (!(gai = getaddrinfo(buf, NULL, &hints, &ai)) && ai && strchr(ai->ai_canonname, '.')) {
>> +			strbuf_addstr(out, ai->ai_canonname);
>> +			freeaddrinfo(ai);
>> +		}
>> +		else
> Colud be written in one line as "} else"
>> +			strbuf_addf(out, "%s.(none)", buf);
>> +	}
>> +}
>> +#else /* NO_IPV6 */

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ident.c: add support for IPv6
  2015-10-30 17:26 ` Torsten Bögershausen
  2015-10-30 18:13   ` Junio C Hamano
@ 2015-10-30 18:19   ` Eric Sunshine
  1 sibling, 0 replies; 5+ messages in thread
From: Eric Sunshine @ 2015-10-30 18:19 UTC (permalink / raw)
  To: Torsten Bögershausen; +Cc: Elia Pinto, Git List, Jeff King

On Fri, Oct 30, 2015 at 1:26 PM, Torsten Bögershausen <tboegi@web.de> wrote:
> On 2015-10-30 15.48, Elia Pinto wrote:
>> Add IPv6 support by implementing name resolution with the
>> ---
>> +#ifndef NO_IPV6
>> +
>> +static void add_domainname(struct strbuf *out)
>> +{
>> +     char buf[1024];
>> +     struct addrinfo hints, *ai;
>> +     int gai;
> The scope of these variables can be narrowed, by moving them into the "{" block,
> where they are needed. (Before the memset())
>> +
>> +     if (gethostname(buf, sizeof(buf))) {
>> +             warning("cannot get host name: %s", strerror(errno));
>> +             strbuf_addstr(out, "(none)");
>> +             return;
>> +     }
>> +     if (strchr(buf, '.'))
>> +             strbuf_addstr(out, buf);
>> +     else    {
> Many ' ' between else and '{', one should be enough
>> +             memset (&hints, '\0', sizeof (hints));
>> +             hints.ai_flags = AI_CANONNAME;
>> +             if (!(gai = getaddrinfo(buf, NULL, &hints, &ai)) && ai && strchr(ai->ai_canonname, '.')) {

Why is 'gai' needed and assigned? It's value is never consulted thereafter.

>> +                     strbuf_addstr(out, ai->ai_canonname);
>> +                     freeaddrinfo(ai);

Also, aren't you leaking 'ai' when 'ai_canonname' doesn't contain a '.'?

>> +             }
>> +             else
> Colud be written in one line as "} else"
>> +                     strbuf_addf(out, "%s.(none)", buf);
>> +     }
>> +}
>> +#else /* NO_IPV6 */

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ident.c: add support for IPv6
  2015-10-30 14:48 [PATCH] ident.c: add support for IPv6 Elia Pinto
  2015-10-30 17:26 ` Torsten Bögershausen
@ 2015-10-30 18:29 ` Jeff King
  1 sibling, 0 replies; 5+ messages in thread
From: Jeff King @ 2015-10-30 18:29 UTC (permalink / raw)
  To: Elia Pinto; +Cc: git

On Fri, Oct 30, 2015 at 03:48:07PM +0100, Elia Pinto wrote:

> Add IPv6 support by implementing name resolution with the
> protocol agnostic getaddrinfo(3) API. The old gethostbyname(3)
> code is still available when git is compiled with NO_IPV6.

Makes sense. I'm not excited by the duplication in the early part of the
function, though:

> +#ifndef NO_IPV6
> +
> +static void add_domainname(struct strbuf *out)
> +{
> +	char buf[1024];
> +	struct addrinfo hints, *ai;
> +	int gai;
> +
> +	if (gethostname(buf, sizeof(buf))) {
> +		warning("cannot get host name: %s", strerror(errno));
> +		strbuf_addstr(out, "(none)");
> +		return;
> +	}
> +	if (strchr(buf, '.'))
> +		strbuf_addstr(out, buf);
> +	else	{
> +		memset (&hints, '\0', sizeof (hints));
> +		hints.ai_flags = AI_CANONNAME;
> +		if (!(gai = getaddrinfo(buf, NULL, &hints, &ai)) && ai && strchr(ai->ai_canonname, '.')) {
> +			strbuf_addstr(out, ai->ai_canonname);
> +			freeaddrinfo(ai);
> +		}
> +		else
> +			strbuf_addf(out, "%s.(none)", buf);
> +	}
> +}

Especially the "(none)" stuff is ugly enough as it is, without being
duplicated in two spots. Can we factor out the else clause that calls
gethostbyname(), and just override that part with the #ifdef?

For that matter, we have a few other spots that use getaddrinfo and
#ifdef. I wonder if it would be possible to simply use getaddrinfo
everywhere, and make a compatibility wrapper that uses gethostbyname for
older systems. The cut-and-paste duplication in connect.c, for example,
is pretty egregious.

-Peff

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-10-30 18:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-30 14:48 [PATCH] ident.c: add support for IPv6 Elia Pinto
2015-10-30 17:26 ` Torsten Bögershausen
2015-10-30 18:13   ` Junio C Hamano
2015-10-30 18:19   ` Eric Sunshine
2015-10-30 18:29 ` Jeff King

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).