Skip to content

Commit

Permalink
Properly implement PlatformRef::supports_connection_type in wasm-node (
Browse files Browse the repository at this point in the history
…#923)

* Properly implement PlatformRef::supports_connection_type in wasm-node

* PR link

* Fix flags being mixed up
  • Loading branch information
tomaka authored Jul 16, 2023
1 parent f7869d3 commit 266ccd0
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 24 deletions.
55 changes: 43 additions & 12 deletions light-base/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,30 @@ pub enum SubstreamDirection {
/// Connection type passed to [`PlatformRef::supports_connection_type`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ConnectionType {
/// TCP/IP connections.
Tcp,
/// WebSocket connections.
WebSocket {
/// TCP/IP connection.
TcpIpv4,
/// TCP/IP connection.
TcpIpv6,
/// TCP/IP connection.
TcpDns,
/// Non-secure WebSocket connection.
WebSocketIpv4 {
/// `true` if the target of the connection is `localhost`.
///
/// > **Note**: Some platforms (namely browsers) sometimes only accept non-secure WebSocket
/// > connections only towards `localhost`.
remote_is_localhost: bool,
},
/// Non-secure WebSocket connection.
WebSocketIpv6 {
/// `true` if the target of the connection is `localhost`.
///
/// > **Note**: Some platforms (namely browsers) sometimes only accept non-secure WebSocket
/// > connections only towards `localhost`.
remote_is_localhost: bool,
},
/// WebSocket connection.
WebSocketDns {
/// `true` for WebSocket secure connections.
secure: bool,
/// `true` if the target of the connection is `localhost`.
Expand All @@ -281,28 +301,34 @@ pub enum ConnectionType {
remote_is_localhost: bool,
},
/// Libp2p-specific WebRTC flavour.
WebRtc,
WebRtcIpv4,
/// Libp2p-specific WebRTC flavour.
WebRtcIpv6,
}

impl<'a> From<&'a Address<'a>> for ConnectionType {
fn from(address: &'a Address<'a>) -> ConnectionType {
match address {
Address::TcpDns { .. } | Address::TcpIp { .. } => ConnectionType::Tcp,
Address::TcpIp {
ip: IpAddr::V4(_), ..
} => ConnectionType::TcpIpv4,
Address::TcpIp {
ip: IpAddr::V6(_), ..
} => ConnectionType::TcpIpv6,
Address::TcpDns { .. } => ConnectionType::TcpDns,
Address::WebSocketIp {
ip: IpAddr::V4(ip), ..
} => ConnectionType::WebSocket {
secure: false,
} => ConnectionType::WebSocketIpv4 {
remote_is_localhost: no_std_net::Ipv4Addr::from(*ip).is_loopback(),
},
Address::WebSocketIp {
ip: IpAddr::V6(ip), ..
} => ConnectionType::WebSocket {
secure: false,
} => ConnectionType::WebSocketIpv6 {
remote_is_localhost: no_std_net::Ipv6Addr::from(*ip).is_loopback(),
},
Address::WebSocketDns {
hostname, secure, ..
} => ConnectionType::WebSocket {
} => ConnectionType::WebSocketDns {
secure: *secure,
remote_is_localhost: hostname.eq_ignore_ascii_case("localhost"),
},
Expand All @@ -313,7 +339,12 @@ impl<'a> From<&'a Address<'a>> for ConnectionType {
impl<'a> From<&'a MultiStreamAddress> for ConnectionType {
fn from(address: &'a MultiStreamAddress) -> ConnectionType {
match address {
MultiStreamAddress::WebRtc { .. } => ConnectionType::WebRtc,
MultiStreamAddress::WebRtc {
ip: IpAddr::V4(_), ..
} => ConnectionType::WebRtcIpv4,
MultiStreamAddress::WebRtc {
ip: IpAddr::V6(_), ..
} => ConnectionType::WebRtcIpv6,
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion light-base/src/platform/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ impl PlatformRef for Arc<DefaultPlatform> {
// TODO: support WebSocket secure
matches!(
connection_type,
ConnectionType::Tcp | ConnectionType::WebSocket { secure: false, .. }
ConnectionType::TcpIpv4
| ConnectionType::TcpIpv6
| ConnectionType::TcpDns
| ConnectionType::WebSocketIpv4 { .. }
| ConnectionType::WebSocketIpv6 { .. }
| ConnectionType::WebSocketDns { secure: false, .. }
)
}

Expand Down
4 changes: 4 additions & 0 deletions wasm-node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

- The smoldot binary now longer has SIMD enabled, in order to make it work on a greater range of hardware. It was previously assumed that SIMD instructions were emulated on hardware that doesn't natively support them, but this doesn't seem to be the case for some browser engines. ([#903](https://github.com/smol-dot/smoldot/pull/903))

### Fixed

- Fix regression introduced in version 1.0.12 where only WebSocket secure and WebRTC connections were ever opened, regardless of the `forbid*` configuration flags. ([#923](https://github.com/smol-dot/smoldot/pull/923))

## 1.0.12 - 2023-07-10

### Changed
Expand Down
4 changes: 2 additions & 2 deletions wasm-node/javascript/src/internals/local-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ export async function startLocalInstance(config: Config, wasmModule: WebAssembly
case 4:
case 5:
case 6: {
return config.forbidWs ? 0 : 1
return config.forbidNonLocalWs ? 0 : 1
}
case 7: {
return config.forbidNonLocalWs ? 0 : 1
return config.forbidWs ? 0 : 1
}
case 14: {
return config.forbidWss ? 0 : 1
Expand Down
34 changes: 25 additions & 9 deletions wasm-node/rust/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,31 @@ impl smoldot_light::platform::PlatformRef for PlatformRef {
&self,
connection_type: smoldot_light::platform::ConnectionType,
) -> bool {
// TODO: no, should ask the JS code what it supports
match connection_type {
smoldot_light::platform::ConnectionType::WebRtc => true,
smoldot_light::platform::ConnectionType::WebSocket {
secure,
remote_is_localhost,
} => secure || remote_is_localhost,
smoldot_light::platform::ConnectionType::Tcp => false,
}
let ty = match connection_type {
smoldot_light::platform::ConnectionType::TcpIpv4 => 0,
smoldot_light::platform::ConnectionType::TcpIpv6 => 1,
smoldot_light::platform::ConnectionType::TcpDns => 2,
smoldot_light::platform::ConnectionType::WebSocketIpv4 {
remote_is_localhost: true,
..
}
| smoldot_light::platform::ConnectionType::WebSocketIpv6 {
remote_is_localhost: true,
..
}
| smoldot_light::platform::ConnectionType::WebSocketDns {
secure: false,
remote_is_localhost: true,
} => 7,
smoldot_light::platform::ConnectionType::WebSocketIpv4 { .. } => 4,
smoldot_light::platform::ConnectionType::WebSocketIpv6 { .. } => 5,
smoldot_light::platform::ConnectionType::WebSocketDns { secure: false, .. } => 6,
smoldot_light::platform::ConnectionType::WebSocketDns { secure: true, .. } => 14,
smoldot_light::platform::ConnectionType::WebRtcIpv4 => 16,
smoldot_light::platform::ConnectionType::WebRtcIpv6 => 17,
};

unsafe { bindings::connection_type_supported(ty) != 0 }
}

fn connect_stream(
Expand Down

0 comments on commit 266ccd0

Please sign in to comment.