summaryrefslogtreecommitdiff
path: root/lib/std/dialparse+posixy.myr
blob: 9dd36bd36b2b05611ab122491e4c75c9279b8018 (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
use "alloc"
use "chartype"
use "hasprefix"
use "intparse"
use "ipparse"
use "option"
use "resolve"
use "result"
use "slcp"
use "sleq"
use "strfind"
use "striter"
use "traits"

pkg std =
	pkglocal const parseport	: (port : byte[:] -> option(uint16))
	pkglocal const getaddr		: (addr : byte[:] -> result(netaddr, byte[:]))
	pkglocal const nameseg		: (str : byte[:] -> option((byte[:], byte[:])))
;;

const parseport = {port
	match intparse(port)
	| `Some n:	-> `Some n
	| `None:
		/* a small number of hardcoded ports */
		if eq(port, "http")
			-> `Some 80
		elif eq(port, "https")
			-> `Some 443
		elif eq(port, "ircd")
			-> `Some 6667
		elif eq(port, "dns")
			-> `Some 53
		;;
	;;
	-> `None
}

const getaddr = {addr
	var ip

	match ipparse(addr)
	| `Some a:
		ip = a
	| `None:
		match resolve(addr)
		| `Ok hi:
			if hi.len > 0
				ip = hi[0].addr
				slfree(hi)
			else
				slfree(hi)
				-> `Err "no hosts resolved for addr"
			;;
		| `Err m:
			-> `Err "could not get host info"
		;;
	;;
	-> `Ok ip
}

const nameseg = {str
	match strfind(str, "!")
	| `Some idx:
		-> `Some (str[:idx], str[idx+1:])
	| `None:
		-> `None
	;;
}