Skip to content

Net: the WebSocket transport over libcurl CONNECT_ONLY - #1283

Merged
GLinnik21 merged 7 commits into
plxnative:mainfrom
PilaScat:feat/ws-transport
Nov 9, 2026
Merged

GLinnik21 merged 7 commits into
plxnative:mainfrom
PilaScat:feat/ws-transport

Conversation

@PilaScat

@PilaScat PilaScat commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Part 2 of 4 for Open Watch Party (#1090): the transport that runs the codec from #1273 over the socket libcurl hands back with CURLOPT_CONNECT_ONLY. Nothing in the app calls it yet. The OWP client crate is parts 3 and 4.

What

  • net::connect_only: libcurl does the name lookup, the TCP connect and TLS with peer and host verification, then returns the socket from CURLINFO_ACTIVESOCKET. send and recv wrap curl_easy_send/curl_easy_recv and return Io::Again for CURLE_AGAIN.
    • curl_easy_send, curl_easy_recv and curl_easy_getinfo are their own dynlib! table, with the same candidates in the same order as curl, loaded once and checked before any call. A libcurl without them loses only the WebSocket.
    • Nothing is dialled when !threaded_tls_ready(), for ws:// as well as wss://: on that fallback every easy handle runs under the serial lock, and this one would not.
    • refuse_name applies, through origin::url_host, so /tmp/plxnative-nowan refuses a WebSocket name as it does at the other three resolver doors.
    • The handle is pinned to HTTP/1.1 (CURLOPT_HTTP_VERSION). Otherwise a libcurl with nghttp2 offers h2 by ALPN and, when the server picks it, writes the HTTP/2 preface itself before the upgrade.
    • Trust is Tls::Ca or Tls::CaBundle; Tls::Pinned is refused. CURLOPT_PROTOCOLS (HTTPS or HTTP only), both verify options, CAINFO, HTTP_VERSION and CONNECT_ONLY are checked through require_option and require_trust_store, the funnel's own checks, so a refusal ends the open and logs the same line as in request_tls_sink.
    • The cancel flag is read during the connect through libcurl's progress callback (XFERINFO, or PROGRESS before 7.32), as stream_tls does.
    • Net: an opt-in bundled HTTP/3 curl stack over OpenSSL 3.5, behind CURLSTACK=1 #1270 is still open, so the table opens the system candidate list. If Net: an opt-in bundled HTTP/3 curl stack over OpenSSL 3.5, behind CURLSTACK=1 #1270 lands, the table moves to load_from(net::bundled_curl()), as agreed in Net: an RFC 6455 WebSocket codec, sans-IO, for Open Watch Party #1273.
  • ws::run drives one connection on the calling worker (it takes &OffFrame) until it ends:
    • In: a Dial (ws:// or wss:// URL, trust, message limit, idle bound), a cancel flag and the Inbox, by value, so a channel serves one connection. Out: Opened, Text and Binary events, then an End: Closed { code, reason }, Failed(Fault), Refused(..), Lost, TimedOut, Idle, NoEntropy or Cancelled.
    • An Outbox (clonable, any thread) queues text, binary and close, and refuses a close code or reason the protocol does not allow. It returns false once nothing more will be sent: after its own close, after the connection ends, or once the Inbox is gone. A message queued while the server's close is already on its way can still return true and be dropped. A Unix socket pair wakes the poll() at once; the Inbox holds its write end, so it never reads as closed. Messages queued before the upgrade go out once it is accepted.
    • Every pass reads until CURLE_AGAIN before it polls. A pass stops after 1 MiB and goes round again without polling, so the cancel flag, the deadlines and the outbox are still read under a peer that never pauses.
    • A write that returns CURLE_AGAIN is retried with the same buffer, and frames queued meanwhile wait in a second one (OpenSSL's retry rule).
    • The nonce and the masking keys come from /dev/urandom, read 256 bytes at a time. Every write goes through one check that ends the run with NoEntropy before anything is pushed, once a refill has failed.
    • Bounds: 10 s to connect, 10 s for the upgrade answer, 5 s for the answer to our close, then the caller's idle bound once open. Cancel is seen within 250 ms in the loop and in the close linger, and within about a second during the connect, which is libcurl's idle callback rate. It sends 1001 and keeps flushing it for up to 300 ms. After a close handshake or a protocol failure the client waits up to 2 s for the server to close TCP (§7.1.1). After a refused upgrade it closes at once.
    • Incoming messages are bounded by the limit in Dial, through the codec.

Autobahn

Autobahn|Testsuite 25.10.1 (autobahn 0.10.9), with the fuzzing server from crossbario/autobahn-testsuite in Docker. The client is the ignored test ws::transport::tests::autobahn_client_cases, which echoes every text and binary message, with a 64 MiB limit. Cases 12.* and 13.* (permessage-deflate) are excluded, since no extension is negotiated.

libcurl cases OK NON-STRICT INFORMATIONAL FAILED close
8.5.0 (WSL) 301 289 9 3 0 298 OK, 3 INFORMATIONAL
7.53.1 (built from source, LD_LIBRARY_PATH) 301 289 9 3 0 298 OK, 3 INFORMATIONAL
  • 3.2, 3.3, 4.1.3, 4.1.4, 4.2.3, 4.2.4 and 5.15 (NON-STRICT): a valid message and a bad frame arrive in one read. The codec fails the connection with 1002 in the same receive, before the test's echo of the valid message is queued. Strict expects the echo first.
  • 6.4.3 and 6.4.4 (NON-STRICT): invalid UTF-8 inside one frame sent in chops. The codec checks a frame's text once the frame is whole (fragments are checked one by one), so it fails at the end of the frame rather than at the bad byte.
  • 7.1.6 (INFORMATIONAL): the close was processed before the 256 KiB echo went out, for the same reason as the first group.
  • 7.13.1 and 7.13.2 (INFORMATIONAL): close codes 5000 and 65535, which the spec leaves undefined. The client fails with 1002.

To repeat it:

mkdir -p autobahn/config autobahn/reports
echo '{"url":"ws://0.0.0.0:9001","outdir":"/reports/clients","cases":["*"],"exclude-cases":["12.*","13.*"]}' > autobahn/config/fuzzingserver.json
docker run -d --rm --name autobahn -v $PWD/autobahn/config:/config -v $PWD/autobahn/reports:/reports -p 9001:9001 crossbario/autobahn-testsuite wstest -m fuzzingserver -s /config/fuzzingserver.json
make test-crate C=plx_net T=ws::transport
AUTOBAHN=ws://127.0.0.1:9001 rust-modules/target-fast/debug/deps/plx_net-<hash> autobahn_client_cases --ignored --nocapture

The report is autobahn/reports/clients/index.html.

Against the OWP relay

A scratch test, not in this diff, drove ws::run through create, joined, ping/pong, a hold with no data messages, a second pong, end, closed and a 1000 close:

  • wss://eu.relay.openwatch.party/v1/ws with a 75 s hold (the relay pings every 30 s and drops a connection after 60 s of silence): passed with libcurl 8.5.0 and 7.53.1;
  • owp-relay:1 locally with --timer-scale 0.1 and a 15 s hold: passed with both.

Checks

On the committed tree, WSL2 x86_64:

  • make check on 0669636: 43 suites, 25681 passed, 0 failed, 200 ignored.
  • make check-nodef: clean.
  • cargo check --lib --target armv7-unknown-linux-gnueabi -p plx_base -p plx_net: clean.
  • make test-crate C=plx_net T=ws::: 55 passed (33 codec, 22 transport), plus the ignored Autobahn driver. The same binary with libcurl 7.53.1: 55 passed.
  • Each regression test failed with its rule removed, and passes with it:
    • reading until CURLE_AGAIN: without it the 40 KB TLS burst takes over 2 s, because the tests read in 512-byte chunks and OpenSSL keeps the rest of each record;
    • no dial under serial TLS: without it both schemes dial;
    • no linger after a refused upgrade: without it the client waits 2 s;
    • 1 MiB per pass: without it, under a peer that never pauses, the cancel is seen only when the peer stops, after 10 s;
    • the Outbox close check, and the wake socket's write end held by the Inbox;
    • from the review: a server offering h2 still gets the HTTP/1.1 upgrade; a cancel during a TLS handshake that never completes (10 s without the callback); a cancel during the close linger (2 s without); an Outbox after its close.
  • fw-compat-reviewer (a source review, since no ARM ELF can be built here):
    • The three symbols resolve on all 14 releases in tools/fwcompat.py: libcurl.so.5 on 1.2.0–4.10.0, libcurl.so.4 on 5.3.1–11.2.0.
    • The variadic curl_easy_getinfo has ... before out. The candidates and their order match curl. No #[link], no new DT_NEEDED.
    • CURLINFO_ACTIVESOCKET needs 7.45.0. The database places 4.4.2–4.10.0 between 7.44 (it exports curl_pushheader_bynum) and 7.56 (no curl_mime_init), and you named 7.53.1 for those sets. On a 7.44 the open would end with NoSocket.
  • pr-reviewer: one round, 2 major and 5 minor findings. Six are fixed in this branch: the wake socket at EOF, the ws:// dial under serial TLS, the unbounded linger, the silently refused close, nowan, and the read bound per pass. The seventh is under "Not covered".
  • doc-claim-auditor: three findings, fixed (docs/agent-reference.md twice, ci/module-layers.ini).
  • Your review of 81b74f1: the eight points are fixed in 9a68922, 9c9bcd0, db659e8, 170e7f7 and 0669636, each passing T=ws:: on its own.

No Cargo.toml, Makefile, workflow or module change: the ci/module-layers.ini edit is the net layer's comment. So there is no build-bench table.

Not covered

  • connect_only::open builds its handle outside request_tls_sink. It has no keypin or roots rung, so on a set with a stale trust store or a wrong clock a wss open fails with rc 60 where ordinary requests recover, and it has no ResolvePin. You will open an issue for it.
  • The NoEntropy path and the bounded 1001 flush have no test: nothing here can make /dev/urandom fail, and a partial frame can't be held in flight on loopback.
  • The 2 s linger bound under a server that keeps sending after its close has no test: on loopback the client always catches up and sees CURLE_AGAIN, so the test passed with the bound removed too.
  • No television run yet. You planned a mock room on your webOS 4.5 set before merging.
  • net/connect_only.rs is not in fw-compat-reviewer's scope table. I can add it here.

Refs #1090.

🤖 Generated with Claude Code

PilaScat and others added 2 commits November 8, 2026 20:56
net::connect_only opens a connection with CURLOPT_CONNECT_ONLY, so libcurl
does the name lookup, the TCP connect and the TLS handshake with peer and
host verification on, then hands over the socket from
CURLINFO_ACTIVESOCKET. Bytes then move through curl_easy_send and
curl_easy_recv, which report CURLE_AGAIN instead of blocking.

The three symbols sit in their own dynlib! table with the same candidate
list as the main one, loaded once and checked before any call, so a
libcurl without them turns off only this socket. Nothing is dialled when
only serialised TLS is safe, plain ws:// included: on that fallback every
easy handle runs under the one serial lock, and this socket's would not.
The nowan trigger refuses a name here as at the other three resolver
doors. Trust is the device store or a shipped bundle; the pinned mode is
refused. Every security option is checked and a refusal ends the open.

Also updates docs/agent-reference.md, which counted two libcurl modules
and a fixed number of dynlib! callers, and the refuse_name doc in net.rs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
ws::run drives the merged codec over net::connect_only on the calling
worker: ws:// or wss:// address, a message size limit, an idle bound and a
cancel flag in, Opened/Text/Binary events out, and an End that says how
the connection finished. An Outbox (any thread) queues text, binary and
close, and refuses a close the protocol does not allow; a socket pair
wakes the poll() at once and cannot read as closed while the Inbox lives.

Every pass reads until CURLE_AGAIN before it polls, because TLS can hold
decrypted bytes the socket no longer shows. A pass stops after 1 MiB and
goes round again without polling, so a peer that never pauses cannot hold
off the cancel flag, the deadlines or the outbox. Writes keep the same
buffer until it is taken whole, as an OpenSSL retry requires. After a
close or a protocol failure the client waits up to 2 s for the server to
close the TCP connection; after a refused upgrade it does not wait. Masks
and the key come from /dev/urandom.

Tests run against loopback servers, plain and TLS: echo, a message queued
before the upgrade, ping, the server's close, a 40 KB TLS burst read in
512-byte test chunks, an untrusted certificate, serial TLS, cancel under a
peer that never pauses, idle, a dropped connection, a refused upgrade,
invalid closes, the wake socket and bad addresses. An ignored test drives
the Autobahn client suite.

The connection module exposes close_code_allowed to its sibling. Also
registers the three new bounds in ci/list-caps.ini, and updates the plx_net
crate doc, the net layer comment in ci/module-layers.ini and the Watch
Together entry in docs/parity-gaps.md, which said the transport did not
exist yet.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
Copilot AI lite review requested due to automatic review settings November 8, 2026 20:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@GLinnik21 GLinnik21 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the transport is clear to follow, and the Autobahn run is a solid result for the codec and the echo path. The comments below are from a review of 81b74f19, grouped by priority:

Must fix before merge

  1. HTTP/1.1 is not pinned, so a wss server that picks h2 by ALPN never opens. Reproduced on this branch; the fix is one setopt (inline on connect_only.rs:84).
  2. A test race with SerialTlsGuard / nowan: most dialling tests do not take testlock::serial().
  3. Cancel is not seen in finish() (up to 2 s) or during the connect (up to 10 s). Yes to the progress callback you offered.
  4. Frames masked with [0; 4] can be flushed on the finish path and on the cancel path.

Should fix
5. The 1001 on cancel gets one non-blocking flush and can be lost behind a partial frame.
6. Outbox returns true for messages that will never be sent: after a close, after run ends, and on a reused Inbox.
7. host_of duplicates origin::url_host.
8. require duplicates require_setopt!, with different log wording.

Not blocking: open bypasses request_tls_sink, so it has none of the keypin/roots fallback rungs and no ResolvePin. Please list it under "Not covered"; it will get its own issue.

None of these would show up in Autobahn: it runs over ws://, never cancels, and never sends after a close.


Generated by Claude Code

Comment thread rust-modules/net/src/ws/transport.rs Resolved
Comment thread rust-modules/net/src/ws/transport.rs Outdated
}
}
if let Some(end) = ended {
return if matches!(end, End::Failed(Fault::Handshake(_))) { end } else { self.finish(end) };

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must fix: frames masked with the [0; 4] fallback can reach the wire. The short check runs before each flush in the main loop, but two paths skip it:

  1. ended → finish. If a refill of /dev/urandom fails inside the read loop while the codec answers a Ping or echoes a Close, short is set. ended is Some, though, so this line goes to finish, which pushes take_outgoing() and flushes the all-zero-masked frames. The run also reports Closed/Failed instead of NoEntropy.
  2. The cancel branch (lines 166–169) masks a new 1001 close and flushes it without checking short.

RFC 6455 §5.3 requires an unpredictable masking key. Please check short before every push to wire. The simplest way is in one place: inside a helper that does push(take_outgoing()) and flush.


Generated by Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0669636: every write goes through send_out, which returns NoEntropy before it pushes anything once a refill has failed. finish and the cancel path call it too. There's no test, since nothing here can make /dev/urandom fail.

Comment thread rust-modules/net/src/ws/transport.rs Resolved
Comment thread rust-modules/net/src/ws/transport.rs Resolved
PilaScat and others added 5 commits November 9, 2026 16:17
SerialTlsGuard and the nowan trigger are process-global, and
connect_only::open reads both. A test that dialled while another forced
serial TLS could be refused and fail at random. Every test that dials now
takes testlock::serial(), as the net_stream_tests dials do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
…eading

require_setopt! and the CAINFO refusal in request_tls_sink now call
require_option and require_trust_store, and connect_only::open calls the
same two, so a refused option logs the same line on both paths. The nowan
gate reads the host through origin::url_host instead of a second parser.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
Since 7.62, a libcurl built with nghttp2 offers h2 by ALPN, and in
CONNECT_ONLY mode it writes the HTTP/2 preface itself when the server
picks h2, so the upgrade request went onto an HTTP/2 connection and the
run ended Lost. CURLOPT_HTTP_VERSION is now HTTP/1.1, and checked. The new
test serves wss offering h2 and http/1.1 and must reach Opened; it failed
before the pin.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
… linger

connect_only::open takes the cancel flag and installs libcurl's progress
callback for the connect, XFERINFO or PROGRESS before 7.32, as stream_tls
does. A cancel during the name lookup, the TCP connect or the TLS
handshake ends the open with OpenError::Cancelled within about a second,
libcurl's idle callback rate. finish() reads the flag on every pass and
waits at most POLL_CAP.

Two tests: a TLS handshake that never completes, and a server that holds
the TCP connection after its close. Both failed before, after 10 s and
2 s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
…, an honest Outbox

This commit carries three fixes, all in transport.rs:
- Every write goes through send_out, which stops with NoEntropy before it
  pushes anything when /dev/urandom failed, the finish and cancel paths
  included.
- A cancel keeps flushing the 1001 for up to 300 ms instead of making
  one non-blocking attempt.
- Outbox and Inbox share a live flag. Outbox::close clears it, and so do
  the end of the connection and dropping the Inbox, so a message that will
  never be sent returns false. run takes the Inbox by value, so a channel
  serves one connection.

Tests: an Outbox after its close, and the echo test checks the Outbox
after run. The entropy and 1001 paths have no test: there is no seam to
fail /dev/urandom, and no way to hold a partial frame on loopback.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Filippo Scatigna <[email protected]>
Copilot AI lite review requested due to automatic review settings November 9, 2026 15:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@PilaScat

PilaScat commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All eight points are fixed in five commits on top of 81b74f1, one per group of fixes, and each passes T=ws:: on its own: 9a68922, 9c9bcd0, db659e8, 170e7f7 and 0669636. On 0669636:

  • make check: 43 suites, 25681 passed, 0 failed, 200 ignored;
  • make check-nodef and the armv7 cargo check: clean;
  • make test-crate C=plx_net T=ws::: 55 passed (33 codec, 22 transport), and the same with libcurl 7.53.1;
  • Autobahn is unchanged: 289 OK, 9 NON-STRICT, 3 INFORMATIONAL, 0 FAILED, with 8.5.0 and with 7.53.1;
  • the public relay over wss with a 65 s hold passes with both libcurls;
  • the four new regression tests each fail with their fix removed.

The description now covers the HTTP/1.1 pin, the cancel bounds and the Outbox contract, and lists the funnel bypass under "Not covered".

@PilaScat
PilaScat requested a review from GLinnik21 November 9, 2026 16:22

@GLinnik21 GLinnik21 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, all eight points are addressed in 9a68922..0669636. Checked on 0669636d:

  • Read the fixes: the HTTP/1.1 pin, every dialling test under testlock::serial(), cancel in the connect (progress callback) and in finish(), one send_out() gate for entropy before every write, the bounded 1001 in going_away(), the Outbox live flag, plus origin::url_host and the shared require_option / require_trust_store.
  • make test-crate C=plx_net T=ws:: three runs in a row, libcurl 8.5.0 with nghttp2: 55 passed; 0 failed; 1 ignored each time.
  • CI: 14/14 green.

Agreed that the NoEntropy path and the bounded 1001 flush stay untested for now. The request_tls_sink bypass is tracked under "Not covered" and will get its own issue.


Generated by Claude Code

@GLinnik21
GLinnik21 merged commit 75765b3 into plxnative:main Nov 9, 2026
14 checks passed
@PilaScat
PilaScat deleted the feat/ws-transport branch November 9, 2026 23:20
fraygox added a commit to fraygox/plx-native that referenced this pull request Nov 10, 2026
Conflict: docs/agent-reference.md, the dynlib callers paragraph. Both
sides dropped the old caller count; kept this branch's wording (it
excludes dynlib.rs from the grep) and main's new `net::connect_only`
sentence.

Semantic fix the merge needs: main's plxnative#1283 added `net::connect_only`,
a third libcurl table bound with `raw::load(None)`, i.e. always the
system candidates. Its easy handle comes from `net::curl_easy_init`,
which in a CURLSTACK=1 build is the bundled libcurl-plx.so.4, so
curl_easy_send/recv/getinfo from the system library would be handed a
handle from another libcurl. The table now binds from
`net::bundled_curl()` when it is Some, as curlio's multi table does;
the agent-reference and curlstack module docs say so.

Signed-off-by: fraygox <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants