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