On Thu, Nov 11, 2010 at 11:48:32AM +0000, Richard Kenyon wrote:
> Hi, I've written the following patch for NetSurf.
> 
> This should replace 'bool url_host_is_ip_address(const char *host)'
> in 'utils/url.c'
> 

see commit 10951 for my version (attached too) as the caller to this
is exclusively in content/urldb.c which needs *extensive* fiddling to
be made ipv6 capable right now I am punting the v6 decision untill
later.

-- 
Regards Vincent
http://www.kyllikki.org/
Index: utils/url.c
===================================================================
--- utils/url.c (revision 10950)
+++ utils/url.c (working copy)
@@ -77,39 +77,29 @@
 
 
 /**
- * Check whether a host is an IP address
+ * Check whether a host string is an IPv4 dotted quad address of the
+ * format XXX.XXX.XXX.XXX
  *
- * \param  host    a hostname terminated by '\0' or '/'
+ * @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;
+       unsigned int b1, b2, b3, b4;
+       unsigned char c;
 
-       assert(host);
+       if (strspn(host, "0123456789.") < strlen(host)) 
+               return false;
 
-       /* 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);
+       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;
 }
 
 /**

Reply via email to