#include "port_before.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "port_after.h"
static int inet_net_pton_ipv4(const char *, u_char *, size_t);
static int inet_net_pton_ipv6(const char *, u_char *, size_t);
int
inet_net_pton(int af, const char *src, void *dst, size_t size)
{
switch (af) {
case AF_INET:
return (inet_net_pton_ipv4(src, dst, size));
case AF_INET6:
return (inet_net_pton_ipv6(src, dst, size));
default:
errno = EAFNOSUPPORT;
return (-1);
}
}
static int
inet_net_pton_ipv4(const char *src, u_char *dst, size_t size)
{
static const char
xdigits[] = "0123456789abcdef",
digits[] = "0123456789";
int n, ch, tmp, dirty, bits;
const u_char *odst = dst;
ch = (unsigned char)*src++;
if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
&& isascii((unsigned char)src[1]) && isxdigit((unsigned char)src[1])) {
if (size == 0)
goto emsgsize;
tmp = 0, dirty = 0;
src++;
while ((ch = (unsigned char)*src++) != '\0' &&
isascii(ch) && isxdigit(ch)) {
if (isupper(ch))
ch = tolower(ch);
n = strchr(xdigits, ch) - xdigits;
assert(n >= 0 && n <= 15);
if (dirty == 0)
tmp = n;
else
tmp = (tmp << 4) | n;
if (++dirty == 2) {
if (size-- == 0)
goto emsgsize;
*dst++ = (u_char) tmp;
dirty = 0;
}
}
if (dirty) {
if (size-- == 0)
goto emsgsize;
*dst++ = (u_char) (tmp << 4);
}
} else if (isascii(ch) && isdigit(ch)) {
for (;;) {
tmp = 0;
do {
n = strchr(digits, ch) - digits;
assert(n >= 0 && n <= 9);
tmp *= 10;
tmp += n;
if (tmp > 255)
goto enoent;
} while ((ch = (unsigned char)*src++) != '\0' &&
isascii(ch) && isdigit(ch));
if (size-- == 0)
goto emsgsize;
*dst++ = (u_char) tmp;
if (ch == '\0' || ch == '/')
break;
if (ch != '.')
goto enoent;
ch = (unsigned char)*src++;
if (!isascii(ch) || !isdigit(ch))
goto enoent;
}
} else
goto enoent;
bits = -1;
if (ch == '/' && isascii((unsigned char)src[0]) &&
isdigit((unsigned char)src[0]) && dst > odst) {
ch = (unsigned char)*src++;
bits = 0;
do {
n = strchr(digits, ch) - digits;
assert(n >= 0 && n <= 9);
bits *= 10;
bits += n;
if (bits > 32)
goto emsgsize;
} while ((ch = (unsigned char)*src++) != '\0' &&
isascii(ch) && isdigit(ch));
if (ch != '\0')
goto enoent;
}
if (ch != '\0')
goto enoent;
if (dst == odst)
goto enoent;
if (bits == -1) {
if (*odst >= 240)
bits = 32;
else if (*odst >= 224)
bits = 4;
else if (*odst >= 192)
bits = 24;
else if (*odst >= 128)
bits = 16;
else
bits = 8;
if (bits < ((dst - odst) * 8))
bits = (dst - odst) * 8;
}
while (bits > ((dst - odst) * 8)) {
if (size-- == 0)
goto emsgsize;
*dst++ = '\0';
}
return (bits);
enoent:
errno = ENOENT;
return (-1);
emsgsize:
errno = EMSGSIZE;
return (-1);
}
static int
inet_net_pton_ipv6(const char *src, u_char *dst, size_t size)
{
struct in6_addr in6;
int ret;
int bits;
size_t bytes;
char buf[INET6_ADDRSTRLEN + sizeof("/128")];
char *sep;
const char *errstr;
if (strlcpy(buf, src, sizeof buf) >= sizeof buf) {
errno = EMSGSIZE;
return (-1);
}
sep = strchr(buf, '/');
if (sep != NULL)
*sep++ = '\0';
ret = inet_pton(AF_INET6, buf, &in6);
if (ret != 1)
return (-1);
if (sep == NULL)
bits = 128;
else {
bits = strtonum(sep, 0, 128, &errstr);
if (errstr) {
errno = EINVAL;
return (-1);
}
}
bytes = (bits + 7) / 8;
if (bytes > size) {
errno = EMSGSIZE;
return (-1);
}
memcpy(dst, &in6.s6_addr, bytes);
return (bits);
}
#undef inet_net_pton
__weak_reference(__inet_net_pton, inet_net_pton);