Do not log unless all connect() attempts fail

IPv6 hosts are often unreachable on the primarily IPv4 Internet and therefore we shouldn't print an error if there are still other hosts we can try to connect() to. This helps "git fetch --quiet" stay quiet. Signed-off-by: Dave Zarzycki <zarzycki@apple.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Dave Zarzycki committed Jul 12, 2011 at 09:28 UTC 63a995b657dab9c1021f56dfe17804d2ad07577e
1 file changed +11 -14
connect.c
+11 -14
@@ -192,7 +192,8 @@ static const char *ai_name(const struct addrinfo *ai)
192 */
193 static int git_tcp_connect_sock(char *host, int flags)
194 {
195 - int sockfd = -1, saved_errno = 0;
195 + struct strbuf error_message = STRBUF_INIT;
196 + int sockfd = -1;
197 const char *port = STR(DEFAULT_GIT_PORT);
198 struct addrinfo hints, *ai0, *ai;
199 int gai;
@@ -219,18 +220,12 @@ static int git_tcp_connect_sock(char *host, int flags)
220 for (ai0 = ai; ai; ai = ai->ai_next) {
221 sockfd = socket(ai->ai_family,
222 ai->ai_socktype, ai->ai_protocol);
222 - if (sockfd < 0) {
223 - saved_errno = errno;
224 - continue;
225 - }
226 - if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
227 - saved_errno = errno;
228 - fprintf(stderr, "%s[%d: %s]: errno=%s\n",
229 - host,
230 - cnt,
231 - ai_name(ai),
232 - strerror(saved_errno));
233 - close(sockfd);
223 + if ((sockfd < 0) ||
224 + (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0)) {
225 + strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
226 + host, cnt, ai_name(ai), strerror(errno));
227 + if (0 <= sockfd)
228 + close(sockfd);
229 sockfd = -1;
230 continue;
231 }
@@ -242,11 +237,13 @@ static int git_tcp_connect_sock(char *host, int flags)
237 freeaddrinfo(ai0);
238
239 if (sockfd < 0)
245 - die("unable to connect a socket (%s)", strerror(saved_errno));
240 + die("unable to connect to %s:\n%s", host, error_message.buf);
241
242 if (flags & CONNECT_VERBOSE)
243 fprintf(stderr, "done.\n");
244
245 + strbuf_release(&error_message);
246 +
247 return sockfd;
248 }
249