{"thread":{"id":"40835","subject":"[PATCHv2] ident.c: add support for IPv6","startedAt":"2015-11-19T13:54:20Z","lastAt":"2015-11-19T14:46:32Z","messageCount":2,"participants":["Elia Pinto","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"273500","messageId":"1447941260-5395-1-git-send-email-gitter.spiros@gmail.com","threadId":"40835","inReplyTo":null,"subject":"[PATCHv2] ident.c: add support for IPv6","fromName":"Elia Pinto","fromEmail":"gitter.spiros@gmail.com","sentAt":"2015-11-19T13:54:20Z","receivedAt":"2015-11-19T13:54:20Z","isPatch":false,"sender":{"key":"gitter.spiros@gmail.com","avatar":"https://avatars.githubusercontent.com/u/158490?v=4"},"body":"Add IPv6 support by implementing name resolution with the\nprotocol agnostic getaddrinfo(3) API. The old gethostbyname(3)\ncode is still available when git is compiled with NO_IPV6.\n\nSigned-off-by: Elia Pinto <gitter.spiros@gmail.com>\nHelped-by: Jeff King <peff@peff.net> \nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\n---\nThis is the second version of the patch ($gmane/280488)\nChanges from previous:\n\n- Simplified the implementation, avoiding the duplication of \nthe function add_domainname (Jeff King) ($gmane/280512)\n\n- Fixed a possible memory leak with getaddrinfo (Eric Sunshine)\n  ($gmane/280507)\n\n\n ident.c | 26 ++++++++++++++++++++++----\n 1 file changed, 22 insertions(+), 4 deletions(-)\n\ndiff --git a/ident.c b/ident.c\nindex 5ff1aad..283e83f 100644\n--- a/ident.c\n+++ b/ident.c\n@@ -73,7 +73,12 @@ static int add_mailname_host(struct strbuf *buf)\n static void add_domainname(struct strbuf *out)\n {\n \tchar buf[1024];\n+#ifndef NO_IPV6\n+\tstruct addrinfo hints, *ai;\n+\tint gai;\n+#else\n \tstruct hostent *he;\n+#endif /* NO_IPV6 */\n \n \tif (gethostname(buf, sizeof(buf))) {\n \t\twarning(\"cannot get host name: %s\", strerror(errno));\n@@ -82,10 +87,23 @@ static void add_domainname(struct strbuf *out)\n \t}\n \tif (strchr(buf, '.'))\n \t\tstrbuf_addstr(out, buf);\n-\telse if ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))\n-\t\tstrbuf_addstr(out, he->h_name);\n-\telse\n-\t\tstrbuf_addf(out, \"%s.(none)\", buf);\n+\telse\t{\n+#ifndef NO_IPV6\n+\t\tmemset (&hints, '\\0', sizeof (hints));\n+\t\thints.ai_flags = AI_CANONNAME;\n+\t\tif (!(gai = getaddrinfo(buf, NULL, &hints, &ai)) && ai && strchr(ai->ai_canonname, '.')) \n+\t\t\tstrbuf_addstr(out, ai->ai_canonname);\n+#else\n+\t\tif ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))\n+\t\t\tstrbuf_addstr(out, he->h_name);\n+#endif /* NO_IPV6 */\n+\t\telse\n+\t\t\tstrbuf_addf(out, \"%s.(none)\", buf);\n+\n+#ifndef NO_IPV6\n+\t\tif (gai) freeaddrinfo(ai);\n+#endif /* NO_IPV6 */\n+\t}\n }\n \n static void copy_email(const struct passwd *pw, struct strbuf *email)\n-- \n2.5.0\n"},{"id":"273504","messageId":"20151119144632.GD9353@sigill.intra.peff.net","threadId":"40835","inReplyTo":"1447941260-5395-1-git-send-email-gitter.spiros@gmail.com","subject":"Re: [PATCHv2] ident.c: add support for IPv6","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2015-11-19T14:46:32Z","receivedAt":"2015-11-19T14:46:32Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Nov 19, 2015 at 02:54:20PM +0100, Elia Pinto wrote:\n\n> Add IPv6 support by implementing name resolution with the\n> protocol agnostic getaddrinfo(3) API. The old gethostbyname(3)\n> code is still available when git is compiled with NO_IPV6.\n> \n> Signed-off-by: Elia Pinto <gitter.spiros@gmail.com>\n> Helped-by: Jeff King <peff@peff.net> \n> Helped-by: Eric Sunshine <sunshine@sunshineco.com>\n> ---\n> This is the second version of the patch ($gmane/280488)\n> Changes from previous:\n> \n> - Simplified the implementation, avoiding the duplication of \n> the function add_domainname (Jeff King) ($gmane/280512)\n> \n> - Fixed a possible memory leak with getaddrinfo (Eric Sunshine)\n>   ($gmane/280507)\n\nThanks, this looks much nicer than the last round. I still have a few\ncomments:\n\n> diff --git a/ident.c b/ident.c\n> index 5ff1aad..283e83f 100644\n> --- a/ident.c\n> +++ b/ident.c\n> @@ -73,7 +73,12 @@ static int add_mailname_host(struct strbuf *buf)\n>  static void add_domainname(struct strbuf *out)\n>  {\n>  \tchar buf[1024];\n> +#ifndef NO_IPV6\n> +\tstruct addrinfo hints, *ai;\n> +\tint gai;\n> +#else\n>  \tstruct hostent *he;\n> +#endif /* NO_IPV6 */\n\nCan we bump these declarations closer to the point of use? I think that\nmakes it easier to read, as the matching bits are all together.\n\nSo this:\n\n> +\telse\t{\n> +#ifndef NO_IPV6\n> +\t\tmemset (&hints, '\\0', sizeof (hints));\n> +\t\thints.ai_flags = AI_CANONNAME;\n> +\t\tif (!(gai = getaddrinfo(buf, NULL, &hints, &ai)) && ai && strchr(ai->ai_canonname, '.')) \n> +\t\t\tstrbuf_addstr(out, ai->ai_canonname);\n> +#else\n> +\t\tif ((he = gethostbyname(buf)) && strchr(he->h_name, '.'))\n> +\t\t\tstrbuf_addstr(out, he->h_name);\n> +#endif /* NO_IPV6 */\n\nbecomes:\n\n      else {\n  #ifndef NO_IPV6\n\tstruct addrinfo hints, *ai;\n\tint gai;\n\tif (!(gai = getaddrinfo()...))\n  #else\n\tstruct hostent *he;\n\tif ((he = gethostbyname()....))\n  #endif\n\n> +\t\telse\n> +\t\t\tstrbuf_addf(out, \"%s.(none)\", buf);\n\nThis depends on both sides of the #if ending with a hanging \"if\" clause.\nI guess that's OK in such a small function, but I do still wonder if we\ncan have a helper like:\n\n  static void canonical_name(const char *host, struct strbuf *out)\n  {\n  #ifndef NO_IPV6\n\tstruct addrinfo hints, *ai;\n\tint gai = getaddrinfo(host, NULL, &hints, &ai);\n\tif (!gai && ai && strchr(ai->ai_canonname, '.')) {\n\t\tstrbuf_addstr(out, ai->ai_canonname);\n\t\tfreeaddrinfo(ai);\n\t}\n  #else\n\tstruct hostent *he = gethostbyname(buf);\n\tif (he && strchr(he->h_name, '.'))\n\t\tstrbuf_addstr(out, he->h_name);\n  }\n\n  ...\n  canonical_name(buf, out);\n  if (!out.len)\n\tstrbuf_addf(out, \"%s.(none)\", buf);\n\n(or possibly have an integer return value from canonical_name instead of\nchecking the strbuf length).\n\n> +#ifndef NO_IPV6\n> +\t\tif (gai) freeaddrinfo(ai);\n> +#endif /* NO_IPV6 */\n\nShould this be \"if (!gai)\"?\n\n-Peff\n"}]}