From 03550ec097156c76aea19af95c18bf2da62ccba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Tue, 4 Aug 2020 22:17:01 +0400 Subject: [PATCH] hostutils: accept zoneid in IPv6 addresses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marc-André Lureau --- glib/ghostutils.c | 6 ++++-- glib/tests/hostutils.c | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/glib/ghostutils.c b/glib/ghostutils.c index 1b497205a..66be5c9a6 100644 --- a/glib/ghostutils.c +++ b/glib/ghostutils.c @@ -685,6 +685,8 @@ g_hostname_is_ascii_encoded (const gchar *hostname) * Tests if @hostname is the string form of an IPv4 or IPv6 address. * (Eg, "192.168.0.1".) * + * Since 2.66, IPv6 addresses with a zone-id are accepted (RFC6874). + * * Returns: %TRUE if @hostname is an IP address * * Since: 2.22 @@ -716,7 +718,7 @@ g_hostname_is_ip_address (const gchar *hostname) nsegments = 0; skipped = FALSE; - while (*p && nsegments < 8) + while (*p && *p != '%' && nsegments < 8) { /* Each segment after the first must be preceded by a ':'. * (We also handle half of the "string starts with ::" case @@ -760,7 +762,7 @@ g_hostname_is_ip_address (const gchar *hostname) p = end; } - return !*p && (nsegments == 8 || skipped); + return (!*p || (p[0] == '%' && p[1])) && (nsegments == 8 || skipped); } parse_ipv4: diff --git a/glib/tests/hostutils.c b/glib/tests/hostutils.c index be17d65c8..d694e626c 100644 --- a/glib/tests/hostutils.c +++ b/glib/tests/hostutils.c @@ -207,6 +207,11 @@ static const struct { { "0123::123.45.67.89", TRUE }, { "::123.45.67.89", TRUE }, + /* accept zone-id from rfc6874 */ + { "0123:4567:89AB:cdef:3210:7654:ba98:FeDc%zoneid0", TRUE }, + { "fe80::dead:beef%zonÉid0%weird", TRUE }, + { "fe80::dead:beef%", FALSE }, + /* Contain non-hex chars */ { "012x:4567:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, { "0123:45x7:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, @@ -261,7 +266,6 @@ static const struct { { "0123:4567:89AB:cdef:123.45.67.89", FALSE }, { "0123:4567:89AB:cdef:3210:123.45.67.89:FeDc", FALSE }, - /* IPv4 tests */ { "123.45.67.89", TRUE },