From 6a3b4fa05ac996566e7b8037edf80d0f06fa2a90 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Mon, 29 Dec 2008 09:00:17 -0500 Subject: [PATCH] Add hostname-related utilities in glib/ghostutils.h Functions for converting between UTF-8 IDNs (Internationalized Domain Names) and their ASCII-Compatible Encodings, plus a function to recognize IP addresses. Part of #548466. --- docs/reference/glib/glib-docs.sgml | 2 + docs/reference/glib/glib-sections.txt | 13 + docs/reference/glib/tmpl/ghostutils.sgml | 64 ++ glib/Makefile.am | 2 + glib/ghostutils.c | 758 +++++++++++++++++++++++ glib/ghostutils.h | 40 ++ glib/glib.h | 1 + glib/glib.symbols | 10 + glib/tests/.gitignore | 1 + glib/tests/Makefile.am | 3 + glib/tests/hostutils.c | 267 ++++++++ 11 files changed, 1161 insertions(+) create mode 100644 docs/reference/glib/tmpl/ghostutils.sgml create mode 100644 glib/ghostutils.c create mode 100644 glib/ghostutils.h create mode 100644 glib/tests/hostutils.c diff --git a/docs/reference/glib/glib-docs.sgml b/docs/reference/glib/glib-docs.sgml index cef6e8c87..4d7c36d56 100644 --- a/docs/reference/glib/glib-docs.sgml +++ b/docs/reference/glib/glib-docs.sgml @@ -66,6 +66,7 @@ + @@ -162,6 +163,7 @@ synchronize their operation. &glib-Spawn; &glib-Fileutils; &glib-Uri; + &glib-Hostutils; &glib-Shell; &glib-Option; &glib-Pattern-Matching; diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 6df70cbdb..4feea7808 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -2653,3 +2653,16 @@ g_test_log_buffer_push g_test_log_buffer_pop g_test_log_msg_free + + +
+ghostutils +Hostname Utilities +g_hostname_to_ascii +g_hostname_to_unicode + +g_hostname_is_non_ascii +g_hostname_is_ascii_encoded + +g_hostname_is_ip_address +
diff --git a/docs/reference/glib/tmpl/ghostutils.sgml b/docs/reference/glib/tmpl/ghostutils.sgml new file mode 100644 index 000000000..facd3f6fc --- /dev/null +++ b/docs/reference/glib/tmpl/ghostutils.sgml @@ -0,0 +1,64 @@ + +Hostname Utilities + + + + + + + + + + + + + + + + + + + + + + +@hostname: +@Returns: + + + + + + + +@hostname: +@Returns: + + + + + + + +@hostname: +@Returns: + + + + + + + +@hostname: +@Returns: + + + + + + + +@hostname: +@Returns: + + diff --git a/glib/Makefile.am b/glib/Makefile.am index c6f20bbc9..eb220e66e 100644 --- a/glib/Makefile.am +++ b/glib/Makefile.am @@ -116,6 +116,7 @@ libglib_2_0_la_SOURCES = \ gfileutils.c \ ghash.c \ ghook.c \ + ghostutils.c \ giochannel.c \ gkeyfile.c \ glibintl.h \ @@ -199,6 +200,7 @@ glibsubinclude_HEADERS = \ gfileutils.h \ ghash.h \ ghook.h \ + ghostutils.h \ gi18n.h \ gi18n-lib.h \ giochannel.h \ diff --git a/glib/ghostutils.c b/glib/ghostutils.c new file mode 100644 index 000000000..f6c41d0d6 --- /dev/null +++ b/glib/ghostutils.c @@ -0,0 +1,758 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ + +/* GLIB - Library of useful routines for C programming + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include "glib.h" +#include "glibintl.h" + +#include + +#include "galias.h" + +/** + * SECTION:ghostutils + * @short_description: Internet hostname utilities + * @include: glib.h + * + * Functions for manipulating internet hostnames; in particular, for + * converting between Unicode and ASCII-encoded forms of + * Internationalized Domain Names (IDNs). + * + * The Internationalized Domain + * Names for Applications (IDNA) standards allow for the use + * of Unicode domain names in applications, while providing + * backward-compatibility with the old ASCII-only DNS, by defining an + * ASCII-Compatible Encoding of any given Unicode name, which can be + * used with non-IDN-aware applications and protocols. (For example, + * "Παν語.org" maps to "xn--4wa8awb4637h.org".) + **/ + +#define IDNA_ACE_PREFIX "xn--" +#define IDNA_ACE_PREFIX_LEN 4 + +/* Punycode constants, from RFC 3492. */ + +#define PUNYCODE_BASE 36 +#define PUNYCODE_TMIN 1 +#define PUNYCODE_TMAX 26 +#define PUNYCODE_SKEW 38 +#define PUNYCODE_DAMP 700 +#define PUNYCODE_INITIAL_BIAS 72 +#define PUNYCODE_INITIAL_N 0x80 + +#define PUNYCODE_IS_BASIC(cp) ((guint)(cp) < 0x80) + +/* Encode/decode a single base-36 digit */ +static inline gchar +encode_digit (guint dig) +{ + if (dig < 26) + return dig + 'a'; + else + return dig - 26 + '0'; +} + +static inline guint +decode_digit (gchar dig) +{ + if (dig >= 'A' && dig <= 'Z') + return dig - 'A'; + else if (dig >= 'a' && dig <= 'z') + return dig - 'a'; + else if (dig >= '0' && dig <= '9') + return dig - '0' + 26; + else + return G_MAXUINT; +} + +/* Punycode bias adaptation algorithm, RFC 3492 section 6.1 */ +static guint +adapt (guint delta, + guint numpoints, + gboolean firsttime) +{ + guint k; + + delta = firsttime ? delta / PUNYCODE_DAMP : delta / 2; + delta += delta / numpoints; + + k = 0; + while (delta > ((PUNYCODE_BASE - PUNYCODE_TMIN) * PUNYCODE_TMAX) / 2) + { + delta /= PUNYCODE_BASE - PUNYCODE_TMIN; + k += PUNYCODE_BASE; + } + + return k + ((PUNYCODE_BASE - PUNYCODE_TMIN + 1) * delta / + (delta + PUNYCODE_SKEW)); +} + +/* Punycode encoder, RFC 3492 section 6.3. The algorithm is + * sufficiently bizarre that it's not really worth trying to explain + * here. + */ +static gboolean +punycode_encode (const gchar *input_utf8, + gsize input_utf8_length, + GString *output) +{ + guint delta, handled_chars, num_basic_chars, bias, j, q, k, t, digit; + gunichar n, m, *input; + glong input_length; + gboolean success = FALSE; + + /* Convert from UTF-8 to Unicode code points */ + input = g_utf8_to_ucs4 (input_utf8, input_utf8_length, NULL, + &input_length, NULL); + if (!input) + return FALSE; + + /* Copy basic chars */ + for (j = num_basic_chars = 0; j < input_length; j++) + { + if (PUNYCODE_IS_BASIC (input[j])) + { + g_string_append_c (output, g_ascii_tolower (input[j])); + num_basic_chars++; + } + } + if (num_basic_chars) + g_string_append_c (output, '-'); + + handled_chars = num_basic_chars; + + /* Encode non-basic chars */ + delta = 0; + bias = PUNYCODE_INITIAL_BIAS; + n = PUNYCODE_INITIAL_N; + while (handled_chars < input_length) + { + /* let m = the minimum {non-basic} code point >= n in the input */ + for (m = G_MAXUINT, j = 0; j < input_length; j++) + { + if (input[j] >= n && input[j] < m) + m = input[j]; + } + + if (m - n > (G_MAXUINT - delta) / (handled_chars + 1)) + goto fail; + delta += (m - n) * (handled_chars + 1); + n = m; + + for (j = 0; j < input_length; j++) + { + if (input[j] < n) + { + if (++delta == 0) + goto fail; + } + else if (input[j] == n) + { + q = delta; + for (k = PUNYCODE_BASE; ; k += PUNYCODE_BASE) + { + if (k <= bias) + t = PUNYCODE_TMIN; + else if (k >= bias + PUNYCODE_TMAX) + t = PUNYCODE_TMAX; + else + t = k - bias; + if (q < t) + break; + digit = t + (q - t) % (PUNYCODE_BASE - t); + g_string_append_c (output, encode_digit (digit)); + q = (q - t) / (PUNYCODE_BASE - t); + } + + g_string_append_c (output, encode_digit (q)); + bias = adapt (delta, handled_chars + 1, handled_chars == num_basic_chars); + delta = 0; + handled_chars++; + } + } + + delta++; + n++; + } + + success = TRUE; + + fail: + g_free (input); + return success; +} + +/* From RFC 3454, Table B.1 */ +#define idna_is_junk(ch) ((ch) == 0x00AD || (ch) == 0x1806 || (ch) == 0x200B || (ch) == 0x2060 || (ch) == 0xFEFF || (ch) == 0x034F || (ch) == 0x180B || (ch) == 0x180C || (ch) == 0x180D || (ch) == 0x200C || (ch) == 0x200D || ((ch) >= 0xFE00 && (ch) <= 0xFE0F)) + +/* Scan @str for "junk" and return a cleaned-up string if any junk + * is found. Else return %NULL. + */ +static gchar * +remove_junk (const gchar *str, + gint len) +{ + GString *cleaned = NULL; + const gchar *p; + gunichar ch; + + for (p = str; len == -1 ? *p : p < str + len; p = g_utf8_next_char (p)) + { + ch = g_utf8_get_char (p); + if (idna_is_junk (ch)) + { + if (!cleaned) + { + cleaned = g_string_new (NULL); + g_string_append_len (cleaned, str, p - str); + } + } + else if (cleaned) + g_string_append_unichar (cleaned, ch); + } + + if (cleaned) + return g_string_free (cleaned, FALSE); + else + return NULL; +} + +static inline gboolean +contains_uppercase_letters (const gchar *str, + gint len) +{ + const gchar *p; + + for (p = str; len == -1 ? *p : p < str + len; p = g_utf8_next_char (p)) + { + if (g_unichar_isupper (g_utf8_get_char (p))) + return TRUE; + } + return FALSE; +} + +static inline gboolean +contains_non_ascii (const gchar *str, + gint len) +{ + const gchar *p; + + for (p = str; len == -1 ? *p : p < str + len; p++) + { + if ((guchar)*p > 0x80) + return TRUE; + } + return FALSE; +} + +/* RFC 3454, Appendix C. ish. */ +static inline gboolean +idna_is_prohibited (gunichar ch) +{ + switch (g_unichar_type (ch)) + { + case G_UNICODE_CONTROL: + case G_UNICODE_FORMAT: + case G_UNICODE_UNASSIGNED: + case G_UNICODE_PRIVATE_USE: + case G_UNICODE_SURROGATE: + case G_UNICODE_LINE_SEPARATOR: + case G_UNICODE_PARAGRAPH_SEPARATOR: + case G_UNICODE_SPACE_SEPARATOR: + return TRUE; + + case G_UNICODE_OTHER_SYMBOL: + if (ch == 0xFFFC || ch == 0xFFFD || + (ch >= 0x2FF0 && ch <= 0x2FFB)) + return TRUE; + return FALSE; + + case G_UNICODE_NON_SPACING_MARK: + if (ch == 0x0340 || ch == 0x0341) + return TRUE; + return FALSE; + + default: + return FALSE; + } +} + +/* RFC 3491 IDN cleanup algorithm. */ +static gchar * +nameprep (const gchar *hostname, + gint len) +{ + gchar *name, *tmp = NULL, *p; + + /* It would be nice if we could do this without repeatedly + * allocating strings and converting back and forth between + * gunichars and UTF-8... The code does at least avoid doing most of + * the sub-operations when they would just be equivalent to a + * g_strdup(). + */ + + /* Remove presentation-only characters */ + name = remove_junk (hostname, len); + if (name) + { + tmp = name; + len = -1; + } + else + name = (gchar *)hostname; + + /* Convert to lowercase */ + if (contains_uppercase_letters (name, len)) + { + name = g_utf8_strdown (name, len); + g_free (tmp); + tmp = name; + len = -1; + } + + /* If there are no UTF8 characters, we're done. */ + if (!contains_non_ascii (name, len)) + { + if (name == (gchar *)hostname) + return len == -1 ? g_strdup (hostname) : g_strndup (hostname, len); + else + return name; + } + + /* Normalize */ + name = g_utf8_normalize (name, len, G_NORMALIZE_NFKC); + g_free (tmp); + tmp = name; + + /* KC normalization may have created more capital letters (eg, + * angstrom -> capital A with ring). So we have to lowercasify a + * second time. (This is more-or-less how the nameprep algorithm + * does it. If tolower(nfkc(tolower(X))) is guaranteed to be the + * same as tolower(nfkc(X)), then we could skip the first tolower, + * but I'm not sure it is.) + */ + if (contains_uppercase_letters (name, -1)) + { + name = g_utf8_strdown (name, -1); + g_free (tmp); + tmp = name; + } + + /* Check for prohibited characters */ + for (p = name; *p; p = g_utf8_next_char (p)) + { + if (idna_is_prohibited (g_utf8_get_char (p))) + { + name = NULL; + g_free (tmp); + goto done; + } + } + + /* FIXME: We're supposed to verify certain constraints on bidi + * characters, but glib does not appear to have that information. + */ + + done: + return name; +} + +/** + * g_hostname_to_ascii: + * @hostname: a valid UTF-8 or ASCII hostname + * + * Converts @hostname to its canonical ASCII form; an ASCII-only + * string containing no uppercase letters and not ending with a + * trailing dot. + * + * Return value: an ASCII hostname, which must be freed, or %NULL if + * @hostname is in some way invalid. + * + * Since: 2.22 + **/ +gchar * +g_hostname_to_ascii (const gchar *hostname) +{ + gchar *name, *label, *p; + GString *out; + gssize llen, oldlen; + gboolean unicode; + + out = g_string_new (NULL); + label = name = nameprep (hostname, -1); + + do + { + unicode = FALSE; + for (p = label; *p && *p != '.'; p++) + { + if ((guchar)*p > 0x80) + unicode = TRUE; + } + + oldlen = out->len; + llen = p - label; + if (unicode) + { + if (!strncmp (label, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN)) + goto fail; + + g_string_append (out, IDNA_ACE_PREFIX); + if (!punycode_encode (label, llen, out)) + goto fail; + } + else + g_string_append_len (out, label, llen); + + if (out->len - oldlen > 63) + goto fail; + + label += llen; + if (*label && *++label) + g_string_append_c (out, '.'); + } + while (*label); + + g_free (name); + return g_string_free (out, FALSE); + + fail: + g_free (name); + g_string_free (out, TRUE); + return NULL; +} + +/** + * g_hostname_is_non_ascii: + * @hostname: a hostname + * + * Tests if @hostname contains Unicode characters. If this returns + * %TRUE, you need to encode the hostname with g_hostname_to_ascii() + * before using it in non-IDN-aware contexts. + * + * Note that a hostname might contain a mix of encoded and unencoded + * segments, and so it is possible for g_hostname_is_non_ascii() and + * g_hostname_is_ascii_encoded() to both return %TRUE for a name. + * + * Return value: %TRUE if @hostname contains any non-ASCII characters + * + * Since: 2.22 + **/ +gboolean +g_hostname_is_non_ascii (const gchar *hostname) +{ + return contains_non_ascii (hostname, -1); +} + +/* Punycode decoder, RFC 3492 section 6.2. As with punycode_encode(), + * read the RFC if you want to understand what this is actually doing. + */ +static gboolean +punycode_decode (const gchar *input, + gsize input_length, + GString *output) +{ + GArray *output_chars; + gunichar n; + guint i, bias; + guint oldi, w, k, digit, t; + const gchar *split; + + n = PUNYCODE_INITIAL_N; + i = 0; + bias = PUNYCODE_INITIAL_BIAS; + + split = input + input_length - 1; + while (split > input && *split != '-') + split--; + if (split > input) + { + output_chars = g_array_sized_new (FALSE, FALSE, sizeof (gunichar), + split - input); + input_length -= (split - input) + 1; + while (input < split) + { + gunichar ch = (gunichar)*input++; + if (!PUNYCODE_IS_BASIC (ch)) + goto fail; + g_array_append_val (output_chars, ch); + } + input++; + } + else + output_chars = g_array_new (FALSE, FALSE, sizeof (gunichar)); + + while (input_length) + { + oldi = i; + w = 1; + for (k = PUNYCODE_BASE; ; k += PUNYCODE_BASE) + { + if (!input_length--) + goto fail; + digit = decode_digit (*input++); + if (digit >= PUNYCODE_BASE) + goto fail; + if (digit > (G_MAXUINT - i) / w) + goto fail; + i += digit * w; + if (k <= bias) + t = PUNYCODE_TMIN; + else if (k >= bias + PUNYCODE_TMAX) + t = PUNYCODE_TMAX; + else + t = k - bias; + if (digit < t) + break; + if (w > G_MAXUINT / (PUNYCODE_BASE - t)) + goto fail; + w *= (PUNYCODE_BASE - t); + } + + bias = adapt (i - oldi, output_chars->len + 1, oldi == 0); + + if (i / (output_chars->len + 1) > G_MAXUINT - n) + goto fail; + n += i / (output_chars->len + 1); + i %= (output_chars->len + 1); + + g_array_insert_val (output_chars, i++, n); + } + + for (i = 0; i < output_chars->len; i++) + g_string_append_unichar (output, g_array_index (output_chars, gunichar, i)); + g_array_free (output_chars, TRUE); + return TRUE; + + fail: + g_array_free (output_chars, TRUE); + return FALSE; +} + +/** + * g_hostname_to_unicode: + * @hostname: a valid UTF-8 or ASCII hostname + * + * Converts @hostname to its canonical presentation form; a UTF-8 + * string in Unicode normalization form C, containing no uppercase + * letters, no forbidden characters, and no ASCII-encoded segments, + * and not ending with a trailing dot. + * + * Of course if @hostname is not an internationalized hostname, then + * the canonical presentation form will be entirely ASCII. + * + * Return value: a UTF-8 hostname, which must be freed, or %NULL if + * @hostname is in some way invalid. + * + * Since: 2.22 + **/ +gchar * +g_hostname_to_unicode (const gchar *hostname) +{ + GString *out; + gssize llen; + + out = g_string_new (NULL); + + do + { + llen = strcspn (hostname, "."); + if (!g_ascii_strncasecmp (hostname, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN)) + { + hostname += IDNA_ACE_PREFIX_LEN; + llen -= IDNA_ACE_PREFIX_LEN; + if (!punycode_decode (hostname, llen, out)) + { + g_string_free (out, TRUE); + return NULL; + } + } + else + { + gchar *canonicalized = nameprep (hostname, llen); + + g_string_append (out, canonicalized); + g_free (canonicalized); + } + + hostname += llen; + if (*hostname && *++hostname) + g_string_append_c (out, '.'); + } + while (*hostname); + + return g_string_free (out, FALSE); +} + +/** + * g_hostname_is_ascii_encoded: + * @hostname: a hostname + * + * Tests if @hostname contains segments with an ASCII-compatible + * encoding of an Internationalized Domain Name. If this returns + * %TRUE, you should decode the hostname with g_hostname_to_unicode() + * before displaying it to the user. + * + * Note that a hostname might contain a mix of encoded and unencoded + * segments, and so it is possible for g_hostname_is_non_ascii() and + * g_hostname_is_ascii_encoded() to both return %TRUE for a name. + * + * Return value: %TRUE if @hostname contains any ASCII-encoded + * segments. + * + * Since: 2.22 + **/ +gboolean +g_hostname_is_ascii_encoded (const gchar *hostname) +{ + while (1) + { + if (!g_ascii_strncasecmp (hostname, IDNA_ACE_PREFIX, IDNA_ACE_PREFIX_LEN)) + return TRUE; + hostname = strchr (hostname, '.'); + if (!hostname++) + return FALSE; + } +} + +/** + * g_hostname_is_ip_address: + * @hostname: a hostname (or IP address in string form) + * + * Tests if @hostname is the string form of an IPv4 or IPv6 address. + * (Eg, "192.168.0.1".) + * + * Return value: %TRUE if @hostname is an IP address + * + * Since: 2.22 + **/ +gboolean +g_hostname_is_ip_address (const gchar *hostname) +{ + gchar *p, *end; + gint nsegments, octet; + + /* On Linux we could implement this using inet_pton, but the Windows + * equivalent of that requires linking against winsock, so we just + * figure this out ourselves. Tested by tests/hostutils.c. + */ + + p = (char *)hostname; + + if (strchr (p, ':')) + { + gboolean skipped; + + /* If it contains a ':', it's an IPv6 address (assuming it's an + * IP address at all). This consists of eight ':'-separated + * segments, each containing a 1-4 digit hex number, except that + * optionally: (a) the last two segments can be replaced by an + * IPv4 address, and (b) a single span of 1 to 8 "0000" segments + * can be replaced with just "::". + */ + + nsegments = 0; + skipped = FALSE; + while (*p && nsegments < 8) + { + /* Each segment after the first must be preceded by a ':'. + * (We also handle half of the "string starts with ::" case + * here.) + */ + if (p != (char *)hostname || (p[0] == ':' && p[1] == ':')) + { + if (*p != ':') + return FALSE; + p++; + } + + /* If there's another ':', it means we're skipping some segments */ + if (*p == ':' && !skipped) + { + skipped = TRUE; + nsegments++; + + /* Handle the "string ends with ::" case */ + if (!p[1]) + p++; + + continue; + } + + /* Read the segment, make sure it's valid. */ + for (end = p; g_ascii_isxdigit (*end); end++) + ; + if (end == p || end > p + 4) + return FALSE; + + if (*end == '.') + { + if ((nsegments == 6 && !skipped) || (nsegments <= 6 && skipped)) + goto parse_ipv4; + else + return FALSE; + } + + nsegments++; + p = end; + } + + return !*p && (nsegments == 8 || skipped); + } + + parse_ipv4: + + /* Parse IPv4: N.N.N.N, where each N <= 255 and doesn't have leading 0s. */ + for (nsegments = 0; nsegments < 4; nsegments++) + { + if (nsegments != 0) + { + if (*p != '.') + return FALSE; + p++; + } + + /* Check the segment; a little tricker than the IPv6 case since + * we can't allow extra leading 0s, and we can't assume that all + * strings of valid length are within range. + */ + octet = 0; + if (*p == '0') + end = p + 1; + else + { + for (end = p; g_ascii_isdigit (*end); end++) + octet = 10 * octet + (*end - '0'); + } + if (end == p || end > p + 3 || octet > 255) + return FALSE; + + p = end; + } + + /* If there's nothing left to parse, then it's ok. */ + return !*p; +} + +#define __G_HOST_UTILS_C__ +#include "galiasdef.c" diff --git a/glib/ghostutils.h b/glib/ghostutils.h new file mode 100644 index 000000000..0349da364 --- /dev/null +++ b/glib/ghostutils.h @@ -0,0 +1,40 @@ +/* GLIB - Library of useful routines for C programming + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION) +#error "Only can be included directly." +#endif + +#ifndef __G_HOST_UTILS_H__ +#define __G_HOST_UTILS_H__ + +#include + +G_BEGIN_DECLS + +gboolean g_hostname_is_non_ascii (const gchar *hostname); +gboolean g_hostname_is_ascii_encoded (const gchar *hostname); +gboolean g_hostname_is_ip_address (const gchar *hostname); + +gchar *g_hostname_to_ascii (const gchar *hostname); +gchar *g_hostname_to_unicode (const gchar *hostname); + +G_END_DECLS + +#endif /* __G_HOST_UTILS_H__ */ diff --git a/glib/glib.h b/glib/glib.h index 000d41768..f8acdd259 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include diff --git a/glib/glib.symbols b/glib/glib.symbols index a8b7b44f0..082aed513 100644 --- a/glib/glib.symbols +++ b/glib/glib.symbols @@ -1637,6 +1637,16 @@ g_win32_locale_filename_from_utf8 #endif #endif +#if IN_HEADER(__G_HOST_UTILS_H__) +#if IN_FILE(__G_HOST_UTILS_C__) +g_hostname_is_non_ascii +g_hostname_is_ascii_encoded +g_hostname_is_ip_address +g_hostname_to_ascii +g_hostname_to_unicode +#endif +#endif + #ifdef INCLUDE_VARIABLES g_ascii_table g_utf8_skip diff --git a/glib/tests/.gitignore b/glib/tests/.gitignore index 5e917297e..76c25a8c1 100644 --- a/glib/tests/.gitignore +++ b/glib/tests/.gitignore @@ -1,5 +1,6 @@ array-test fileutils +hostutils keyfile markup-subparser option-context diff --git a/glib/tests/Makefile.am b/glib/tests/Makefile.am index 3d497ac23..673d33892 100644 --- a/glib/tests/Makefile.am +++ b/glib/tests/Makefile.am @@ -44,6 +44,9 @@ markup_subparser_LDADD = $(progs_ldadd) TEST_PROGS += array-test array_test_LDADD = $(progs_ldadd) +TEST_PROGS += hostutils +hostutils_LDADD = $(progs_ldadd) + if OS_UNIX # some testing of gtester funcitonality diff --git a/glib/tests/hostutils.c b/glib/tests/hostutils.c new file mode 100644 index 000000000..515145aff --- /dev/null +++ b/glib/tests/hostutils.c @@ -0,0 +1,267 @@ +/* + * Copyright (C) 2008 Red Hat, Inc + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include + +#include +#include + +static const struct { + const gchar *ascii_name, *unicode_name; +} idn_test_domains[] = { + /* "example.test" in various languages */ + { "xn--mgbh0fb.xn--kgbechtv", "\xd9\x85\xd8\xab\xd8\xa7\xd9\x84.\xd8\xa5\xd8\xae\xd8\xaa\xd8\xa8\xd8\xa7\xd8\xb1" }, + { "xn--fsqu00a.xn--0zwm56d", "\xe4\xbe\x8b\xe5\xad\x90.\xe6\xb5\x8b\xe8\xaf\x95" }, + { "xn--fsqu00a.xn--g6w251d", "\xe4\xbe\x8b\xe5\xad\x90.\xe6\xb8\xac\xe8\xa9\xa6" }, + { "xn--hxajbheg2az3al.xn--jxalpdlp", "\xcf\x80\xce\xb1\xcf\x81\xce\xac\xce\xb4\xce\xb5\xce\xb9\xce\xb3\xce\xbc\xce\xb1.\xce\xb4\xce\xbf\xce\xba\xce\xb9\xce\xbc\xce\xae" }, + { "xn--p1b6ci4b4b3a.xn--11b5bs3a9aj6g", "\xe0\xa4\x89\xe0\xa4\xa6\xe0\xa4\xbe\xe0\xa4\xb9\xe0\xa4\xb0\xe0\xa4\xa3.\xe0\xa4\xaa\xe0\xa4\xb0\xe0\xa5\x80\xe0\xa4\x95\xe0\xa5\x8d\xe0\xa4\xb7\xe0\xa4\xbe" }, + { "xn--r8jz45g.xn--zckzah", "\xe4\xbe\x8b\xe3\x81\x88.\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88" }, + { "xn--9n2bp8q.xn--9t4b11yi5a", "\xec\x8b\xa4\xeb\xa1\x80.\xed\x85\x8c\xec\x8a\xa4\xed\x8a\xb8" }, + { "xn--mgbh0fb.xn--hgbk6aj7f53bba", "\xd9\x85\xd8\xab\xd8\xa7\xd9\x84.\xd8\xa2\xd8\xb2\xd9\x85\xd8\xa7\xdb\x8c\xd8\xb4\xdb\x8c" }, + { "xn--e1afmkfd.xn--80akhbyknj4f", "\xd0\xbf\xd1\x80\xd0\xb8\xd0\xbc\xd0\xb5\xd1\x80.\xd0\xb8\xd1\x81\xd0\xbf\xd1\x8b\xd1\x82\xd0\xb0\xd0\xbd\xd0\xb8\xd0\xb5" }, + { "xn--zkc6cc5bi7f6e.xn--hlcj6aya9esc7a", "\xe0\xae\x89\xe0\xae\xa4\xe0\xae\xbe\xe0\xae\xb0\xe0\xae\xa3\xe0\xae\xae\xe0\xaf\x8d.\xe0\xae\xaa\xe0\xae\xb0\xe0\xae\xbf\xe0\xae\x9f\xe0\xaf\x8d\xe0\xae\x9a\xe0\xaf\x88" }, + { "xn--fdbk5d8ap9b8a8d.xn--deba0ad", "\xd7\x91\xd7\xb2\xd6\xb7\xd7\xa9\xd7\xa4\xd6\xbc\xd7\x99\xd7\x9c.\xd7\x98\xd7\xa2\xd7\xa1\xd7\x98" }, + + /* further examples without their own IDN-ized TLD */ + { "xn--1xd0bwwra.idn.icann.org", "\xe1\x8a\xa0\xe1\x88\x9b\xe1\x88\xad\xe1\x8a\x9b.idn.icann.org" }, + { "xn--54b7fta0cc.idn.icann.org", "\xe0\xa6\xac\xe0\xa6\xbe\xe0\xa6\x82\xe0\xa6\xb2\xe0\xa6\xbe.idn.icann.org" }, + { "xn--5dbqzzl.idn.icann.org", "\xd7\xa2\xd7\x91\xd7\xa8\xd7\x99\xd7\xaa.idn.icann.org" }, + { "xn--j2e7beiw1lb2hqg.idn.icann.org", "\xe1\x9e\x97\xe1\x9e\xb6\xe1\x9e\x9f\xe1\x9e\xb6\xe1\x9e\x81\xe1\x9f\x92\xe1\x9e\x98\xe1\x9f\x82\xe1\x9e\x9a.idn.icann.org" }, + { "xn--o3cw4h.idn.icann.org", "\xe0\xb9\x84\xe0\xb8\x97\xe0\xb8\xa2.idn.icann.org" }, + { "xn--mgbqf7g.idn.icann.org", "\xd8\xa7\xd8\xb1\xd8\xaf\xd9\x88.idn.icann.org" } +}; +static const gint num_idn_test_domains = G_N_ELEMENTS (idn_test_domains); + +static void +test_to_ascii (void) +{ + gint i; + gchar *ascii; + + for (i = 0; i < num_idn_test_domains; i++) + { + g_assert (g_hostname_is_non_ascii (idn_test_domains[i].unicode_name)); + ascii = g_hostname_to_ascii (idn_test_domains[i].unicode_name); + g_assert_cmpstr (idn_test_domains[i].ascii_name, ==, ascii); + g_free (ascii); + + ascii = g_hostname_to_ascii (idn_test_domains[i].ascii_name); + g_assert_cmpstr (idn_test_domains[i].ascii_name, ==, ascii); + g_free (ascii); + } +} + +static void +test_to_unicode (void) +{ + gint i; + gchar *unicode; + + for (i = 0; i < num_idn_test_domains; i++) + { + g_assert (g_hostname_is_ascii_encoded (idn_test_domains[i].ascii_name)); + unicode = g_hostname_to_unicode (idn_test_domains[i].ascii_name); + g_assert_cmpstr (idn_test_domains[i].unicode_name, ==, unicode); + g_free (unicode); + + unicode = g_hostname_to_unicode (idn_test_domains[i].unicode_name); + g_assert_cmpstr (idn_test_domains[i].unicode_name, ==, unicode); + g_free (unicode); + } +} + +static const struct { + const gchar *addr; + gboolean is_addr; +} ip_addr_tests[] = { + /* IPv6 tests */ + + { "0123:4567:89AB:cdef:3210:7654:ba98:FeDc", TRUE }, + + { "0123:4567:89AB:cdef:3210:7654:ba98::", TRUE }, + { "0123:4567:89AB:cdef:3210:7654::", TRUE }, + { "0123:4567:89AB:cdef:3210::", TRUE }, + { "0123:4567:89AB:cdef::", TRUE }, + { "0123:4567:89AB::", TRUE }, + { "0123:4567::", TRUE }, + { "0123::", TRUE }, + + { "::4567:89AB:cdef:3210:7654:ba98:FeDc", TRUE }, + { "::89AB:cdef:3210:7654:ba98:FeDc", TRUE }, + { "::cdef:3210:7654:ba98:FeDc", TRUE }, + { "::3210:7654:ba98:FeDc", TRUE }, + { "::7654:ba98:FeDc", TRUE }, + { "::ba98:FeDc", TRUE }, + { "::FeDc", TRUE }, + + { "0123::89AB:cdef:3210:7654:ba98:FeDc", TRUE }, + { "0123:4567::cdef:3210:7654:ba98:FeDc", TRUE }, + { "0123:4567:89AB::3210:7654:ba98:FeDc", TRUE }, + { "0123:4567:89AB:cdef::7654:ba98:FeDc", TRUE }, + { "0123:4567:89AB:cdef:3210::ba98:FeDc", TRUE }, + { "0123:4567:89AB:cdef:3210:7654::FeDc", TRUE }, + + { "0123::cdef:3210:7654:ba98:FeDc", TRUE }, + { "0123:4567::3210:7654:ba98:FeDc", TRUE }, + { "0123:4567:89AB::7654:ba98:FeDc", TRUE }, + { "0123:4567:89AB:cdef::ba98:FeDc", TRUE }, + { "0123:4567:89AB:cdef:3210::FeDc", TRUE }, + + { "0123::3210:7654:ba98:FeDc", TRUE }, + { "0123:4567::7654:ba98:FeDc", TRUE }, + { "0123:4567:89AB::ba98:FeDc", TRUE }, + { "0123:4567:89AB:cdef::FeDc", TRUE }, + + { "0123::7654:ba98:FeDc", TRUE }, + { "0123:4567::ba98:FeDc", TRUE }, + { "0123:4567:89AB::FeDc", TRUE }, + + { "0123::ba98:FeDc", TRUE }, + { "0123:4567::FeDc", TRUE }, + + { "0123::FeDc", TRUE }, + + { "::", TRUE }, + + { "0:12:345:6789:a:bc:def::", TRUE }, + + { "0123:4567:89AB:cdef:3210:7654:123.45.67.89", TRUE }, + { "0123:4567:89AB:cdef:3210::123.45.67.89", TRUE }, + { "0123:4567:89AB:cdef::123.45.67.89", TRUE }, + { "0123:4567:89AB::123.45.67.89", TRUE }, + { "0123:4567::123.45.67.89", TRUE }, + { "0123::123.45.67.89", TRUE }, + { "::123.45.67.89", TRUE }, + + /* Contain non-hex chars */ + { "012x:4567:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, + { "0123:45x7:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, + { "0123:4567:8xAB:cdef:3210:7654:ba98:FeDc", FALSE }, + { "0123:4567:89AB:xdef:3210:7654:ba98:FeDc", FALSE }, + { "0123:4567:89AB:cdef:321;:7654:ba98:FeDc", FALSE }, + { "0123:4567:89AB:cdef:3210:76*4:ba98:FeDc", FALSE }, + { "0123:4567:89AB:cdef:3210:7654:b-98:FeDc", FALSE }, + { "0123:4567:89AB:cdef:3210:7654:ba98:+eDc", FALSE }, + { "0123:4567:89AB:cdef:3210:7654:ba98:FeDc and some trailing junk", FALSE }, + { " 123:4567:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, + { "012 :4567:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, + { "0123: 567:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, + { "0123:4567:89AB:cdef:3210:7654:ba98:FeD ", FALSE }, + + /* Contains too-long/out-of-range segments */ + { "00123:4567:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, + { "0123:04567:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, + { "0123:4567:189AB:cdef:3210:7654:ba98:FeDc", FALSE }, + + /* Too short */ + { "0123:4567:89AB:cdef:3210:7654:ba98", FALSE }, + { "0123:4567:89AB:cdef:3210:7654", FALSE }, + { "0123:4567:89AB:cdef:3210", FALSE }, + { "0123", FALSE }, + { "", FALSE }, + + /* Too long */ + { "0123:4567:89AB:cdef:3210:7654:ba98:FeDc:9999", FALSE }, + { "0123::4567:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, + { "0123:4567::89AB:cdef:3210:7654:ba98:FeDc", FALSE }, + { "0123:4567:89AB::cdef:3210:7654:ba98:FeDc", FALSE }, + { "0123:4567:89AB:cdef::3210:7654:ba98:FeDc", FALSE }, + { "0123:4567:89AB:cdef:3210::7654:ba98:FeDc", FALSE }, + { "0123:4567:89AB:cdef:3210:7654::ba98:FeDc", FALSE }, + { "0123:4567:89AB:cdef:3210:7654:ba98::FeDc", FALSE }, + + /* Invalid use of ":"s */ + { "0123::89AB::3210:7654:ba98:FeDc", FALSE }, + { "::4567:89AB:cdef:3210:7654::FeDc", FALSE }, + { "0123::89AB:cdef:3210:7654:ba98::", FALSE }, + { ":4567:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, + { "0123:4567:89AB:cdef:3210:7654:ba98:", FALSE }, + { "0123:::cdef:3210:7654:ba98:FeDc", FALSE }, + { "0123:4567:89AB:cdef:3210:7654:ba98:FeDc:", FALSE }, + { ":0123:4567:89AB:cdef:3210:7654:ba98:FeDc", FALSE }, + { ":::", FALSE }, + + /* IPv4 address at wrong place */ + { "0123:4567:89AB:cdef:3210:123.45.67.89", FALSE }, + { "0123:4567:89AB:cdef:3210:7654::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 }, + + + /* IPv4 tests */ + + { "123.45.67.89", TRUE }, + { "1.2.3.4", TRUE }, + { "1.2.3.0", TRUE }, + + { "023.045.067.089", FALSE }, + { "1234.5.67.89", FALSE }, + { "123.45.67.00", FALSE }, + { " 1.2.3.4", FALSE }, + { "1 .2.3.4", FALSE }, + { "1. 2.3.4", FALSE }, + { "1.2.3.4 ", FALSE }, + { "1.2.3", FALSE }, + { "1.2.3.4.5", FALSE }, + { "1.b.3.4", FALSE }, + { "1.2.3:4", FALSE }, + { "1.2.3.4, etc", FALSE }, + { "1,2,3,4", FALSE }, + { "1.2.3.com", FALSE }, + { "1.2.3.4.", FALSE }, + { "1.2.3.", FALSE }, + { ".1.2.3.4", FALSE }, + { ".2.3.4", FALSE }, + { "1..2.3.4", FALSE }, + { "1..3.4", FALSE } +}; +static const gint num_ip_addr_tests = G_N_ELEMENTS (ip_addr_tests); + +static void +test_is_ip_addr (void) +{ + gint i; + + for (i = 0; i < num_ip_addr_tests; i++) + { + if (g_hostname_is_ip_address (ip_addr_tests[i].addr) != ip_addr_tests[i].is_addr) + { + char *msg = g_strdup_printf ("g_hostname_is_ip_address (\"%s\") == %s", + ip_addr_tests[i].addr, + ip_addr_tests[i].is_addr ? "TRUE" : "FALSE"); + g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg); + } + } +} + +/* FIXME: test names with both unicode and ACE-encoded labels */ +/* FIXME: test invalid unicode names */ + +int +main (int argc, + char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/hostutils/to_ascii", test_to_ascii); + g_test_add_func ("/hostutils/to_unicode", test_to_unicode); + g_test_add_func ("/hostutils/is_ip_addr", test_is_ip_addr); + + return g_test_run (); +}