r10951 vince - /trunk/netsurf/utils/url.c

netsurf at semichrome.net netsurf at semichrome.net
Fri Nov 26 15:36:28 GMT 2010


Author: vince
Date: Fri Nov 26 09:36:27 2010
New Revision: 10951

URL: http://source.netsurf-browser.org?rev=10951&view=rev
Log:
Improve url_host_is_ip_address 

Modified:
    trunk/netsurf/utils/url.c

Modified: trunk/netsurf/utils/url.c
URL: http://source.netsurf-browser.org/trunk/netsurf/utils/url.c?rev=10951&r1=10950&r2=10951&view=diff
==============================================================================
--- trunk/netsurf/utils/url.c (original)
+++ trunk/netsurf/utils/url.c Fri Nov 26 09:36:27 2010
@@ -77,39 +77,29 @@
 
 
 /**
- * Check whether a host is an IP address
- *
- * \param  host    a hostname terminated by '\0' or '/'
+ * Check whether a host string is an IPv4 dotted quad address of the
+ * format XXX.XXX.XXX.XXX
+ *
+ * @todo This *should* be implemented with inet_pton but that requires
+ * implementing compatability glue for several operating systems.
+ *
+ * \param  host a hostname terminated by '\0' or '/'
  * \return true if the hostname is an IP address, false otherwise
  */
 bool url_host_is_ip_address(const char *host) {
-  	int b;
-  	bool n;
-
-	assert(host);
-
-	/* an IP address is of the format XXX.XXX.XXX.XXX, ie totally
-	 * numeric with 3 full stops between the numbers */
-	b = 0; // number of breaks
-	n = false; // number present
-	do {
-		if (*host == '.') {
-		  	if (!n)
-		  		return false;
-			b++;
-			n = false;
-		} else if ((*host == '\0') || (*host == '/')) {
-		  	if (!n)
-		  		return false;
-		  	/* todo: check the values are in 0-255 range */
-		  	return (b == 3);
-		} else if (*host < '0' || *host > '9') {
-		  	return false;
-                } else {
-                	n = true;
-                }
-                host++;
-        } while (1);
+	unsigned int b1, b2, b3, b4;
+	unsigned char c;
+
+	if (strspn(host, "0123456789.") < strlen(host)) 
+		return false;
+
+	if (sscanf(host, "%3u.%3u.%3u.%3u%c", &b1, &b2, &b3, &b4, &c) != 4)
+		return false;
+
+	if ((b1 > 255) || (b2 > 255) || (b3 > 255) || (b4 > 255))
+		return false;
+
+	return true;
 }
 
 /**




More information about the netsurf-commits mailing list