-
Notifications
You must be signed in to change notification settings - Fork 35
webrtc: Decode webrtc message from multiple SCTP messages #594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
eac3387
webrtc: Decode webrtc message from 2 SCTP messages
lexnv 17b8e2c
Fix build
lexnv 16c5991
clippy: Fix indentation for docs
lexnv 0423a69
webrtc: Rename decode protobuf to simply decode
lexnv 870129a
webrtc: Refactor common extraction msg under util module
lexnv dd2a8f4
webrtc/tests: Test extract framed message supports 1 or multiple SCTP
lexnv 2b1cb5d
webrtc: Capture format errors and terminate connectin
lexnv 5457891
webrtc: Avoid extra allocs on extract_framed_message paths
lexnv c111c33
webrtc: Add trace logs
lexnv ca20c71
webrtc: Reject oversized frames
lexnv ca90d49
webrtc: Cap total inflight SCTP packet sizes across all connection
lexnv d3540ac
Revert "webrtc: Cap total inflight SCTP packet sizes across all conne…
lexnv 38b62d1
Update src/transport/webrtc/connection.rs
lexnv bbbe8c6
Update src/transport/webrtc/opening.rs
lexnv 2ddf86a
webrtc: Make logging uniform to expose peer address and connection ID
lexnv 38a2b66
Merge remote-tracking branch 'origin/master' into lexnv/decode-webrtc
lexnv b411936
Fix indenting
lexnv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,11 +23,15 @@ | |
| use crate::{ | ||
| config::Role, | ||
| crypto::{ed25519::Keypair, noise::NoiseContext}, | ||
| transport::{webrtc::util::WebRtcMessage, Endpoint}, | ||
| transport::{ | ||
| webrtc::util::{extract_framed_message, WebRtcMessage}, | ||
| Endpoint, | ||
| }, | ||
| types::ConnectionId, | ||
| Error, PeerId, | ||
| }; | ||
|
|
||
| use bytes::BytesMut; | ||
| use multiaddr::{Multiaddr, Protocol}; | ||
| use multihash_codetable::Code; | ||
| use str0m::{ | ||
|
|
@@ -111,6 +115,18 @@ pub struct OpeningWebRtcConnection { | |
|
|
||
| /// Local address. | ||
| local_address: SocketAddr, | ||
|
|
||
| /// Inbound noise-channel byte buffer for reassembling pbio-delimited frames. | ||
|
lexnv marked this conversation as resolved.
Outdated
|
||
| /// | ||
| /// The libp2p-go msgio implementation issues two separate `Write` calls: | ||
| /// - variant length | ||
| /// - protobuf body | ||
| /// | ||
| /// These will become two distinct SCTP messages on the data channel. | ||
| /// | ||
| /// Accumulate raw bytes here and only attempt protobuf decode once a | ||
| /// full `varint length ++ body` frame is available. | ||
| noise_recv_buffer: BytesMut, | ||
| } | ||
|
|
||
| /// Connection state. | ||
|
|
@@ -168,6 +184,7 @@ impl OpeningWebRtcConnection { | |
| id_keypair, | ||
| peer_address, | ||
| local_address, | ||
| noise_recv_buffer: BytesMut::new(), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -244,7 +261,28 @@ impl OpeningWebRtcConnection { | |
| /// | ||
| /// If the peer is accepted, [`OpeningWebRtcConnection::on_accept()`] is called which creates | ||
| /// the final Noise message and sends it to the remote peer, concluding the handshake. | ||
| fn on_noise_channel_data(&mut self, data: Vec<u8>) -> crate::Result<WebRtcEvent> { | ||
| fn on_noise_channel_data(&mut self, data: Vec<u8>) -> crate::Result<Option<WebRtcEvent>> { | ||
| tracing::trace!( | ||
| target: LOG_TARGET, | ||
| len = data.len(), | ||
| buffered = self.noise_recv_buffer.len(), | ||
| "noise channel data received", | ||
| ); | ||
|
|
||
| self.noise_recv_buffer.extend_from_slice(&data); | ||
|
|
||
| let body = match extract_framed_message(&mut self.noise_recv_buffer)? { | ||
| Some(body) => body, | ||
| None => { | ||
| tracing::trace!( | ||
| target: LOG_TARGET, | ||
| buffered = self.noise_recv_buffer.len(), | ||
| "incomplete noise frame, waiting for more bytes", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to also log peerid/remote endpoint here and in other logs. |
||
| ); | ||
| return Ok(None); | ||
| } | ||
| }; | ||
|
|
||
| tracing::trace!(target: LOG_TARGET, "handle noise handshake reply"); | ||
|
|
||
| let State::HandshakeSent { mut context } = | ||
|
|
@@ -253,7 +291,7 @@ impl OpeningWebRtcConnection { | |
| return Err(Error::InvalidState); | ||
| }; | ||
|
|
||
| let message = WebRtcMessage::decode(&data)?.payload.ok_or(Error::InvalidData)?; | ||
| let message = WebRtcMessage::decode(&body)?.payload.ok_or(Error::InvalidData)?; | ||
| let remote_peer_id = context.get_remote_peer_id(&message)?; | ||
|
|
||
| tracing::trace!( | ||
|
|
@@ -283,10 +321,10 @@ impl OpeningWebRtcConnection { | |
| .with(Protocol::Certhash(certificate)) | ||
| .with(Protocol::P2p(remote_peer_id.into())); | ||
|
|
||
| Ok(WebRtcEvent::ConnectionOpened { | ||
| Ok(Some(WebRtcEvent::ConnectionOpened { | ||
| peer: remote_peer_id, | ||
| endpoint: Endpoint::listener(address, self.connection_id), | ||
| }) | ||
| })) | ||
| } | ||
|
|
||
| /// Accept connection by sending the final Noise handshake message | ||
|
|
@@ -436,7 +474,8 @@ impl OpeningWebRtcConnection { | |
| } | ||
|
|
||
| match self.on_noise_channel_data(data.data) { | ||
| Ok(event) => return event, | ||
| Ok(Some(event)) => return event, | ||
| Ok(None) => continue, | ||
| Err(error) => { | ||
| tracing::debug!( | ||
| target: LOG_TARGET, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.