diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/url.c | 36 | ||||
-rw-r--r-- | utils/url.h | 1 |
2 files changed, 37 insertions, 0 deletions
diff --git a/utils/url.c b/utils/url.c index 1c8005a5d..f6f7fdc9d 100644 --- a/utils/url.c +++ b/utils/url.c @@ -64,6 +64,42 @@ void url_init(void) /** + * Check whether a host is an IP address + * + * \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); +} + +/** * Normalize a URL. * * \param url an absolute URL diff --git a/utils/url.h b/utils/url.h index e9ad7fcb4..1467811b7 100644 --- a/utils/url.h +++ b/utils/url.h @@ -29,6 +29,7 @@ struct url_components { }; void url_init(void); +bool url_host_is_ip_address(const char *host); url_func_result url_normalize(const char *url, char **result); url_func_result url_join(const char *rel, const char *base, char **result); url_func_result url_host(const char *url, char **result); |