interplane.Get Omega
Read in your language

mailhand

mailhand: agents look after a person's email in seeded Omega. Maildir + MIME parsing, JSONL CLI and MCP, rule triage, policy-gated sending, hash-chained audit. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

mailhand / src / tx.om

357 lines · 6453 bytes Raw Blame History
1note ======== src/tx.om: one network connection at a time (Transport, prefix tx_) ========
2note Plain (a unix socket or 127.0.0.1 port: the loopback sink and tests only, or the TCP leg before
3note STARTTLS) or TLS through the omtls client API (ot_tl_*, built from DEPS).
4note Received bytes go through the window tx_rb (a streaming window, not an input limit). Every
5note operation has a monotonic deadline (tx_timeout; config io_timeout, default 30 s); EINTR and
6note EAGAIN are retried; partial sends and short reads are looped.
7global tx_v ulong 8
8note tx_v: 0 mode (0 none, 1 plain, 2 TLS), 1 owned fd + 1, 2 window read position, 3 window bytes,
9note 4 deadline ns (0 = 30 s), 5 why: 1 timeout, 2 closed, 3 socket error, 4 TLS error, 5 bytes
10note arrived before the TLS handshake (refused), 6 not connected, 7 cannot resolve or connect
11global tx_rb uchar 65536
12text tx_kto "io_timeout"
13text tx_wto "timeout"
14text tx_wclosed "connection closed by the server"
15text tx_wsock "socket error"
16text tx_wtls "TLS: "
17text tx_winj "bytes arrived before the TLS handshake (refused)"
18text tx_wnone "not connected"
19text tx_wconn "cannot resolve or connect"
20
21fn tx_ns to long
22v = get tx_v 4
23if eq v 0
24ret 30000000000
25end
26ret long v
27end
28
29note tx_timeout: config "io_timeout SECONDS" (1..86400) sets the per-operation deadline
30fn tx_timeout to void
31put tx_v 4 0
32if not cf_get tx_kto
33ret
34end
35o = get gv VCFO
36n = get gv VCFN
37if or eq n 0 gt n 5
38ret
39end
40v = ulong 0
41for i ulong n bound 5
42c = s_get S_CFG add o i
43if or lt c 48 gt c 57
44ret
45end
46v = add mul v 10 ulong sub c 48
47end
48if or eq v 0 gt v 86400
49ret
50end
51put tx_v 4 mul v 1000000000
52end
53
54fn tx_reset to void
55put tx_v 0 0
56put tx_v 1 0
57put tx_v 2 0
58put tx_v 3 0
59put tx_v 5 0
60end
61
62fn tx_fd to int
63ret int sub get tx_v 1 1
64end
65
66fn tx_why to ulong
67ret get tx_v 5
68end
69
70note tx_plain: adopt a connected nonblocking socket (the loopback sink: sm_endpoint)
71fn tx_plain fd long to void
72tx_reset
73put tx_v 0 1
74put tx_v 1 add ulong fd 1
75end
76
77note tx_tls: implicit TLS to host:port (via: test-only IPv4 connect override, empty for none);
78note 0 or a nonzero code (tx_why 4 or 7)
79fn tx_tls host view uchar * port ulong via view uchar * to int
80tx_reset
81ot_tl_deadline tx_ns
82if not ot_tl_via via
83put tx_v 5 7
84ret 1
85end
86alpn = new uchar 1 0
87r = ot_tl_connect host port part alpn 0 0
88if ne r 0
89put tx_v 5 4
90ret r
91end
92put tx_v 0 2
93ret 0
94end
95
96note tx_tcp: plain TCP to host:port for a later STARTTLS; 0 or 1 (tx_why 7)
97fn tx_tcp host view uchar * port ulong via view uchar * to int
98tx_reset
99ot_tl_deadline tx_ns
100if not ot_tl_via via
101put tx_v 5 7
102ret 1
103end
104fd = ot_tl_tcp host port
105if lt fd 0
106put tx_v 5 7
107ret 1
108end
109tx_plain fd
110ret 0
111end
112
113note tx_starttls: upgrade the plain connection; from here the TLS layer owns the socket (ot_tl_start closes
114note it on failure, ot_tl_close after success). A byte already received after the server's
115note go-ahead is refused: it would be read as if it had come over TLS (response injection).
116fn tx_starttls host view uchar * to int
117if ne get tx_v 0 1
118put tx_v 5 6
119ret 1
120end
121if ne get tx_v 2 get tx_v 3
122put tx_v 5 5
123ret 1
124end
125fd = tx_fd
126r = waitio fd 1 0
127if gt r 0
128put tx_v 5 5
129ret 1
130end
131ot_tl_deadline tx_ns
132alpn = new uchar 1 0
133k = ot_tl_start fd host part alpn 0 0
134put tx_v 1 0
135if ne k 0
136put tx_v 5 4
137ret k
138end
139put tx_v 0 2
140ret 0
141end
142
143note tx_wait: poll fd for ev until the deadline; true when ready (or interrupted: the caller retries)
144fn tx_wait fd int ev int until long to bool
145r = sub until monons
146if le r 0
147put tx_v 5 1
148ret false
149end
150z = waitio fd ev r
151if eq z -4
152ret true
153end
154if eq z 0
155put tx_v 5 1
156end
157if lt z 0
158put tx_v 5 3
159end
160ret gt z 0
161end
162
163fn tx_write b view uchar * to bool
164m = get tx_v 0
165if eq m 2
166if ot_tl_write b
167ret true
168end
169put tx_v 5 4
170ret false
171end
172if ne m 1
173put tx_v 5 6
174ret false
175end
176fd = tx_fd
177p = ulong 0
178t = add monons tx_ns
179while lt p len b bound 9223372036854775807
180if not tx_wait fd 4 t
181ret false
182end
183r = send fd b p sub len b p 0
184if or eq r -4 eq r -11
185continue
186end
187if le r 0
188put tx_v 5 3
189ret false
190end
191p = add p ulong r
192end
193ret true
194end
195
196note tx_wstore: write the range [off, off+n) of store s
197fn tx_wstore s ulong off ulong n ulong to bool
198b = new uchar 65536 0
199p = ulong 0
200while lt p n bound 9223372036854775807
201m = sub n p
202if gt m 65536
203m = 65536
204end
205k = s_read s add off p part b 0 m
206if eq k 0
207ret false
208end
209if not tx_write part b 0 k
210ret false
211end
212p = add p k
213end
214ret true
215end
216
217note tx_fill: refill the empty window; bytes read (> 0), 0 at end of stream, < 0 on error
218fn tx_fill to long
219put tx_v 2 0
220put tx_v 3 0
221b = new uchar 65536 0
222r = long 0
223m = get tx_v 0
224if eq m 2
225r = ot_tl_read b
226if eq r 0
227put tx_v 5 2
228end
229if lt r 0
230put tx_v 5 4
231end
232else
233if ne m 1
234put tx_v 5 6
235ret -1
236end
237r = tx_recv b
238end
239if le r 0
240ret r
241end
242for i ulong ulong r bound 65536
243put tx_rb i get b i
244put b i 0
245end
246put tx_v 3 ulong r
247ret r
248end
249
250fn tx_recv b edit uchar * to long
251fd = tx_fd
252t = add monons tx_ns
253while true bound 9223372036854775807
254if not tx_wait fd 1 t
255ret -110
256end
257r = recv fd b 0 len b 0
258if or eq r -4 eq r -11
259continue
260end
261if eq r 0
262put tx_v 5 2
263end
264if lt r 0
265put tx_v 5 3
266end
267ret r
268end
269ret -1
270end
271
272note tx_getc: the next received byte, or -1 (end of stream, timeout or error: see tx_why)
273fn tx_getc to long
274p = get tx_v 2
275if ge p get tx_v 3
276r = tx_fill
277if le r 0
278ret -1
279end
280p = 0
281end
282put tx_v 2 add p 1
283ret long get tx_rb p
284end
285
286note tx_take: copy received bytes into dst: at least one, at most len dst; 0 = end or error
287fn tx_take dst edit uchar * to ulong
288p = get tx_v 2
289if ge p get tx_v 3
290r = tx_fill
291if le r 0
292ret 0
293end
294p = 0
295end
296n = sub get tx_v 3 p
297if gt n len dst
298n = len dst
299end
300for i ulong n bound 9223372036854775807
301put dst i get tx_rb add p i
302end
303put tx_v 2 add p n
304ret n
305end
306
307note tx_close: end TLS, close the owned socket, zero the receive window
308fn tx_close to void
309if eq get tx_v 0 2
310ot_tl_close
311end
312if gt get tx_v 1 0
313z = fclose tx_fd
314end
315for i ulong 65536
316put tx_rb i 0
317end
318w = get tx_v 5
319tx_reset
320put tx_v 5 w
321end
322
323note tx_diag: one stderr line naming why the transport failed (never a payload byte)
324fn tx_diag what view uchar * to void
325w = get tx_v 5
326b = new uchar 128 0
327if eq w 4
328n = ot_tl_why b
329r = write 2 kpref 0 len kpref
330r = write 2 what 0 len what
331r = write 2 tx_wtls 0 len tx_wtls
332r = write 2 b 0 n
333r = write 2 knl 0 1
334ret
335end
336if eq w 1
337diag2 what tx_wto
338ret
339end
340if eq w 2
341diag2 what tx_wclosed
342ret
343end
344if eq w 5
345diag2 what tx_winj
346ret
347end
348if eq w 6
349diag2 what tx_wnone
350ret
351end
352if eq w 7
353diag2 what tx_wconn
354ret
355end
356diag2 what tx_wsock
357end