diff options
| author | Ruben De Smet <[email protected]> | 2023-06-05 16:00:53 +0200 |
|---|---|---|
| committer | Ruben De Smet <[email protected]> | 2023-06-06 17:58:45 +0200 |
| commit | e871324bde25bd61241aed83416caf6e49376d5a (patch) | |
| tree | 41519982c64cc331ec855a64159988c0146d5bc1 /embassy-net | |
| parent | 54bab33c7342510be538bc6d8545fe50146557cf (diff) | |
net: StaticV4 config behind proto-ipv4
Diffstat (limited to 'embassy-net')
| -rw-r--r-- | embassy-net/src/device.rs | 5 | ||||
| -rw-r--r-- | embassy-net/src/dns.rs | 1 | ||||
| -rw-r--r-- | embassy-net/src/lib.rs | 46 | ||||
| -rw-r--r-- | embassy-net/src/tcp.rs | 3 |
4 files changed, 43 insertions, 12 deletions
diff --git a/embassy-net/src/device.rs b/embassy-net/src/device.rs index 5daa00544..583cdc87f 100644 --- a/embassy-net/src/device.rs +++ b/embassy-net/src/device.rs | |||
| @@ -59,7 +59,10 @@ where | |||
| 59 | smolcaps.checksum.ipv4 = convert(caps.checksum.ipv4); | 59 | smolcaps.checksum.ipv4 = convert(caps.checksum.ipv4); |
| 60 | smolcaps.checksum.tcp = convert(caps.checksum.tcp); | 60 | smolcaps.checksum.tcp = convert(caps.checksum.tcp); |
| 61 | smolcaps.checksum.udp = convert(caps.checksum.udp); | 61 | smolcaps.checksum.udp = convert(caps.checksum.udp); |
| 62 | smolcaps.checksum.icmpv4 = convert(caps.checksum.icmpv4); | 62 | #[cfg(feature = "proto-ipv4")] |
| 63 | { | ||
| 64 | smolcaps.checksum.icmpv4 = convert(caps.checksum.icmpv4); | ||
| 65 | } | ||
| 63 | #[cfg(feature = "proto-ipv6")] | 66 | #[cfg(feature = "proto-ipv6")] |
| 64 | { | 67 | { |
| 65 | smolcaps.checksum.icmpv6 = convert(caps.checksum.icmpv6); | 68 | smolcaps.checksum.icmpv6 = convert(caps.checksum.icmpv6); |
diff --git a/embassy-net/src/dns.rs b/embassy-net/src/dns.rs index 3fd235b2c..94f75f108 100644 --- a/embassy-net/src/dns.rs +++ b/embassy-net/src/dns.rs | |||
| @@ -88,6 +88,7 @@ where | |||
| 88 | let addrs = self.query(host, qtype).await?; | 88 | let addrs = self.query(host, qtype).await?; |
| 89 | if let Some(first) = addrs.get(0) { | 89 | if let Some(first) = addrs.get(0) { |
| 90 | Ok(match first { | 90 | Ok(match first { |
| 91 | #[cfg(feature = "proto-ipv4")] | ||
| 91 | IpAddress::Ipv4(addr) => IpAddr::V4(addr.0.into()), | 92 | IpAddress::Ipv4(addr) => IpAddr::V4(addr.0.into()), |
| 92 | #[cfg(feature = "proto-ipv6")] | 93 | #[cfg(feature = "proto-ipv6")] |
| 93 | IpAddress::Ipv6(addr) => IpAddr::V6(addr.0.into()), | 94 | IpAddress::Ipv6(addr) => IpAddr::V6(addr.0.into()), |
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index ddb325c6c..2e713cd10 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs | |||
| @@ -34,7 +34,9 @@ use smoltcp::socket::dhcpv4::{self, RetryConfig}; | |||
| 34 | pub use smoltcp::wire::IpListenEndpoint; | 34 | pub use smoltcp::wire::IpListenEndpoint; |
| 35 | #[cfg(feature = "medium-ethernet")] | 35 | #[cfg(feature = "medium-ethernet")] |
| 36 | pub use smoltcp::wire::{EthernetAddress, HardwareAddress}; | 36 | pub use smoltcp::wire::{EthernetAddress, HardwareAddress}; |
| 37 | pub use smoltcp::wire::{IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}; | 37 | pub use smoltcp::wire::{IpAddress, IpCidr}; |
| 38 | #[cfg(feature = "proto-ipv4")] | ||
| 39 | pub use smoltcp::wire::{Ipv4Address, Ipv4Cidr}; | ||
| 38 | #[cfg(feature = "proto-ipv6")] | 40 | #[cfg(feature = "proto-ipv6")] |
| 39 | pub use smoltcp::wire::{Ipv6Address, Ipv6Cidr}; | 41 | pub use smoltcp::wire::{Ipv6Address, Ipv6Cidr}; |
| 40 | 42 | ||
| @@ -67,6 +69,7 @@ impl<const SOCK: usize> StackResources<SOCK> { | |||
| 67 | } | 69 | } |
| 68 | 70 | ||
| 69 | /// Static IP address configuration. | 71 | /// Static IP address configuration. |
| 72 | #[cfg(feature = "proto-ipv4")] | ||
| 70 | #[derive(Debug, Clone, PartialEq, Eq)] | 73 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 71 | pub struct StaticConfigV4 { | 74 | pub struct StaticConfigV4 { |
| 72 | /// IP address and subnet mask. | 75 | /// IP address and subnet mask. |
| @@ -114,7 +117,8 @@ impl Default for DhcpConfig { | |||
| 114 | /// Network stack configuration. | 117 | /// Network stack configuration. |
| 115 | pub enum Config { | 118 | pub enum Config { |
| 116 | /// Use a static IP address configuration. | 119 | /// Use a static IP address configuration. |
| 117 | Static(StaticConfigV4), | 120 | #[cfg(feature = "proto-ipv4")] |
| 121 | StaticV4(StaticConfigV4), | ||
| 118 | /// Use DHCP to obtain an IP address configuration. | 122 | /// Use DHCP to obtain an IP address configuration. |
| 119 | #[cfg(feature = "dhcpv4")] | 123 | #[cfg(feature = "dhcpv4")] |
| 120 | Dhcp(DhcpConfig), | 124 | Dhcp(DhcpConfig), |
| @@ -131,7 +135,8 @@ pub struct Stack<D: Driver> { | |||
| 131 | struct Inner<D: Driver> { | 135 | struct Inner<D: Driver> { |
| 132 | device: D, | 136 | device: D, |
| 133 | link_up: bool, | 137 | link_up: bool, |
| 134 | config: Option<StaticConfigV4>, | 138 | #[cfg(feature = "proto-ipv4")] |
| 139 | static_v4: Option<StaticConfigV4>, | ||
| 135 | #[cfg(feature = "dhcpv4")] | 140 | #[cfg(feature = "dhcpv4")] |
| 136 | dhcp_socket: Option<SocketHandle>, | 141 | dhcp_socket: Option<SocketHandle>, |
| 137 | #[cfg(feature = "dns")] | 142 | #[cfg(feature = "dns")] |
| @@ -187,7 +192,8 @@ impl<D: Driver + 'static> Stack<D> { | |||
| 187 | let mut inner = Inner { | 192 | let mut inner = Inner { |
| 188 | device, | 193 | device, |
| 189 | link_up: false, | 194 | link_up: false, |
| 190 | config: None, | 195 | #[cfg(feature = "proto-ipv4")] |
| 196 | static_v4: None, | ||
| 191 | #[cfg(feature = "dhcpv4")] | 197 | #[cfg(feature = "dhcpv4")] |
| 192 | dhcp_socket: None, | 198 | dhcp_socket: None, |
| 193 | #[cfg(feature = "dns")] | 199 | #[cfg(feature = "dns")] |
| @@ -200,7 +206,8 @@ impl<D: Driver + 'static> Stack<D> { | |||
| 200 | }; | 206 | }; |
| 201 | 207 | ||
| 202 | match config { | 208 | match config { |
| 203 | Config::Static(config) => { | 209 | #[cfg(feature = "proto-ipv4")] |
| 210 | Config::StaticV4(config) => { | ||
| 204 | inner.apply_config(&mut socket, config); | 211 | inner.apply_config(&mut socket, config); |
| 205 | } | 212 | } |
| 206 | #[cfg(feature = "dhcpv4")] | 213 | #[cfg(feature = "dhcpv4")] |
| @@ -239,12 +246,21 @@ impl<D: Driver + 'static> Stack<D> { | |||
| 239 | /// Get whether the network stack has a valid IP configuration. | 246 | /// Get whether the network stack has a valid IP configuration. |
| 240 | /// This is true if the network stack has a static IP configuration or if DHCP has completed | 247 | /// This is true if the network stack has a static IP configuration or if DHCP has completed |
| 241 | pub fn is_config_up(&self) -> bool { | 248 | pub fn is_config_up(&self) -> bool { |
| 242 | self.with(|_s, i| i.config.is_some()) | 249 | #[cfg(feature = "proto-ipv4")] |
| 250 | { | ||
| 251 | return self.config_v4().is_some(); | ||
| 252 | } | ||
| 253 | |||
| 254 | #[cfg(not(any(feature = "proto-ipv4")))] | ||
| 255 | { | ||
| 256 | false | ||
| 257 | } | ||
| 243 | } | 258 | } |
| 244 | 259 | ||
| 245 | /// Get the current IP configuration. | 260 | /// Get the current IP configuration. |
| 246 | pub fn config(&self) -> Option<StaticConfigV4> { | 261 | #[cfg(feature = "proto-ipv4")] |
| 247 | self.with(|_s, i| i.config.clone()) | 262 | pub fn config_v4(&self) -> Option<StaticConfigV4> { |
| 263 | self.with(|_s, i| i.static_v4.clone()) | ||
| 248 | } | 264 | } |
| 249 | 265 | ||
| 250 | /// Run the network stack. | 266 | /// Run the network stack. |
| @@ -264,6 +280,7 @@ impl<D: Driver + 'static> Stack<D> { | |||
| 264 | pub async fn dns_query(&self, name: &str, qtype: dns::DnsQueryType) -> Result<Vec<IpAddress, 1>, dns::Error> { | 280 | pub async fn dns_query(&self, name: &str, qtype: dns::DnsQueryType) -> Result<Vec<IpAddress, 1>, dns::Error> { |
| 265 | // For A and AAAA queries we try detect whether `name` is just an IP address | 281 | // For A and AAAA queries we try detect whether `name` is just an IP address |
| 266 | match qtype { | 282 | match qtype { |
| 283 | #[cfg(feature = "proto-ipv4")] | ||
| 267 | dns::DnsQueryType::A => { | 284 | dns::DnsQueryType::A => { |
| 268 | if let Ok(ip) = name.parse().map(IpAddress::Ipv4) { | 285 | if let Ok(ip) = name.parse().map(IpAddress::Ipv4) { |
| 269 | return Ok([ip].into_iter().collect()); | 286 | return Ok([ip].into_iter().collect()); |
| @@ -374,6 +391,7 @@ impl SocketStack { | |||
| 374 | } | 391 | } |
| 375 | 392 | ||
| 376 | impl<D: Driver + 'static> Inner<D> { | 393 | impl<D: Driver + 'static> Inner<D> { |
| 394 | #[cfg(feature = "proto-ipv4")] | ||
| 377 | fn apply_config(&mut self, s: &mut SocketStack, config: StaticConfigV4) { | 395 | fn apply_config(&mut self, s: &mut SocketStack, config: StaticConfigV4) { |
| 378 | #[cfg(feature = "medium-ethernet")] | 396 | #[cfg(feature = "medium-ethernet")] |
| 379 | let medium = self.device.capabilities().medium; | 397 | let medium = self.device.capabilities().medium; |
| @@ -410,7 +428,7 @@ impl<D: Driver + 'static> Inner<D> { | |||
| 410 | socket.update_servers(&servers[..]); | 428 | socket.update_servers(&servers[..]); |
| 411 | } | 429 | } |
| 412 | 430 | ||
| 413 | self.config = Some(config) | 431 | self.static_v4 = Some(config) |
| 414 | } | 432 | } |
| 415 | 433 | ||
| 416 | #[cfg(feature = "dhcpv4")] | 434 | #[cfg(feature = "dhcpv4")] |
| @@ -430,9 +448,15 @@ impl<D: Driver + 'static> Inner<D> { | |||
| 430 | s.iface.update_ip_addrs(|ip_addrs| ip_addrs.clear()); | 448 | s.iface.update_ip_addrs(|ip_addrs| ip_addrs.clear()); |
| 431 | #[cfg(feature = "medium-ethernet")] | 449 | #[cfg(feature = "medium-ethernet")] |
| 432 | if medium == Medium::Ethernet { | 450 | if medium == Medium::Ethernet { |
| 433 | s.iface.routes_mut().remove_default_ipv4_route(); | 451 | #[cfg(feature = "proto-ipv4")] |
| 452 | { | ||
| 453 | s.iface.routes_mut().remove_default_ipv4_route(); | ||
| 454 | } | ||
| 455 | } | ||
| 456 | #[cfg(feature = "proto-ipv4")] | ||
| 457 | { | ||
| 458 | self.static_v4 = None | ||
| 434 | } | 459 | } |
| 435 | self.config = None | ||
| 436 | } | 460 | } |
| 437 | 461 | ||
| 438 | fn poll(&mut self, cx: &mut Context<'_>, s: &mut SocketStack) { | 462 | fn poll(&mut self, cx: &mut Context<'_>, s: &mut SocketStack) { |
diff --git a/embassy-net/src/tcp.rs b/embassy-net/src/tcp.rs index 7babb5293..52fee6883 100644 --- a/embassy-net/src/tcp.rs +++ b/embassy-net/src/tcp.rs | |||
| @@ -472,7 +472,10 @@ pub mod client { | |||
| 472 | Self: 'a, | 472 | Self: 'a, |
| 473 | { | 473 | { |
| 474 | let addr: crate::IpAddress = match remote.ip() { | 474 | let addr: crate::IpAddress = match remote.ip() { |
| 475 | #[cfg(feature = "proto-ipv4")] | ||
| 475 | IpAddr::V4(addr) => crate::IpAddress::Ipv4(crate::Ipv4Address::from_bytes(&addr.octets())), | 476 | IpAddr::V4(addr) => crate::IpAddress::Ipv4(crate::Ipv4Address::from_bytes(&addr.octets())), |
| 477 | #[cfg(not(feature = "proto-ipv4"))] | ||
| 478 | IpAddr::V4(_) => panic!("ipv4 support not enabled"), | ||
| 476 | #[cfg(feature = "proto-ipv6")] | 479 | #[cfg(feature = "proto-ipv6")] |
| 477 | IpAddr::V6(addr) => crate::IpAddress::Ipv6(crate::Ipv6Address::from_bytes(&addr.octets())), | 480 | IpAddr::V6(addr) => crate::IpAddress::Ipv6(crate::Ipv6Address::from_bytes(&addr.octets())), |
| 478 | #[cfg(not(feature = "proto-ipv6"))] | 481 | #[cfg(not(feature = "proto-ipv6"))] |
