interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / core / hive-view / src / main.om

408 lines · 7213 bytes Raw Blame History
1note hive-view src/main.om: entry points.
2note server serve RUNDIR PORT ADDR [ADDR] listen on each IPv4 ADDR:PORT (127.0.0.1 or the
3note Tailscale range 100.64.0.0/10 only), fork the
4note shared poller and one process per connection
5note server mktoken create ~/.config/hive-view/token (mode 600, 64 hex)
6note server poll-once RUNDIR one full poller pass, then exit (tests)
7note Exit codes: 2 usage, 3 token missing or unsafe, 4 address refused, 5 listen failed,
8note 6 HOME unset, 17 token already exists, 18 token could not be created or written,
9note 19 an internal path or tmux argument buffer would overflow (util.om vwpc, vwkc).
10global gpoll uchar 32
11global glf ulong 4
12global glfn ulong 1
13text tserve "serve"
14text tmktok "mktoken"
15text tpoll1 "poll-once"
16text tenvh "HOME="
17text tcfgd "/.config/hive-view"
18text ttokf "/.config/hive-view/token"
19text tstd1 "/.local/state"
20text tstd2 "/.local/state/hive-view"
21text tlhost "localhost:"
22text tlspark "spark.hub.internal:"
23text tlspk "spark:"
24text tmsgtok "token written to ~/.config/hive-view/token (mode 600)\n"
25text tmsgex "refused: ~/.config/hive-view/token already exists\n"
26text tmsgcr "refused: cannot create ~/.config/hive-view/token (does ~/.config exist and is it writable?)\n"
27fn vwhome to bool
28eb = new uchar 600 0
29for i ulong ulong envcnt
30n = envread i eb 0 600
31if gt n 5
32if heq view part eb 0 5 tenvh
33k = hmin sub ulong n 5 511
34for j ulong k
35put ghome j get eb add 5 j
36end
37put ghomel 0 k
38ret gt k 0
39end
40end
41end
42ret false
43end
44note vwarg: argument i into ab; its length, or 0
45fn vwarg i ulong ab edit uchar 512 to ulong
46if ge i ulong argcnt
47ret 0
48end
49n = argread i ab 0 512
50if le n 0
51ret 0
52end
53ret ulong n
54end
55fn vwargis ab edit uchar 512 n ulong t view uchar * to bool
56ret heq view part ab 0 n t
57end
58note vwtokld: the token file is ours, mode 0600 (no group/other bits), and 64 lowercase hex
59fn vwtokld to bool
60vwhpath ttokf
61fd = vwfopen
62if lt fd 0
63ret false
64end
65r = fstat int fd gstb 0 128
66if lt r 0
67x = fclose int fd
68ret false
69end
70mode = vwst32 16
71uid = vwst32 24
72size = vwst64 48
73ok = and eq and mode 63 0 eq uid ulong getuid
74ok = and ok or eq size 64 eq size 65
75if ok
76r = read int fd gtok 0 64
77ok = eq r 64
78end
79x = fclose int fd
80if not ok
81ret false
82end
83for i ulong 64
84if not vwhex get gtok i
85ret false
86end
87end
88ret true
89end
90fn vwmktok to int
91vwhpath tcfgd
92r = makedir gpth 0 get gpl 0 448
93vwhpath ttokf
94fd = fopen gpth 0 get gpl 0 524481 384
95if lt fd 0
96if eq fd -17
97x = write 2 tmsgex 0 len tmsgex
98ret 17
99end
100x = write 2 tmsgcr 0 len tmsgcr
101ret 18
102end
103rb = new uchar 32 0
104hx = new uchar 65 0
105k = random rb 0 32 0
106if ne k 32
107x = fclose int fd
108ret 18
109end
110for i ulong 32
111c = get rb i
112put hx mul i 2 get thexd shr c 4
113put hx add mul i 2 1 get thexd and c 15
114end
115put hx 64 10
116w = write int fd hx 0 65
117s = fsync int fd
118x = fclose int fd
119if ne w 65
120ret 18
121end
122x = write 1 tmsgtok 0 len tmsgtok
123ret 0
124end
125note vwip: dotted IPv4 in ab[0, n) as a host-order uint, or 0 when malformed
126fn vwip ab edit uchar 512 n ulong to ulong
127v = ulong 0
128part8 = ulong 0
129digs = ulong 0
130dots = ulong 0
131for i ulong n
132c = get ab i
133if eq c 46
134if or eq digs 0 gt part8 255
135ret 0
136end
137v = or shl v 8 part8
138part8 = 0
139digs = 0
140dots = add dots 1
141else
142if not vwdig c
143ret 0
144end
145part8 = add mul part8 10 ulong sub c 48
146digs = add digs 1
147if gt digs 3
148ret 0
149end
150end
151end
152if or or ne dots 3 eq digs 0 gt part8 255
153ret 0
154end
155ret or shl v 8 part8
156end
157note vwipok: loopback 127.0.0.1 or the Tailscale CGNAT range 100.64.0.0/10; never 0.0.0.0
158fn vwipok ip ulong to bool
159if eq ip 2130706433
160ret true
161end
162ret eq shr ip 22 401
163end
164fn vwahadd ab edit uchar 512 n ulong to void
165k = get gahn 0
166if ge k 8
167ret
168end
169b = ulong 0
170if gt k 0
171b = add get gaho sub k 1 get gahl sub k 1
172end
173if gt add b add n 6 len gah
174ret
175end
176for i ulong n
177put gah add b i get ab i
178end
179put gaho k b
180put gahl k n
181put gahn 0 add k 1
182end
183fn vwahport t view uchar * to void
184ab = new uchar 512 0
185n = ulong 0
186for i ulong len t
187put ab n get t i
188n = add n 1
189end
190vwahtail ab n
191end
192note vwahtail: append ":PORT" (unless already ends in ':') and add as an allowed Host
193fn vwahtail ab edit uchar 512 n ulong to void
194m = n
195if gt m 0
196if ne get ab sub m 1 58
197put ab m 58
198m = add m 1
199end
200end
201d = new uchar 20 0
202i = ulong 20
203x = get gport 0
204for it ulong 20
205i = sub i 1
206put d i uchar add rem x 10 48
207x = div x 10
208if eq x 0
209break
210end
211end
212for k ulong sub 20 i
213put ab m get d add i k
214m = add m 1
215end
216vwahadd ab m
217end
218fn vwlisten ab edit uchar 512 n ulong tail bool to int
219ip = vwip ab n
220if eq ip 0
221ret 4
222end
223if not vwipok ip
224ret 4
225end
226a = new uchar 16 0
227r = haddr4 a uint ip get gport 0
228lf = hlisten a 64
229if lt lf 0
230ret 5
231end
232k = get glfn 0
233put glf k ulong lf
234put glfn 0 add k 1
235vwahtail ab n
236if tail
237vwahport tlspark
238vwahport tlspk
239end
240ret 0
241end
242fn vwclosel to void
243for k ulong get glfn 0
244x = fclose int get glf k
245end
246end
247fn vwspawn to long
248pid = fork
249if eq pid 0
250vwclosel
251vwpoll
252exit 0
253end
254ret pid
255end
256fn vwpollset to ulong
257for k ulong get glfn 0
258b = mul k 8
259fd = get glf k
260put gpoll b uchar and fd 255
261put gpoll add b 1 uchar and shr fd 8 255
262put gpoll add b 2 0
263put gpoll add b 3 0
264put gpoll add b 4 1
265put gpoll add b 5 0
266put gpoll add b 6 0
267put gpoll add b 7 0
268end
269ret mul get glfn 0 8
270end
271fn vwaccept1 k ulong to long
272if eq and get gpoll add mul k 8 6 1 0
273ret 0
274end
275c = accept int get glf k 526336
276if lt c 0
277ret 0
278end
279pid = fork
280if eq pid 0
281vwclosel
282vwconn c
283exit 0
284end
285x = fclose int c
286if gt pid 0
287ret 1
288end
289ret 0
290end
291fn vwserve to int
292ab = new uchar 512 0
293n = vwarg 2 ab
294if or eq n 0 gt n 400
295ret 2
296end
297for i ulong n
298put grun i get ab i
299end
300put grunl 0 n
301n = vwarg 3 ab
302if not hdigits view part ab 0 n
303ret 2
304end
305port = hdecv view part ab 0 n
306if or eq port 0 gt port 65535
307ret 2
308end
309put gport 0 port
310if not vwtokld
311ret 3
312end
313put gahn 0 0
314vwahport tlhost
315argn = ulong argcnt
316if or lt argn 5 gt argn 6
317ret 2
318end
319for i ulong sub argn 4
320n = vwarg add i 4 ab
321r = vwlisten ab n not eq vwip ab n 2130706433
322if ne r 0
323ret r
324end
325end
326vwhpath tstd1
327r = makedir gpth 0 get gpl 0 448
328vwhpath tstd2
329r = makedir gpth 0 get gpl 0 448
330r = makedir grun 0 get grunl 0 448
331ret vwloop
332end
333fn vwloop to int
334poller = vwspawn
335backoff = long 0
336active = ulong 0
337np = vwpollset
338for it ulong 9223372036854775807
339for k ulong 128
340w = waitpid -1 gwst 0 4 1
341if le w 0
342break
343end
344if eq w poller
345poller = -1
346backoff = add long monons 2000000000
347else
348if gt active 0
349active = sub active 1
350end
351end
352end
353if and lt poller 0 gt long monons backoff
354poller = vwspawn
355end
356if ge active 48
357x = sleepns 50000000
358continue
359end
360for k ulong 4
361put gpoll add mul k 8 6 0
362end
363r = pollfds gpoll 0 np 500000000
364if le r 0
365continue
366end
367for k ulong get glfn 0
368active = add active ulong vwaccept1 k
369end
370end
371ret 0
372end
373fn vwpollonce to int
374ab = new uchar 512 0
375n = vwarg 2 ab
376if or eq n 0 gt n 400
377ret 2
378end
379for i ulong n
380put grun i get ab i
381end
382put grunl 0 n
383r = makedir grun 0 get grunl 0 448
384vwinitsec
385put ggen 0 vwnowms
386vwpanes
387vwslow
388vwkanban
389vwindex 0
390ret 0
391end
392fn main to int
393if not vwhome
394ret 6
395end
396ab = new uchar 512 0
397n = vwarg 1 ab
398if vwargis ab n tserve
399ret vwserve
400end
401if vwargis ab n tmktok
402ret vwmktok
403end
404if vwargis ab n tpoll1
405ret vwpollonce
406end
407ret 2
408end