Skip to content

Commit

Permalink
Update the UDP part in sixlowpan
Browse files Browse the repository at this point in the history
  • Loading branch information
thvdveld committed Aug 18, 2021
1 parent 6bb0cff commit 4a5a0aa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
27 changes: 14 additions & 13 deletions src/iface/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ where
},
#[cfg(feature = "medium-sixlowpan")]
Medium::Sixlowpan => inner
.process_sixlowpan(sockets, timestamp, &frame)
.process_sixlowpan(cx, sockets, &frame)
.map_err(|err| {
net_debug!("cannot process ingress packet: {}", err);
err
Expand All @@ -765,7 +765,7 @@ where
processed_any = true;
match response {
Some(packet) => inner
.dispatch_ip(tx_token, timestamp, packet)
.dispatch_ip(cx, tx_token, packet)
.map_err(|err| {
net_debug!("cannot dispatch response packet: {}", err);
err
Expand All @@ -774,7 +774,6 @@ where
}
}),
}
Ok(())
}) {
net_debug!("Failed to consume RX token: {}", err);
}
Expand Down Expand Up @@ -1108,8 +1107,8 @@ impl<'a> InterfaceInner<'a> {
#[cfg(feature = "medium-sixlowpan")]
fn process_sixlowpan<'frame, T: AsRef<[u8]> + ?Sized>(
&mut self,
cx: &Context,
sockets: &mut SocketSet,
timestamp: Instant,
sixlowpan_payload: &'frame T,
) -> Result<Option<IpPacket<'frame>>> {
let ieee802154_frame = Ieee802154Frame::new_checked(sixlowpan_payload)?;
Expand Down Expand Up @@ -1159,7 +1158,7 @@ impl<'a> InterfaceInner<'a> {
continue;
}

