[PATCH] GIT: Try all addresses for given remote name

Try all addresses for given remote name until it succeeds.  Also
supports IPv6.

Signed-off-by: Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
This commit is contained in:
YOSHIFUJI Hideaki 2005-07-21 09:10:36 -04:00 committed by Linus Torvalds
parent 34862b2e24
commit 5ba884483f

View File

@ -96,42 +96,57 @@ static enum protocol get_protocol(const char *name)
die("I don't handle protocol '%s'", name); die("I don't handle protocol '%s'", name);
} }
static void lookup_host(const char *host, struct sockaddr *in) #define STR_(s) # s
{ #define STR(s) STR_(s)
struct addrinfo *res;
int ret;
ret = getaddrinfo(host, NULL, NULL, &res);
if (ret)
die("Unable to look up %s (%s)", host, gai_strerror(ret));
*in = *res->ai_addr;
freeaddrinfo(res);
}
static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path) static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
{ {
struct sockaddr addr; int sockfd = -1;
int port = DEFAULT_GIT_PORT, sockfd; char *colon, *end;
char *colon; char *port = STR(DEFAULT_GIT_PORT);
struct addrinfo hints, *ai0, *ai;
int gai;
colon = strchr(host, ':'); if (host[0] == '[') {
if (colon) { end = strchr(host + 1, ']');
char *end; if (end) {
unsigned long n = strtoul(colon+1, &end, 0); *end = 0;
if (colon[1] && !*end) { end++;
*colon = 0; host++;
port = n; } else
end = host;
} else
end = host;
colon = strchr(end, ':');
if (colon)
port = colon + 1;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
gai = getaddrinfo(host, port, &hints, &ai);
if (gai)
die("Unable to look up %s (%s)", host, gai_strerror(gai));
for (ai0 = ai; ai; ai = ai->ai_next) {
sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (sockfd < 0)
continue;
if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
close(sockfd);
sockfd = -1;
continue;
} }
break;
} }
lookup_host(host, &addr); freeaddrinfo(ai0);
((struct sockaddr_in *)&addr)->sin_port = htons(port);
sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
if (sockfd < 0) if (sockfd < 0)
die("unable to create socket (%s)", strerror(errno)); die("unable to connect a socket (%s)", strerror(errno));
if (connect(sockfd, (void *)&addr, sizeof(addr)) < 0)
die("unable to connect (%s)", strerror(errno));
fd[0] = sockfd; fd[0] = sockfd;
fd[1] = sockfd; fd[1] = sockfd;
packet_write(sockfd, "%s %s\n", prog, path); packet_write(sockfd, "%s %s\n", prog, path);