Skip to content

Commit 660efae

Browse files
committed
wire: use core::net::Ipv6Addr as the ipv6 address type.
1 parent 6987f61 commit 660efae

32 files changed

+335
-907
lines changed

benches/bench.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@ mod wire {
1313
extern crate test;
1414

1515
#[cfg(feature = "proto-ipv6")]
16-
const SRC_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address([
17-
0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
18-
]));
16+
const SRC_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1));
1917
#[cfg(feature = "proto-ipv6")]
20-
const DST_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address([
21-
0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
22-
]));
18+
const DST_ADDR: IpAddress = IpAddress::Ipv6(Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 2));
2319

2420
#[cfg(all(not(feature = "proto-ipv6"), feature = "proto-ipv4"))]
2521
const SRC_ADDR: IpAddress = IpAddress::Ipv4(Ipv4Address::new(192, 168, 1, 1));
@@ -102,8 +98,8 @@ mod wire {
10298
#[cfg(feature = "proto-ipv6")]
10399
fn bench_emit_ipv6(b: &mut test::Bencher) {
104100
let repr = Ipv6Repr {
105-
src_addr: Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]),
106-
dst_addr: Ipv6Address([0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]),
101+
src_addr: Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 1),
102+
dst_addr: Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 2),
107103
next_header: IpProtocol::Tcp,
108104
payload_len: 100,
109105
hop_limit: 64,

examples/multicast6.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv6Address};
1919
// will send packets to the multicast group we join below on tap0.
2020

2121
const PORT: u16 = 8123;
22-
const GROUP: [u16; 8] = [0xff02, 0, 0, 0, 0, 0, 0, 0x1234];
23-
const LOCAL_ADDR: [u16; 8] = [0xfe80, 0, 0, 0, 0, 0, 0, 0x101];
24-
const ROUTER_ADDR: [u16; 8] = [0xfe80, 0, 0, 0, 0, 0, 0, 0x100];
22+
const GROUP: Ipv6Address = Ipv6Address::new(0xff02, 0, 0, 0, 0, 0, 0, 0x1234);
23+
const LOCAL_ADDR: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x101);
24+
const ROUTER_ADDR: Ipv6Address = Ipv6Address::new(0xfe80, 0, 0, 0, 0, 0, 0, 0x100);
2525

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

3939
// Create interface
40-
let local_addr = Ipv6Address::from_parts(&LOCAL_ADDR);
4140
let ethernet_addr = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x02]);
4241
let mut config = match device.capabilities().medium {
4342
Medium::Ethernet => Config::new(ethernet_addr.into()),
@@ -49,12 +48,12 @@ fn main() {
4948
let mut iface = Interface::new(config, &mut device, Instant::now());
5049
iface.update_ip_addrs(|ip_addrs| {
5150
ip_addrs
52-
.push(IpCidr::new(IpAddress::from(local_addr), 64))
51+
.push(IpCidr::new(IpAddress::from(LOCAL_ADDR), 64))
5352
.unwrap();
5453
});
5554
iface
5655
.routes_mut()
57-
.add_default_ipv6_route(Ipv6Address::from_parts(&ROUTER_ADDR))
56+
.add_default_ipv6_route(ROUTER_ADDR)
5857
.unwrap();
5958

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

6766
// Join a multicast group
68-
iface
69-
.join_multicast_group(Ipv6Address::from_parts(&GROUP))
70-
.unwrap();
67+
iface.join_multicast_group(GROUP).unwrap();
7168

7269
loop {
7370
let timestamp = Instant::now();

src/iface/interface/ipv6.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl InterfaceInner {
5858
fn common_prefix_length(dst_addr: &Ipv6Cidr, src_addr: &Ipv6Address) -> usize {
5959
let addr = dst_addr.address();
6060
let mut bits = 0;
61-
for (l, r) in addr.as_bytes().iter().zip(src_addr.as_bytes().iter()) {
61+
for (l, r) in addr.octets().iter().zip(src_addr.octets().iter()) {
6262
if l == r {
6363
bits += 8;
6464
} else {
@@ -82,7 +82,7 @@ impl InterfaceInner {
8282
.count()
8383
== 0
8484
{
85-
return Ipv6Address::LOOPBACK;
85+
return Ipv6Address::LOCALHOST;
8686
}
8787

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

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

528528
// Create a dummy IPv6 extension header so we can calculate the total length of the packet.
529529
// The actual extension header will be created later by Packet::emit_payload().

src/iface/interface/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -832,10 +832,10 @@ impl InterfaceInner {
832832
#[cfg(feature = "proto-ipv4")]
833833
IpAddress::Ipv4(key) => key == IPV4_MULTICAST_ALL_SYSTEMS,
834834
#[cfg(feature = "proto-rpl")]
835-
IpAddress::Ipv6(Ipv6Address::LINK_LOCAL_ALL_RPL_NODES) => true,
835+
IpAddress::Ipv6(IPV6_LINK_LOCAL_ALL_RPL_NODES) => true,
836836
#[cfg(feature = "proto-ipv6")]
837837
IpAddress::Ipv6(key) => {
838-
key == Ipv6Address::LINK_LOCAL_ALL_NODES || self.has_solicited_node(key)
838+
key == IPV6_LINK_LOCAL_ALL_NODES || self.has_solicited_node(key)
839839
}
840840
#[allow(unreachable_patterns)]
841841
_ => false,
@@ -1011,7 +1011,7 @@ impl InterfaceInner {
10111011
IpAddress::Ipv6(addr) => match self.caps.medium {
10121012
#[cfg(feature = "medium-ethernet")]
10131013
Medium::Ethernet => {
1014-
let b = addr.as_bytes();
1014+
let b = addr.octets();
10151015
HardwareAddress::Ethernet(EthernetAddress::from_bytes(&[
10161016
0x33, 0x33, b[12], b[13], b[14], b[15],
10171017
]))

0 commit comments

Comments
 (0)