Luau Bindings — net

The require("net") script API — the Ant64's one networking library, over DeMon's CM5 stack (wired Gigabit Ethernet or Wi-Fi via the ESP32-C5). It consolidates what were four separate P4 libraries — HTTP/WebSocket/mail (curl), connectivity & TCP (internet), datagrams (udp), and icmp — into a single surface. Every function is [antos]. Design & native core: antos_net.

local net = require("net")
print(net.get("https://ant64.com").status)

Blocking calls return a result table or nil, err; long transfers take an optional progress callback. All addresses resolve through the system DNS.


HTTP

Function Behaviour
net.get(url [, opts]) GET → { status, headers, body, time_ms }, or nil, err.
net.post(url, body [, opts]) POST; body a string, or a table (JSON when opts.json, form when opts.form).
net.head(url [, opts]) HEAD → headers only.
net.request(opts) General form — { method, url, headers, body, … } plus every option below.
net.download(url, path [, on_progress]) Stream a URL straight to a DBFS file with on_progress(done, total).

opts (all optional): headers (table), timeout / connect_timeout (ms), auth = {user, pass} or bearer = "<token>", json = value (encode + set content-type), form = {field=…, file="D:/…"} (multipart), cookies, proxy, follow_redirects (default true), verify_ssl (default true), user_agent, on_progress, on_header, out = "D:/path" (stream to file).

local r = net.get("https://api.example.com/status", { timeout = 5000, bearer = tok })
if r and r.status == 200 then
  local d = require("data").json_decode(r.body)
end

-- multipart upload
net.post("https://host/upload", nil, { form = { note = "hi", file = "D:/pic.png" } })

HTTPS depends on the system clock being set (certificate validity). On a cold boot that means after RTC/NTP sync — see os.rtc / RTC.


WebSockets

Function Behaviour
net.ws(url [, opts]) Open a WebSocket → a ws handle, or nil, err. wss:// for TLS.

Handle methods: ws:send(data [, binary]), ws:recv([timeout]) → message or nil on timeout, ws:close().

local ws = net.ws("wss://host/feed")
ws:send('{"subscribe":"prices"}')
print(ws:recv(2000))
ws:close()

TCP & UDP sockets

Function Behaviour
net.connect(host, port [, opts]) TCP connection → a socket handle. opts.tls = true for TLS.
net.listen(port [, opts]) Listen → a server handle; server:accept() yields sockets.
net.udp([opts]) A UDP handle — udp:send(data, host, port), udp:recv([timeout]) → data, host, port.

Socket handles deliberately echo the io file-handle shape: sock:read([n]), sock:write(data), sock:lines(), sock:settimeout(ms), sock:close().

UDP extras: udp:join(group) / udp:leave(group) (multicast), udp:broadcast(true).

local sock = net.connect("ant64.com", 23)
sock:write("hello\r\n"); print(sock:read("l")); sock:close()

Name resolution & diagnostics

Function Behaviour
net.dns(host) (alias net.resolve) Resolve to one address, or nil, err.
net.resolve_all(host) All addresses as an array.
net.ping(host [, opts]) ICMP echo → { sent, recv, loss, min, avg, max }. opts: { count, timeout }.
net.traceroute(host) Array of hops.
net.hostname([name]) Get, or set, the hostname.
net.status() Connectivity summary — interfaces, addresses, routes, link state.

Time (NTP)

Function Behaviour
net.ntp_sync([server]) Query NTP and set the system clock. Returns the new epoch, or nil, err.
net.ntp_query([server]) Query NTP without setting the clock — returns the epoch.

NTP is the network half of timekeeping; the hardware half (the battery-backed clock) is os.rtc. On a cold boot, net.ntp_sync() is what makes HTTPS work.


Mail (SMTP send)

Function Behaviour
net.mail(opts) Send a message — { server, from, to, subject, body, auth, attachments }. Returns true, or nil, err.

(Receiving mail is not part of net; this is send-only, the SMTP path from the old curl core.)


Related

Important: The Ant64 family of home computers are at early design/prototype stage, everything you see here is subject to change.