connect: only log if all attempts failed (ipv4)

In 63a995b (Do not log unless all connect() attempts fail), a mechanism to only log connection errors if all attempts failed was introduced for the IPv6 code-path, but not for the IPv4 one. Introduce a matching mechanism so IPv4-users also benefit from this noise-reduction. Move the call to socket after filling in sa, to make it more apparent that errno can't change in between. Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Erik Faye-Lund committed Aug 1, 2011 at 13:16 UTC 7203a2d1ca18b32feda71a48c21e80fb03f2e909
1 file changed +10 -13
connect.c
+10 -13
@@ -254,7 +254,8 @@ static int git_tcp_connect_sock(char *host, int flags)
254 */
255 static int git_tcp_connect_sock(char *host, int flags)
256 {
257 - int sockfd = -1, saved_errno = 0;
257 + struct strbuf error_message = STRBUF_INIT;
258 + int sockfd = -1;
259 const char *port = STR(DEFAULT_GIT_PORT);
260 char *ep;
261 struct hostent *he;
@@ -284,25 +285,21 @@ static int git_tcp_connect_sock(char *host, int flags)
285 fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
286
287 for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
287 - sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
288 - if (sockfd < 0) {
289 - saved_errno = errno;
290 - continue;
291 - }
292 -
288 memset(&sa, 0, sizeof sa);
289 sa.sin_family = he->h_addrtype;
290 sa.sin_port = htons(nport);
291 memcpy(&sa.sin_addr, *ap, he->h_length);
292
298 - if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
299 - saved_errno = errno;
300 - fprintf(stderr, "%s[%d: %s]: errno=%s\n",
293 + sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
294 + if ((sockfd < 0) ||
295 + connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
296 + strbuf_addf(&error_message, "%s[%d: %s]: errno=%s\n",
297 host,
298 cnt,
299 inet_ntoa(*(struct in_addr *)&sa.sin_addr),
304 - strerror(saved_errno));
305 - close(sockfd);
300 + strerror(errno));
301 + if (0 <= sockfd)
302 + close(sockfd);
303 sockfd = -1;
304 continue;
305 }
@@ -313,7 +310,7 @@ static int git_tcp_connect_sock(char *host, int flags)
310 }
311
312 if (sockfd < 0)
316 - die("unable to connect a socket (%s)", strerror(saved_errno));
313 + die("unable to connect to %s:\n%s", host, error_message.buf);
314
315 if (flags & CONNECT_VERBOSE)
316 fprintf(stderr, "done.\n");