Skip to content

WebRTC transport support #698

Open
Open
@Menduist

Description

@Menduist

WebRTC Stack Implementation for libp2p in Nim

This issue covers the current status of our WebRTC stack implementation in Nim for our libp2p project. Below is an overview of the protocols involved, progress so far, and details about what’s left to complete. This issue will serve as a reference for the team to continue the implementation and maintenance.

Overview

The WebRTC stack we’re building consists of five main protocols:

You can currently find all these protocols merged in this PR: vacp2p/nim-webrtc#24

While some are fully implemented, others require additional work, especially around cross-platform stability and testing. Here’s a detailed look at the status of each protocol, what’s working, what isn’t, and areas needing follow-up.


Protocol Breakdown

1. UDP

  • Status: Fully implemented
  • Library: nim-chronos
  • Notes: No additional implementation required. We are leveraging nim-chronos, a reliable library we already use widely, so no issues are anticipated here.

2. STUN (NAT Traversal)

  • Status: Implemented by us
  • Details: STUN is implemented with the mandatory attributes required for our application. Some optional attributes, like realm or nonce, are not included since they are unnecessary for our use case as far as I know.
  • To Do: None, unless additional STUN attributes are required in the future.

3. DTLS (Encryption)

  • Status: Wrapped from Mbed-TLS
  • Library: nim wrapper nim-mbedtls / C library Mbed-TLS
  • Details: By wrapping Mbed-TLS, we avoided a full DTLS implementation. However, wrapping with c2nim introduces several pain points:
    • Wrapping Limitations: The wrapper tool we used (c2nim if I remember correctly) is not always able to handle some C specificity, requiring portions of the code to be wrapped manually.
    • Maintenance Overhead: To keep the wrapper updated with the latest Mbed-TLS fixes and improvements, we often need to reapply these manual adjustments, adding to maintenance time and complexity.
    • Cross-OS Compilation: Variability in OS environments complicates compilation, especially for cross-compiling between systems as a lot of these things are done using C macros which are not really well detected by the wrappers.
  • To Do: Long-term, explore options to improve cross-platform compatibility. Also, set up regular checks for upstream Mbed-TLS updates to stay secure and maintain compatibility.

4. SCTP (Reliable Data Transmission)

  • Status: Wrapped from Usrsctp
  • Library: nim wrapper nim-usrsctp / C library usrsctp
  • Details: Similar to DTLS, wrapping Usrsctp provides SCTP functionality without a full implementation. The trade-offs are:
  • To Do: Same as DTLS; monitor Usrsctp for updates and be mindful of cross-platform compilation needs.

5. DataChannel

  • Status: Implemented by us. Initial implementation is complete, but several important remain untested or incomplete.
  • Details: Our DataChannel implementation should support reliable, ordered messaging across peers.
  • To Do:
    • Extensive Testing: Run a broad suite of tests to ensure stability and identify any missing or incorrect functionality.
    • Stream Management: Ensure we can request additional streams if necessary, depending on usage patterns.
    • Data Handling: Implement handling for both string and binary data, including differentiation based on incoming data type.
    • Client/Server Mode Differences: Account for different behaviors when the DataChannel operates as a client vs. as a server.
    • Graceful Stream Closure: Close DataChannel streams in line with the other protocols to ensure consistency and clean disconnection.

Summary

The WebRTC stack implementation is well underway, with STUN fully implemented and stable UDP handling through nim-chronos. Wrapping for DTLS (via Mbed-TLS) and SCTP (via Usrsctp) provides essential protocol support, but comes with challenges in maintenance and cross-platform compatibility. DataChannel is in place but needs further testing.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

backlog

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions