Refactor git_tcp_connect() functions a little.
Add client side sending of "\0host=%s\0" extended arg for git native protocol, backwards compatibly. Signed-off-by: Jon Loeliger <jdl@jdl.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Jon Loeliger committed
Jun 6, 2006 at 22:58 UTC
5ad312bedede83f63de5e69ef5e5ddff46484eae
2 files changed
+49
-20
connect.c
+42
-18
@@ -322,7 +322,10 @@ static enum protocol get_protocol(const char *name)
322
323
#ifndef NO_IPV6
324
325
-static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
325
+/*
326
+ * Returns a connected socket() fd, or else die()s.
327
+ */
328
+static int git_tcp_connect_sock(char *host)
329
{
330
int sockfd = -1;
331
char *colon, *end;
@@ -356,7 +359,8 @@ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
359
die("Unable to look up %s (%s)", host, gai_strerror(gai));
360
361
for (ai0 = ai; ai; ai = ai->ai_next) {
359
- sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
362
+ sockfd = socket(ai->ai_family,
363
+ ai->ai_socktype, ai->ai_protocol);
364
if (sockfd < 0)
365
continue;
366
if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
@@ -372,15 +376,15 @@ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
376
if (sockfd < 0)
377
die("unable to connect a socket (%s)", strerror(errno));
378
375
- fd[0] = sockfd;
376
- fd[1] = sockfd;
377
- packet_write(sockfd, "%s %s\n", prog, path);
378
- return 0;
379
+ return sockfd;
380
}
381
382
#else /* NO_IPV6 */
383
383
-static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
384
+/*
385
+ * Returns a connected socket() fd, or else die()s.
386
+ */
387
+static int git_tcp_connect_sock(char *host)
388
{
389
int sockfd = -1;
390
char *colon, *end;
@@ -407,7 +411,6 @@ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
411
port = colon + 1;
412
}
413
410
-
414
he = gethostbyname(host);
415
if (!he)
416
die("Unable to look up %s (%s)", host, hstrerror(h_errno));
@@ -441,13 +444,21 @@ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
444
if (sockfd < 0)
445
die("unable to connect a socket (%s)", strerror(errno));
446
447
+ return sockfd;
448
+}
449
+
450
+#endif /* NO_IPV6 */
451
+
452
+
453
+static void git_tcp_connect(int fd[2],
454
+ const char *prog, char *host, char *path)
455
+{
456
+ int sockfd = git_tcp_connect_sock(host);
457
+
458
fd[0] = sockfd;
459
fd[1] = sockfd;
446
- packet_write(sockfd, "%s %s\n", prog, path);
447
- return 0;
460
}
461
450
-#endif /* NO_IPV6 */
462
463
static char *git_proxy_command = NULL;
464
static const char *rhost_name = NULL;
@@ -510,7 +521,8 @@ static int git_use_proxy(const char *host)
521
return (git_proxy_command && *git_proxy_command);
522
}
523
513
-static int git_proxy_connect(int fd[2], const char *prog, char *host, char *path)
524
+static void git_proxy_connect(int fd[2],
525
+ const char *prog, char *host, char *path)
526
{
527
char *port = STR(DEFAULT_GIT_PORT);
528
char *colon, *end;
@@ -547,12 +559,12 @@ static int git_proxy_connect(int fd[2], const char *prog, char *host, char *path
559
execlp(git_proxy_command, git_proxy_command, host, port, NULL);
560
die("exec failed");
561
}
562
+ if (pid < 0)
563
+ die("fork failed");
564
fd[0] = pipefd[0][0];
565
fd[1] = pipefd[1][1];
566
close(pipefd[0][1]);
567
close(pipefd[1][0]);
554
- packet_write(fd[1], "%s %s\n", prog, path);
555
- return pid;
568
}
569
570
/*
@@ -620,14 +632,26 @@ int git_connect(int fd[2], char *url, const char *prog)
632
}
633
634
if (protocol == PROTO_GIT) {
623
- int ret;
635
+ /* These underlying connection commands die() if they
636
+ * cannot connect.
637
+ */
638
+ char *target_host = strdup(host);
639
if (git_use_proxy(host))
625
- ret = git_proxy_connect(fd, prog, host, path);
640
+ git_proxy_connect(fd, prog, host, path);
641
else
627
- ret = git_tcp_connect(fd, prog, host, path);
642
+ git_tcp_connect(fd, prog, host, path);
643
+ /*
644
+ * Separate original protocol components prog and path
645
+ * from extended components with a NUL byte.
646
+ */
647
+ packet_write(fd[1],
648
+ "%s %s%chost=%s%c",
649
+ prog, path, 0,
650
+ target_host, 0);
651
+ free(target_host);
652
if (free_path)
653
free(path);
630
- return ret;
654
+ return 0;
655
}
656
657
if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
daemon.c
+7
-2
@@ -267,12 +267,17 @@ static int upload(char *dir)
267
static int execute(void)
268
{
269
static char line[1000];
270
- int len;
270
+ int pktlen, len;
271
272
alarm(init_timeout ? init_timeout : timeout);
273
- len = packet_read_line(0, line, sizeof(line));
273
+ pktlen = packet_read_line(0, line, sizeof(line));
274
alarm(0);
275
276
+ len = strlen(line);
277
+ if (pktlen != len)
278
+ loginfo("Extended attributes (%d bytes) exist <%.*s>",
279
+ (int) pktlen - len,
280
+ (int) pktlen - len, line + len + 1);
281
if (len && line[len-1] == '\n')
282
line[--len] = 0;
283