Verbose connect messages to show the IP addresses used
Also, the patch makes the error messages more verbose. Helps when diagnosing connect problems on weird systems. Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Alex Riesen committed
May 23, 2007 at 23:34 UTC
ba505322a4f0ac052f059e7381b55c4ab8bfeacd
1 file changed
+37
-1
connect.c
+37
-1
@@ -391,6 +391,23 @@ static enum protocol get_protocol(const char *name)
391
392
#ifndef NO_IPV6
393
394
+static const char *ai_name(const struct addrinfo *ai)
395
+{
396
+ static char addr[INET_ADDRSTRLEN];
397
+ if ( AF_INET == ai->ai_family ) {
398
+ struct sockaddr_in *in;
399
+ in = (struct sockaddr_in *)ai->ai_addr;
400
+ inet_ntop(ai->ai_family, &in->sin_addr, addr, sizeof(addr));
401
+ } else if ( AF_INET6 == ai->ai_family ) {
402
+ struct sockaddr_in6 *in;
403
+ in = (struct sockaddr_in6 *)ai->ai_addr;
404
+ inet_ntop(ai->ai_family, &in->sin6_addr, addr, sizeof(addr));
405
+ } else {
406
+ strcpy(addr, "(unknown)");
407
+ }
408
+ return addr;
409
+}
410
+
411
/*
412
* Returns a connected socket() fd, or else die()s.
413
*/
@@ -401,6 +418,7 @@ static int git_tcp_connect_sock(char *host, int flags)
418
const char *port = STR(DEFAULT_GIT_PORT);
419
struct addrinfo hints, *ai0, *ai;
420
int gai;
421
+ int cnt = 0;
422
423
if (host[0] == '[') {
424
end = strchr(host + 1, ']');
@@ -444,10 +462,18 @@ static int git_tcp_connect_sock(char *host, int flags)
462
}
463
if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
464
saved_errno = errno;
465
+ fprintf(stderr, "%s[%d: %s]: net=%s, errno=%s\n",
466
+ host,
467
+ cnt,
468
+ ai_name(ai),
469
+ hstrerror(h_errno),
470
+ strerror(saved_errno));
471
close(sockfd);
472
sockfd = -1;
473
continue;
474
}
475
+ if (flags & CONNECT_VERBOSE)
476
+ fprintf(stderr, "%s ", ai_name(ai));
477
break;
478
}
479
@@ -476,6 +502,7 @@ static int git_tcp_connect_sock(char *host, int flags)
502
struct sockaddr_in sa;
503
char **ap;
504
unsigned int nport;
505
+ int cnt;
506
507
if (host[0] == '[') {
508
end = strchr(host + 1, ']');
@@ -512,7 +539,7 @@ static int git_tcp_connect_sock(char *host, int flags)
539
if (flags & CONNECT_VERBOSE)
540
fprintf(stderr, "done.\nConnecting to %s (port %s) ... ", host, port);
541
515
- for (ap = he->h_addr_list; *ap; ap++) {
542
+ for (cnt = 0, ap = he->h_addr_list; *ap; ap++, cnt++) {
543
sockfd = socket(he->h_addrtype, SOCK_STREAM, 0);
544
if (sockfd < 0) {
545
saved_errno = errno;
@@ -526,10 +553,19 @@ static int git_tcp_connect_sock(char *host, int flags)
553
554
if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) {
555
saved_errno = errno;
556
+ fprintf(stderr, "%s[%d: %s]: net=%s, errno=%s\n",
557
+ host,
558
+ cnt,
559
+ inet_ntoa(*(struct in_addr *)&sa.sin_addr),
560
+ hstrerror(h_errno),
561
+ strerror(saved_errno));
562
close(sockfd);
563
sockfd = -1;
564
continue;
565
}
566
+ if (flags & CONNECT_VERBOSE)
567
+ fprintf(stderr, "%s ",
568
+ inet_ntoa(*(struct in_addr *)&sa.sin_addr));
569
break;
570
}
571