Warning
This post was published 55 days ago. The information described in this article may have changed.
I am using smoltcp-rs smoltcp ยท GitHub
I have created 4 functions doing similar stuff, however parse_udp enforced me to use the lifetime while previous calls didn't.
But finally parse_dhcp isn't working because "returns a value referencing data owned by the current function"
Please explain me the differences...
fn parse_ethernet(payload: &[u8])
-> Result<(&[u8], EthernetRepr), PacketParseError> {
let eth_frame =
EthernetFrame::new_checked(payload)
.map_err(PacketParseError::EthernetError)?;
let eth_repr =
EthernetRepr::parse(ð_frame)
.map_err(PacketParseError::EthernetError)?;
Ok((eth_frame.payload(), eth_repr))
}
fn parse_ipv4(payload: &[u8])
-> Result<(&[u8], Ipv4Repr), PacketParseError> {
let ipv4_packet =
Ipv4Packet::new_checked(payload)
.map_err(|e| PacketParseError::Ipv4PacketError(e))?;
let ipv4_repr =
Ipv4Repr::parse(&ipv4_packet, &ChecksumCapabilities::ignored())
.map_err(PacketParseError::Ipv4ReprParseError)?; // shorthand for |e| PacketParseError::Ipv4ReprParseError(e)
Ok((ipv4_packet.payload(), ipv4_repr))
}
fn parse_udp<'a>(payload: &'a [u8], ipv4_repr: &Ipv4Repr)
-> Result<(&'a [u8], UdpRepr), PacketParseError> {
let udp_packet =
UdpPacket::new_checked(payload)
.map_err(PacketParseError::UdpPacketError)?;
let udp_repr =
UdpRepr::parse(
&udp_packet,
&IpAddress::from(ipv4_repr.src_addr), // Convert Ipv4Address to IpAddress
&IpAddress::from(ipv4_repr.dst_addr), // Convert Ipv4Address to IpAddress
&ChecksumCapabilities::ignored())
.map_err(PacketParseError::UdpReprParseError)?;
Ok((udp_packet.payload(), udp_repr))
}
fn parse_dhcp(payload: &[u8])
-> Result<DhcpRepr, PacketParseError> {
let dhcp_packet =
DhcpPacket::new_checked(payload)
.map_err(PacketParseError::DhcpPacketError)?;
let dhcp_repr =
DhcpRepr::parse(&dhcp_packet)
.map_err(PacketParseError::DhcpReprParseError)?;
Ok(dhcp_repr) // this gives an error :: returns a value referencing data owned by the current function
}
Thanx.
9 posts - 3 participants
๐ท๏ธ rust_feed