Skip to content

Expose secret-stream datagram C API#3

Merged
jjacke13 merged 2 commits intojjacke13:mainfrom
lukeburns:datagram-ffi
Apr 26, 2026
Merged

Expose secret-stream datagram C API#3
jjacke13 merged 2 commits intojjacke13:mainfrom
lukeburns:datagram-ffi

Conversation

@lukeburns
Copy link
Copy Markdown
Contributor

Summary

  • Exposes secret-stream unordered datagrams through the C API with send, try-send, and receive callback hooks.
  • Wires C API streams to SecretStreamDuplex::send_udp / on_udp_message for encrypted unreliable packet delivery.
  • Fixes the datagram NS_SEND namespace constant to match @hyperswarm/secret-stream, so C++ and JS peers can authenticate unordered messages.

Test plan

  • Verified from a downstream Spoon mobile integration that WebRTC DataChannel packets bridge over HyperDHT datagrams to a JS hyperdht peer and back.
  • Existing C++ unit coverage for in-process SecretStreamDuplex UDP round-trip is present.
  • TODO: add dedicated C++ <-> JS cross-implementation datagram tests for both send directions before treating this as fully covered upstream.

Made with Cursor

The existing C API only surfaced the reliable byte stream from
SecretStreamDuplex. Wire up the unreliable/datagram side too so
embedders that want UDP-like semantics (e.g. carrying raw IP
packets across hyperdht-routed peers) don't have to drop down
into C++.

Adds three new C entry points on hyperdht_stream_t:

  * hyperdht_stream_set_on_udp_message — install a callback that
    fires for each inbound UDP datagram (SecretStreamDuplex::
    on_udp_message). If unset, datagrams are silently dropped.
  * hyperdht_stream_send_udp           — best-effort send via
    SecretStreamDuplex::send_udp.
  * hyperdht_stream_try_send_udp       — non-blocking variant
    that returns immediately if the path is congested.

ffi_internal.hpp gains storage for the callback + userdata, and
ffi_stream.cpp installs an on_udp_message dispatcher inside
hyperdht_stream_open so the C callback slot is always wired
once a stream is open.

No behavioural change for existing callers: the new callback
slot is opt-in and the send entry points are additive.

Made-with: Cursor
Use the same NS_SEND constant as @hyperswarm/secret-stream so unordered datagrams authenticate across the C++ and JS implementations.

Made-with: Cursor
@jjacke13 jjacke13 merged commit ba96286 into jjacke13:main Apr 26, 2026
@jjacke13
Copy link
Copy Markdown
Owner

thanks for the fixes :)

jjacke13 added a commit that referenced this pull request Apr 26, 2026
- REMAINING-WORK.md: remove completed items (ESP32, aarch64 CI,
  Kotlin wrapper, Python smoke test, stream drain), update FFI count
  76→84, note Luke Burns' PRs #1-#3
- rpc.cpp: add DHT_LOG in check_persistent() showing NAT classification
  (UNKNOWN/OPEN/CONSISTENT/RANDOM) — aids debugging persistent transition
- test/js/test_node_queries.js: verifies a server's announcement on DHT
- test/js/test_node_serves.js: verifies a node serves PING, FIND_NODE,
  FIND_PEER, LOOKUP when queried directly
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.

2 participants