summaryrefslogtreecommitdiff
path: root/lib/std/dial+plan9.myr
blob: 35d97e972c67ee2b4f8f0f3db203f70691faa95c (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use sys

use "alloc.use"
use "die.use"
use "fmt.use"
use "option.use"
use "pathjoin.use"
use "result.use"
use "sleq.use"
use "strfind.use"
use "strstrip.use"
use "syswrap.use"
use "utf.use"


pkg std =
	const dial	: (dialstr : byte[:] -> result(fd, byte[:]))
;;

const Maxpath = 512

const dial = {str
	var netdir, proto, rem

	(netdir, proto, rem) = parsedial(str)
	if netdir.len != 0
		-> csdial(netdir, proto, rem)
	;;

	match csdial("/net", proto, rem)
	| `Ok fd:	-> `Ok fd
	| `Fail m:
		-> csdial("/net.alt", proto, rem)
	;;
}

const csdial = {netdir, proto, rem
	var dir, clone, addr, csaddr
	var ret, csfd, n
	var buf	: byte[Maxpath]

	/* Try using the connection server */
	dir = fmt("{}/cs", netdir)
	csfd = open(dir, Ordwr)
	if csfd < 0
		clone = fmt("{}/{}/clone", netdir, proto)
		ret = call(clone, rem, netdir)
		slfree(clone)
		if ret == -1
			-> `Fail "unable to dial without cs"
		else
			-> `Ok ret
		;;
	;;
	slfree(dir)

	csaddr = fmt("{}!{}", proto, rem)
	if write(csfd, csaddr) < 0
		close(csfd)
		-> `Fail "couldn't blah cs"
	;;
	slfree(csaddr)

	seek(csfd, 0, 0)
	while true
		n = read(csfd, buf[:])
		if n <= 0
			break
		;;

		match strfind(buf[:n], " ")
		| `None:	continue
		| `Some i:
			clone = buf[:i]
			addr = buf[i+1:n]
		;;

		ret = call(clone, addr, netdir)
		if ret >= 0
			break
		;;
	;;

	close(csfd)
	if ret < 0
		-> `Fail "unable to dial"
	;;
	-> `Ok ret
}

const call = {clone, addr, netdir
	var namebuf : byte[Maxpath]
	var databuf : byte[Maxpath]
	var name, base, dpath
	var cfd, datafd
	var c, n

	datafd = -1
	c = nsclonestr(clone, netdir)
	cfd = open(c, Ordwr)
	if cfd < 0
		goto cleanup
	;;

	n = read(cfd, namebuf[:])
	if n < 0
		goto cleanup
	;;
	fput(cfd, "connect {}", addr)
	name = strstrip(namebuf[:n])
	match strrfind(c, "/")
	| `None:	die("there should be a '/' here\n")
	| `Some i:	base = c[:i]
	;;
	dpath = bfmt(databuf[:], "{}/{}/data", base, name)
	datafd = open(dpath, Ordwr)
:cleanup
	close(cfd)
	slfree(c)
	-> datafd
}

const nsclonestr = {clone, netdir
	if decode(clone) == '#' || decode(clone) == '/'
		match std.strfind(clone[1:], "/")
		| `Some i:	clone = clone[i+1:]
		| `None:	/* nothing */
		;;
	;;
	-> pathcat(netdir, clone)
}

const parsedial = {str
	var netdir, proto, rem, hd, tl

	netdir=""
	proto = ""
	rem = ""
	match strfind(str, "!")
	| `None:
		proto = "net"
		rem = str
	| `Some sep:
		hd = str[:sep]
		tl = str[sep+1:]
		if decode(hd) == '#' || decode(hd) == '/'
			match strrfind(hd, "/")
			| `Some idx:
				netdir = hd[:idx]
				proto = hd[idx+1:]
			| `None:
				netdir = ""
				proto = hd
			;;
		else
			netdir = ""
			proto = hd

		;;
		rem = tl
	;;

	-> (netdir, proto, rem)
}