summaryrefslogtreecommitdiff
path: root/lib/std/listen+posixy.myr
blob: 7f4b35b8ee9c7c4ff3bb6e5f61a4fb407f185968 (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
use sys

use "alloc"
use "chartype"
use "dialparse"
use "die"
use "endian"
use "option"
use "resolve"
use "result"
use "slcp"
use "sleq"
use "strfind"
use "striter"
use "syswrap"
use "utf"

pkg std =
	const announce	: (ds : byte[:] -> result(fd, byte[:]))
	const accept	: (lfd : fd -> result(fd, byte[:]))
;;

const announce = {ds
	match nameseg(ds)
	| `Some ("tcp", str):	-> announcesock(sys.Sockstream, str)
	| `Some ("udp", str):	-> announcesock(sys.Sockdgram, str)
	| `Some ("unix", str):	-> announceunix(str)
	| `Some (proto, str):	-> `Err "unknown protocol"
	| `None:	-> `Err "missing protocol"
	;;
}

const announcesock = {proto, str
	var sa4 : sys.sockaddr_in
	var sa6 : sys.sockaddr_in6
	var sa	: sys.sockaddr#
	var hoststr, portstr, port
	var sock, sz
	var yes

	match nameseg(str)
	| `None:	-> `Err "required addr!port for ip listen"
	| `Some ("", _):	-> `Err "empty host"
	| `Some (_, ""):	-> `Err "empty port"
	| `Some segs:	(hoststr, portstr) = segs
	;;

	match parseport(portstr)
	| `Some p:	port = p
	| `None:	-> `Err("invalid port")
	;;

	if std.sleq(hoststr, "*")
		sa6=[.fam=sys.Afinet6, .port=hosttonet(port)]
		sa = (&sa6 : sys.sockaddr#)
		sz = sizeof(sys.sockaddr_in6)
	else
		match getaddr(hoststr)
		| `Ok `Ipv4 bits:
			sa4=[.fam=sys.Afinet, .addr=bits, .port=hosttonet(port)]
			sa = (&sa4 : sys.sockaddr#)
			sz = sizeof(sys.sockaddr_in)
		| `Ok `Ipv6 bits:
			sa6=[.fam=sys.Afinet6, .addr=bits, .port=hosttonet(port)]
			sa = (&sa6 : sys.sockaddr#)
			sz = sizeof(sys.sockaddr_in6)
		| `Err m:
			-> `Err m
		;;
	;;
	sock = sys.socket(sa.fam, proto, 0)
	if sock < 0
		-> `Err "failed to create socket"
	;;
	yes = 1
	if sys.setsockopt(sock, sys.Solsocket, sys.Soreuseaddr, (&yes : void#), sizeof(int)) < 0
		-> `Err "failed to set sock opts"
	;;
	if sys.bind(sock, sa, sz) < 0
		-> `Err "failed to bind socket"
	;;
	if sys.listen((sock : sys.fd), 10) < 0
		-> `Err "unable to listen on socket"
	;;
	-> `Ok (sock : fd)
}

const announceunix = {path
	var sa : sys.sockaddr_un
	var sock
	var yes
	
	sa = [.fam = sys.Afunix]
	if path.len >= sa.path.len
		-> `Err "path too long"
	;;

	slcp(sa.path[:path.len], path)
	sock = sys.socket(sys.Afunix, sys.Sockstream, 0)
	if sock < 0
		-> `Err "failed to create socket"
	;;
	yes = 1
	if sys.setsockopt(sock, sys.Solsocket, sys.Soreuseaddr, (&yes : void#), sizeof(int)) < 0
		-> `Err "failed to set sock opts"
	;;
	sys.unlink(path)
	if sys.bind(sock, (&sa : sys.sockaddr#), sizeof(sys.sockaddr_un)) < 0
		-> `Err "failed to bind address"
	;;
	-> `Ok (sock : fd)

}

const accept = {lfd
	var sa : sys.sockaddr_storage
	var len : sys.size
	var fd

	fd = sys.accept((lfd : sys.fd), (0 : sys.sockaddr#), (0 : sys.size#))
	if fd < 0
		-> `Err "unable to accept socket"
	;;
	-> `Ok (fd : fd)
}