diff --git a/benches/bench.rs b/benches/bench.rs index a05ccff91..6e3ddd063 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -13,9 +13,9 @@ mod wire { extern crate test; #[cfg(feature = "proto-ipv6")] - const SRC_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)); + const SRC_ADDR: IpAddress = IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)); #[cfg(feature = "proto-ipv6")] - const DST_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 2)); + const DST_ADDR: IpAddress = IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 2)); #[cfg(all(not(feature = "proto-ipv6"), feature = "proto-ipv4"))] const SRC_ADDR: IpAddress = IpAddress::Ipv4(Ipv4Address::new(192, 168, 1, 1)); diff --git a/examples/benchmark.rs b/examples/benchmark.rs index be1959f6d..6ac431bf1 100644 --- a/examples/benchmark.rs +++ b/examples/benchmark.rs @@ -11,7 +11,7 @@ use smoltcp::iface::{Config, Interface, SocketSet}; use smoltcp::phy::{wait as phy_wait, Device, Medium}; use smoltcp::socket::tcp; use smoltcp::time::{Duration, Instant}; -use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr}; +use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address}; const AMOUNT: usize = 1_000_000_000; @@ -98,7 +98,10 @@ fn main() { let mut iface = Interface::new(config, &mut device, Instant::now()); iface.update_ip_addrs(|ip_addrs| { ip_addrs - .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)) + .push(IpCidr::new( + IpAddress::V4(Ipv4Address::new(192, 168, 69, 1)), + 24, + )) .unwrap(); }); diff --git a/examples/client.rs b/examples/client.rs index c18c08ff7..544176fc2 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -41,13 +41,22 @@ fn main() { let mut iface = Interface::new(config, &mut device, Instant::now()); iface.update_ip_addrs(|ip_addrs| { ip_addrs - .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)) + .push(IpCidr::new( + IpAddress::V4(Ipv4Address::new(192, 168, 69, 1)), + 24, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfdaa, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); }); iface diff --git a/examples/dns.rs b/examples/dns.rs index 977f40546..b50d8b6ef 100644 --- a/examples/dns.rs +++ b/examples/dns.rs @@ -36,13 +36,22 @@ fn main() { let mut iface = Interface::new(config, &mut device, Instant::now()); iface.update_ip_addrs(|ip_addrs| { ip_addrs - .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)) + .push(IpCidr::new( + IpAddress::V4(Ipv4Address::new(192, 168, 69, 1)), + 24, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfdaa, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); }); iface diff --git a/examples/httpclient.rs b/examples/httpclient.rs index 8f3a53aa7..c6827220c 100644 --- a/examples/httpclient.rs +++ b/examples/httpclient.rs @@ -41,13 +41,22 @@ fn main() { let mut iface = Interface::new(config, &mut device, Instant::now()); iface.update_ip_addrs(|ip_addrs| { ip_addrs - .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)) + .push(IpCidr::new( + IpAddress::V4(Ipv4Address::new(192, 168, 69, 1)), + 24, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfdaa, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); }); iface diff --git a/examples/loopback.rs b/examples/loopback.rs index 574ac9d0b..b552220bb 100644 --- a/examples/loopback.rs +++ b/examples/loopback.rs @@ -12,7 +12,7 @@ use smoltcp::iface::{Config, Interface, SocketSet}; use smoltcp::phy::{Device, Loopback, Medium}; use smoltcp::socket::tcp; use smoltcp::time::{Duration, Instant}; -use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr}; +use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address}; #[cfg(not(feature = "std"))] mod mock { @@ -93,7 +93,10 @@ fn main() { let mut iface = Interface::new(config, &mut device, Instant::now()); iface.update_ip_addrs(|ip_addrs| { ip_addrs - .push(IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8)) + .push(IpCidr::new( + IpAddress::V4(Ipv4Address::new(127, 0, 0, 1)), + 8, + )) .unwrap(); }); @@ -153,7 +156,11 @@ fn main() { if !did_connect { debug!("connecting"); socket - .connect(cx, (IpAddress::v4(127, 0, 0, 1), 1234), 65000) + .connect( + cx, + (IpAddress::V4(Ipv4Address::new(127, 0, 0, 1)), 1234), + 65000, + ) .unwrap(); did_connect = true; } diff --git a/examples/loopback_benchmark.rs b/examples/loopback_benchmark.rs index f49602ffc..402e23a56 100644 --- a/examples/loopback_benchmark.rs +++ b/examples/loopback_benchmark.rs @@ -6,7 +6,7 @@ use smoltcp::iface::{Config, Interface, SocketSet}; use smoltcp::phy::{Device, Loopback, Medium}; use smoltcp::socket::tcp; use smoltcp::time::Instant; -use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr}; +use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address}; fn main() { let device = Loopback::new(Medium::Ethernet); @@ -33,7 +33,10 @@ fn main() { let mut iface = Interface::new(config, &mut device, Instant::now()); iface.update_ip_addrs(|ip_addrs| { ip_addrs - .push(IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8)) + .push(IpCidr::new( + IpAddress::V4(Ipv4Address::new(127, 0, 0, 1)), + 8, + )) .unwrap(); }); @@ -81,7 +84,11 @@ fn main() { if !socket.is_open() && !did_connect { debug!("connecting"); socket - .connect(cx, (IpAddress::v4(127, 0, 0, 1), 1234), 65000) + .connect( + cx, + (IpAddress::V4(Ipv4Address::new(127, 0, 0, 1)), 1234), + 65000, + ) .unwrap(); did_connect = true; } diff --git a/examples/multicast.rs b/examples/multicast.rs index ab86a4bce..e60760e0a 100644 --- a/examples/multicast.rs +++ b/examples/multicast.rs @@ -7,8 +7,8 @@ use smoltcp::phy::{wait as phy_wait, Device, Medium}; use smoltcp::socket::{raw, udp}; use smoltcp::time::Instant; use smoltcp::wire::{ - EthernetAddress, IgmpPacket, IgmpRepr, IpAddress, IpCidr, IpProtocol, IpVersion, Ipv4Address, - Ipv4Packet, Ipv6Address, + EthernetAddress, IgmpPacket, IgmpRepr, IpAddress, IpCidr, IpProtocol, Ipv4Address, Ipv4Packet, + Ipv6Address, }; const MDNS_PORT: u16 = 5353; @@ -40,13 +40,22 @@ fn main() { let mut iface = Interface::new(config, &mut device, Instant::now()); iface.update_ip_addrs(|ip_addrs| { ip_addrs - .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)) + .push(IpCidr::new( + IpAddress::V4(Ipv4Address::new(192, 168, 69, 1)), + 24, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfdaa, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); }); iface @@ -65,12 +74,7 @@ fn main() { let raw_rx_buffer = raw::PacketBuffer::new(vec![raw::PacketMetadata::EMPTY; 2], vec![0; 512]); // Will not send IGMP let raw_tx_buffer = raw::PacketBuffer::new(vec![], vec![]); - let raw_socket = raw::Socket::new( - IpVersion::Ipv4, - IpProtocol::Igmp, - raw_rx_buffer, - raw_tx_buffer, - ); + let raw_socket = raw::Socket::new_v4(IpProtocol::Igmp, raw_rx_buffer, raw_tx_buffer); let raw_handle = sockets.add(raw_socket); // Must fit mDNS payload of at least one packet diff --git a/examples/ping.rs b/examples/ping.rs index 29c6bcd4c..62b8d4a9c 100644 --- a/examples/ping.rs +++ b/examples/ping.rs @@ -117,13 +117,22 @@ fn main() { let mut iface = Interface::new(config, &mut device, Instant::now()); iface.update_ip_addrs(|ip_addrs| { ip_addrs - .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)) + .push(IpCidr::new( + IpAddress::V4(Ipv4Address::new(192, 168, 69, 1)), + 24, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfdaa, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); }); iface @@ -164,7 +173,7 @@ fn main() { NetworkEndian::write_i64(&mut echo_payload, timestamp.total_millis()); match remote_addr { - IpAddress::Ipv4(_) => { + IpAddress::V4(_) => { let (icmp_repr, mut icmp_packet) = send_icmp_ping!( Icmpv4Repr, Icmpv4Packet, @@ -176,7 +185,7 @@ fn main() { ); icmp_repr.emit(&mut icmp_packet, &device_caps.checksum); } - IpAddress::Ipv6(address) => { + IpAddress::V6(address) => { let (icmp_repr, mut icmp_packet) = send_icmp_ping!( Icmpv6Repr, Icmpv6Packet, @@ -204,7 +213,7 @@ fn main() { let (payload, _) = socket.recv().unwrap(); match remote_addr { - IpAddress::Ipv4(_) => { + IpAddress::V4(_) => { let icmp_packet = Icmpv4Packet::new_checked(&payload).unwrap(); let icmp_repr = Icmpv4Repr::parse(&icmp_packet, &device_caps.checksum).unwrap(); get_icmp_pong!( @@ -217,7 +226,7 @@ fn main() { received ); } - IpAddress::Ipv6(address) => { + IpAddress::V6(address) => { let icmp_packet = Icmpv6Packet::new_checked(&payload).unwrap(); let icmp_repr = Icmpv6Repr::parse( &address, diff --git a/examples/server.rs b/examples/server.rs index 33d95c5d5..77d101ea1 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -37,13 +37,22 @@ fn main() { let mut iface = Interface::new(config, &mut device, Instant::now()); iface.update_ip_addrs(|ip_addrs| { ip_addrs - .push(IpCidr::new(IpAddress::v4(192, 168, 69, 1), 24)) + .push(IpCidr::new( + IpAddress::V4(Ipv4Address::new(192, 168, 69, 1)), + 24, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfdaa, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfdaa, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); }); iface diff --git a/examples/sixlowpan.rs b/examples/sixlowpan.rs index 0d9ec21d8..989e4f32a 100644 --- a/examples/sixlowpan.rs +++ b/examples/sixlowpan.rs @@ -51,7 +51,9 @@ use smoltcp::phy::{wait as phy_wait, Device, Medium, RawSocket}; use smoltcp::socket::tcp; use smoltcp::socket::udp; use smoltcp::time::Instant; -use smoltcp::wire::{EthernetAddress, Ieee802154Address, Ieee802154Pan, IpAddress, IpCidr}; +use smoltcp::wire::{ + EthernetAddress, Ieee802154Address, Ieee802154Pan, IpAddress, IpCidr, Ipv6Address, +}; fn main() { utils::setup_logging(""); @@ -83,7 +85,9 @@ fn main() { iface.update_ip_addrs(|ip_addrs| { ip_addrs .push(IpCidr::new( - IpAddress::v6(0xfe80, 0, 0, 0, 0x180b, 0x4242, 0x4242, 0x4242), + IpAddress::V6(Ipv6Address::new( + 0xfe80, 0, 0, 0, 0x180b, 0x4242, 0x4242, 0x4242, + )), 64, )) .unwrap(); diff --git a/examples/sixlowpan_benchmark.rs b/examples/sixlowpan_benchmark.rs index 4e61491fe..74519a3eb 100644 --- a/examples/sixlowpan_benchmark.rs +++ b/examples/sixlowpan_benchmark.rs @@ -49,7 +49,9 @@ use std::str; use smoltcp::iface::{Config, Interface, SocketSet}; use smoltcp::phy::{wait as phy_wait, Device, Medium, RawSocket}; use smoltcp::socket::tcp; -use smoltcp::wire::{EthernetAddress, Ieee802154Address, Ieee802154Pan, IpAddress, IpCidr}; +use smoltcp::wire::{ + EthernetAddress, Ieee802154Address, Ieee802154Pan, IpAddress, IpCidr, Ipv6Address, +}; //For benchmark use smoltcp::time::{Duration, Instant}; @@ -163,7 +165,9 @@ fn main() { iface.update_ip_addrs(|ip_addrs| { ip_addrs .push(IpCidr::new( - IpAddress::v6(0xfe80, 0, 0, 0, 0x180b, 0x4242, 0x4242, 0x4242), + IpAddress::V6(Ipv6Address::new( + 0xfe80, 0, 0, 0, 0x180b, 0x4242, 0x4242, 0x4242, + )), 64, )) .unwrap(); diff --git a/src/iface/interface/ipv4.rs b/src/iface/interface/ipv4.rs index 7fda3ae3b..05bbefc59 100644 --- a/src/iface/interface/ipv4.rs +++ b/src/iface/interface/ipv4.rs @@ -197,7 +197,7 @@ impl InterfaceInner { if self .routes - .lookup(&IpAddress::Ipv4(ipv4_repr.dst_addr), self.now) + .lookup(&IpAddress::V4(ipv4_repr.dst_addr), self.now) .map_or(true, |router_addr| !self.has_ip_addr(router_addr)) { net_trace!("Rejecting IPv4 packet; no matching routes"); @@ -209,7 +209,7 @@ impl InterfaceInner { #[cfg(feature = "medium-ethernet")] if self.is_unicast_v4(ipv4_repr.dst_addr) { self.neighbor_cache.reset_expiry_if_existing( - IpAddress::Ipv4(ipv4_repr.src_addr), + IpAddress::V4(ipv4_repr.src_addr), source_hardware_addr, self.now, ); @@ -279,7 +279,7 @@ impl InterfaceInner { return None; } - if !self.in_same_network(&IpAddress::Ipv4(source_protocol_addr)) { + if !self.in_same_network(&IpAddress::V4(source_protocol_addr)) { net_debug!("arp: source IP address not in same network as us"); return None; } @@ -437,11 +437,11 @@ impl InterfaceInner { frame.set_src_addr(src_addr); frame.set_dst_addr(frag.ipv4.dst_hardware_addr); - match repr.version() { + match repr { #[cfg(feature = "proto-ipv4")] - IpVersion::Ipv4 => frame.set_ethertype(EthernetProtocol::Ipv4), + IpRepr::Ipv4(_) => frame.set_ethertype(EthernetProtocol::Ipv4), #[cfg(feature = "proto-ipv6")] - IpVersion::Ipv6 => frame.set_ethertype(EthernetProtocol::Ipv6), + IpRepr::Ipv6(_) => frame.set_ethertype(EthernetProtocol::Ipv6), } }; diff --git a/src/iface/interface/ipv6.rs b/src/iface/interface/ipv6.rs index 96e999f7e..14643c625 100644 --- a/src/iface/interface/ipv6.rs +++ b/src/iface/interface/ipv6.rs @@ -227,7 +227,7 @@ impl InterfaceInner { if self .routes - .lookup(&IpAddress::Ipv6(ipv6_repr.dst_addr), self.now) + .lookup(&IpAddress::V6(ipv6_repr.dst_addr), self.now) .map_or(true, |router_addr| !self.has_ip_addr(router_addr)) { net_trace!("Rejecting IPv6 packet; no matching routes"); @@ -244,7 +244,7 @@ impl InterfaceInner { #[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))] if ipv6_repr.dst_addr.x_is_unicast() { self.neighbor_cache.reset_expiry_if_existing( - IpAddress::Ipv6(ipv6_repr.src_addr), + IpAddress::V6(ipv6_repr.src_addr), source_hardware_addr, self.now, ); diff --git a/src/iface/interface/mod.rs b/src/iface/interface/mod.rs index cfac7f205..778f5a77c 100644 --- a/src/iface/interface/mod.rs +++ b/src/iface/interface/mod.rs @@ -786,9 +786,9 @@ impl InterfaceInner { pub(crate) fn get_source_address(&self, dst_addr: &IpAddress) -> Option { match dst_addr { #[cfg(feature = "proto-ipv4")] - IpAddress::Ipv4(addr) => self.get_source_address_ipv4(addr).map(|a| a.into()), + IpAddress::V4(addr) => self.get_source_address_ipv4(addr).map(|a| a.into()), #[cfg(feature = "proto-ipv6")] - IpAddress::Ipv6(addr) => Some(self.get_source_address_ipv6(addr).into()), + IpAddress::V6(addr) => Some(self.get_source_address_ipv6(addr).into()), } } @@ -807,7 +807,7 @@ impl InterfaceInner { fn check_ip_addrs(addrs: &[IpCidr]) { for cidr in addrs { - if !cidr.address().is_unicast() && !cidr.address().is_unspecified() { + if !cidr.address().x_is_unicast() && !cidr.address().is_unspecified() { panic!("IP address {} is not unicast", cidr.address()) } } @@ -830,13 +830,11 @@ impl InterfaceInner { match addr { #[cfg(feature = "proto-ipv4")] - IpAddress::Ipv4(key) => key == IPV4_MULTICAST_ALL_SYSTEMS, + IpAddress::V4(key) => key == IPV4_MULTICAST_ALL_SYSTEMS, #[cfg(feature = "proto-rpl")] - IpAddress::Ipv6(IPV6_LINK_LOCAL_ALL_RPL_NODES) => true, + IpAddress::V6(IPV6_LINK_LOCAL_ALL_RPL_NODES) => true, #[cfg(feature = "proto-ipv6")] - IpAddress::Ipv6(key) => { - key == IPV6_LINK_LOCAL_ALL_NODES || self.has_solicited_node(key) - } + IpAddress::V6(key) => key == IPV6_LINK_LOCAL_ALL_NODES || self.has_solicited_node(key), #[allow(unreachable_patterns)] _ => false, } @@ -893,9 +891,9 @@ impl InterfaceInner { pub(crate) fn is_broadcast(&self, address: &IpAddress) -> bool { match address { #[cfg(feature = "proto-ipv4")] - IpAddress::Ipv4(address) => self.is_broadcast_v4(*address), + IpAddress::V4(address) => self.is_broadcast_v4(*address), #[cfg(feature = "proto-ipv6")] - IpAddress::Ipv6(_) => false, + IpAddress::V6(_) => false, } } @@ -989,7 +987,7 @@ impl InterfaceInner { if dst_addr.is_multicast() { let hardware_addr = match *dst_addr { #[cfg(feature = "proto-ipv4")] - IpAddress::Ipv4(addr) => match self.caps.medium { + IpAddress::V4(addr) => match self.caps.medium { #[cfg(feature = "medium-ethernet")] Medium::Ethernet => { let b = addr.octets(); @@ -1008,7 +1006,7 @@ impl InterfaceInner { Medium::Ip => unreachable!(), }, #[cfg(feature = "proto-ipv6")] - IpAddress::Ipv6(addr) => match self.caps.medium { + IpAddress::V6(addr) => match self.caps.medium { #[cfg(feature = "medium-ethernet")] Medium::Ethernet => { let b = addr.octets(); @@ -1041,7 +1039,7 @@ impl InterfaceInner { match dst_addr { #[cfg(all(feature = "medium-ethernet", feature = "proto-ipv4"))] - IpAddress::Ipv4(dst_addr) if matches!(self.caps.medium, Medium::Ethernet) => { + IpAddress::V4(dst_addr) if matches!(self.caps.medium, Medium::Ethernet) => { net_debug!( "address {} not in neighbor cache, sending ARP request", dst_addr @@ -1072,7 +1070,7 @@ impl InterfaceInner { } #[cfg(feature = "proto-ipv6")] - IpAddress::Ipv6(dst_addr) => { + IpAddress::V6(dst_addr) => { net_debug!( "address {} not in neighbor cache, sending Neighbor Solicitation", dst_addr @@ -1177,11 +1175,11 @@ impl InterfaceInner { frame.set_src_addr(src_addr); frame.set_dst_addr(dst_hardware_addr); - match repr.version() { + match repr { #[cfg(feature = "proto-ipv4")] - IpVersion::Ipv4 => frame.set_ethertype(EthernetProtocol::Ipv4), + IpRepr::Ipv4(_) => frame.set_ethertype(EthernetProtocol::Ipv4), #[cfg(feature = "proto-ipv6")] - IpVersion::Ipv6 => frame.set_ethertype(EthernetProtocol::Ipv6), + IpRepr::Ipv6(_) => frame.set_ethertype(EthernetProtocol::Ipv6), } Ok(()) diff --git a/src/iface/interface/multicast.rs b/src/iface/interface/multicast.rs index 68b1c7750..c3589595b 100644 --- a/src/iface/interface/multicast.rs +++ b/src/iface/interface/multicast.rs @@ -156,7 +156,7 @@ impl Interface { { match addr { #[cfg(feature = "proto-ipv4")] - IpAddress::Ipv4(addr) => { + IpAddress::V4(addr) => { if let Some(pkt) = self.inner.igmp_report_packet(IgmpVersion::Version2, addr) { let Some(tx_token) = device.transmit(self.inner.now) else { break; @@ -169,7 +169,7 @@ impl Interface { } } #[cfg(feature = "proto-ipv6")] - IpAddress::Ipv6(addr) => { + IpAddress::V6(addr) => { if let Some(pkt) = self.inner.mldv2_report_packet(&[MldAddressRecordRepr::new( MldRecordType::ChangeToInclude, addr, @@ -204,7 +204,7 @@ impl Interface { { match addr { #[cfg(feature = "proto-ipv4")] - IpAddress::Ipv4(addr) => { + IpAddress::V4(addr) => { if let Some(pkt) = self.inner.igmp_leave_packet(addr) { let Some(tx_token) = device.transmit(self.inner.now) else { break; @@ -217,7 +217,7 @@ impl Interface { } } #[cfg(feature = "proto-ipv6")] - IpAddress::Ipv6(addr) => { + IpAddress::V6(addr) => { if let Some(pkt) = self.inner.mldv2_report_packet(&[MldAddressRecordRepr::new( MldRecordType::ChangeToExclude, addr, @@ -267,7 +267,7 @@ impl Interface { .groups .iter() .filter_map(|(addr, _)| match addr { - IpAddress::Ipv4(addr) => Some(*addr), + IpAddress::V4(addr) => Some(*addr), #[allow(unreachable_patterns)] _ => None, }) @@ -339,7 +339,7 @@ impl InterfaceInner { .multicast .groups .keys() - .filter(|a| matches!(a, IpAddress::Ipv4(_))) + .filter(|a| matches!(a, IpAddress::V4(_))) .count(); // Are we member in any groups? diff --git a/src/iface/interface/tests/ipv4.rs b/src/iface/interface/tests/ipv4.rs index d3b8c61e8..aff055520 100644 --- a/src/iface/interface/tests/ipv4.rs +++ b/src/iface/interface/tests/ipv4.rs @@ -322,7 +322,7 @@ fn test_icmp_error_port_unreachable(#[case] medium: Medium) { udp_repr.emit( &mut packet_broadcast, &ip_repr.src_addr(), - &IpAddress::Ipv4(Ipv4Address::BROADCAST), + &IpAddress::V4(Ipv4Address::BROADCAST), UDP_PAYLOAD.len(), |buf| buf.copy_from_slice(&UDP_PAYLOAD), &ChecksumCapabilities::default(), @@ -464,7 +464,7 @@ fn test_handle_valid_arp_request(#[case] medium: Medium) { assert_eq!( iface.inner.lookup_hardware_addr( MockTxToken, - &IpAddress::Ipv4(remote_ip_addr), + &IpAddress::V4(remote_ip_addr), &mut iface.fragmenter, ), Ok((HardwareAddress::Ethernet(remote_hw_addr), MockTxToken)) @@ -512,7 +512,7 @@ fn test_handle_other_arp_request(#[case] medium: Medium) { assert_eq!( iface.inner.lookup_hardware_addr( MockTxToken, - &IpAddress::Ipv4(remote_ip_addr), + &IpAddress::V4(remote_ip_addr), &mut iface.fragmenter, ), Err(DispatchError::NeighborPending) @@ -570,7 +570,7 @@ fn test_arp_flush_after_update_ip(#[case] medium: Medium) { assert_eq!( iface.inner.lookup_hardware_addr( MockTxToken, - &IpAddress::Ipv4(remote_ip_addr), + &IpAddress::V4(remote_ip_addr), &mut iface.fragmenter, ), Ok((HardwareAddress::Ethernet(remote_hw_addr), MockTxToken)) @@ -585,7 +585,7 @@ fn test_arp_flush_after_update_ip(#[case] medium: Medium) { }); // ARP cache flush after address change - assert!(!iface.inner.has_neighbor(&IpAddress::Ipv4(remote_ip_addr))); + assert!(!iface.inner.has_neighbor(&IpAddress::V4(remote_ip_addr))); } #[rstest] @@ -660,7 +660,7 @@ fn test_icmpv4_socket(#[case] medium: Medium) { socket.recv(), Ok(( icmp_data, - IpAddress::Ipv4(Ipv4Address::new(0x7f, 0x00, 0x00, 0x02)) + IpAddress::V4(Ipv4Address::new(0x7f, 0x00, 0x00, 0x02)) )) ); } @@ -765,7 +765,7 @@ fn test_handle_igmp(#[case] medium: Medium) { #[case(Medium::Ethernet)] #[cfg(all(feature = "socket-raw", feature = "medium-ethernet"))] fn test_raw_socket_no_reply(#[case] medium: Medium) { - use crate::wire::{IpVersion, UdpPacket, UdpRepr}; + use crate::wire::{UdpPacket, UdpRepr}; let (mut iface, mut sockets, _) = setup(medium); @@ -776,7 +776,7 @@ fn test_raw_socket_no_reply(#[case] medium: Medium) { vec![raw::PacketMetadata::EMPTY; packets], vec![0; 48 * packets], ); - let raw_socket = raw::Socket::new(IpVersion::Ipv4, IpProtocol::Udp, rx_buffer, tx_buffer); + let raw_socket = raw::Socket::new_v4(IpProtocol::Udp, rx_buffer, tx_buffer); sockets.add(raw_socket); let src_addr = Ipv4Address::new(127, 0, 0, 2); @@ -847,7 +847,7 @@ fn test_raw_socket_no_reply(#[case] medium: Medium) { ))] fn test_raw_socket_with_udp_socket(#[case] medium: Medium) { use crate::socket::udp; - use crate::wire::{IpEndpoint, IpVersion, UdpPacket, UdpRepr}; + use crate::wire::{IpEndpoint, UdpPacket, UdpRepr}; static UDP_PAYLOAD: [u8; 5] = [0x48, 0x65, 0x6c, 0x6c, 0x6f]; @@ -871,12 +871,7 @@ fn test_raw_socket_with_udp_socket(#[case] medium: Medium) { vec![raw::PacketMetadata::EMPTY; packets], vec![0; 48 * packets], ); - let raw_socket = raw::Socket::new( - IpVersion::Ipv4, - IpProtocol::Udp, - raw_rx_buffer, - raw_tx_buffer, - ); + let raw_socket = raw::Socket::new_v4(IpProtocol::Udp, raw_rx_buffer, raw_tx_buffer); sockets.add(raw_socket); let src_addr = Ipv4Address::new(127, 0, 0, 2); diff --git a/src/iface/interface/tests/ipv6.rs b/src/iface/interface/tests/ipv6.rs index adc058fd3..5944e92d9 100644 --- a/src/iface/interface/tests/ipv6.rs +++ b/src/iface/interface/tests/ipv6.rs @@ -73,7 +73,7 @@ fn any_ip(#[case] medium: Medium) { Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 0), 64, )), - via_router: IpAddress::Ipv6(Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 0x0001)), + via_router: IpAddress::V6(Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 0x0001)), preferred_until: None, expires_at: None, }) @@ -644,7 +644,7 @@ fn ndsic_neighbor_advertisement_ethernet(#[case] medium: Medium) { assert_eq!( iface.inner.neighbor_cache.lookup( - &IpAddress::Ipv6(Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 0x0002)), + &IpAddress::V6(Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 0x0002)), iface.inner.now, ), NeighborAnswer::Found(HardwareAddress::Ethernet(EthernetAddress::from_bytes(&[ @@ -701,7 +701,7 @@ fn ndsic_neighbor_advertisement_ethernet_multicast_addr(#[case] medium: Medium) assert_eq!( iface.inner.neighbor_cache.lookup( - &IpAddress::Ipv6(Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 0x0002)), + &IpAddress::V6(Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 0x0002)), iface.inner.now, ), NeighborAnswer::NotFound, @@ -754,7 +754,7 @@ fn ndsic_neighbor_advertisement_ieee802154(#[case] medium: Medium) { assert_eq!( iface.inner.neighbor_cache.lookup( - &IpAddress::Ipv6(Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 0x0002)), + &IpAddress::V6(Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 0x0002)), iface.inner.now, ), NeighborAnswer::Found(HardwareAddress::Ieee802154(Ieee802154Address::from_bytes( @@ -832,7 +832,7 @@ fn test_handle_valid_ndisc_request(#[case] medium: Medium) { assert_eq!( iface.inner.lookup_hardware_addr( MockTxToken, - &IpAddress::Ipv6(remote_ip_addr), + &IpAddress::V6(remote_ip_addr), &mut iface.fragmenter, ), Ok((HardwareAddress::Ethernet(remote_hw_addr), MockTxToken)) @@ -850,11 +850,14 @@ fn test_solicited_node_addrs(#[case] medium: Medium) { let (mut iface, _, _) = setup(medium); let mut new_addrs = heapless::Vec::::new(); new_addrs - .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 1, 2, 0, 2), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 1, 2, 0, 2)), + 64, + )) .unwrap(); new_addrs .push(IpCidr::new( - IpAddress::v6(0xfe80, 0, 0, 0, 3, 4, 0, 0xffff), + IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 3, 4, 0, 0xffff)), 64, )) .unwrap(); diff --git a/src/iface/interface/tests/sixlowpan.rs b/src/iface/interface/tests/sixlowpan.rs index 56e1fb8ca..35ccd6e5e 100644 --- a/src/iface/interface/tests/sixlowpan.rs +++ b/src/iface/interface/tests/sixlowpan.rs @@ -215,7 +215,7 @@ fn test_echo_request_sixlowpan_128_bytes() { ); iface.inner.neighbor_cache.fill( - IpAddress::Ipv6(Ipv6Address::new( + IpAddress::V6(Ipv6Address::new( 0xfe80, 0, 0, 0, 0x4042, 0x4242, 0x4242, 0x0b1a, )), HardwareAddress::Ieee802154(Ieee802154Address::default()), @@ -361,7 +361,7 @@ In at rhoncus tortor. Cras blandit tellus diam, varius vestibulum nibh commodo n Ipv6Address::new(0xfe80, 0, 0, 0, 0x92fc, 0x48c2, 0xa441, 0xfc76).into() ), ..IpEndpoint { - addr: IpAddress::Ipv6(Ipv6Address::new( + addr: IpAddress::V6(Ipv6Address::new( 0xfe80, 0, 0, 0, 0x4042, 0x4242, 0x4242, 0x0b1a )), port: 54217, diff --git a/src/iface/neighbor.rs b/src/iface/neighbor.rs index dcef2bb87..6f9c2ccc0 100644 --- a/src/iface/neighbor.rs +++ b/src/iface/neighbor.rs @@ -5,7 +5,7 @@ use heapless::LinearMap; use crate::config::IFACE_NEIGHBOR_CACHE_COUNT; use crate::time::{Duration, Instant}; -use crate::wire::{HardwareAddress, IpAddress}; +use crate::wire::{HardwareAddress, IpAddress, IpAddressExt}; /// A cached neighbor. /// @@ -86,7 +86,7 @@ impl Cache { hardware_addr: HardwareAddress, timestamp: Instant, ) { - debug_assert!(protocol_addr.is_unicast()); + debug_assert!(protocol_addr.x_is_unicast()); debug_assert!(hardware_addr.is_unicast()); let expires_at = timestamp + Self::ENTRY_LIFETIME; @@ -99,7 +99,7 @@ impl Cache { hardware_addr: HardwareAddress, expires_at: Instant, ) { - debug_assert!(protocol_addr.is_unicast()); + debug_assert!(protocol_addr.x_is_unicast()); debug_assert!(hardware_addr.is_unicast()); let neighbor = Neighbor { @@ -148,7 +148,7 @@ impl Cache { } pub(crate) fn lookup(&self, protocol_addr: &IpAddress, timestamp: Instant) -> Answer { - assert!(protocol_addr.is_unicast()); + assert!(protocol_addr.x_is_unicast()); if let Some(&Neighbor { expires_at, diff --git a/src/iface/route.rs b/src/iface/route.rs index f14a57d39..8baf72b68 100644 --- a/src/iface/route.rs +++ b/src/iface/route.rs @@ -2,7 +2,7 @@ use heapless::Vec; use crate::config::IFACE_MAX_ROUTE_COUNT; use crate::time::Instant; -use crate::wire::{IpAddress, IpCidr}; +use crate::wire::{IpAddress, IpAddressExt, IpCidr}; #[cfg(feature = "proto-ipv4")] use crate::wire::{Ipv4Address, Ipv4Cidr}; #[cfg(feature = "proto-ipv6")] @@ -147,7 +147,7 @@ impl Routes { } pub(crate) fn lookup(&self, addr: &IpAddress, timestamp: Instant) -> Option { - assert!(addr.is_unicast()); + assert!(addr.x_is_unicast()); self.storage .iter() diff --git a/src/parsers.rs b/src/parsers.rs index 99ce28453..fd45c114b 100644 --- a/src/parsers.rs +++ b/src/parsers.rs @@ -297,24 +297,6 @@ impl<'a> Parser<'a> { Ok(Ipv4Address::from_bytes(&octets)) } - fn accept_ip(&mut self) -> Result { - #[cfg(feature = "proto-ipv4")] - #[allow(clippy::single_match)] - match self.try_do(|p| p.accept_ipv4()) { - Some(ipv4) => return Ok(IpAddress::Ipv4(ipv4)), - None => (), - } - - #[cfg(feature = "proto-ipv6")] - #[allow(clippy::single_match)] - match self.try_do(|p| p.accept_ipv6()) { - Some(ipv6) => return Ok(IpAddress::Ipv6(ipv6)), - None => (), - } - - Err(()) - } - #[cfg(feature = "proto-ipv4")] fn accept_ipv4_endpoint(&mut self) -> Result { let ip = self.accept_ipv4()?; @@ -327,7 +309,7 @@ impl<'a> Parser<'a> { }; Ok(IpEndpoint { - addr: IpAddress::Ipv4(ip), + addr: IpAddress::V4(ip), port: port as u16, }) } @@ -342,13 +324,13 @@ impl<'a> Parser<'a> { let port = self.accept_number(5, 65535, false)?; Ok(IpEndpoint { - addr: IpAddress::Ipv6(ip), + addr: IpAddress::V6(ip), port: port as u16, }) } else { let ip = self.accept_ipv6()?; Ok(IpEndpoint { - addr: IpAddress::Ipv6(ip), + addr: IpAddress::V6(ip), port: 0, }) } @@ -383,15 +365,6 @@ impl FromStr for EthernetAddress { } } -impl FromStr for IpAddress { - type Err = (); - - /// Parse a string representation of an IP address. - fn from_str(s: &str) -> Result { - Parser::new(s).until_eof(|p| p.accept_ip()) - } -} - #[cfg(feature = "proto-ipv4")] impl FromStr for Ipv4Cidr { type Err = (); @@ -509,25 +482,23 @@ mod test { #[test] #[cfg(feature = "proto-ipv4")] fn test_ip_ipv4() { - assert_eq!(IpAddress::from_str(""), Err(())); + assert!(IpAddress::from_str("").is_err()); assert_eq!( IpAddress::from_str("1.2.3.4"), - Ok(IpAddress::Ipv4(Ipv4Address::new(1, 2, 3, 4))) + Ok(IpAddress::V4(Ipv4Address::new(1, 2, 3, 4))) ); - assert_eq!(IpAddress::from_str("x"), Err(())); + assert!(IpAddress::from_str("x").is_err()); } #[test] #[cfg(feature = "proto-ipv6")] fn test_ip_ipv6() { - assert_eq!(IpAddress::from_str(""), Err(())); + assert!(IpAddress::from_str("").is_err()); assert_eq!( IpAddress::from_str("fe80::1"), - Ok(IpAddress::Ipv6(Ipv6Address::new( - 0xfe80, 0, 0, 0, 0, 0, 0, 1 - ))) + Ok(IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1))) ); - assert_eq!(IpAddress::from_str("x"), Err(())); + assert!(IpAddress::from_str("x").is_err()); } #[test] @@ -604,14 +575,14 @@ mod test { assert_eq!( IpEndpoint::from_str("127.0.0.1"), Ok(IpEndpoint { - addr: IpAddress::v4(127, 0, 0, 1), + addr: IpAddress::V4(Ipv4Address::new(127, 0, 0, 1)), port: 0 }) ); assert_eq!( IpEndpoint::from_str("127.0.0.1:12345"), Ok(IpEndpoint { - addr: IpAddress::v4(127, 0, 0, 1), + addr: IpAddress::V4(Ipv4Address::new(127, 0, 0, 1)), port: 12345 }) ); @@ -625,21 +596,21 @@ mod test { assert_eq!( IpEndpoint::from_str("fe80::1"), Ok(IpEndpoint { - addr: IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), + addr: IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)), port: 0 }) ); assert_eq!( IpEndpoint::from_str("[fe80::1]:12345"), Ok(IpEndpoint { - addr: IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), + addr: IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)), port: 12345 }) ); assert_eq!( IpEndpoint::from_str("[::]:12345"), Ok(IpEndpoint { - addr: IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 0), + addr: IpAddress::V6(Ipv6Address::new(0, 0, 0, 0, 0, 0, 0, 0)), port: 12345 }) ); diff --git a/src/socket/dhcpv4.rs b/src/socket/dhcpv4.rs index 72980a946..28a1238f4 100644 --- a/src/socket/dhcpv4.rs +++ b/src/socket/dhcpv4.rs @@ -5,9 +5,9 @@ use crate::iface::Context; use crate::time::{Duration, Instant}; use crate::wire::dhcpv4::field as dhcpv4_field; use crate::wire::{ - DhcpMessageType, DhcpPacket, DhcpRepr, IpAddress, IpProtocol, Ipv4Address, Ipv4AddressExt, - Ipv4Cidr, Ipv4Repr, UdpRepr, DHCP_CLIENT_PORT, DHCP_MAX_DNS_SERVER_COUNT, DHCP_SERVER_PORT, - UDP_HEADER_LEN, + DhcpMessageType, DhcpPacket, DhcpRepr, IpAddress, IpAddressExt, IpProtocol, Ipv4Address, + Ipv4AddressExt, Ipv4Cidr, Ipv4Repr, UdpRepr, DHCP_CLIENT_PORT, DHCP_MAX_DNS_SERVER_COUNT, + DHCP_SERVER_PORT, UDP_HEADER_LEN, }; use crate::wire::{DhcpOption, HardwareAddress}; use heapless::Vec; @@ -454,7 +454,7 @@ impl<'a> Socket<'a> { } }; - let prefix_len = match IpAddress::Ipv4(subnet_mask).prefix_len() { + let prefix_len = match IpAddress::V4(subnet_mask).prefix_len() { Some(prefix_len) => prefix_len, None => { net_debug!("DHCP ignoring ACK because subnet_mask is not a valid mask"); diff --git a/src/socket/dns.rs b/src/socket/dns.rs index 681cf10f2..056d346f9 100644 --- a/src/socket/dns.rs +++ b/src/socket/dns.rs @@ -22,13 +22,13 @@ const RETRANSMIT_TIMEOUT: Duration = Duration::from_millis(10_000); // Should ge #[cfg(feature = "proto-ipv6")] #[allow(unused)] -const MDNS_IPV6_ADDR: IpAddress = IpAddress::Ipv6(crate::wire::Ipv6Address::new( +const MDNS_IPV6_ADDR: IpAddress = IpAddress::V6(crate::wire::Ipv6Address::new( 0xff02, 0, 0, 0, 0, 0, 0, 0xfb, )); #[cfg(feature = "proto-ipv4")] #[allow(unused)] -const MDNS_IPV4_ADDR: IpAddress = IpAddress::Ipv4(crate::wire::Ipv4Address::new(224, 0, 0, 251)); +const MDNS_IPV4_ADDR: IpAddress = IpAddress::V4(crate::wire::Ipv4Address::new(224, 0, 0, 251)); /// Error returned by [`Socket::start_query`] #[derive(Debug, PartialEq, Eq, Clone, Copy)] diff --git a/src/socket/icmp.rs b/src/socket/icmp.rs index 2a9258e49..80e2a7cad 100644 --- a/src/socket/icmp.rs +++ b/src/socket/icmp.rs @@ -561,7 +561,7 @@ impl<'a> Socket<'a> { ); match *remote_endpoint { #[cfg(feature = "proto-ipv4")] - IpAddress::Ipv4(dst_addr) => { + IpAddress::V4(dst_addr) => { let src_addr = match cx.get_source_address_ipv4(&dst_addr) { Some(addr) => addr, None => { @@ -593,7 +593,7 @@ impl<'a> Socket<'a> { emit(cx, (ip_repr, IcmpRepr::Ipv4(repr))) } #[cfg(feature = "proto-ipv6")] - IpAddress::Ipv6(dst_addr) => { + IpAddress::V6(dst_addr) => { let src_addr = cx.get_source_address_ipv6(&dst_addr); let packet = Icmpv6Packet::new_unchecked(&*packet_buf); @@ -681,7 +681,7 @@ mod test_ipv4 { const REMOTE_IPV4: Ipv4Address = Ipv4Address::new(192, 168, 1, 2); const LOCAL_IPV4: Ipv4Address = Ipv4Address::new(192, 168, 1, 1); const LOCAL_END_V4: IpEndpoint = IpEndpoint { - addr: IpAddress::Ipv4(LOCAL_IPV4), + addr: IpAddress::V4(LOCAL_IPV4), port: LOCAL_PORT, }; @@ -711,7 +711,7 @@ mod test_ipv4 { fn test_send_unaddressable() { let mut socket = socket(buffer(0), buffer(1)); assert_eq!( - socket.send_slice(b"abcdef", IpAddress::Ipv4(Ipv4Address::new(0, 0, 0, 0))), + socket.send_slice(b"abcdef", IpAddress::V4(Ipv4Address::new(0, 0, 0, 0))), Err(SendError::Unaddressable) ); assert_eq!(socket.send_slice(b"abcdef", REMOTE_IPV4.into()), Ok(())); @@ -943,7 +943,7 @@ mod test_ipv6 { const REMOTE_IPV6: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 2); const LOCAL_IPV6: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1); const LOCAL_END_V6: IpEndpoint = IpEndpoint { - addr: IpAddress::Ipv6(LOCAL_IPV6), + addr: IpAddress::V6(LOCAL_IPV6), port: LOCAL_PORT, }; static ECHOV6_REPR: Icmpv6Repr = Icmpv6Repr::EchoRequest { @@ -972,7 +972,7 @@ mod test_ipv6 { fn test_send_unaddressable() { let mut socket = socket(buffer(0), buffer(1)); assert_eq!( - socket.send_slice(b"abcdef", IpAddress::Ipv6(Ipv6Address::UNSPECIFIED)), + socket.send_slice(b"abcdef", IpAddress::V6(Ipv6Address::UNSPECIFIED)), Err(SendError::Unaddressable) ); assert_eq!(socket.send_slice(b"abcdef", REMOTE_IPV6.into()), Ok(())); diff --git a/src/socket/raw.rs b/src/socket/raw.rs index 7df7830e3..394448da1 100644 --- a/src/socket/raw.rs +++ b/src/socket/raw.rs @@ -95,9 +95,7 @@ pub struct Socket<'a> { } impl<'a> Socket<'a> { - /// Create a raw IP socket bound to the given IP version and datagram protocol, - /// with the given buffers. - pub fn new( + fn new( ip_version: IpVersion, ip_protocol: IpProtocol, rx_buffer: PacketBuffer<'a>, @@ -115,6 +113,24 @@ impl<'a> Socket<'a> { } } + /// Create a raw IPv4 socket bound to the datagram protocol with the given buffers. + pub fn new_v4( + ip_protocol: IpProtocol, + rx_buffer: PacketBuffer<'a>, + tx_buffer: PacketBuffer<'a>, + ) -> Socket<'a> { + Self::new(IpVersion::Ipv4, ip_protocol, rx_buffer, tx_buffer) + } + + /// Create a raw IPv6 socket bound to the datagram protocol with the given buffers. + pub fn new_v6( + ip_protocol: IpProtocol, + rx_buffer: PacketBuffer<'a>, + tx_buffer: PacketBuffer<'a>, + ) -> Socket<'a> { + Self::new(IpVersion::Ipv6, ip_protocol, rx_buffer, tx_buffer) + } + /// Register a waker for receive operations. /// /// The waker is woken on state changes that might affect the return value @@ -150,10 +166,16 @@ impl<'a> Socket<'a> { self.tx_waker.register(waker) } - /// Return the IP version the socket is bound to. + /// Returns true if this socket is an IPv4 raw socket, and false otherwise. + #[inline] + pub fn is_ipv4(&self) -> bool { + self.ip_version == IpVersion::Ipv4 + } + + /// Returns true if this socket is an IPv6 raw socket, and false otherwise. #[inline] - pub fn ip_version(&self) -> IpVersion { - self.ip_version + pub fn is_ipv6(&self) -> bool { + self.ip_version == IpVersion::Ipv6 } /// Return the IP protocol the socket is bound to. @@ -328,7 +350,7 @@ impl<'a> Socket<'a> { } pub(crate) fn accepts(&self, ip_repr: &IpRepr) -> bool { - if ip_repr.version() != self.ip_version { + if ip_repr.is_ipv4() != self.is_ipv4() { return false; } if ip_repr.next_header() != self.ip_protocol { @@ -484,12 +506,7 @@ mod test { rx_buffer: PacketBuffer<'static>, tx_buffer: PacketBuffer<'static>, ) -> Socket<'static> { - Socket::new( - IpVersion::Ipv4, - IpProtocol::Unknown(IP_PROTO), - rx_buffer, - tx_buffer, - ) + Socket::new_v4(IpProtocol::Unknown(IP_PROTO), rx_buffer, tx_buffer) } pub const IP_PROTO: u8 = 63; @@ -515,12 +532,7 @@ mod test { rx_buffer: PacketBuffer<'static>, tx_buffer: PacketBuffer<'static>, ) -> Socket<'static> { - Socket::new( - IpVersion::Ipv6, - IpProtocol::Unknown(IP_PROTO), - rx_buffer, - tx_buffer, - ) + Socket::new_v6(IpProtocol::Unknown(IP_PROTO), rx_buffer, tx_buffer) } pub const IP_PROTO: u8 = 63; @@ -816,8 +828,7 @@ mod test { fn test_doesnt_accept_wrong_proto() { #[cfg(feature = "proto-ipv4")] { - let socket = Socket::new( - IpVersion::Ipv4, + let socket = Socket::new_v4( IpProtocol::Unknown(ipv4_locals::IP_PROTO + 1), buffer(1), buffer(1), @@ -828,8 +839,7 @@ mod test { } #[cfg(feature = "proto-ipv6")] { - let socket = Socket::new( - IpVersion::Ipv6, + let socket = Socket::new_v6( IpProtocol::Unknown(ipv6_locals::IP_PROTO + 1), buffer(1), buffer(1), diff --git a/src/socket/tcp.rs b/src/socket/tcp.rs index 8c8911c7e..70c4d14b0 100644 --- a/src/socket/tcp.rs +++ b/src/socket/tcp.rs @@ -894,7 +894,7 @@ impl<'a> Socket<'a> { /// # { /// # use smoltcp::socket::tcp::{Socket, SocketBuffer}; /// # use smoltcp::iface::Interface; - /// # use smoltcp::wire::IpAddress; + /// # use smoltcp::wire::{IpAddress, Ipv4Address}; /// # /// # fn get_ephemeral_port() -> u16 { /// # 49152 @@ -909,7 +909,7 @@ impl<'a> Socket<'a> { /// # /// socket.connect( /// iface.context(), - /// (IpAddress::v4(10, 0, 0, 1), 80), + /// (IpAddress::V4(Ipv4Address::new(10, 0, 0, 1)), 80), /// get_ephemeral_port() /// ).unwrap(); /// # } @@ -959,7 +959,7 @@ impl<'a> Socket<'a> { port: local_endpoint.port, }; - if local_endpoint.addr.version() != remote_endpoint.addr.version() { + if local_endpoint.addr.is_ipv4() != remote_endpoint.addr.is_ipv4() { return Err(ConnectError::Unaddressable); } @@ -2103,9 +2103,9 @@ impl<'a> Socket<'a> { fn seq_to_transmit(&self, cx: &mut Context) -> bool { let ip_header_len = match self.tuple.unwrap().local.addr { #[cfg(feature = "proto-ipv4")] - IpAddress::Ipv4(_) => crate::wire::IPV4_HEADER_LEN, + IpAddress::V4(_) => crate::wire::IPV4_HEADER_LEN, #[cfg(feature = "proto-ipv6")] - IpAddress::Ipv6(_) => crate::wire::IPV6_HEADER_LEN, + IpAddress::V6(_) => crate::wire::IPV6_HEADER_LEN, }; // Max segment size we're able to send due to MTU limitations. @@ -2603,11 +2603,11 @@ mod test { const BASE_MSS: u16 = 1460; const LOCAL_END: IpEndpoint = IpEndpoint { - addr: IpAddress::Ipv4(LOCAL_ADDR), + addr: IpAddress::V4(LOCAL_ADDR), port: LOCAL_PORT, }; const REMOTE_END: IpEndpoint = IpEndpoint { - addr: IpAddress::Ipv4(REMOTE_ADDR), + addr: IpAddress::V4(REMOTE_ADDR), port: REMOTE_PORT, }; } else { @@ -2735,8 +2735,8 @@ mod test { .socket .dispatch(&mut socket.cx, |_, (ip_repr, tcp_repr)| { assert_eq!(ip_repr.next_header(), IpProtocol::Tcp); - assert_eq!(ip_repr.src_addr(), LOCAL_ADDR.into()); - assert_eq!(ip_repr.dst_addr(), REMOTE_ADDR.into()); + assert_eq!(ip_repr.src_addr(), LOCAL_ADDR); + assert_eq!(ip_repr.dst_addr(), REMOTE_ADDR); assert_eq!(ip_repr.payload_len(), tcp_repr.buffer_len()); net_trace!("recv: {}", tcp_repr); diff --git a/src/socket/udp.rs b/src/socket/udp.rs index 92ccfe657..aa3845d4b 100644 --- a/src/socket/udp.rs +++ b/src/socket/udp.rs @@ -624,11 +624,11 @@ mod test { const OTHER_ADDR: IpvXAddress = IpvXAddress::new(192, 168, 1, 3); const LOCAL_END: IpEndpoint = IpEndpoint { - addr: IpAddress::Ipv4(LOCAL_ADDR), + addr: IpAddress::V4(LOCAL_ADDR), port: LOCAL_PORT, }; const REMOTE_END: IpEndpoint = IpEndpoint { - addr: IpAddress::Ipv4(REMOTE_ADDR), + addr: IpAddress::V4(REMOTE_ADDR), port: REMOTE_PORT, }; } else { diff --git a/src/tests.rs b/src/tests.rs index 165bd52f7..00e1a1d26 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -27,10 +27,16 @@ pub(crate) fn setup<'a>(medium: Medium) -> (Interface, SocketSet<'a>, TestingDev { iface.update_ip_addrs(|ip_addrs| { ip_addrs - .push(IpCidr::new(IpAddress::v4(192, 168, 1, 1), 24)) + .push(IpCidr::new( + IpAddress::V4(Ipv4Address::new(192, 168, 1, 1)), + 24, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v4(127, 0, 0, 1), 8)) + .push(IpCidr::new( + IpAddress::V4(Ipv4Address::new(127, 0, 0, 1)), + 8, + )) .unwrap(); }); } @@ -39,13 +45,22 @@ pub(crate) fn setup<'a>(medium: Medium) -> (Interface, SocketSet<'a>, TestingDev { iface.update_ip_addrs(|ip_addrs| { ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfe80, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0, 0, 0, 0, 0, 0, 0, 1), 128)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0, 0, 0, 0, 0, 0, 0, 1)), + 128, + )) .unwrap(); ip_addrs - .push(IpCidr::new(IpAddress::v6(0xfdbe, 0, 0, 0, 0, 0, 0, 1), 64)) + .push(IpCidr::new( + IpAddress::V6(Ipv6Address::new(0xfdbe, 0, 0, 0, 0, 0, 0, 1)), + 64, + )) .unwrap(); }); } diff --git a/src/wire/ip.rs b/src/wire/ip.rs index e61541cd0..1a0c64a81 100644 --- a/src/wire/ip.rs +++ b/src/wire/ip.rs @@ -11,7 +11,7 @@ use crate::wire::{Ipv6Address, Ipv6AddressExt, Ipv6Cidr, Ipv6Packet, Ipv6Repr}; /// Internet protocol version. #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] -pub enum Version { +pub(crate) enum Version { #[cfg(feature = "proto-ipv4")] Ipv4, #[cfg(feature = "proto-ipv6")] @@ -23,7 +23,7 @@ impl Version { /// /// This function never returns `Ok(IpVersion::Unspecified)`; instead, /// unknown versions result in `Err(Error)`. - pub const fn of_packet(data: &[u8]) -> Result { + pub(crate) const fn of_packet(data: &[u8]) -> Result { match data[0] >> 4 { #[cfg(feature = "proto-ipv4")] 4 => Ok(Version::Ipv4), @@ -83,156 +83,41 @@ impl fmt::Display for Protocol { } } -/// An internetworking address. -#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] -pub enum Address { - /// An IPv4 address. - #[cfg(feature = "proto-ipv4")] - Ipv4(Ipv4Address), - /// An IPv6 address. - #[cfg(feature = "proto-ipv6")] - Ipv6(Ipv6Address), -} - -impl Address { - /// Create an address wrapping an IPv4 address with the given octets. - #[cfg(feature = "proto-ipv4")] - pub const fn v4(a0: u8, a1: u8, a2: u8, a3: u8) -> Address { - Address::Ipv4(Ipv4Address::new(a0, a1, a2, a3)) - } - - /// Create an address wrapping an IPv6 address with the given octets. - #[cfg(feature = "proto-ipv6")] - #[allow(clippy::too_many_arguments)] - pub const fn v6( - a0: u16, - a1: u16, - a2: u16, - a3: u16, - a4: u16, - a5: u16, - a6: u16, - a7: u16, - ) -> Address { - Address::Ipv6(Ipv6Address::new(a0, a1, a2, a3, a4, a5, a6, a7)) - } - - /// Return the protocol version. - pub const fn version(&self) -> Version { - match self { - #[cfg(feature = "proto-ipv4")] - Address::Ipv4(_) => Version::Ipv4, - #[cfg(feature = "proto-ipv6")] - Address::Ipv6(_) => Version::Ipv6, - } - } - - /// Query whether the address is a valid unicast address. - pub fn is_unicast(&self) -> bool { - match self { - #[cfg(feature = "proto-ipv4")] - Address::Ipv4(addr) => addr.x_is_unicast(), - #[cfg(feature = "proto-ipv6")] - Address::Ipv6(addr) => addr.x_is_unicast(), - } - } - - /// Query whether the address is a valid multicast address. - pub const fn is_multicast(&self) -> bool { - match self { - #[cfg(feature = "proto-ipv4")] - Address::Ipv4(addr) => addr.is_multicast(), - #[cfg(feature = "proto-ipv6")] - Address::Ipv6(addr) => addr.is_multicast(), - } - } +pub use core::net::IpAddr as Address; - /// Query whether the address is the broadcast address. - pub fn is_broadcast(&self) -> bool { - match self { - #[cfg(feature = "proto-ipv4")] - Address::Ipv4(addr) => addr.is_broadcast(), - #[cfg(feature = "proto-ipv6")] - Address::Ipv6(_) => false, - } - } +pub(crate) trait AddressExt { + /// Query whether the address is an unicast address. + /// + /// `x_` prefix is to avoid a collision with the still-unstable method in `core::ip`. + fn x_is_unicast(&self) -> bool; - /// Query whether the address falls into the "unspecified" range. - pub fn is_unspecified(&self) -> bool { - match self { - #[cfg(feature = "proto-ipv4")] - Address::Ipv4(addr) => addr.is_unspecified(), - #[cfg(feature = "proto-ipv6")] - Address::Ipv6(addr) => addr.is_unspecified(), - } - } + // /// Query whether the address is the broadcast address. + fn is_broadcast(&self) -> bool; /// If `self` is a CIDR-compatible subnet mask, return `Some(prefix_len)`, /// where `prefix_len` is the number of leading zeroes. Return `None` otherwise. - pub fn prefix_len(&self) -> Option { - match self { - #[cfg(feature = "proto-ipv4")] - Address::Ipv4(addr) => addr.prefix_len(), - #[cfg(feature = "proto-ipv6")] - Address::Ipv6(addr) => addr.prefix_len(), - } - } -} - -#[cfg(all(feature = "proto-ipv4", feature = "proto-ipv6"))] -impl From<::core::net::IpAddr> for Address { - fn from(x: ::core::net::IpAddr) -> Address { - match x { - ::core::net::IpAddr::V4(ipv4) => Address::Ipv4(ipv4), - ::core::net::IpAddr::V6(ipv6) => Address::Ipv6(ipv6), - } - } + fn prefix_len(&self) -> Option; } -impl From
for ::core::net::IpAddr { - fn from(x: Address) -> ::core::net::IpAddr { - match x { - #[cfg(feature = "proto-ipv4")] - Address::Ipv4(ipv4) => ::core::net::IpAddr::V4(ipv4), - #[cfg(feature = "proto-ipv6")] - Address::Ipv6(ipv6) => ::core::net::IpAddr::V6(ipv6), +impl AddressExt for Address { + fn x_is_unicast(&self) -> bool { + match self { + Address::V4(addr) => addr.x_is_unicast(), + Address::V6(addr) => addr.x_is_unicast(), } } -} -#[cfg(feature = "proto-ipv4")] -impl From for Address { - fn from(ipv4: Ipv4Address) -> Address { - Address::Ipv4(ipv4) - } -} - -#[cfg(feature = "proto-ipv6")] -impl From for Address { - fn from(addr: Ipv6Address) -> Self { - Address::Ipv6(addr) - } -} - -impl fmt::Display for Address { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - #[cfg(feature = "proto-ipv4")] - Address::Ipv4(addr) => write!(f, "{addr}"), - #[cfg(feature = "proto-ipv6")] - Address::Ipv6(addr) => write!(f, "{addr}"), + fn is_broadcast(&self) -> bool { + match self { + Address::V4(addr) => addr.is_broadcast(), + Address::V6(_) => false, } } -} -#[cfg(feature = "defmt")] -impl defmt::Format for Address { - fn format(&self, f: defmt::Formatter) { + fn prefix_len(&self) -> Option { match self { - #[cfg(feature = "proto-ipv4")] - &Address::Ipv4(addr) => defmt::write!(f, "{:?}", addr), - #[cfg(feature = "proto-ipv6")] - &Address::Ipv6(addr) => defmt::write!(f, "{:?}", addr), + Address::V4(addr) => addr.prefix_len(), + Address::V6(addr) => addr.prefix_len(), } } } @@ -255,9 +140,9 @@ impl Cidr { pub fn new(addr: Address, prefix_len: u8) -> Cidr { match addr { #[cfg(feature = "proto-ipv4")] - Address::Ipv4(addr) => Cidr::Ipv4(Ipv4Cidr::new(addr, prefix_len)), + Address::V4(addr) => Cidr::Ipv4(Ipv4Cidr::new(addr, prefix_len)), #[cfg(feature = "proto-ipv6")] - Address::Ipv6(addr) => Cidr::Ipv6(Ipv6Cidr::new(addr, prefix_len)), + Address::V6(addr) => Cidr::Ipv6(Ipv6Cidr::new(addr, prefix_len)), } } @@ -265,9 +150,9 @@ impl Cidr { pub const fn address(&self) -> Address { match *self { #[cfg(feature = "proto-ipv4")] - Cidr::Ipv4(cidr) => Address::Ipv4(cidr.address()), + Cidr::Ipv4(cidr) => Address::V4(cidr.address()), #[cfg(feature = "proto-ipv6")] - Cidr::Ipv6(cidr) => Address::Ipv6(cidr.address()), + Cidr::Ipv6(cidr) => Address::V6(cidr.address()), } } @@ -286,9 +171,9 @@ impl Cidr { pub fn contains_addr(&self, addr: &Address) -> bool { match (self, addr) { #[cfg(feature = "proto-ipv4")] - (Cidr::Ipv4(cidr), Address::Ipv4(addr)) => cidr.contains_addr(addr), + (Cidr::Ipv4(cidr), Address::V4(addr)) => cidr.contains_addr(addr), #[cfg(feature = "proto-ipv6")] - (Cidr::Ipv6(cidr), Address::Ipv6(addr)) => cidr.contains_addr(addr), + (Cidr::Ipv6(cidr), Address::V6(addr)) => cidr.contains_addr(addr), #[allow(unreachable_patterns)] _ => false, } @@ -368,7 +253,7 @@ impl Endpoint { impl From<::core::net::SocketAddr> for Endpoint { fn from(x: ::core::net::SocketAddr) -> Endpoint { Endpoint { - addr: x.ip().into(), + addr: x.ip(), port: x.port(), } } @@ -439,7 +324,7 @@ impl ListenEndpoint { impl From<::core::net::SocketAddr> for ListenEndpoint { fn from(x: ::core::net::SocketAddr) -> ListenEndpoint { ListenEndpoint { - addr: Some(x.ip().into()), + addr: Some(x.ip()), port: x.port(), } } @@ -548,7 +433,7 @@ impl Repr { ) -> Self { match (src_addr, dst_addr) { #[cfg(feature = "proto-ipv4")] - (Address::Ipv4(src_addr), Address::Ipv4(dst_addr)) => Self::Ipv4(Ipv4Repr { + (Address::V4(src_addr), Address::V4(dst_addr)) => Self::Ipv4(Ipv4Repr { src_addr, dst_addr, next_header, @@ -556,7 +441,7 @@ impl Repr { hop_limit, }), #[cfg(feature = "proto-ipv6")] - (Address::Ipv6(src_addr), Address::Ipv6(dst_addr)) => Self::Ipv6(Ipv6Repr { + (Address::V6(src_addr), Address::V6(dst_addr)) => Self::Ipv6(Ipv6Repr { src_addr, dst_addr, next_header, @@ -568,13 +453,23 @@ impl Repr { } } - /// Return the protocol version. - pub const fn version(&self) -> Version { + /// Returns true if this address is an IPv4 address, and false otherwise. + pub const fn is_ipv4(&self) -> bool { + match self { + #[cfg(feature = "proto-ipv4")] + Repr::Ipv4(_) => true, + #[cfg(feature = "proto-ipv6")] + Repr::Ipv6(_) => false, + } + } + + /// Returns true if this address is an IPv6 address, and false otherwise. + pub const fn is_ipv6(&self) -> bool { match *self { #[cfg(feature = "proto-ipv4")] - Repr::Ipv4(_) => Version::Ipv4, + Repr::Ipv4(_) => false, #[cfg(feature = "proto-ipv6")] - Repr::Ipv6(_) => Version::Ipv6, + Repr::Ipv6(_) => true, } } @@ -582,9 +477,9 @@ impl Repr { pub const fn src_addr(&self) -> Address { match *self { #[cfg(feature = "proto-ipv4")] - Repr::Ipv4(repr) => Address::Ipv4(repr.src_addr), + Repr::Ipv4(repr) => Address::V4(repr.src_addr), #[cfg(feature = "proto-ipv6")] - Repr::Ipv6(repr) => Address::Ipv6(repr.src_addr), + Repr::Ipv6(repr) => Address::V6(repr.src_addr), } } @@ -592,9 +487,9 @@ impl Repr { pub const fn dst_addr(&self) -> Address { match *self { #[cfg(feature = "proto-ipv4")] - Repr::Ipv4(repr) => Address::Ipv4(repr.dst_addr), + Repr::Ipv4(repr) => Address::V4(repr.dst_addr), #[cfg(feature = "proto-ipv6")] - Repr::Ipv6(repr) => Address::Ipv6(repr.dst_addr), + Repr::Ipv6(repr) => Address::V6(repr.dst_addr), } } @@ -766,11 +661,11 @@ pub mod checksum { ) -> u16 { match (src_addr, dst_addr) { #[cfg(feature = "proto-ipv4")] - (Address::Ipv4(src_addr), Address::Ipv4(dst_addr)) => { + (Address::V4(src_addr), Address::V4(dst_addr)) => { pseudo_header_v4(src_addr, dst_addr, next_header, length) } #[cfg(feature = "proto-ipv6")] - (Address::Ipv6(src_addr), Address::Ipv6(dst_addr)) => { + (Address::V6(src_addr), Address::V6(dst_addr)) => { pseudo_header_v6(src_addr, dst_addr, next_header, length) } #[allow(unreachable_patterns)] diff --git a/src/wire/mod.rs b/src/wire/mod.rs index a6db11c3d..0083d606d 100644 --- a/src/wire/mod.rs +++ b/src/wire/mod.rs @@ -181,9 +181,10 @@ pub use self::ieee802154::{ pub use self::ip::{ Address as IpAddress, Cidr as IpCidr, Endpoint as IpEndpoint, ListenEndpoint as IpListenEndpoint, Protocol as IpProtocol, Repr as IpRepr, - Version as IpVersion, }; +pub(crate) use self::ip::{AddressExt as IpAddressExt, Version as IpVersion}; + #[cfg(feature = "proto-ipv4")] pub use self::ipv4::{ Address as Ipv4Address, Cidr as Ipv4Cidr, Key as Ipv4FragKey, Packet as Ipv4Packet, diff --git a/src/wire/udp.rs b/src/wire/udp.rs index 9d341e0ee..053706222 100644 --- a/src/wire/udp.rs +++ b/src/wire/udp.rs @@ -234,7 +234,7 @@ impl Repr { match (src_addr, dst_addr) { // ... except on UDP-over-IPv4, where it can be omitted. #[cfg(feature = "proto-ipv4")] - (&IpAddress::Ipv4(_), &IpAddress::Ipv4(_)) if packet.checksum() == 0 => (), + (&IpAddress::V4(_), &IpAddress::V4(_)) if packet.checksum() == 0 => (), _ => return Err(Error), } }