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 / apps / interplane-forge / infra / test / netprobe.om

186 lines · 3450 bytes Raw Blame History
1om2
2note netprobe (test-only, sandbox gate). Runs inside the sandbox in place of forged.
3note netprobe TARGET ... TARGET is A.B.C.D:PORT (TCP) or @NAME (abstract unix stream socket).
4note For each target it tries one connect (nonblocking, at most 2 s) and prints
5note "TARGET connected" or "TARGET refused errno E". It sends nothing. Then it prints the
6note interface names from /proc/net/dev. Exit 0: no target connected. 1: at least one did.
7note 2: usage error.
8note Linux ARM64: SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC = 526337; EINPROGRESS 115, EISCONN 106;
9note POLLOUT 4. A second connect after the socket turns writable gives 0 or EISCONN when the
10note connection was made, otherwise the error of the attempt.
11global arg uchar 256
12global sa uchar 128
13global num uchar 32
14global dev uchar 8192
15text tok " connected\n"
16text tno " refused errno "
17text tbad " unparsable\n"
18text tnl "\n"
19text tdev "/proc/net/dev"
20text tif "interface "
21fn putdec v ulong to void
22x = v
23i = ulong 32
24while gt i 0 bound 32
25i = sub i 1
26put num i add uchar rem x 10 48
27x = div x 10
28if eq x 0
29break
30end
31end
32k = write 1 num i sub 32 i
33end
34fn tcpaddr n ulong to ulong
35note arg[0..n) as A.B.C.D:PORT into sa (sockaddr_in, 16 bytes); returns 16, or 0 if malformed.
36for i ulong 16
37put sa i 0
38end
39put sa 0 2
40pt = ulong 0
41v = ulong 0
42d = ulong 0
43for i ulong n bound 256
44c = get arg i
45if and ge c 48 le c 57
46v = add mul v 10 ulong sub c 48
47d = add d 1
48if or gt v 65535 gt d 5
49ret 0
50end
51else
52if or eq d 0 gt v 255
53ret 0
54end
55if eq c 46
56if ge pt 3
57ret 0
58end
59put sa add 4 pt uchar v
60else
61if or ne c 58 ne pt 3
62ret 0
63end
64put sa 7 uchar v
65end
66pt = add pt 1
67v = ulong 0
68d = ulong 0
69end
70end
71if or ne pt 4 eq d 0
72ret 0
73end
74put sa 2 uchar shr v 8
75put sa 3 uchar and v 255
76ret 16
77end
78fn unixaddr n ulong to ulong
79note arg[0..n) as @NAME into sa (sockaddr_un, abstract: family 1, a zero byte, NAME).
80if or lt n 2 gt n 100
81ret 0
82end
83put sa 0 1
84put sa 1 0
85put sa 2 0
86for i ulong sub n 1 bound 100
87put sa add i 3 get arg add i 1
88end
89ret add n 2
90end
91fn try fam int al ulong to long
92fd = socknew fam 526337 0
93if lt fd 0
94ret fd
95end
96r = connect int fd sa 0 al
97if eq r -115
98w = waitio int fd 4 2000000000
99r = connect int fd sa 0 al
100if eq r -106
101r = long 0
102end
103if eq w 0
104r = long -110
105end
106end
107c = fclose int fd
108ret r
109end
110fn prname s ulong e ulong to void
111note Print "interface NAME" for dev[s..e), leading spaces skipped.
112if ge s e
113ret
114end
115p = s
116while lt p e bound 8192
117if ne get dev p 32
118break
119end
120p = add p 1
121end
122k = write 1 tif 0 len tif
123k = write 1 dev p sub e p
124k = write 1 tnl 0 len tnl
125end
126fn devs to void
127note Print the interface names: the text before ':' on each line after the two header lines.
128fd = fopen tdev 0 len tdev 524288 0
129if lt fd 0
130ret
131end
132n = read int fd dev 0 len dev
133c = fclose int fd
134if le n 0
135ret
136end
137line = ulong 0
138s = ulong 0
139for i ulong ulong n bound 8192
140b = get dev i
141if eq b 10
142line = add line 1
143s = add i 1
144end
145if and eq b 58 ge line 2
146prname s i
147s = ulong n
148end
149end
150end
151fn main to int
152if lt argcnt 2
153ret 2
154end
155hit = int 0
156for t ulong ulong sub argcnt 1 bound 64
157n = argread add t 1 arg 0 len arg
158if le n 0
159ret 2
160end
161fam = int 2
162al = ulong 0
163if eq get arg 0 64
164fam = int 1
165al = unixaddr ulong n
166else
167al = tcpaddr ulong n
168end
169k = write 1 arg 0 ulong n
170if eq al 0
171k = write 1 tbad 0 len tbad
172ret 2
173end
174r = try fam al
175if eq r 0
176k = write 1 tok 0 len tok
177hit = int 1
178else
179k = write 1 tno 0 len tno
180putdec ulong neg r
181k = write 1 tnl 0 len tnl
182end
183end
184devs
185ret hit
186end