summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Kendrick <rjek@netsurf-browser.org>2010-12-04 14:47:03 +0000
committerRob Kendrick <rjek@netsurf-browser.org>2010-12-04 14:47:03 +0000
commitfe56f45ac683aeae69201d1c4ff0d9a8b0e4ca32 (patch)
treec377e8a00576d013e050b949648788b7b770c092
parent7a3a96045ccc061b9d0bce59d2bc3532f2268660 (diff)
downloadnetsurf-fe56f45ac683aeae69201d1c4ff0d9a8b0e4ca32.tar.gz
netsurf-fe56f45ac683aeae69201d1c4ff0d9a8b0e4ca32.tar.bz2
Reimplement url_host_is_ipaddress() to cope with IPv6 addresses and decimal/hex IPv4 addresses in addition to dotted-quad v4 addresses.
svn path=/trunk/netsurf/; revision=10959
-rw-r--r--utils/url.c37
1 files changed, 20 insertions, 17 deletions
diff --git a/utils/url.c b/utils/url.c
index bc20e28e2..d50026bb1 100644
--- a/utils/url.c
+++ b/utils/url.c
@@ -31,6 +31,7 @@
#include <strings.h>
#include <regex.h>
#include <unistd.h>
+#include <arpa/inet.h>
#include "curl/curl.h"
#include "utils/log.h"
#include "utils/url.h"
@@ -77,29 +78,31 @@ void url_init(void)
/**
- * Check whether a host string is an IPv4 dotted quad address of the
- * format XXX.XXX.XXX.XXX
+ * Check whether a host string is an IP address. It should support and
+ * detect IPv4 addresses (all of dotted-quad or subsets, decimal or
+ * hexadecimal notations) and IPv6 addresses (including those containing
+ * embedded IPv4 addresses.)
*
- * @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 '/'
+ * \param host a hostname terminated by '\0'
* \return true if the hostname is an IP address, false otherwise
*/
-bool url_host_is_ip_address(const char *host) {
- 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)
+bool url_host_is_ip_address(const char *host)
+{
+ struct in_addr ipv4;
+#ifndef NO_IPV6
+ struct in6_addr ipv6;
+#endif
+ if (strspn(host, "0123456789abcdefABCDEF[].:") < strlen(host))
return false;
- if ((b1 > 255) || (b2 > 255) || (b3 > 255) || (b4 > 255))
- return false;
+ if (inet_aton(host, &ipv4) != 0)
+ return true;
+#ifndef NO_IPV6
+ if (inet_pton(AF_INET6, host, &ipv6) == 1)
+ return true;
+#endif
- return true;
+ return false;
}
/**