interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / net / tcp / peer.om

299 lines · 4929 bytes Raw Blame History
1note peer.om: host side of the L232 QEMU gate. Seeded Omega over host TCP sockets
2note on 127.0.0.1; QEMU slirp carries each connection into the guest stack.
3note Arguments: MODE PORT FILE LIMIT [OUT]
4note e: connect, send FILE, close the write side, require the echo to equal FILE
5note s: connect, send FILE, close the write side, require EOF and no data back
6note r: listen on PORT, accept one connection, require the stream to equal FILE
7note and end in EOF; write the received bytes to OUT
8note Prints PEER_PASS or PEER_FAIL with counters; exit 0 only on PEER_PASS.
9global Pbuf uchar 16777216
10global Pin uchar 65536
11global Psa uchar 16
12global Parg uchar 256
13note Pcf: 0 file length, 1 sent, 2 received, 3 mismatched bytes, 4 eof seen,
14note 5 write side shut, 6 out fd (0 none), 7 errors.
15global Pcf ulong 8
16global Pnum uchar 32
17text Mpass "PEER_PASS"
18text Mfail "PEER_FAIL"
19text Msent " sent="
20text Mgot " got="
21text Mbad " bad="
22text Meof " eof="
23text Merr " err="
24text Mnl "\n"
25fn pnum v ulong to void
26i = ulong 32
27while true bound 21
28i = sub i 1
29put Pnum i uchar add 48 rem v 10
30v = div v 10
31if eq v 0
32break
33end
34end
35k = write 1 Pnum i sub 32 i
36end
37fn parg i ulong to ulong
38if ge long i argcnt
39ret 0
40end
41k = argread i Parg 0 255
42if lt k 1
43exit 2
44end
45ret ulong k
46end
47fn pdec n ulong to ulong
48v = ulong 0
49if eq n 0
50exit 2
51end
52for i ulong n bound 20
53c = ulong get Parg i
54if or lt c 48 gt c 57
55exit 2
56end
57v = add mul v 10 sub c 48
58end
59ret v
60end
61fn pload n ulong to void
62f = fopen Parg 0 n 0 0
63if lt f 0
64exit 3
65end
66o = ulong 0
67while true bound 100000
68k = read int f Pbuf o umin 1048576 sub 16777216 o
69if lt k 0
70exit 3
71end
72if eq k 0
73break
74end
75o = add o ulong k
76if eq o 16777216
77exit 3
78end
79end
80c = fclose int f
81put Pcf 0 o
82end
83fn umin a ulong b ulong to ulong
84if lt a b
85ret a
86end
87ret b
88end
89fn psa port ulong to void
90put Psa 0 2
91put Psa 1 0
92put Psa 2 uchar shr port 8
93put Psa 3 uchar and port 255
94put Psa 4 127
95put Psa 5 0
96put Psa 6 0
97put Psa 7 1
98for i ulong 8
99put Psa add 8 i 0
100end
101end
102fn pnow to ulong
103t = monons
104if lt t 0
105exit 4
106end
107ret div ulong t 1000000
108end
109fn pdial port ulong lim ulong to int
110psa port
111s = socknew 2 526337 0
112if lt s 0
113exit 5
114end
115r = connect int s Psa 0 16
116if and ne r 0 ne r neg long 115
117exit 5
118end
119d = add pnow lim
120while lt pnow d bound 100000000
121w = waitio int s 4 10000000
122if ne and w 24 0
123exit 5
124end
125if ne and w 4 0
126ret int s
127end
128end
129exit 5
130end
131fn paccept port ulong lim ulong to int
132psa port
133s = socknew 2 524289 0
134if lt s 0
135exit 5
136end
137if ne bind int s Psa 0 16 0
138exit 5
139end
140if ne listen int s 1 0
141exit 5
142end
143d = add pnow lim
144while lt pnow d bound 100000000
145w = waitio int s 1 10000000
146if ne and w 1 0
147a = accept int s 526336
148if lt a 0
149exit 5
150end
151c = fclose int s
152ret int a
153end
154end
155exit 5
156end
157note Compare n received bytes with the file at the current receive offset.
158fn pcheck n ulong to void
159g = get Pcf 2
160for i ulong n bound 65536
161j = add g i
162if ge j get Pcf 0
163put Pcf 3 add get Pcf 3 1
164else
165if ne get Pin i get Pbuf j
166put Pcf 3 add get Pcf 3 1
167end
168end
169end
170put Pcf 2 add g n
171end
172fn pout n ulong to void
173f = get Pcf 6
174if eq f 0
175ret
176end
177o = ulong 0
178while lt o n bound 65536
179k = write int f Pin o sub n o
180if le k 0
181exit 3
182end
183o = add o ulong k
184end
185end
186fn psend s int to void
187o = get Pcf 1
188if lt o get Pcf 0
189k = send s Pbuf o umin 65536 sub get Pcf 0 o 64
190if gt k 0
191put Pcf 1 add o ulong k
192else
193if ne k neg long 11
194put Pcf 7 add get Pcf 7 1
195put Pcf 1 get Pcf 0
196end
197end
198end
199end
200fn precv s int to void
201k = recv s Pin 0 65536 64
202if gt k 0
203pout ulong k
204pcheck ulong k
205ret
206end
207if eq k 0
208put Pcf 4 1
209ret
210end
211if ne k neg long 11
212put Pcf 7 add get Pcf 7 1
213put Pcf 4 1
214end
215end
216fn prun s int mode ulong lim ulong to void
217d = add pnow lim
218while lt pnow d bound 100000000
219ev = ulong 1
220if and ne mode 114 lt get Pcf 1 get Pcf 0
221ev = ulong 5
222end
223w = waitio s int ev 10000000
224if ne and w 4 0
225psend s
226end
227if and ne mode 114 eq get Pcf 5 0
228if ge get Pcf 1 get Pcf 0
229r = shutdown int s 1
230put Pcf 5 1
231end
232end
233if ne and w 25 0
234precv s
235end
236if ne get Pcf 4 0
237ret
238end
239end
240put Pcf 7 add get Pcf 7 1
241end
242fn presult mode ulong to bool
243ok = and eq get Pcf 7 0 ne get Pcf 4 0
244ok = and ok eq get Pcf 3 0
245if eq mode 115
246ok = and ok eq get Pcf 2 0
247else
248ok = and ok eq get Pcf 2 get Pcf 0
249end
250if ne mode 114
251ok = and ok eq get Pcf 1 get Pcf 0
252end
253if ok
254k = write 1 Mpass 0 len Mpass
255else
256k = write 1 Mfail 0 len Mfail
257end
258k = write 1 Msent 0 len Msent
259pnum get Pcf 1
260k = write 1 Mgot 0 len Mgot
261pnum get Pcf 2
262k = write 1 Mbad 0 len Mbad
263pnum get Pcf 3
264k = write 1 Meof 0 len Meof
265pnum get Pcf 4
266k = write 1 Merr 0 len Merr
267pnum get Pcf 7
268k = write 1 Mnl 0 len Mnl
269ret ok
270end
271fn main to int
272n = parg 1
273if ne n 1
274exit 2
275end
276mode = ulong get Parg 0
277port = pdec parg 2
278pload parg 3
279lim = mul pdec parg 4 1000
280n = parg 5
281if gt n 0
282f = fopen Parg 0 n 577 420
283if lt f 0
284exit 3
285end
286put Pcf 6 ulong f
287end
288s = int 0
289if eq mode 114
290s = paccept port lim
291else
292s = pdial port lim
293end
294prun s mode lim
295if presult mode
296ret 0
297end
298ret 1
299end