[PATCH] resolv: add IPv6 support to inet_net_pton()

Florian Weimer fweimer@redhat.com
Sun Mar 17 11:18:14 GMT 2024


* Job Snijders:

> +/*
> + * Convert an IPv6 prefix from presentation format to network format.
> + * Return the number of bits specified, or -1 as error (check errno).
> + */
> +static int
> +inet_net_pton_ipv6 (const char *src, u_char *dst, size_t size)
> +{
> +	struct in6_addr	 in6;
> +	int		 bits, save_errno;
> +	long		 lbits;
> +	size_t		 bytes;
> +	char		 buf[INET6_ADDRSTRLEN + sizeof("/128")];
> +	char		*ep, *sep;
> +
> +	save_errno = errno;
> +
> +	if (strlcpy(buf, src, sizeof(buf)) >= sizeof(buf)) {
> +		__set_errno (EMSGSIZE);
> +		return (-1);
> +	}
> +
> +	sep = strchr(buf, '/');
> +	if (sep != NULL)
> +		*sep++ = '\0';
> +
> +	if (inet_pton(AF_INET6, buf, &in6) != 1) {
> +		__set_errno (ENOENT);
> +		return (-1);
> +	}

I think you can use __inet_pton_length here.  Then you won't need to
make a copy.

Thanks,
Florian



More information about the Libc-alpha mailing list