Skip to content

Commit

Permalink
wire: use core::net::Ipv4Addr as the ipv4 address type.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Sep 20, 2024
1 parent 0341cc9 commit 72aa012
Show file tree
Hide file tree
Showing 25 changed files with 344 additions and 444 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ byteorder = { version = "1.0", default-features = false }
log = { version = "0.4.4", default-features = false, optional = true }
libc = { version = "0.2.18", optional = true }
bitflags = { version = "1.0", default-features = false }
defmt = { version = "0.3", optional = true }
defmt = { version = "0.3.8", optional = true, features = ["ip_in_core"] }
cfg-if = "1.0.0"
heapless = "0.8"

Expand Down
8 changes: 4 additions & 4 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ mod wire {
]));

#[cfg(all(not(feature = "proto-ipv6"), feature = "proto-ipv4"))]
const SRC_ADDR: IpAddress = IpAddress::Ipv4(Ipv4Address([192, 168, 1, 1]));
const SRC_ADDR: IpAddress = IpAddress::Ipv4(Ipv4Address::new(192, 168, 1, 1));
#[cfg(all(not(feature = "proto-ipv6"), feature = "proto-ipv4"))]
const DST_ADDR: IpAddress = IpAddress::Ipv4(Ipv4Address([192, 168, 1, 2]));
const DST_ADDR: IpAddress = IpAddress::Ipv4(Ipv4Address::new(192, 168, 1, 2));

#[bench]
#[cfg(any(feature = "proto-ipv6", feature = "proto-ipv4"))]
Expand Down Expand Up @@ -84,8 +84,8 @@ mod wire {
#[cfg(feature = "proto-ipv4")]
fn bench_emit_ipv4(b: &mut test::Bencher) {
let repr = Ipv4Repr {
src_addr: Ipv4Address([192, 168, 1, 1]),
dst_addr: Ipv4Address([192, 168, 1, 2]),
src_addr: Ipv4Address::new(192, 168, 1, 1),
dst_addr: Ipv4Address::new(192, 168, 1, 2),
next_header: IpProtocol::Tcp,
payload_len: 100,
hop_limit: 64,
Expand Down
6 changes: 2 additions & 4 deletions examples/multicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use smoltcp::wire::{
};

const MDNS_PORT: u16 = 5353;
const MDNS_GROUP: [u8; 4] = [224, 0, 0, 251];
const MDNS_GROUP: Ipv4Address = Ipv4Address::new(224, 0, 0, 251);

fn main() {
utils::setup_logging("warn");
Expand Down Expand Up @@ -81,9 +81,7 @@ fn main() {
let udp_handle = sockets.add(udp_socket);

// Join a multicast group to receive mDNS traffic
iface
.join_multicast_group(Ipv4Address::from_bytes(&MDNS_GROUP))
.unwrap();
iface.join_multicast_group(MDNS_GROUP).unwrap();

loop {
let timestamp = Instant::now();
Expand Down
8 changes: 4 additions & 4 deletions src/iface/fragmentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ impl Fragmenter {
#[cfg(feature = "proto-ipv4-fragmentation")]
ipv4: Ipv4Fragmenter {
repr: Ipv4Repr {
src_addr: Ipv4Address::default(),
dst_addr: Ipv4Address::default(),
src_addr: Ipv4Address::new(0, 0, 0, 0),
dst_addr: Ipv4Address::new(0, 0, 0, 0),
next_header: IpProtocol::Unknown(0),
payload_len: 0,
hop_limit: 0,
Expand Down Expand Up @@ -373,8 +373,8 @@ impl Fragmenter {
#[cfg(feature = "proto-ipv4-fragmentation")]
{
self.ipv4.repr = Ipv4Repr {
src_addr: Ipv4Address::default(),
dst_addr: Ipv4Address::default(),
src_addr: Ipv4Address::new(0, 0, 0, 0),
dst_addr: Ipv4Address::new(0, 0, 0, 0),
next_header: IpProtocol::Unknown(0),
payload_len: 0,
hop_limit: 0,
Expand Down
35 changes: 20 additions & 15 deletions src/iface/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ impl InterfaceInner {

match addr {
#[cfg(feature = "proto-ipv4")]
IpAddress::Ipv4(key) => key == Ipv4Address::MULTICAST_ALL_SYSTEMS,
IpAddress::Ipv4(key) => key == IPV4_MULTICAST_ALL_SYSTEMS,
#[cfg(feature = "proto-rpl")]
IpAddress::Ipv6(Ipv6Address::LINK_LOCAL_ALL_RPL_NODES) => true,
#[cfg(feature = "proto-ipv6")]
Expand Down Expand Up @@ -987,30 +987,35 @@ impl InterfaceInner {
}

if dst_addr.is_multicast() {
let b = dst_addr.as_bytes();
let hardware_addr = match *dst_addr {
#[cfg(feature = "proto-ipv4")]
IpAddress::Ipv4(_addr) => match self.caps.medium {
IpAddress::Ipv4(addr) => match self.caps.medium {
#[cfg(feature = "medium-ethernet")]
Medium::Ethernet => HardwareAddress::Ethernet(EthernetAddress::from_bytes(&[
0x01,
0x00,
0x5e,
b[1] & 0x7F,
b[2],
b[3],
])),
Medium::Ethernet => {
let b = addr.octets();
HardwareAddress::Ethernet(EthernetAddress::from_bytes(&[
0x01,
0x00,
0x5e,
b[1] & 0x7F,
b[2],
b[3],
]))
}
#[cfg(feature = "medium-ieee802154")]
Medium::Ieee802154 => unreachable!(),
#[cfg(feature = "medium-ip")]
Medium::Ip => unreachable!(),
},
#[cfg(feature = "proto-ipv6")]
IpAddress::Ipv6(_addr) => match self.caps.medium {
IpAddress::Ipv6(addr) => match self.caps.medium {
#[cfg(feature = "medium-ethernet")]
Medium::Ethernet => HardwareAddress::Ethernet(EthernetAddress::from_bytes(&[
0x33, 0x33, b[12], b[13], b[14], b[15],
])),
Medium::Ethernet => {
let b = addr.as_bytes();
HardwareAddress::Ethernet(EthernetAddress::from_bytes(&[
0x33, 0x33, b[12], b[13], b[14], b[15],
]))
}
#[cfg(feature = "medium-ieee802154")]
Medium::Ieee802154 => {
// Not sure if this is correct
Expand Down
6 changes: 2 additions & 4 deletions src/iface/interface/multicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,7 @@ impl InterfaceInner {
max_resp_time,
} => {
// General query
if group_addr.is_unspecified()
&& ipv4_repr.dst_addr == Ipv4Address::MULTICAST_ALL_SYSTEMS
{
if group_addr.is_unspecified() && ipv4_repr.dst_addr == IPV4_MULTICAST_ALL_SYSTEMS {
let ipv4_multicast_group_count = self
.multicast
.groups
Expand Down Expand Up @@ -418,7 +416,7 @@ impl InterfaceInner {
Packet::new_ipv4(
Ipv4Repr {
src_addr: iface_addr,
dst_addr: Ipv4Address::MULTICAST_ALL_ROUTERS,
dst_addr: IPV4_MULTICAST_ALL_ROUTERS,
next_header: IpProtocol::Igmp,
payload_len: igmp_repr.buffer_len(),
hop_limit: 1,
Expand Down
Loading

0 comments on commit 72aa012

Please sign in to comment.