git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jonathan Nieder <jrnieder@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>,
	Eric Wong <normalperson@yhbt.net>,
	Erik Faye-Lund <kusmabite@gmail.com>
Subject: [PATCH 6/5] tcp: make dns_resolve() return an error code
Date: Thu, 8 Mar 2012 07:16:20 -0600	[thread overview]
Message-ID: <20120308131620.GF9426@burratino> (raw)
In-Reply-To: <20120308124857.GA7666@burratino>

Date: Thu, 23 Jun 2011 05:23:57 -0500

getaddrinfo() and gethostbyname() use error codes to describe their
errors.  Pass that error code back to the caller when dns_resolve()
fails and the RESOLVE_FAIL_QUIETLY flag is set, so callers can save
the diagnosis and print it later:

	int saved_rerrno = dns_resolve(...);
	...
	if (saved_rerrno)
		die("resolver failed: %s", dns_strerror(saved_rerrno));

In the ipv4 codepath, we assume that h_errno is never 0 on error.
POSIX.1-2004 does not specify whether 0 is a valid value for h_errno,
but luckily common practice is for h_errno to be a strictly positive
integer (HOST_NOT_FOUND = 1, NO_DATA = 2, NO_RECOVERY = 3, or
TRY_AGAIN = 4).  If gethostbyname errors out with h_errno == 0 on some
platform, just let git die with a message indicating a BUG so the bad
assumption can be corrected.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
This makes saving up errors from a host resolution failure as in
v1.7.7-rc0~40^2 (connect: only log if all attempts failed, 2011-08-01)
possible.  By the way, this series is currently against "maint" for no
particular reason and presumably it would conflict with that patch. ;-)

I'll be happy to rebase against "master" some time soon.

 dns-ipv4.c |    7 +++++--
 dns-ipv4.h |    5 +++++
 dns-ipv6.c |    2 +-
 dns-ipv6.h |    1 +
 4 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/dns-ipv4.c b/dns-ipv4.c
index 911a8569..8820c9e8 100644
--- a/dns-ipv4.c
+++ b/dns-ipv4.c
@@ -9,8 +9,11 @@ int dns_resolve(const char *host, const char *port, int flags,
 	unsigned int nport;
 
 	he = gethostbyname(host);
-	if (!he && (flags & RESOLVE_FAIL_QUIETLY))
-		return -1;
+	if (!he && (flags & RESOLVE_FAIL_QUIETLY)) {
+		if (!h_errno)
+			die("BUG: gethostbyname failed but h_errno == 0");
+		return h_errno;
+	}
 	if (!he)
 		die("Unable to look up %s (%s)", host, hstrerror(h_errno));
 
diff --git a/dns-ipv4.h b/dns-ipv4.h
index c5f1778f..6f70d639 100644
--- a/dns-ipv4.h
+++ b/dns-ipv4.h
@@ -18,6 +18,10 @@ typedef struct ipv4_address resolved_address;
 
 enum {
 	RESOLVE_CANONNAME = 1,
+	/*
+	 * Quietly return an error code instead of exiting on error.
+	 * Callers can use dns_strerror() to get an error string.
+	 */
 	RESOLVE_FAIL_QUIETLY = 2
 };
 extern int dns_resolve(const char *host, const char *port, int flags,
@@ -63,6 +67,7 @@ static inline int dns_fill_sockaddr_(char *ap,
 #define dns_addrlen(addr, ai) sizeof((addr).sa)
 #define dns_canonname(addr, ai) ((ai).he->h_name)
 
+#define dns_strerror(n) hstrerror(n)
 #define dns_free(ai) do { /* nothing */ } while (0)
 
 #endif
diff --git a/dns-ipv6.c b/dns-ipv6.c
index f2325681..0b0e0602 100644
--- a/dns-ipv6.c
+++ b/dns-ipv6.c
@@ -41,7 +41,7 @@ int dns_resolve(const char *host, const char *port, int flags,
 
 	gai = getaddrinfo(host, port, &hints, res);
 	if (gai && (flags & RESOLVE_FAIL_QUIETLY))
-		return -1;
+		return gai;
 	if (gai)
 		die("Unable to look up %s (port %s) (%s)", host, port, gai_strerror(gai));
 
diff --git a/dns-ipv6.h b/dns-ipv6.h
index 16bf84b5..4211c9e2 100644
--- a/dns-ipv6.h
+++ b/dns-ipv6.h
@@ -26,6 +26,7 @@ extern char *dns_ip_address(const resolved_address *i,
 #define dns_addrlen(i, ai) ((i)->ai_addrlen)
 #define dns_canonname(i, ai) ((i)->ai_canonname)
 
+#define dns_strerror(gai) gai_strerror(gai)
 #define dns_free(ai) freeaddrinfo(ai)
 
 #endif
-- 
1.7.9.2

  parent reply	other threads:[~2012-03-08 13:16 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-08 12:48 [PATCH 0/5] transport: unify ipv4 and ipv6 code paths Jonathan Nieder
2012-03-08 13:03 ` [PATCH 1/5] transport: expose git_tcp_connect() and friends in new tcp.h Jonathan Nieder
2012-03-08 15:28   ` Erik Faye-Lund
2012-03-08 13:05 ` [PATCH 2/5] daemon: make host resolution a separate function Jonathan Nieder
2012-03-08 13:06 ` [PATCH 3/5] daemon: move locate_host() to tcp lib Jonathan Nieder
2012-03-08 13:09 ` [PATCH 4/5] tcp: unify ipv4 and ipv6 code paths Jonathan Nieder
2012-03-08 15:39   ` Erik Faye-Lund
2012-03-08 21:10     ` Jonathan Nieder
2012-03-08 13:11 ` [PATCH 5/5] daemon: check for errors retrieving IP address Jonathan Nieder
2012-03-08 13:16 ` Jonathan Nieder [this message]
2012-03-08 13:21 ` [PATCH 7/5] transport: optionally honor DNS SRV records Jonathan Nieder
2012-03-08 16:18   ` Erik Faye-Lund
2012-03-08 21:35     ` Jonathan Nieder
2012-03-09  7:07       ` Johannes Sixt
2012-03-09  8:00         ` Jonathan Nieder
2012-03-08 13:23 ` [PATCH 8/5] srv: tolerate broken DNS replies Jonathan Nieder
2012-03-08 22:28   ` Richard Hartmann
2012-06-11 18:12 ` [PATCH 0/5] transport: unify ipv4 and ipv6 code paths Erik Faye-Lund
2012-06-11 18:59   ` Junio C Hamano
2012-06-14  5:02   ` Jonathan Nieder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120308131620.GF9426@burratino \
    --to=jrnieder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=kusmabite@gmail.com \
    --cc=normalperson@yhbt.net \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).