# [PATCH 1/2] Support addresses with ':' in git-daemon

5 messages from 2010-01-26 to 2010-01-26. Participants: Ilari Liusvaara, Junio C Hamano, Tay Ray Chuan.
Thread: https://gitlist.dev/t/22398

## Ilari Liusvaara, 2010-01-26 18:24

Subject: [PATCH 0/2] Allow using ':' in git:// hostname.
Message-ID: <1264530282-4783-1-git-send-email-ilari.liusvaara@elisanet.fi>
URL: https://gitlist.dev/e/1264530282-4783-1-git-send-email-ilari.liusvaara%40elisanet.fi

```
This series fixes two problems with using addresses containing ':'
(e.g. IPv6 numeric addresses) with git://:

1) ':' in hostname makes vhost headers impossible to parse

If there is ':' in address, the vhost headers become impossible
to parse because ':' is also splits host and port and port is
optional. Change git-daemon to be able to perform address unwrapping
so there is uniquely parseable syntax for hostnames containg
':' (this is compatible to how git-remote-gits encodes such vhost
headers and how git-daemon2[1] decodes them).

2) Client double-unwraps addresses

With git://, the addresses are unwrapped twice, which breaks
address parsing for addresses enclosed by [], which in turn is
required for hostnames containing ':'.  This is changed to unwarp
the addresses only once. This also changes wrapped addresses to
be sent as wrapped for vhost headers (the first patch adds ability
to parse this).

[1] The reference implementation of gits:// server daemon.

Ilari Liusvaara (2):
  Support addresses with ':' in git-daemon
  Allow use of []-wrapped addresses in git://

 connect.c |   10 ++++++++--
 daemon.c  |   34 ++++++++++++++++++++++++++++++----
 2 files changed, 38 insertions(+), 6 deletions(-)

```

## Ilari Liusvaara, 2010-01-26 18:24

Subject: [PATCH 1/2] Support addresses with ':' in git-daemon
Message-ID: <1264530282-4783-2-git-send-email-ilari.liusvaara@elisanet.fi>
URL: https://gitlist.dev/e/1264530282-4783-2-git-send-email-ilari.liusvaara%40elisanet.fi
In-Reply-To: <1264530282-4783-1-git-send-email-ilari.liusvaara@elisanet.fi>

```
If host address could have ':' in it (e.g. numeric IPv6 address), then
host and port could not be uniquely parsed. Fix this by parsing the
"["<host>"]":<port> and "["<host>"]" notations. Currently the built-in
git:// client would send <host>:<port> or <host> for such thing, but
it doesn't matter as due to bugs, resolving address fails if <host>
contains ':'.

Signed-off-by: Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
---
 daemon.c |   34 ++++++++++++++++++++++++++++++----
 1 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/daemon.c b/daemon.c
index 360635e..6c2bd97 100644
--- a/daemon.c
+++ b/daemon.c
@@ -399,6 +399,33 @@ static char *xstrdup_tolower(const char *str)
 	return dup;
 }
 
+static void parse_host_and_port(char *hostport, char **host,
+	char **port)
+{
+	if (*hostport == '[') {
+		char *end;
+
+		end = strchr(hostport, ']');
+		if (!end)
+			die("Invalid reqeuest ('[' without ']')");
+		*end = '\0';
+		*host = hostport + 1;
+		if (!end[1])
+			*port = NULL;
+		else if (end[1] == ':')
+			*port = end + 2;
+		else
+			die("Garbage after end of host part");
+	} else {
+		*host = hostport;
+		*port = strrchr(hostport, ':');
+		if (*port) {
+			*port = '\0';
+			++*port;
+		}
+	}
+}
+
 /*
  * Read the host as supplied by the client connection.
  */
@@ -415,11 +442,10 @@ static void parse_host_arg(char *extra_args, int buflen)
 			vallen = strlen(val) + 1;
 			if (*val) {
 				/* Split <host>:<port> at colon. */
-				char *host = val;
-				char *port = strrchr(host, ':');
+				char *host;
+				char *port;
+				parse_host_and_port(val, &host, &port);
 				if (port) {
-					*port = 0;
-					port++;
 					free(tcp_port);
 					tcp_port = xstrdup(port);
 				}
-- 
1.6.6.1.439.gf06b6

```

## Ilari Liusvaara, 2010-01-26 18:24

Subject: [PATCH 2/2] Allow use of []-wrapped addresses in git://
Message-ID: <1264530282-4783-3-git-send-email-ilari.liusvaara@elisanet.fi>
URL: https://gitlist.dev/e/1264530282-4783-3-git-send-email-ilari.liusvaara%40elisanet.fi
In-Reply-To: <1264530282-4783-1-git-send-email-ilari.liusvaara@elisanet.fi>

```
Allow using "["<host>"]":<port> and "["<host>"]" notations in git://
host addresses. This is needed to be able to connect to addresses
that contain ':' (e.g. numeric IPv6 addresses). Also send the host
header []-wrapped so it can actually be parsed by remote end.

Signed-off-by: Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
---
 connect.c |   10 ++++++++--
 1 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/connect.c b/connect.c
index 3a12562..20054e4 100644
--- a/connect.c
+++ b/connect.c
@@ -502,12 +502,18 @@ struct child_process *git_connect(int fd[2], const char *url_orig,
 		c = ':';
 	}
 
+	/*
+	 * Don't do destructive transforms with git:// as that
+	 * protocol code does '[]' dewrapping of its own.
+	 */
 	if (host[0] == '[') {
 		end = strchr(host + 1, ']');
 		if (end) {
-			*end = 0;
+			if (protocol != PROTO_GIT) {
+				*end = 0;
+				host++;
+			}
 			end++;
-			host++;
 		} else
 			end = host;
 	} else
-- 
1.6.6.1.439.gf06b6

```

## Junio C Hamano, 2010-01-26 19:59

Subject: Re: [PATCH 0/2] Allow using ':' in git:// hostname.
Message-ID: <7vfx5szlgm.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vfx5szlgm.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1264530282-4783-1-git-send-email-ilari.liusvaara@elisanet.fi>

```
Seems like a maint material (together with your xmallocz() one).
Thanks.

```

## Tay Ray Chuan, 2010-01-26 22:53

Subject: Re: [PATCH 1/2] Support addresses with ':' in git-daemon
Message-ID: <be6fef0d1001261453v2fd624fdv88640aa706c0009@mail.gmail.com>
URL: https://gitlist.dev/e/be6fef0d1001261453v2fd624fdv88640aa706c0009%40mail.gmail.com
In-Reply-To: <1264530282-4783-2-git-send-email-ilari.liusvaara@elisanet.fi>

```
Hi,

On Wed, Jan 27, 2010 at 2:24 AM, Ilari Liusvaara
<ilari.liusvaara@elisanet.fi> wrote:
> diff --git a/daemon.c b/daemon.c
> index 360635e..6c2bd97 100644
> --- a/daemon.c
> +++ b/daemon.c
> @@ -399,6 +399,33 @@ static char *xstrdup_tolower(const char *str)
>        return dup;
>  }
>
> +static void parse_host_and_port(char *hostport, char **host,
> +       char **port)
> +{
> +       if (*hostport == '[') {
> +               char *end;
> +
> +               end = strchr(hostport, ']');
> +               if (!end)
> +                       die("Invalid reqeuest ('[' without ']')");

s/reqeuest/request/.

-- 
Cheers,
Ray Chuan

```
