hostutils: accept zoneid in IPv6 addresses

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
This commit is contained in:
Marc-André Lureau 2020-08-04 22:17:01 +04:00
parent 50343afb6e
commit 03550ec097
2 changed files with 9 additions and 3 deletions

View File

@ -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. * Tests if @hostname is the string form of an IPv4 or IPv6 address.
* (Eg, "192.168.0.1".) * (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 * Returns: %TRUE if @hostname is an IP address
* *
* Since: 2.22 * Since: 2.22
@ -716,7 +718,7 @@ g_hostname_is_ip_address (const gchar *hostname)
nsegments = 0; nsegments = 0;
skipped = FALSE; skipped = FALSE;
while (*p && nsegments < 8) while (*p && *p != '%' && nsegments < 8)
{ {
/* Each segment after the first must be preceded by a ':'. /* Each segment after the first must be preceded by a ':'.
* (We also handle half of the "string starts with ::" case * (We also handle half of the "string starts with ::" case
@ -760,7 +762,7 @@ g_hostname_is_ip_address (const gchar *hostname)
p = end; p = end;
} }
return !*p && (nsegments == 8 || skipped); return (!*p || (p[0] == '%' && p[1])) && (nsegments == 8 || skipped);
} }
parse_ipv4: parse_ipv4:

View File

@ -207,6 +207,11 @@ static const struct {
{ "0123::123.45.67.89", TRUE }, { "0123::123.45.67.89", TRUE },
{ "::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 */ /* Contain non-hex chars */
{ "012x:4567:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, { "012x:4567:89AB:cdef:3210:7654:ba98:FeDc", FALSE },
{ "0123:45x7: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:123.45.67.89", FALSE },
{ "0123:4567:89AB:cdef:3210:123.45.67.89:FeDc", FALSE }, { "0123:4567:89AB:cdef:3210:123.45.67.89:FeDc", FALSE },
/* IPv4 tests */ /* IPv4 tests */
{ "123.45.67.89", TRUE }, { "123.45.67.89", TRUE },