url.c patch

John-Mark Bell jmb at netsurf-browser.org
Thu Nov 25 23:03:29 GMT 2010


On Thu, 2010-11-11 at 11:48 +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'

I'd be inclined to retain the existing API, as it'll save reintroducing
it when we get around to fully supporting IPv6 (and thus want to know if
the host part is any kind of IP address).

For now, the implementation can simply be:

bool url_host_is_ip_address(const char *host)
{
	return url_host_is_ipv4_address(host);
}

> -------------------------------------------------------------------------------
> 
> /* Each digit of an octet is represented as a character
>  * so the maximum 'length' of an octet is 3
>  */
> 
> #define MAX_OCTET_LEN 3

Where is this used?

> #define N_OCTETS 4 /* ipv4 addresses consist of 4 octets */
> 
> #define DECIMAL 10 /* octets are in base 10 */
> 
> /**
>  * Determine if host is a valid ipv4 address.
>  *
>  * \param host		Address to validate.
>  *
>  * \return		Boolean: true if host is valid, else false.
>  */
> 
> bool url_host_is_ip4_address(const char *host)
> {
> 	/* We accept an ipv4 address written in dot-decimal notation
> 	 * ipv4 addresses consist of 4 octets of the address 
> 	 * separately in decimal and separated by periods
> 	 * for example 123.0.42.255
> 	 */
> 
> 	assert(host != NULL);
> 	
> 	char *p;
> 	int octet_count = 0;
> 	int octet;
> 	
> 	do {
> 		octet = strtol(host, &p, DECIMAL);

I'd be inclined to just use 10 here -- it's actually clearer to me than
using a preprocessor definition :)

> 		host = p + 1;

Ensure that *p == '.', here

> 		if (octet >= 0 && octet <= 255)
> 			octet_count++;
> 		else
> 			return false;
> 	} while (*p != '\0');
> 	
> 	/* If we don't have 4 octets then host is invalid */
> 	return (octet_count == N_OCTETS);
> }
> 
> -------------------------------------------------------------------------------

Otherwise, this looks fine to me.

Thanks,


J.




More information about the netsurf-dev mailing list