match udp_socket.process(&ip_repr, &udp_repr) {
match udp_socket.process(cx, &ip_repr, &udp_repr, udp_packet.payload()) {
Ok(()) => return Ok(None),
Err(e) => return Err(e),
}
Expand Down Expand Up @@ -1189,7 +1188,7 @@ impl<'a> InterfaceInner<'a> {
}
SixlowpanNextHeader::Uncompressed(nxt_hdr) => match nxt_hdr {
IpProtocol::Icmpv6 => {
self.process_icmpv6(sockets, timestamp, ip_repr, iphc_packet.payload())
self.process_icmpv6(cx, sockets, ip_repr.into(), iphc_packet.payload())
}
hdr => todo!("{:?}", hdr),
},
Expand Down Expand Up @@ -1631,7 +1630,7 @@ impl<'a> InterfaceInner<'a> {
#[cfg(feature = "medium-sixlowpan")]
IpRepr::Sixlowpan(sixlowpan_repr) => {
self.process_ndisc(
_timestamp,
cx.now,
Ipv6Repr {
src_addr: sixlowpan_repr.src_addr,
dst_addr: sixlowpan_repr.dst_addr,
Expand Down Expand Up @@ -2265,7 +2264,7 @@ impl<'a> InterfaceInner<'a> {
.neighbor_cache
.as_mut()
.unwrap()
.lookup(&ip_repr.dst_addr(), timestamp)
.lookup(&ip_repr.dst_addr(), cx.now)
{
NeighborAnswer::Found(HardwareAddress::Ieee802154(addr)) => addr,
r => return Err(Error::Unaddressable),
Expand Down Expand Up @@ -2322,9 +2321,9 @@ impl<'a> InterfaceInner<'a> {
tx_len += iphc_repr.buffer_len();

match &packet {
IpPacket::Udp((_, udp_repr)) => {
IpPacket::Udp((_, udp_repr, payload)) => {
let udp_repr = SixlowpanUdpRepr(*udp_repr);
tx_len += udp_repr.buffer_len();
tx_len += udp_repr.header_len() + payload.len();
}
IpPacket::Icmpv6((_, icmp)) => {
tx_len += icmp.buffer_len();
Expand All @@ -2334,7 +2333,7 @@ impl<'a> InterfaceInner<'a> {

//tx_len += 2; // XXX: FCS calculation not needed when doing it in hardware

tx_token.consume(timestamp, tx_len, |mut tx_buffer| {
tx_token.consume(cx.now, tx_len, |mut tx_buffer| {
// 1. Create the header of 802.15.4
let mut ieee_packet = Ieee802154Frame::new_unchecked(&mut tx_buffer);
ieee_repr.emit(&mut ieee_packet);
Expand All @@ -2348,14 +2347,16 @@ impl<'a> InterfaceInner<'a> {
start = start + iphc_repr.buffer_len();

match packet {
IpPacket::Udp((_, udp_repr)) => {
IpPacket::Udp((_, udp_repr, payload)) => {
// 3. Create the header for 6LoWPAN UDP
let mut udp_packet =
SixlowpanUdpPacket::new_unchecked(&mut tx_buffer[start..tx_len]);
SixlowpanUdpRepr(udp_repr).emit(
&mut udp_packet,
&iphc_repr.src_addr,
&iphc_repr.dst_addr,
payload.len(),
|buf| buf.copy_from_slice(&payload),
);
}
IpPacket::Icmpv6((_, icmp_repr)) => {
Expand All @@ -2367,7 +2368,7 @@ impl<'a> InterfaceInner<'a> {
&iphc_repr.src_addr.into(),
&iphc_repr.dst_addr.into(),
&mut icmp_packet,
&self.device_capabilities.checksum,
&cx.caps.checksum,
);
}
_ => return Err(Error::Unrecognized),
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ compile_error!("If you enable the socket feature, you must enable at least one o

#[cfg(all(
feature = "socket",
not(any(feature = "medium-ethernet", feature = "medium-ip",))
not(any(
feature = "medium-ethernet",
feature = "medium-ip",
feature = "medium-sixlowpan",
))
))]
compile_error!("If you enable the socket feature, you must enable at least one of the following features: medium-ip, medium-ethernet");

Expand Down
27 changes: 14 additions & 13 deletions src/wire/sixlowpan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,16 +1481,16 @@ pub mod nhc {

/// A high-level representation of a LOWPAN_NHC UDP header.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct UdpNhcRepr<'a>(pub UdpRepr<'a>);
pub struct UdpNhcRepr(pub UdpRepr);

impl<'a> UdpNhcRepr<'a> {
impl<'a> UdpNhcRepr {
/// Parse a LOWWPAN_NHC UDP packet and return a high-level representation.
pub fn parse<T: AsRef<[u8]> + ?Sized>(
packet: &UdpPacket<&'a T>,
_src_addr: &ipv6::Address,
_dst_addr: &ipv6::Address,
_checksum: Option<u16>,
) -> Result<UdpNhcRepr<'a>> {
) -> Result<UdpNhcRepr> {
// Ensure basic accessors will work.
packet.check_len()?;

Expand All @@ -1502,12 +1502,11 @@ pub mod nhc {
Ok(UdpNhcRepr(UdpRepr {
src_port: packet.src_port(),
dst_port: packet.dst_port(),
payload: packet.payload(),
}))
}

/// Return the length of a packet that will be emitted from this high-level representation.
pub fn buffer_len(&self) -> usize {
pub fn header_len(&self) -> usize {
let mut len = 1; // The minimal header size

len += 2; // XXX We assume we will add the checksum at the end
Expand All @@ -1519,7 +1518,7 @@ pub mod nhc {
(_, _) => len + 4,
};

len + self.payload.len()
len
}

/// Emit a high-level representation into a LOWPAN_NHC UDP header.
Expand All @@ -1528,6 +1527,8 @@ pub mod nhc {
packet: &mut UdpPacket<T>,
src_addr: &Address,
dst_addr: &Address,
payload_len: usize,
emit_payload: impl FnOnce(&mut [u8]),
) {
packet.set_dispatch_field();
packet.set_ports(self.src_port, self.dst_port);
Expand All @@ -1537,28 +1538,28 @@ pub mod nhc {
&IpAddress::Ipv6(*src_addr),
&IpAddress::Ipv6(*dst_addr),
crate::wire::ip::Protocol::Udp,
self.payload.len() as u32 + 8,
payload_len as u32 + 8,
),
self.dst_port,
self.src_port,
self.payload.len() as u16 + 8,
checksum::data(self.payload),
payload_len as u16 + 8,
checksum::data(packet.payload_mut()),
]) ^ 0xffff;

packet.set_checksum(chk_sum);
packet.payload_mut()[..self.payload.len()].copy_from_slice(self.payload);
emit_payload(packet.payload_mut());
}
}

impl<'a> core::ops::Deref for UdpNhcRepr<'a> {
type Target = UdpRepr<'a>;
impl core::ops::Deref for UdpNhcRepr {
type Target = UdpRepr;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<'a> core::ops::DerefMut for UdpNhcRepr<'a> {
impl core::ops::DerefMut for UdpNhcRepr {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
Expand Down

0 comments on commit 4a5a0aa

Please sign in to comment.