summaryrefslogtreecommitdiff
path: root/libstd/ipparse.myr
blob: 17bc6f08bc9456d7c66936ceb7a1c04a5d39e8a6 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use "die.use"
use "intparse.use"
use "option.use"
use "strfind.use"
use "types.use"
use "chartype.use"
use "fmt.use"
use "slcp.use"
use "slfill.use"
use "sleq.use"

 /* FIXME: needed for decls which should be pulled in as hidden */
use "hasprefix.use"
use "utf.use"

pkg std =

	type netaddr = union
		`Ipv4	byte[4]
		`Ipv6	byte[16]
	;;

	const ipparse	: (ip : byte[:]	-> option(netaddr))
	const ip4parse	: (ip : byte[:] -> option(netaddr))
	const ip6parse	: (ip : byte[:] -> option(netaddr))
;;

const ipparse = {ip
	match strfind(ip, ":")
	| `Some _:	-> ip6parse(ip)
	| `None:	-> ip4parse(ip)
	;;
}

const ip4parse = {ip
	var a, b, c, d
	var ok
	/*
	var addr
	var last : size
	var x : option(int32)
	var val : int32 /* need int32 to check for overflow */
	var i
	var j : size
	*/

	(a, ip, ok) = num(ip, 0, 255, 10, '.', true)
	(ip, ok) = delim(ip, '.', ok)
	(b, ip, ok) = num(ip, 0, 255, 10, '.', ok)
	(ip, ok) = delim(ip, '.', ok)
	(c, ip, ok) = num(ip, 0, 255, 10, '.', ok)
	(ip, ok) = delim(ip, '.', ok)
	(d, ip, ok) = num(ip, 0, 255, 10, '.', ok)

	if ok && ip.len == 0
		-> `Some (`Ipv4 [a, b, c, d])
	else
		-> `None
	;;
}

const ip6parse = {ip
	var val : byte[16]
	var expand, split
	var v, ok
	var i, nseg

	ok = true
	expand = false
	split = 0
	if ip.len > 2 && std.sleq(ip[:2], "::")
		expand = true
		split = 0
	;;
	nseg = 0
	for i = 0; ip.len > 0 && ok; i++
		/* parse 'num' segment */
		(v, ip, ok) = num(ip, 0, 65536, 16, ':', ok)
		nseg++
		if ip.len == 0 || nseg == 8
			break
		;;
		(ip, ok) = delim(ip, ':', ok)
		/* only one '::' allowed once */
		if ip.len > 0 && ip[0] == ':'castto(byte) && !expand
			expand = true
			split = i
			(ip, ok) = delim(ip, ':', ok)
		;;

		/* pack it into the bytes */
		val[i*2] = ((v & 0xff00) >> 8) castto(byte)
		val[i*2 + 1] = (v & 0xff) castto(byte)
	;;

	if ok && ip.len == 0
		if expand
			expandsplit(val[:], split, i)
		elif nseg != 8
			-> `None
		;;
		-> `Some `Ipv6 val
	else
		-> `None
	;;
}

/* take "a:b::c:d" and expand it to "a:b:0:0:...:0:c:d" */
const expandsplit = {ip, split, len
	var width

	width = 16 - len
	std.slcp(ip[split:len], ip[split+width:len+width])
	std.slfill(ip[len:len+width], 0)
}

const delim = {ip, sep, ok
	if ip.len > 0 && ip[0] == sep castto(byte)
		-> (ip[1:], ok)
	else
		-> ("", false)
	;;
}

generic num = {ip, lo, hi, base, sep, ok -> (@a::(numeric,integral), byte[:], bool)
	var len

	if !ok
		-> (0, "", false)
	;;

	for len = 0; len < ip.len; len++
		if ip[len] == sep castto(byte)
			break
		;;
	;;
	match intparsebase(ip[:len], base)
	| `std.Some v:
		if v < lo || v > hi
			-> (0, "", false)
		;;
		-> (v castto(@a::(numeric,integral)), ip[len:], true)
	| `std.None:
		-> (0, "", false)
	;;
}