1 # Byte-exact TCP, UDP, and TLS sockets. Source: https://github.com/paymog/bend-kit/tree/main/wire 2 import 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); a miss is ETIMEDOUT. 7 # import ./wire.bend as Wire 8 9 # TCP connect to a numeric IPv4 or IPv6 address, with a deadline. 10 def connect(host: String, port: U32, ms: U32) -> 11 IO(Result<&1, &1, U32 & String, Socket>): 12 import "./effs/wire.c" 13 import "./effs/wire.js" 14 15 def recv(sock: Socket, max: U32, ms: U32) -> 16 IO(Socket & Result<&1, &1, U32 & String, String>): 17 import "./effs/wire.c" 18 import "./effs/wire.js" 19 20 def send(sock: Socket, data: String) -> 21 IO(Socket & Result<&1, &1, U32 & String, Unit>): 22 import "./effs/wire.c" 23 import "./effs/wire.js" 24 25 def recv_from(sock: Socket, max: U32, ms: U32) -> 26 IO(Socket & Result<&1, &1, U32 & String, String & U32 & String>): 27 import "./effs/wire.c" 28 import "./effs/wire.js" 29 30 def send_to(sock: Socket, host: String, port: U32, data: String) -> 31 IO(Socket & Result<&1, &1, U32 & String, Unit>): 32 import "./effs/wire.c" 33 import "./effs/wire.js" 34 35 # TLS over a connected TCP socket (OpenSSL 3, loaded at run time; BEND_LIBSSL 36 # overrides its path). The certificate chain and host name are always checked. 37 def tls.connect(sock: Socket, host: String, ms: U32) -> 38 IO(Socket & Result<&1, &1, U32 & String, Unit>): 39 import "./effs/wire.c" 40 import "./effs/wire.js" 41 42 # protos is a comma-separated ALPN list (for example "h2,http/1.1"). 43 def tls.connect.alpn(sock: Socket, host: String, ms: U32, protos: String) -> 44 IO(Socket & Result<&1, &1, U32 & String, String>): 45 import "./effs/wire.c" 46 import "./effs/wire.js" 47 48 def tls.send(sock: Socket, data: String) -> 49 IO(Socket & Result<&1, &1, U32 & String, Unit>): 50 import "./effs/wire.c" 51 import "./effs/wire.js" 52 53 # "" once the peer sends close_notify. A bare EOF is an error. 54 def tls.recv(sock: Socket, max: U32, ms: U32) -> 55 IO(Socket & Result<&1, &1, U32 & String, String>): 56 import "./effs/wire.c" 57 import "./effs/wire.js" 58 59 # Ends the TLS session and closes the socket. 60 def tls.close(sock: Socket) -> IO(Unit): 61 import "./effs/wire.c" 62 import "./effs/wire.js" 63 64 # The .words forms move (len, words): bytes packed four to a U32, laid out as 65 # bytes/bytes.bend packs them, so no String cell is made per byte. 66 def recv.words(sock: Socket, max: U32, ms: U32) -> 67 IO(Socket & Result<&1, &1, U32 & String, U32 & Array>): 68 import "./effs/wire.c" 69 import "./effs/wire.js" 70 71 # len past the end of words fails with EINVAL. 72 def send.words(sock: Socket, len: U32, words: Array) -> 73 IO(Socket & Result<&1, &1, U32 & String, Unit>): 74 import "./effs/wire.c" 75 import "./effs/wire.js" 76 77 # len 0 once the peer sends close_notify. 78 def tls.recv.words(sock: Socket, max: U32, ms: U32) -> 79 IO(Socket & Result<&1, &1, U32 & String, U32 & Array>): 80 import "./effs/wire.c" 81 import "./effs/wire.js" 82 83 def tls.send.words(sock: Socket, len: U32, words: Array) -> 84 IO(Socket & Result<&1, &1, U32 & String, Unit>): 85 import "./effs/wire.c" 86 import "./effs/wire.js"