From d58d333e31241f26e1b72cfd52a3810c539adde6 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Fri, 17 Mar 2023 12:37:05 +0100 Subject: [PATCH 1/2] fix: Rename `/webrtc` to `/webrtc-direct` See https://github.com/multiformats/multiaddr/pull/150#issuecomment-1468791586 for context. --- CHANGELOG.md | 5 ++++- src/protocol.rs | 14 +++++++------- tests/lib.rs | 18 +++++++++--------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f6fb78..07ab031 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ -# 0.18.0 [unreleased] +# 0.18.0 - unreleased - Add `WebTransport` instance for `Multiaddr`. See [PR 70]. +- Rename `/webrtc` to `/webrt-direct`. See [multiformats/multiaddr discussion] for context. **Note that this is a breaking change.** + +[multiformats/multiaddr discussion]: https://github.com/multiformats/multiaddr/pull/150#issuecomment-1468791586 [PR 70]: https://github.com/multiformats/rust-multiaddr/pull/70 # 0.17.0 diff --git a/src/protocol.rs b/src/protocol.rs index 662921f..ec61a78 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -28,7 +28,7 @@ const IP4: u32 = 4; const IP6: u32 = 41; const P2P_WEBRTC_DIRECT: u32 = 276; const P2P_WEBRTC_STAR: u32 = 275; -const WEBRTC: u32 = 280; +const WEBRTC_DIRECT: u32 = 280; const CERTHASH: u32 = 466; const P2P_WEBSOCKET_STAR: u32 = 479; const MEMORY: u32 = 777; @@ -84,7 +84,7 @@ pub enum Protocol<'a> { Ip6(Ipv6Addr), P2pWebRtcDirect, P2pWebRtcStar, - WebRTC, + WebRTCDirect, Certhash(Multihash), P2pWebSocketStar, /// Contains the "port" to contact. Similar to TCP or UDP, 0 means "assign me a port". @@ -202,7 +202,7 @@ impl<'a> Protocol<'a> { } "p2p-websocket-star" => Ok(Protocol::P2pWebSocketStar), "p2p-webrtc-star" => Ok(Protocol::P2pWebRtcStar), - "webrtc" => Ok(Protocol::WebRTC), + "webrtc-direct" => Ok(Protocol::WebRTCDirect), "certhash" => { let s = iter.next().ok_or(Error::InvalidProtocolString)?; let (_base, decoded) = multibase::decode(s)?; @@ -284,7 +284,7 @@ impl<'a> Protocol<'a> { } P2P_WEBRTC_DIRECT => Ok((Protocol::P2pWebRtcDirect, input)), P2P_WEBRTC_STAR => Ok((Protocol::P2pWebRtcStar, input)), - WEBRTC => Ok((Protocol::WebRTC, input)), + WEBRTC_DIRECT => Ok((Protocol::WebRTCDirect, input)), CERTHASH => { let (n, input) = decode::usize(input)?; let (data, rest) = split_at(n, input)?; @@ -467,7 +467,7 @@ impl<'a> Protocol<'a> { } Protocol::P2pWebSocketStar => w.write_all(encode::u32(P2P_WEBSOCKET_STAR, &mut buf))?, Protocol::P2pWebRtcStar => w.write_all(encode::u32(P2P_WEBRTC_STAR, &mut buf))?, - Protocol::WebRTC => w.write_all(encode::u32(WEBRTC, &mut buf))?, + Protocol::WebRTCDirect => w.write_all(encode::u32(WEBRTC_DIRECT, &mut buf))?, Protocol::Certhash(hash) => { w.write_all(encode::u32(CERTHASH, &mut buf))?; let bytes = hash.to_bytes(); @@ -499,7 +499,7 @@ impl<'a> Protocol<'a> { Ip6(a) => Ip6(a), P2pWebRtcDirect => P2pWebRtcDirect, P2pWebRtcStar => P2pWebRtcStar, - WebRTC => WebRTC, + WebRTCDirect => WebRTCDirect, Certhash(hash) => Certhash(hash), P2pWebSocketStar => P2pWebSocketStar, Memory(a) => Memory(a), @@ -537,7 +537,7 @@ impl<'a> Protocol<'a> { Ip6(_) => "ip6", P2pWebRtcDirect => "p2p-webrtc-direct", P2pWebRtcStar => "p2p-webrtc-star", - WebRTC => "webrtc", + WebRTCDirect => "webrtc-direct", Certhash(_) => "certhash", P2pWebSocketStar => "p2p-websocket-star", Memory(_) => "memory", diff --git a/tests/lib.rs b/tests/lib.rs index 7b14686..8641803 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -104,7 +104,7 @@ impl Arbitrary for Proto { 8 => Proto(Ip6(Ipv6Addr::arbitrary(g))), 9 => Proto(P2pWebRtcDirect), 10 => Proto(P2pWebRtcStar), - 11 => Proto(WebRTC), + 11 => Proto(WebRTCDirect), 12 => Proto(Certhash(Mh::arbitrary(g).0)), 13 => Proto(P2pWebSocketStar), 14 => Proto(Memory(Arbitrary::arbitrary(g))), @@ -360,20 +360,20 @@ fn construct_success() { ); ma_valid( - "/ip4/127.0.0.1/udp/1234/webrtc", + "/ip4/127.0.0.1/udp/1234/webrtc-direct", "047F000001910204D29802", - vec![Ip4(local), Udp(1234), WebRTC], + vec![Ip4(local), Udp(1234), WebRTCDirect], ); let (_base, decoded) = multibase::decode("uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g").unwrap(); ma_valid( - "/ip4/127.0.0.1/udp/1234/webrtc/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g", + "/ip4/127.0.0.1/udp/1234/webrtc-direct/certhash/uEiDDq4_xNyDorZBH3TlGazyJdOWSwvo4PUo5YHFMrvDE8g", "047F000001910204D29802D203221220C3AB8FF13720E8AD9047DD39466B3C8974E592C2FA383D4A3960714CAEF0C4F2", vec![ Ip4(local), Udp(1234), - WebRTC, + WebRTCDirect, Certhash(Multihash::from_bytes(&decoded).unwrap()), ], ); @@ -432,8 +432,8 @@ fn construct_fail() { "/ip4/127.0.0.1/p2p", "/ip4/127.0.0.1/p2p/tcp", "/p2p-circuit/50", - "/ip4/127.0.0.1/udp/1234/webrtc/certhash", - "/ip4/127.0.0.1/udp/1234/webrtc/certhash/b2uaraocy6yrdblb4sfptaddgimjmmp", // 1 character missing from certhash + "/ip4/127.0.0.1/udp/1234/webrtc-direct/certhash", + "/ip4/127.0.0.1/udp/1234/webrtc-direct/certhash/b2uaraocy6yrdblb4sfptaddgimjmmp", // 1 character missing from certhash ]; for address in &addresses { @@ -605,7 +605,7 @@ fn protocol_stack() { "/ip4/127.0.0.1/tcp/127/tls", "/ip4/127.0.0.1/tcp/127/tls/ws", "/ip4/127.0.0.1/tcp/127/noise", - "/ip4/127.0.0.1/udp/1234/webrtc", + "/ip4/127.0.0.1/udp/1234/webrtc-direct", ]; let argless = std::collections::HashSet::from([ "http", @@ -620,7 +620,7 @@ fn protocol_stack() { "tls", "udt", "utp", - "webrtc", + "webrtc-direct", "ws", "wss", ]); From 684661e251d9231decb1960dd22accccf2a2d89e Mon Sep 17 00:00:00 2001 From: Max Inden Date: Fri, 17 Mar 2023 14:12:47 +0100 Subject: [PATCH 2/2] feat: Add `/webrtc` protocol (browser-to-browser) See decision making in https://github.com/multiformats/multiaddr/pull/150#issuecomment-1468791586. --- CHANGELOG.md | 2 ++ src/protocol.rs | 7 +++++++ tests/lib.rs | 51 +++++++++++++++++++++++++++++-------------------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07ab031..45a924e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - Rename `/webrtc` to `/webrt-direct`. See [multiformats/multiaddr discussion] for context. **Note that this is a breaking change.** +- Add `WebRTC` (browser-to-browser) instance for `Multiaddr`. + [multiformats/multiaddr discussion]: https://github.com/multiformats/multiaddr/pull/150#issuecomment-1468791586 [PR 70]: https://github.com/multiformats/rust-multiaddr/pull/70 diff --git a/src/protocol.rs b/src/protocol.rs index ec61a78..5ded54c 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -29,6 +29,7 @@ const IP6: u32 = 41; const P2P_WEBRTC_DIRECT: u32 = 276; const P2P_WEBRTC_STAR: u32 = 275; const WEBRTC_DIRECT: u32 = 280; +const WEBRTC: u32 = 281; const CERTHASH: u32 = 466; const P2P_WEBSOCKET_STAR: u32 = 479; const MEMORY: u32 = 777; @@ -85,6 +86,7 @@ pub enum Protocol<'a> { P2pWebRtcDirect, P2pWebRtcStar, WebRTCDirect, + WebRTC, Certhash(Multihash), P2pWebSocketStar, /// Contains the "port" to contact. Similar to TCP or UDP, 0 means "assign me a port". @@ -203,6 +205,7 @@ impl<'a> Protocol<'a> { "p2p-websocket-star" => Ok(Protocol::P2pWebSocketStar), "p2p-webrtc-star" => Ok(Protocol::P2pWebRtcStar), "webrtc-direct" => Ok(Protocol::WebRTCDirect), + "webrtc" => Ok(Protocol::WebRTC), "certhash" => { let s = iter.next().ok_or(Error::InvalidProtocolString)?; let (_base, decoded) = multibase::decode(s)?; @@ -285,6 +288,7 @@ impl<'a> Protocol<'a> { P2P_WEBRTC_DIRECT => Ok((Protocol::P2pWebRtcDirect, input)), P2P_WEBRTC_STAR => Ok((Protocol::P2pWebRtcStar, input)), WEBRTC_DIRECT => Ok((Protocol::WebRTCDirect, input)), + WEBRTC => Ok((Protocol::WebRTC, input)), CERTHASH => { let (n, input) = decode::usize(input)?; let (data, rest) = split_at(n, input)?; @@ -468,6 +472,7 @@ impl<'a> Protocol<'a> { Protocol::P2pWebSocketStar => w.write_all(encode::u32(P2P_WEBSOCKET_STAR, &mut buf))?, Protocol::P2pWebRtcStar => w.write_all(encode::u32(P2P_WEBRTC_STAR, &mut buf))?, Protocol::WebRTCDirect => w.write_all(encode::u32(WEBRTC_DIRECT, &mut buf))?, + Protocol::WebRTC => w.write_all(encode::u32(WEBRTC, &mut buf))?, Protocol::Certhash(hash) => { w.write_all(encode::u32(CERTHASH, &mut buf))?; let bytes = hash.to_bytes(); @@ -500,6 +505,7 @@ impl<'a> Protocol<'a> { P2pWebRtcDirect => P2pWebRtcDirect, P2pWebRtcStar => P2pWebRtcStar, WebRTCDirect => WebRTCDirect, + WebRTC => WebRTC, Certhash(hash) => Certhash(hash), P2pWebSocketStar => P2pWebSocketStar, Memory(a) => Memory(a), @@ -538,6 +544,7 @@ impl<'a> Protocol<'a> { P2pWebRtcDirect => "p2p-webrtc-direct", P2pWebRtcStar => "p2p-webrtc-star", WebRTCDirect => "webrtc-direct", + WebRTC => "webrtc", Certhash(_) => "certhash", P2pWebSocketStar => "p2p-websocket-star", Memory(_) => "memory", diff --git a/tests/lib.rs b/tests/lib.rs index 8641803..323e8e8 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -86,7 +86,7 @@ impl Arbitrary for Ma { struct Proto(Protocol<'static>); impl Proto { - const IMPL_VARIANT_COUNT: u8 = 32; + const IMPL_VARIANT_COUNT: u8 = 33; } impl Arbitrary for Proto { @@ -105,10 +105,11 @@ impl Arbitrary for Proto { 9 => Proto(P2pWebRtcDirect), 10 => Proto(P2pWebRtcStar), 11 => Proto(WebRTCDirect), - 12 => Proto(Certhash(Mh::arbitrary(g).0)), - 13 => Proto(P2pWebSocketStar), - 14 => Proto(Memory(Arbitrary::arbitrary(g))), - 15 => { + 12 => Proto(WebRTC), + 13 => Proto(Certhash(Mh::arbitrary(g).0)), + 14 => Proto(P2pWebSocketStar), + 15 => Proto(Memory(Arbitrary::arbitrary(g))), + 16 => { let a = iter::repeat_with(|| u8::arbitrary(g)) .take(10) .collect::>() @@ -116,7 +117,7 @@ impl Arbitrary for Proto { .unwrap(); Proto(Onion(Cow::Owned(a), std::cmp::max(1, u16::arbitrary(g)))) } - 16 => { + 17 => { let a: [u8; 35] = iter::repeat_with(|| u8::arbitrary(g)) .take(35) .collect::>() @@ -124,21 +125,21 @@ impl Arbitrary for Proto { .unwrap(); Proto(Onion3((a, std::cmp::max(1, u16::arbitrary(g))).into())) } - 17 => Proto(P2p(Mh::arbitrary(g).0)), - 18 => Proto(P2pCircuit), - 19 => Proto(Quic), - 20 => Proto(QuicV1), - 21 => Proto(Sctp(Arbitrary::arbitrary(g))), - 22 => Proto(Tcp(Arbitrary::arbitrary(g))), - 23 => Proto(Tls), - 24 => Proto(Noise), - 25 => Proto(Udp(Arbitrary::arbitrary(g))), - 26 => Proto(Udt), - 27 => Proto(Unix(Cow::Owned(SubString::arbitrary(g).0))), - 28 => Proto(Utp), - 29 => Proto(WebTransport), - 30 => Proto(Ws("/".into())), - 31 => Proto(Wss("/".into())), + 18 => Proto(P2p(Mh::arbitrary(g).0)), + 19 => Proto(P2pCircuit), + 20 => Proto(Quic), + 21 => Proto(QuicV1), + 22 => Proto(Sctp(Arbitrary::arbitrary(g))), + 23 => Proto(Tcp(Arbitrary::arbitrary(g))), + 24 => Proto(Tls), + 25 => Proto(Noise), + 26 => Proto(Udp(Arbitrary::arbitrary(g))), + 27 => Proto(Udt), + 28 => Proto(Unix(Cow::Owned(SubString::arbitrary(g).0))), + 29 => Proto(Utp), + 30 => Proto(WebTransport), + 31 => Proto(Ws("/".into())), + 32 => Proto(Wss("/".into())), _ => panic!("outside range"), } } @@ -378,6 +379,12 @@ fn construct_success() { ], ); + ma_valid( + "/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/4001/wss/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN/p2p-circuit/webrtc/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", + "29200108A07AC542013AC986FFFE317095060FA1DE03A50322122006B3608AA000274049EB28AD8E793A26FF6FAB281A7D3BD77CD18EB745DFAABBA2029902A503221220D52EBB89D85B02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B", + vec![Ip6(addr6),Tcp(4001), Wss("/".into()), P2p(multihash("QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN")), P2pCircuit, WebRTC, P2p(multihash("QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC"))], + ); + ma_valid( "/ip4/127.0.0.1/udp/1234/quic/webtransport", "047F000001910204D2CC03D103", @@ -606,6 +613,7 @@ fn protocol_stack() { "/ip4/127.0.0.1/tcp/127/tls/ws", "/ip4/127.0.0.1/tcp/127/noise", "/ip4/127.0.0.1/udp/1234/webrtc-direct", + "/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095/tcp/4001/wss/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN/p2p-circuit/webrtc/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC", ]; let argless = std::collections::HashSet::from([ "http", @@ -621,6 +629,7 @@ fn protocol_stack() { "udt", "utp", "webrtc-direct", + "webrtc", "ws", "wss", ]);