diff options
| author | Ruben De Smet <[email protected]> | 2023-06-07 12:04:15 +0200 |
|---|---|---|
| committer | Ruben De Smet <[email protected]> | 2023-06-07 13:18:19 +0200 |
| commit | 352f0b6c3823797576c36f417d6be40189bca5d5 (patch) | |
| tree | 4dc2111b5c797883756fd55329b6af1f02c1d1d5 /embassy-net/src/lib.rs | |
| parent | ca47af6978e85012ff70a98726fcb63ed985109d (diff) | |
net: Support dual stackĀ IP
Diffstat (limited to 'embassy-net/src/lib.rs')
| -rw-r--r-- | embassy-net/src/lib.rs | 75 |
1 files changed, 62 insertions, 13 deletions
diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index 4cc191d44..cf7ebad3f 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs | |||
| @@ -127,16 +127,61 @@ impl Default for DhcpConfig { | |||
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | /// Network stack configuration. | 129 | /// Network stack configuration. |
| 130 | pub enum Config { | 130 | pub struct Config { |
| 131 | /// Use a static IPv4 address configuration. | ||
| 132 | #[cfg(feature = "proto-ipv4")] | 131 | #[cfg(feature = "proto-ipv4")] |
| 133 | StaticV4(StaticConfigV4), | 132 | pub ipv4: ConfigV4, |
| 134 | /// Use a static IPv6 address configuration. | ||
| 135 | #[cfg(feature = "proto-ipv6")] | 133 | #[cfg(feature = "proto-ipv6")] |
| 136 | StaticV6(StaticConfigV6), | 134 | pub ipv6: ConfigV6, |
| 135 | } | ||
| 136 | |||
| 137 | impl Config { | ||
| 138 | #[cfg(feature = "proto-ipv4")] | ||
| 139 | pub fn ipv4_static(config: StaticConfigV4) -> Self { | ||
| 140 | Self { | ||
| 141 | ipv4: ConfigV4::Static(config), | ||
| 142 | #[cfg(feature = "proto-ipv6")] | ||
| 143 | ipv6: ConfigV6::None, | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | #[cfg(feature = "proto-ipv6")] | ||
| 148 | pub fn ipv6_static(config: StaticConfigV6) -> Self { | ||
| 149 | Self { | ||
| 150 | #[cfg(feature = "proto-ipv4")] | ||
| 151 | ipv4: ConfigV4::None, | ||
| 152 | ipv6: ConfigV6::Static(config), | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | #[cfg(feature = "dhcpv4")] | ||
| 157 | pub fn dhcpv4(config: DhcpConfig) -> Self { | ||
| 158 | Self { | ||
| 159 | ipv4: ConfigV4::Dhcp(config), | ||
| 160 | #[cfg(feature = "proto-ipv6")] | ||
| 161 | ipv6: ConfigV6::None, | ||
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | /// Network stack IPv4 configuration. | ||
| 167 | #[cfg(feature = "proto-ipv4")] | ||
| 168 | pub enum ConfigV4 { | ||
| 169 | /// Use a static IPv4 address configuration. | ||
| 170 | Static(StaticConfigV4), | ||
| 137 | /// Use DHCP to obtain an IP address configuration. | 171 | /// Use DHCP to obtain an IP address configuration. |
| 138 | #[cfg(feature = "dhcpv4")] | 172 | #[cfg(feature = "dhcpv4")] |
| 139 | Dhcp(DhcpConfig), | 173 | Dhcp(DhcpConfig), |
| 174 | /// Do not configure IPv6. | ||
| 175 | None, | ||
| 176 | } | ||
| 177 | |||
| 178 | /// Network stack IPv6 configuration. | ||
| 179 | #[cfg(feature = "proto-ipv6")] | ||
| 180 | pub enum ConfigV6 { | ||
| 181 | /// Use a static IPv6 address configuration. | ||
| 182 | Static(StaticConfigV6), | ||
| 183 | /// Do not configure IPv6. | ||
| 184 | None, | ||
| 140 | } | 185 | } |
| 141 | 186 | ||
| 142 | /// A network stack. | 187 | /// A network stack. |
| @@ -224,22 +269,26 @@ impl<D: Driver + 'static> Stack<D> { | |||
| 224 | dns_waker: WakerRegistration::new(), | 269 | dns_waker: WakerRegistration::new(), |
| 225 | }; | 270 | }; |
| 226 | 271 | ||
| 227 | match config { | 272 | #[cfg(feature = "proto-ipv4")] |
| 228 | #[cfg(feature = "proto-ipv4")] | 273 | match config.ipv4 { |
| 229 | Config::StaticV4(config) => { | 274 | ConfigV4::Static(config) => { |
| 230 | inner.apply_config_v4(&mut socket, config); | 275 | inner.apply_config_v4(&mut socket, config); |
| 231 | } | 276 | } |
| 232 | #[cfg(feature = "proto-ipv6")] | ||
| 233 | Config::StaticV6(config) => { | ||
| 234 | inner.apply_config_v6(&mut socket, config); | ||
| 235 | } | ||
| 236 | #[cfg(feature = "dhcpv4")] | 277 | #[cfg(feature = "dhcpv4")] |
| 237 | Config::Dhcp(config) => { | 278 | ConfigV4::Dhcp(config) => { |
| 238 | let mut dhcp_socket = smoltcp::socket::dhcpv4::Socket::new(); | 279 | let mut dhcp_socket = smoltcp::socket::dhcpv4::Socket::new(); |
| 239 | inner.apply_dhcp_config(&mut dhcp_socket, config); | 280 | inner.apply_dhcp_config(&mut dhcp_socket, config); |
| 240 | let handle = socket.sockets.add(dhcp_socket); | 281 | let handle = socket.sockets.add(dhcp_socket); |
| 241 | inner.dhcp_socket = Some(handle); | 282 | inner.dhcp_socket = Some(handle); |
| 242 | } | 283 | } |
| 284 | ConfigV4::None => {} | ||
| 285 | } | ||
| 286 | #[cfg(feature = "proto-ipv6")] | ||
| 287 | match config.ipv6 { | ||
| 288 | ConfigV6::Static(config) => { | ||
| 289 | inner.apply_config_v6(&mut socket, config); | ||
| 290 | } | ||
| 291 | ConfigV6::None => {} | ||
| 243 | } | 292 | } |
| 244 | 293 | ||
| 245 | Self { | 294 | Self { |
