Skip to content

Commit

Permalink
wire: use core::net::Ipv6Addr as the ipv6 address type.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Sep 20, 2024
1 parent 72aa012 commit e57dcc3
Show file tree
Hide file tree
Showing 32 changed files with 335 additions and 907 deletions.
12 changes: 4 additions & 8 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@ mod wire {
extern crate test;

#[cfg(feature = "proto-ipv6")]
const SRC_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address([
0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]));
const SRC_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1));
#[cfg(feature = "proto-ipv6")]
const DST_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address([
0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
]));
const DST_ADDR: IpAddress = IpAddress::Ipv6(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));
Expand Down Expand Up @@ -102,8 +98,8 @@ mod wire {
#[cfg(feature = "proto-ipv6")]
fn bench_emit_ipv6(b: &mut test::Bencher) {
let repr = Ipv6Repr {
src_addr: Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
dst_addr: Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]),
src_addr: Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1),
dst_addr: Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 2),
next_header: IpProtocol::Tcp,
payload_len: 100,
hop_limit: 64,
Expand Down
15 changes: 6 additions & 9 deletions examples/multicast6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv6Address};
// will send packets to the multicast group we join below on tap0.

const PORT: u16 = 8123;
const GROUP: [u16; 8] = [0xff02, 0, 0, 0, 0, 0, 0, 0x1234];
const LOCAL_ADDR: [u16; 8] = [0xfe80, 0, 0, 0, 0, 0, 0, 0x101];
const ROUTER_ADDR: [u16; 8] = [0xfe80, 0, 0, 0, 0, 0, 0, 0x100];
const GROUP: Ipv6Address = Ipv6Address::new(0xff02, 0, 0, 0, 0, 0, 0, 0x1234);
const LOCAL_ADDR: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x101);
const ROUTER_ADDR: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100);

fn main() {
utils::setup_logging("warn");
Expand All @@ -37,7 +37,6 @@ fn main() {
utils::parse_middleware_options(&mut matches, device, /*loopback=*/ false);

// Create interface
let local_addr = Ipv6Address::from_parts(&LOCAL_ADDR);
let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
let mut config = match device.capabilities().medium {
Medium::Ethernet => Config::new(ethernet_addr.into()),
Expand All @@ -49,12 +48,12 @@ fn main() {
let mut iface = Interface::new(config, &mut device, Instant::now());
iface.update_ip_addrs(|ip_addrs| {
ip_addrs
.push(IpCidr::new(IpAddress::from(local_addr), 64))
.push(IpCidr::new(IpAddress::from(LOCAL_ADDR), 64))
.unwrap();
});
iface
.routes_mut()
.add_default_ipv6_route(Ipv6Address::from_parts(&ROUTER_ADDR))
.add_default_ipv6_route(ROUTER_ADDR)
.unwrap();

// Create sockets
Expand All @@ -65,9 +64,7 @@ fn main() {
let udp_handle = sockets.add(udp_socket);

// Join a multicast group
iface
.join_multicast_group(Ipv6Address::from_parts(&GROUP))
.unwrap();
iface.join_multicast_group(GROUP).unwrap();

loop {
let timestamp = Instant::now();
Expand Down
10 changes: 5 additions & 5 deletions src/iface/interface/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl InterfaceInner {
fn common_prefix_length(dst_addr: &Ipv6Cidr, src_addr: &Ipv6Address) -> usize {
let addr = dst_addr.address();
let mut bits = 0;
for (l, r) in addr.as_bytes().iter().zip(src_addr.as_bytes().iter()) {
for (l, r) in addr.octets().iter().zip(src_addr.octets().iter()) {
if l == r {
bits += 8;
} else {
Expand All @@ -82,7 +82,7 @@ impl InterfaceInner {
.count()
== 0
{
return Ipv6Address::LOOPBACK;
return Ipv6Address::LOCALHOST;
}

let mut candidate = self
Expand Down Expand Up @@ -147,10 +147,10 @@ impl InterfaceInner {
pub fn has_solicited_node(&self, addr: Ipv6Address) -> bool {
self.ip_addrs.iter().any(|cidr| {
match *cidr {
IpCidr::Ipv6(cidr) if cidr.address() != Ipv6Address::LOOPBACK => {
IpCidr::Ipv6(cidr) if cidr.address() != Ipv6Address::LOCALHOST => {
// Take the lower order 24 bits of the IPv6 address and
// append those bits to FF02:0:0:0:0:1:FF00::/104.
addr.as_bytes()[14..] == cidr.address().as_bytes()[14..]
addr.octets()[14..] == cidr.address().octets()[14..]
}
_ => false,
}
Expand Down Expand Up @@ -523,7 +523,7 @@ impl InterfaceInner {

// Per [RFC 3810 § 5.2.14], all MLDv2 reports are sent to ff02::16.
// [RFC 3810 § 5.2.14]: https://tools.ietf.org/html/rfc3810#section-5.2.14
let dst_addr = Ipv6Address::LINK_LOCAL_ALL_MLDV2_ROUTERS;
let dst_addr = IPV6_LINK_LOCAL_ALL_MLDV2_ROUTERS;

// Create a dummy IPv6 extension header so we can calculate the total length of the packet.
// The actual extension header will be created later by Packet::emit_payload().
Expand Down
6 changes: 3 additions & 3 deletions src/iface/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,10 +832,10 @@ impl InterfaceInner {
#[cfg(feature = "proto-ipv4")]
IpAddress::Ipv4(key) => key == IPV4_MULTICAST_ALL_SYSTEMS,
#[cfg(feature = "proto-rpl")]
IpAddress::Ipv6(Ipv6Address::LINK_LOCAL_ALL_RPL_NODES) => true,
IpAddress::Ipv6(IPV6_LINK_LOCAL_ALL_RPL_NODES) => true,
#[cfg(feature = "proto-ipv6")]
IpAddress::Ipv6(key) => {
key == Ipv6Address::LINK_LOCAL_ALL_NODES || self.has_solicited_node(key)
key == IPV6_LINK_LOCAL_ALL_NODES || self.has_solicited_node(key)
}
#[allow(unreachable_patterns)]
_ => false,
Expand Down Expand Up @@ -1011,7 +1011,7 @@ impl InterfaceInner {
IpAddress::Ipv6(addr) => match self.caps.medium {
#[cfg(feature = "medium-ethernet")]
Medium::Ethernet => {
let b = addr.as_bytes();
let b = addr.octets();
HardwareAddress::Ethernet(EthernetAddress::from_bytes(&[
0x33, 0x33, b[12], b[13], b[14], b[15],
]))
Expand Down
Loading

0 comments on commit e57dcc3

Please sign in to comment.