diff libpurple/util.c @ 27545:8c41a23e6b44

Add purple_ipv6_address_is_valid; guess what it does?
author Paul Aurich <paul@darkrain42.org>
date Sat, 11 Jul 2009 07:32:27 +0000
parents 60d128c6413a
children 1d30e9d6de9b
line wrap: on
line diff
--- a/libpurple/util.c	Sat Jul 11 06:52:07 2009 +0000
+++ b/libpurple/util.c	Sat Jul 11 07:32:27 2009 +0000
@@ -4446,6 +4446,7 @@
 	return ((c - domain) > 3 ? TRUE : FALSE);
 }
 
+/* TODO 3.0.0: Rename this to purple_ipv4_address_is_valid */
 gboolean
 purple_ip_address_is_valid(const char *ip)
 {
@@ -4460,6 +4461,51 @@
 	return TRUE;
 }
 
+gboolean
+purple_ipv6_address_is_valid(const gchar *ip)
+{
+	const gchar *c;
+	gboolean double_colon = FALSE;
+	gint chunks = 1;
+	gint in = 0;
+
+	g_return_val_if_fail(ip != NULL, FALSE);
+
+	if (*ip == '\0')
+		return FALSE;
+
+	for (c = ip; *c; ++c) {
+		if ((*c >= '0' && *c <= '9') ||
+		        (*c >= 'a' && *c <= 'f') ||
+		        (*c >= 'A' && *c <= 'F')) {
+			if (++in > 4)
+				/* Only four hex digits per chunk */
+				return FALSE;
+			continue;
+		} else if (*c == ':') {
+			/* The start of a new chunk */
+			++chunks;
+			in = 0;
+			if (*(c + 1) == ':') {
+				/*
+				 * '::' indicates a consecutive series of chunks full
+				 * of zeroes. There can be only one of these per address.
+				 */
+				if (double_colon)
+					return FALSE;
+				double_colon = TRUE;
+			}
+		} else
+			return FALSE;
+	}
+
+	/*
+	 * Either we saw a '::' and there were fewer than 8 chunks -or-
+	 * we didn't see a '::' and saw exactly 8 chunks.
+	 */
+	return (double_colon && chunks < 8) || (!double_colon && chunks == 8);
+}
+
 /* Stolen from gnome_uri_list_extract_uris */
 GList *
 purple_uri_list_extract_uris(const gchar *uri_list)