changeset 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 7980a2bbad35
children e90fa379fcec
files ChangeLog.API libpurple/tests/test_util.c libpurple/util.c libpurple/util.h
diffstat 4 files changed, 95 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog.API	Sat Jul 11 06:52:07 2009 +0000
+++ b/ChangeLog.API	Sat Jul 11 07:32:27 2009 +0000
@@ -37,6 +37,8 @@
 		* purple_debug_set_verbose
 		* purple_global_proxy_set_info
 		* purple_group_destroy
+		* purple_ipv4_address_is_valid
+		* purple_ipv6_address_is_valid
 		* purple_log_get_activity_score
 		* purple_markup_is_rtl
 		* purple_markup_escape_text
@@ -82,6 +84,7 @@
 		* buddy-added and buddy-removed blist signals
 		* purple_buddy_get_local_alias
 		* purple_notify_user_info_remove_entry
+		* purple_ip_address_is_valid
 		* purple_status_type_set_primary_attr
 		* purple_status_type_add_attr
 		* purple_status_type_add_attrs
--- a/libpurple/tests/test_util.c	Sat Jul 11 06:52:07 2009 +0000
+++ b/libpurple/tests/test_util.c	Sat Jul 11 07:32:27 2009 +0000
@@ -80,6 +80,25 @@
 }
 END_TEST
 
+START_TEST(test_util_ipv6_is_valid)
+{
+	fail_unless(purple_ipv6_address_is_valid("2001:0db8:85a3:0000:0000:8a2e:0370:7334"));
+	fail_unless(purple_ipv6_address_is_valid("2001:db8:85a3:0:0:8a2e:370:7334"));
+	fail_unless(purple_ipv6_address_is_valid("2001:db8:85a3::8a2e:370:7334"));
+	fail_unless(purple_ipv6_address_is_valid("2001:0db8:0:0::1428:57ab"));
+	fail_unless(purple_ipv6_address_is_valid("::1"));
+	fail_unless(purple_ipv6_address_is_valid("1::"));
+	fail_unless(purple_ipv6_address_is_valid("1::1"));
+	fail_unless(purple_ipv6_address_is_valid("::"));
+	fail_if(purple_ipv6_address_is_valid(""));
+	fail_if(purple_ipv6_address_is_valid(":"));
+	fail_if(purple_ipv6_address_is_valid("1.2.3.4"));
+	fail_if(purple_ipv6_address_is_valid("2001::FFD3::57ab"));
+	fail_if(purple_ipv6_address_is_valid("200000000::1"));
+	fail_if(purple_ipv6_address_is_valid("QWERTY::1"));
+}
+END_TEST
+
 START_TEST(test_util_str_to_time)
 {
 	fail_unless(377182200 == purple_str_to_time("19811214T12:50:00", TRUE, NULL, NULL, NULL));
@@ -132,6 +151,10 @@
 	tcase_add_test(tc, test_util_email_is_valid);
 	suite_add_tcase(s, tc);
 
+	tc = tcase_create("IPv6");
+	tcase_add_test(tc, test_util_ipv6_is_valid);
+	suite_add_tcase(s, tc);
+
 	tc = tcase_create("Time");
 	tcase_add_test(tc, test_util_str_to_time);
 	suite_add_tcase(s, tc);
--- 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)
--- a/libpurple/util.h	Sat Jul 11 06:52:07 2009 +0000
+++ b/libpurple/util.h	Sat Jul 11 07:32:27 2009 +0000
@@ -1203,10 +1203,33 @@
  * @param ip The IP address to validate.
  *
  * @return True if the IP address is syntactically correct.
+ * @deprecated This function will be replaced with one that validates
+ *             as either IPv4 or IPv6 in 3.0.0. If you don't want this,
+ *             behavior, use one of the more specific functions.
  */
 gboolean purple_ip_address_is_valid(const char *ip);
 
 /**
+ * Checks if the given IP address is a syntactically valid IPv4 address.
+ *
+ * @param ip The IP address to validate.
+ *
+ * @return True if the IP address is syntactically correct.
+ * @since 2.6.0
+ */
+gboolean purple_ipv4_address_is_valid(const char *ip);
+
+/**
+ * Checks if the given IP address is a syntactically valid IPv6 address.
+ *
+ * @param ip The IP address to validate.
+ *
+ * @return True if the IP address is syntactically correct.
+ * @since 2.6.0
+ */
+gboolean purple_ipv6_address_is_valid(const char *ip);
+
+/**
  * This function extracts a list of URIs from the a "text/uri-list"
  * string.  It was "borrowed" from gnome_uri_list_extract_uris
  *