summaryrefslogtreecommitdiff
path: root/sys/src/ape/lib/bsd/inet_pton.c
blob: 0e10ab5cfe7536fcdd7e2e7050b9a1a517b59e50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* posix */
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

/* bsd extensions */
#include <sys/uio.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <errno.h>

static int
ipcharok(int c)
{
	return c == ':' || isascii(c) && isxdigit(c);
}

static int
delimchar(int c)
{
	if(c == '\0')
		return 1;
	if(c == ':' || isascii(c) && isalnum(c))
		return 0;
	return 1;
}

int
inet_pton(int af, char *src, void *dst)
{
	int i, elipsis = 0;
	unsigned char *to;
	unsigned long x;
	char *p, *op;

	if(af == AF_INET)
		return inet_aton(src, (struct in_addr*)dst);

	if(af != AF_INET6){
		errno = EAFNOSUPPORT;
		return -1;
	}

	to = ((struct in6_addr*)dst)->s6_addr;
	memset(to, 0, 16);

	p = src;
	for(i = 0; i < 16 && ipcharok(*p); i+=2){
		op = p;
		x = strtoul(p, &p, 16);

		if(x != (unsigned short)x || *p != ':' && !delimchar(*p))
			return 0;			/* parse error */

		to[i] = x>>8;
		to[i+1] = x;
		if(*p == ':'){
			if(*++p == ':'){	/* :: is elided zero short(s) */
				if (elipsis)
					return 0;	/* second :: */
				elipsis = i+2;
				p++;
			}
		} else if (p == op)		/* strtoul made no progress? */
			break;
	}
	if (p == src || !delimchar(*p))
		return 0;				/* parse error */
	if(i < 16){
		memmove(&to[elipsis+16-i], &to[elipsis], i-elipsis);
		memset(&to[elipsis], 0, 16-i);
	}
	return 1;
}