Skip to content

Commit

Permalink
refactor: abstract SocketSet to AnySocketSet trait
Browse files Browse the repository at this point in the history
refactor: abstract SocketSet to AnySocketSet trait
  • Loading branch information
cavivie committed Jul 1, 2024
1 parent 358cf77 commit 987f963
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 121 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ license = "0BSD"
# ensure that the correct features are enabled.
autoexamples = false

[dependencies.managed]
rev = "428ccc2def4ce0e14a669320ef0a676d3c79afac"
git = "https://github.com/cavivie/rust-managed.git"
default-features = false
features = ["map"]

[dependencies]
managed = { version = "0.8", default-features = false, features = ["map"] }
byteorder = { version = "1.0", default-features = false }
log = { version = "0.4.4", default-features = false, optional = true }
libc = { version = "0.2.18", optional = true }
Expand Down
9 changes: 6 additions & 3 deletions src/iface/interface/ethernet.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use super::*;

impl InterfaceInner {
pub(super) fn process_ethernet<'frame>(
pub(super) fn process_ethernet<'frame, 'socket, S>(
&mut self,
sockets: &mut SocketSet,
sockets: &mut S,
meta: crate::phy::PacketMeta,
frame: &'frame [u8],
fragments: &'frame mut FragmentsBuffer,
) -> Option<EthernetPacket<'frame>> {
) -> Option<EthernetPacket<'frame>>
where
S: AnySocketSet<'socket>,
{
let eth_frame = check!(EthernetFrame::new_checked(frame));

// Ignore any packets not directed to our hardware address or any of the multicast groups.
Expand Down
9 changes: 6 additions & 3 deletions src/iface/interface/ieee802154.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ impl InterfaceInner {
no
}

pub(super) fn process_ieee802154<'output, 'payload: 'output>(
pub(super) fn process_ieee802154<'output, 'payload: 'output, 'socket, S>(
&mut self,
sockets: &mut SocketSet,
sockets: &mut S,
meta: PacketMeta,
sixlowpan_payload: &'payload [u8],
_fragments: &'output mut FragmentsBuffer,
) -> Option<Packet<'output>> {
) -> Option<Packet<'output>>
where
S: AnySocketSet<'socket>,
{
let ieee802154_frame = check!(Ieee802154Frame::new_checked(sixlowpan_payload));

if ieee802154_frame.frame_type() != Ieee802154FrameType::Data {
Expand Down
18 changes: 12 additions & 6 deletions src/iface/interface/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,16 @@ impl InterfaceInner {
})
}

pub(super) fn process_ipv4<'a>(
pub(super) fn process_ipv4<'a, 'socket, S>(
&mut self,
sockets: &mut SocketSet,
sockets: &mut S,
meta: PacketMeta,
ipv4_packet: &Ipv4Packet<&'a [u8]>,
frag: &'a mut FragmentsBuffer,
) -> Option<Packet<'a>> {
) -> Option<Packet<'a>>
where
S: AnySocketSet<'socket>,
{
let ipv4_repr = check!(Ipv4Repr::parse(ipv4_packet, &self.caps.checksum));
if !self.is_unicast_v4(ipv4_repr.src_addr) && !ipv4_repr.src_addr.is_unspecified() {
// Discard packets with non-unicast source addresses but allow unspecified
Expand Down Expand Up @@ -292,12 +295,15 @@ impl InterfaceInner {
}
}

pub(super) fn process_icmpv4<'frame>(
pub(super) fn process_icmpv4<'frame, 'socket, S>(
&mut self,
_sockets: &mut SocketSet,
_sockets: &mut S,
ip_repr: Ipv4Repr,
ip_payload: &'frame [u8],
) -> Option<Packet<'frame>> {
) -> Option<Packet<'frame>>
where
S: AnySocketSet<'socket>,
{
let icmp_packet = check!(Icmpv4Packet::new_checked(ip_payload));
let icmp_repr = check!(Icmpv4Repr::parse(&icmp_packet, &self.caps.checksum));

Expand Down
27 changes: 18 additions & 9 deletions src/iface/interface/ipv6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,15 @@ impl InterfaceInner {
})
}

