|
| 1 | +# Datagrams in libp2p |
| 2 | + |
| 3 | +| Lifecycle Stage | Maturity | Status | Latest Revision | |
| 4 | +| --------------- | ------------- | ------ | --------------- | |
| 5 | +| 1A | Working Draft | Active | r0, 2025-05-23 | |
| 6 | + |
| 7 | +Authors: [@marcopolo] |
| 8 | + |
| 9 | +Interest Group: [@sukunrt], [@jxs], [@raulk] |
| 10 | + |
| 11 | +[@marcopolo]: https://github.com/marcopolo |
| 12 | +[@sukunrt]: https://github.com/sukunrt |
| 13 | +[@jxs]: https://github.com/jxs |
| 14 | +[@raulk]: https://github.com/raulk |
| 15 | + |
| 16 | +See the [lifecycle document][lifecycle-spec] for context about the maturity level |
| 17 | +and spec status. |
| 18 | + |
| 19 | +[lifecycle-spec]: https://github.com/libp2p/specs/blob/master/00-framework-01-spec-lifecycle.md |
| 20 | + |
| 21 | +## Table of Contents |
| 22 | + |
| 23 | +- [Introduction](#introduction) |
| 24 | +- [Native support at the transport level](#native-support-at-the-transport-level) |
| 25 | +- [Encoding Datagrams](#encoding-datagrams) |
| 26 | +- [Datagrams on top of a multiplexed stream on top of TCP](#datagrams-on-top-of-a-multiplexed-stream-on-top-of-tcp) |
| 27 | +- [Datagrams with WebRTC](#datagrams-with-webrtc) |
| 28 | + |
| 29 | +## Introduction |
| 30 | + |
| 31 | +This specification defines the datagrams on libp2p, which enable the |
| 32 | +transmission of MTU-sized, unreliable, and low latency messages between peers. |
| 33 | + |
| 34 | +TODO write more about the tradeoffs here as well as the kinds of applications that would benefit from using datagrams. |
| 35 | + |
| 36 | +## Native support at the transport level |
| 37 | + |
| 38 | +Some libp2p transports natively support datagrams, such as QUIC and WebTransport. libp2p implementations MUST use the native datagram support to send datagrams. |
| 39 | + |
| 40 | +QUIC has an Unreliable Datagram Extension [RFC 9221] |
| 41 | +WebTransport supports Datagrams though HTTP Datagrams [WebTransport draft RFC](https://www.ietf.org/archive/id/draft-ietf-webtrans-http3-12.html#name-datagrams) |
| 42 | + |
| 43 | +## Encoding Datagrams |
| 44 | + |
| 45 | +The libp2p datagram encoding introduces minimal framing overhead to application payloads. Each datagram MUST be encoded with the following structure to ensure proper protocol identification and version compatibility. |
| 46 | + |
| 47 | +The datagram frame consists of the following components: |
| 48 | + |
| 49 | +| Field Name | Data Type | Length (bits) | Description | |
| 50 | +| ------------------ | ---------------- | ------------- | ---------------------------------------------------- | |
| 51 | +| version | unsigned integer | 8 | Protocol version identifier | |
| 52 | +| protocol id length | [uvarint] | 8..72 | Length of the protocol identifier field | |
| 53 | +| protocol id | byte sequence | variable | Protocol identifier as defined by protocol id length | |
| 54 | +| application data | byte sequence | variable | Application payload | |
| 55 | + |
| 56 | +The complete datagram frame is constructed as follows: |
| 57 | + |
| 58 | +``` |
| 59 | +version || protocol id length || protocol id || application data |
| 60 | +``` |
| 61 | + |
| 62 | +Where "||" denotes byte concatenation. |
| 63 | + |
| 64 | +The version field provides extensibility for future encoding modifications while maintaining backward compatibility. Implementations MUST verify the version field before processing the remainder of the datagram. |
| 65 | + |
| 66 | +## Datagrams on top of a multiplexed stream on top of TCP |
| 67 | + |
| 68 | +This is currently not specified. |
| 69 | + |
| 70 | +## Datagrams with WebRTC |
| 71 | + |
| 72 | +This is currently not specified. |
| 73 | + |
| 74 | +[uvarint]: https://github.com/multiformats/unsigned-varint |
| 75 | +[RFC 9221]: https://www.rfc-editor.org/rfc/rfc9221 |
0 commit comments