wire.bend

bend-kit-wire@0.4.7.0 · 144 lines · 0 definitions

1# Byte-exact TCP, UDP, and TLS sockets. Source: https://github.com/paymog/bend-kit/tree/main/wire
2import Base
3
4# Socket IO on octets: one Char per byte (0..255). Base's TCP/UDP effects
5# decode UTF-8, which breaks byte counts and binary protocols.
6# ms arguments are deadlines in milliseconds (0: none unless stated otherwise).
7# A deadline miss is ETIMEDOUT.
8#   import ./wire.bend as Wire
9
10# TCP connect to a numeric IPv4 or IPv6 address, with a deadline.
11def connect(host: String, port: U32, ms: U32) ->
12  IO(Result<&1, &1, U32 & String, Socket>):
13  import "./effs/wire.c"
14  import "./effs/wire.js"
15
16# One monotonic accept budget, ms: 1..2147483647. Every outcome returns the
17# actual listener; timeout leaves it usable. Cooperative scheduling does not
18# preempt arbitrary CPU work or blocking effects.
19def accept.deadline(listener: Listener, ms: U32) ->
20  IO(Listener & Result<&1, &1, U32 & String, Socket>):
21  import "./effs/wire.c"
22  import "./effs/wire.js"
23
24# Octet-String TCP and UDP effects are the slow path: one Char cell per byte.
25def recv(sock: Socket, max: U32, ms: U32) ->
26  IO(Socket & Result<&1, &1, U32 & String, String>):
27  import "./effs/wire.c"
28  import "./effs/wire.js"
29
30def send(sock: Socket, data: String) ->
31  IO(Socket & Result<&1, &1, U32 & String, Unit>):
32  import "./effs/wire.c"
33  import "./effs/wire.js"
34
35# True when no byte and no end of stream waits on sock; it reads nothing.
36# False on an idle connection means the peer closed it or is out of step.
37def idle(sock: Socket) -> IO(Socket & Bool):
38  import "./effs/wire.c"
39  import "./effs/wire.js"
40
41def recv_from(sock: Socket, max: U32, ms: U32) ->
42  IO(Socket & Result<&1, &1, U32 & String, String & U32 & String>):
43  import "./effs/wire.c"
44  import "./effs/wire.js"
45
46def send_to(sock: Socket, host: String, port: U32, data: String) ->
47  IO(Socket & Result<&1, &1, U32 & String, Unit>):
48  import "./effs/wire.c"
49  import "./effs/wire.js"
50
51# TLS over a connected TCP socket (OpenSSL 3, loaded at run time; BEND_LIBSSL
52# overrides its path). The certificate chain and host name are always checked.
53def tls.connect(sock: Socket, host: String, ms: U32) ->
54  IO(Socket & Result<&1, &1, U32 & String, Unit>):
55  import "./effs/wire.c"
56  import "./effs/wire.js"
57
58# Use a PEM certificate chain and its private key for mutual TLS.
59# A load failure is EINVAL; a handshake or server verification failure is EPROTO.
60def tls.connect.cert(sock: Socket, host: String, ms: U32, cert_path: String, key_path: String) ->
61  IO(Socket & Result<&1, &1, U32 & String, Unit>):
62  import "./effs/wire.c"
63  import "./effs/wire.js"
64
65# protos is a comma-separated ALPN list (for example "h2,http/1.1").
66def tls.connect.alpn(sock: Socket, host: String, ms: U32, protos: String) ->
67  IO(Socket & Result<&1, &1, U32 & String, String>):
68  import "./effs/wire.c"
69  import "./effs/wire.js"
70
71# Trust the PEM certificates in ca_path for this handshake, instead of the
72# default verify paths. Verification stays on. A load failure is EINVAL.
73def tls.connect.ca(sock: Socket, host: String, ms: U32, ca_path: String) ->
74  IO(Socket & Result<&1, &1, U32 & String, Unit>):
75  import "./effs/wire.c"
76  import "./effs/wire.js"
77
78# tls.connect.ca with a comma-separated ALPN list. The result is the selected protocol.
79def tls.connect.alpn.ca(sock: Socket, host: String, ms: U32, protos: String, ca_path: String) ->
80  IO(Socket & Result<&1, &1, U32 & String, String>):
81  import "./effs/wire.c"
82  import "./effs/wire.js"
83
84# The String TLS effects are the slow path: one Char cell per octet.
85def tls.send(sock: Socket, data: String) ->
86  IO(Socket & Result<&1, &1, U32 & String, Unit>):
87  import "./effs/wire.c"
88  import "./effs/wire.js"
89
90# "" once the peer sends close_notify. A bare EOF is an error.
91def tls.recv(sock: Socket, max: U32, ms: U32) ->
92  IO(Socket & Result<&1, &1, U32 & String, String>):
93  import "./effs/wire.c"
94  import "./effs/wire.js"
95
96# Ends the TLS session and closes the socket.
97def tls.close(sock: Socket) -> IO(Unit):
98  import "./effs/wire.c"
99  import "./effs/wire.js"
100
101# The .words forms move (len, words): bytes packed four to a U32, laid out as
102# bytes/bytes.bend packs them, so no String cell is made per byte.
103def recv.words(sock: Socket, max: U32, ms: U32) ->
104  IO(Socket & Result<&1, &1, U32 & String, U32 & Array<U32>>):
105  import "./effs/wire.c"
106  import "./effs/wire.js"
107
108# len past the end of words fails with EINVAL.
109def send.words(sock: Socket, len: U32, words: Array<U32>) ->
110  IO(Socket & Result<&1, &1, U32 & String, Unit>):
111  import "./effs/wire.c"
112  import "./effs/wire.js"
113
114# Total write budget, including packing; ms must be 1..2147483647.
115# Returns socket, bytes accepted locally, and outcome on every path. Consumes
116# words on every path. Timeout/disconnect may leave a sent prefix: close the
117# returned socket, do not replay the whole buffer. Success is not peer receipt.
118def send.words.deadline(sock: Socket, len: U32, words: Array<U32>, ms: U32) ->
119  IO(Socket & (U32 & Result<&1, &1, U32 & String, Unit>)):
120  import "./effs/wire.c"
121  import "./effs/wire.js"
122
123# UDP keeps the sender address and port beside the packed datagram.
124def recv_from.words(sock: Socket, max: U32, ms: U32) ->
125  IO(Socket & Result<&1, &1, U32 & String, String & U32 & (U32 & Array<U32>)>):
126  import "./effs/wire.c"
127  import "./effs/wire.js"
128
129# len past the end of words fails with EINVAL.
130def send_to.words(sock: Socket, host: String, port: U32, len: U32, words: Array<U32>) ->
131  IO(Socket & Result<&1, &1, U32 & String, Unit>):
132  import "./effs/wire.c"
133  import "./effs/wire.js"
134
135# len 0 once the peer sends close_notify.
136def tls.recv.words(sock: Socket, max: U32, ms: U32) ->
137  IO(Socket & Result<&1, &1, U32 & String, U32 & Array<U32>>):
138  import "./effs/wire.c"
139  import "./effs/wire.js"
140
141def tls.send.words(sock: Socket, len: U32, words: Array<U32>) ->
142  IO(Socket & Result<&1, &1, U32 & String, Unit>):
143  import "./effs/wire.c"
144  import "./effs/wire.js"