pub(super) fn process_ipv6<'frame>(
pub(super) fn process_ipv6<'frame, 'socket, S>(
&mut self,
sockets: &mut SocketSet,
sockets: &mut S,
meta: PacketMeta,
ipv6_packet: &Ipv6Packet<&'frame [u8]>,
) -> Option<Packet<'frame>> {
) -> Option<Packet<'frame>>
where
S: AnySocketSet<'socket>,
{
let ipv6_repr = check!(Ipv6Repr::parse(ipv6_packet));

if !ipv6_repr.src_addr.is_unicast() {
Expand Down Expand Up @@ -297,15 +300,18 @@ impl InterfaceInner {

/// Given the next header value forward the payload onto the correct process
/// function.
fn process_nxt_hdr<'frame>(
fn process_nxt_hdr<'frame, 'socket, S>(
&mut self,
sockets: &mut SocketSet,
sockets: &mut S,
meta: PacketMeta,
ipv6_repr: Ipv6Repr,
nxt_hdr: IpProtocol,
handled_by_raw_socket: bool,
ip_payload: &'frame [u8],
) -> Option<Packet<'frame>> {
) -> Option<Packet<'frame>>
where
S: AnySocketSet<'socket>,
{
match nxt_hdr {
IpProtocol::Icmpv6 => self.process_icmpv6(sockets, ipv6_repr, ip_payload),

Expand Down Expand Up @@ -340,12 +346,15 @@ impl InterfaceInner {
}
}

pub(super) fn process_icmpv6<'frame>(
pub(super) fn process_icmpv6<'frame, 'socket, S>(
&mut self,
_sockets: &mut SocketSet,
_sockets: &mut S,
ip_repr: Ipv6Repr,
ip_payload: &'frame [u8],
) -> Option<Packet<'frame>> {
) -> Option<Packet<'frame>>
where
S: AnySocketSet<'socket>,
{
let icmp_packet = check!(Icmpv6Packet::new_checked(ip_payload));
let icmp_repr = check!(Icmpv6Repr::parse(
&ip_repr.src_addr,
Expand Down
41 changes: 28 additions & 13 deletions src/iface/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use super::fragmentation::{Fragmenter, FragmentsBuffer};

#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
use super::neighbor::{Answer as NeighborAnswer, Cache as NeighborCache};
use super::socket_set::SocketSet;
use super::socket_set::AnySocketSet;
use crate::config::{
IFACE_MAX_ADDR_COUNT, IFACE_MAX_MULTICAST_GROUP_COUNT,
IFACE_MAX_SIXLOWPAN_ADDRESS_CONTEXT_COUNT,
Expand Down Expand Up @@ -396,14 +396,15 @@ impl Interface {
/// This function returns a boolean value indicating whether any packets were
/// processed or emitted, and thus, whether the readiness of any socket might
/// have changed.
pub fn poll<D>(
pub fn poll<'socket, D, S>(
&mut self,
timestamp: Instant,
device: &mut D,
sockets: &mut SocketSet<'_>,
sockets: &mut S,
) -> bool
where
D: Device + ?Sized,
S: AnySocketSet<'socket>,
{
self.inner.now = timestamp;

Expand Down Expand Up @@ -459,7 +460,10 @@ impl Interface {
///
/// [poll]: #method.poll
/// [Instant]: struct.Instant.html
pub fn poll_at(&mut self, timestamp: Instant, sockets: &SocketSet<'_>) -> Option<Instant> {
pub fn poll_at<'socket, S>(&mut self, timestamp: Instant, sockets: &S) -> Option<Instant>
where
S: AnySocketSet<'socket>,
{
self.inner.now = timestamp;

#[cfg(feature = "_proto-fragmentation")]
Expand Down Expand Up @@ -493,17 +497,21 @@ impl Interface {
///
/// [poll]: #method.poll
/// [Duration]: struct.Duration.html
pub fn poll_delay(&mut self, timestamp: Instant, sockets: &SocketSet<'_>) -> Option<Duration> {
pub fn poll_delay<'socket, S>(&mut self, timestamp: Instant, sockets: &S) -> Option<Duration>
where
S: AnySocketSet<'socket>,
{
match self.poll_at(timestamp, sockets) {
Some(poll_at) if timestamp < poll_at => Some(poll_at - timestamp),
Some(_) => Some(Duration::from_millis(0)),
_ => None,
}
}

fn socket_ingress<D>(&mut self, device: &mut D, sockets: &mut SocketSet<'_>) -> bool
fn socket_ingress<'socket, D, S>(&mut self, device: &mut D, sockets: &mut S) -> bool
where
D: Device + ?Sized,
S: AnySocketSet<'socket>,
{
let mut processed_any = false;

Expand Down Expand Up @@ -572,9 +580,10 @@ impl Interface {
processed_any
}

fn socket_egress<D>(&mut self, device: &mut D, sockets: &mut SocketSet<'_>) -> bool
fn socket_egress<'socket, D, S>(&mut self, device: &mut D, sockets: &mut S) -> bool
where
D: Device + ?Sized,
S: AnySocketSet<'socket>,
{
let _caps = device.capabilities();

Expand Down Expand Up @@ -779,13 +788,16 @@ impl InterfaceInner {
}

#[cfg(feature = "medium-ip")]
fn process_ip<'frame>(
fn process_ip<'frame, 'socket, S>(
&mut self,
sockets: &mut SocketSet,
sockets: &mut S,
meta: PacketMeta,
ip_payload: &'frame [u8],
frag: &'frame mut FragmentsBuffer,
) -> Option<Packet<'frame>> {
) -> Option<Packet<'frame>>
where
S: AnySocketSet<'socket>,
{
match IpVersion::of_packet(ip_payload) {
#[cfg(feature = "proto-ipv4")]
Ok(IpVersion::Ipv4) => {
Expand All @@ -804,12 +816,15 @@ impl InterfaceInner {
}

#[cfg(feature = "socket-raw")]
fn raw_socket_filter(
fn raw_socket_filter<'socket, S>(
&mut self,
sockets: &mut SocketSet,
sockets: &mut S,
ip_repr: &IpRepr,
ip_payload: &[u8],
) -> bool {
) -> bool
where
S: AnySocketSet<'socket>,
{
let mut handled_by_raw_socket = false;

// Pass every IP packet to all raw sockets we have registered.
Expand Down
9 changes: 6 additions & 3 deletions src/iface/interface/sixlowpan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,17 @@ impl InterfaceInner {
tag
}

pub(super) fn process_sixlowpan<'output, 'payload: 'output>(
pub(super) fn process_sixlowpan<'output, 'payload: 'output, 'socket, S>(
&mut self,
sockets: &mut SocketSet,
sockets: &mut S,
meta: PacketMeta,
ieee802154_repr: &Ieee802154Repr,
payload: &'payload [u8],
f: &'output mut FragmentsBuffer,
) -> Option<Packet<'output>> {
) -> Option<Packet<'output>>
where
S: AnySocketSet<'socket>,
{
let payload = match check!(SixlowpanPacket::dispatch(payload)) {
#[cfg(not(feature = "proto-sixlowpan-fragmentation"))]
SixlowpanPacket::FragmentHeader => {
Expand Down
9 changes: 6 additions & 3 deletions src/iface/interface/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ use super::*;
use crate::socket::tcp::Socket;

impl InterfaceInner {
pub(crate) fn process_tcp<'frame>(
pub(crate) fn process_tcp<'frame, 'socket, S>(
&mut self,
sockets: &mut SocketSet,
sockets: &mut S,
ip_repr: IpRepr,
ip_payload: &'frame [u8],
) -> Option<Packet<'frame>> {
) -> Option<Packet<'frame>>
where
S: AnySocketSet<'socket>,
{
let (src_addr, dst_addr) = (ip_repr.src_addr(), ip_repr.dst_addr());
let tcp_packet = check!(TcpPacket::new_checked(ip_payload));
let tcp_repr = check!(TcpRepr::parse(
Expand Down
9 changes: 6 additions & 3 deletions src/iface/interface/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ use crate::socket::dns::Socket as DnsSocket;
use crate::socket::udp::Socket as UdpSocket;

impl InterfaceInner {
pub(super) fn process_udp<'frame>(
pub(super) fn process_udp<'frame, 'socket, S>(
&mut self,
sockets: &mut SocketSet,
sockets: &mut S,
meta: PacketMeta,
handled_by_raw_socket: bool,
ip_repr: IpRepr,
ip_payload: &'frame [u8],
) -> Option<Packet<'frame>> {
) -> Option<Packet<'frame>>
where
S: AnySocketSet<'socket>,
{
let (src_addr, dst_addr) = (ip_repr.src_addr(), ip_repr.dst_addr());
let udp_packet = check!(UdpPacket::new_checked(ip_payload));
let udp_repr = check!(UdpRepr::parse(
Expand Down
2 changes: 1 addition & 1 deletion src/iface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ pub use self::interface::MulticastError;
pub use self::interface::{Config, Interface, InterfaceInner as Context};

pub use self::route::{Route, RouteTableFull, Routes};
pub use self::socket_set::{SocketHandle, SocketSet, SocketStorage};
pub use self::socket_set::{AnySocketSet, SocketHandle, SocketSet, SocketStorage};
Loading

0 comments on commit 987f963

Please sign in to comment.