Skip to content

Commit 987f963

Browse files
committed
refactor: abstract SocketSet to AnySocketSet trait
refactor: abstract SocketSet to AnySocketSet trait
1 parent 358cf77 commit 987f963

File tree

11 files changed

+195
-121
lines changed

11 files changed

+195
-121
lines changed

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ license = "0BSD"
1616
# ensure that the correct features are enabled.
1717
autoexamples = false
1818

19+
[dependencies.managed]
20+
rev = "428ccc2def4ce0e14a669320ef0a676d3c79afac"
21+
git = "https://github.com/cavivie/rust-managed.git"
22+
default-features = false
23+
features = ["map"]
24+
1925
[dependencies]
20-
managed = { version = "0.8", default-features = false, features = ["map"] }
2126
byteorder = { version = "1.0", default-features = false }
2227
log = { version = "0.4.4", default-features = false, optional = true }
2328
libc = { version = "0.2.18", optional = true }

src/iface/interface/ethernet.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
use super::*;
22

33
impl InterfaceInner {
4-
pub(super) fn process_ethernet<'frame>(
4+
pub(super) fn process_ethernet<'frame, 'socket, S>(
55
&mut self,
6-
sockets: &mut SocketSet,
6+
sockets: &mut S,
77
meta: crate::phy::PacketMeta,
88
frame: &'frame [u8],
99
fragments: &'frame mut FragmentsBuffer,
10-
) -> Option<EthernetPacket<'frame>> {
10+
) -> Option<EthernetPacket<'frame>>
11+
where
12+
S: AnySocketSet<'socket>,
13+
{
1114
let eth_frame = check!(EthernetFrame::new_checked(frame));
1215

1316
// Ignore any packets not directed to our hardware address or any of the multicast groups.

src/iface/interface/ieee802154.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ impl InterfaceInner {
99
no
1010
}
1111

12-
pub(super) fn process_ieee802154<'output, 'payload: 'output>(
12+
pub(super) fn process_ieee802154<'output, 'payload: 'output, 'socket, S>(
1313
&mut self,
14-
sockets: &mut SocketSet,
14+
sockets: &mut S,
1515
meta: PacketMeta,
1616
sixlowpan_payload: &'payload [u8],
1717
_fragments: &'output mut FragmentsBuffer,
18-
) -> Option<Packet<'output>> {
18+
) -> Option<Packet<'output>>
19+
where
20+
S: AnySocketSet<'socket>,
21+
{
1922
let ieee802154_frame = check!(Ieee802154Frame::new_checked(sixlowpan_payload));
2023

2124
if ieee802154_frame.frame_type() != Ieee802154FrameType::Data {

src/iface/interface/ipv4.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,16 @@ impl InterfaceInner {
8787
})
8888
}
8989

90-
pub(super) fn process_ipv4<'a>(
90+
pub(super) fn process_ipv4<'a, 'socket, S>(
9191
&mut self,
92-
sockets: &mut SocketSet,
92+
sockets: &mut S,
9393
meta: PacketMeta,
9494
ipv4_packet: &Ipv4Packet<&'a [u8]>,
9595
frag: &'a mut FragmentsBuffer,
96-
) -> Option<Packet<'a>> {
96+
) -> Option<Packet<'a>>
97+
where
98+
S: AnySocketSet<'socket>,
99+
{
97100
let ipv4_repr = check!(Ipv4Repr::parse(ipv4_packet, &self.caps.checksum));
98101
if !self.is_unicast_v4(ipv4_repr.src_addr) && !ipv4_repr.src_addr.is_unspecified() {
99102
// Discard packets with non-unicast source addresses but allow unspecified
@@ -292,12 +295,15 @@ impl InterfaceInner {
292295
}
293296
}
294297

295-
pub(super) fn process_icmpv4<'frame>(
298+
pub(super) fn process_icmpv4<'frame, 'socket, S>(
296299
&mut self,
297-
_sockets: &mut SocketSet,
300+
_sockets: &mut S,
298301
ip_repr: Ipv4Repr,
299302
ip_payload: &'frame [u8],
300-
) -> Option<Packet<'frame>> {
303+
) -> Option<Packet<'frame>>
304+
where
305+
S: AnySocketSet<'socket>,
306+
{
301307
let icmp_packet = check!(Icmpv4Packet::new_checked(ip_payload));
302308
let icmp_repr = check!(Icmpv4Repr::parse(&icmp_packet, &self.caps.checksum));
303309

src/iface/interface/ipv6.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,15 @@ impl InterfaceInner {
183183
})
184184
}
185185

186-
pub(super) fn process_ipv6<'frame>(
186+
pub(super) fn process_ipv6<'frame, 'socket, S>(
187187
&mut self,
188-
sockets: &mut SocketSet,
188+
sockets: &mut S,
189189
meta: PacketMeta,
190190
ipv6_packet: &Ipv6Packet<&'frame [u8]>,
191-
) -> Option<Packet<'frame>> {
191+
) -> Option<Packet<'frame>>
192+
where
193+
S: AnySocketSet<'socket>,
194+
{
192195
let ipv6_repr = check!(Ipv6Repr::parse(ipv6_packet));
193196

194197
if !ipv6_repr.src_addr.is_unicast() {
@@ -297,15 +300,18 @@ impl InterfaceInner {
297300

298301
/// Given the next header value forward the payload onto the correct process
299302
/// function.
300-
fn process_nxt_hdr<'frame>(
303+
fn process_nxt_hdr<'frame, 'socket, S>(
301304
&mut self,
302-
sockets: &mut SocketSet,
305+
sockets: &mut S,
303306
meta: PacketMeta,
304307
ipv6_repr: Ipv6Repr,
305308
nxt_hdr: IpProtocol,
306309
handled_by_raw_socket: bool,
307310
ip_payload: &'frame [u8],
308-
) -> Option<Packet<'frame>> {
311+
) -> Option<Packet<'frame>>
312+
where
313+
S: AnySocketSet<'socket>,
314+
{
309315
match nxt_hdr {
310316
IpProtocol::Icmpv6 => self.process_icmpv6(sockets, ipv6_repr, ip_payload),
311317

@@ -340,12 +346,15 @@ impl InterfaceInner {
340346
}
341347
}
342348

343-
pub(super) fn process_icmpv6<'frame>(
349+
pub(super) fn process_icmpv6<'frame, 'socket, S>(
344350
&mut self,
345-
_sockets: &mut SocketSet,
351+
_sockets: &mut S,
346352
ip_repr: Ipv6Repr,
347353
ip_payload: &'frame [u8],
348-
) -> Option<Packet<'frame>> {
354+
) -> Option<Packet<'frame>>
355+
where
356+
S: AnySocketSet<'socket>,
357+
{
349358
let icmp_packet = check!(Icmpv6Packet::new_checked(ip_payload));
350359
let icmp_repr = check!(Icmpv6Repr::parse(
351360
&ip_repr.src_addr,

src/iface/interface/mod.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use super::fragmentation::{Fragmenter, FragmentsBuffer};
4040

4141
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
4242
use super::neighbor::{Answer as NeighborAnswer, Cache as NeighborCache};
43-
use super::socket_set::SocketSet;
43+
use super::socket_set::AnySocketSet;
4444
use crate::config::{
4545
IFACE_MAX_ADDR_COUNT, IFACE_MAX_MULTICAST_GROUP_COUNT,
4646
IFACE_MAX_SIXLOWPAN_ADDRESS_CONTEXT_COUNT,
@@ -396,14 +396,15 @@ impl Interface {
396396
/// This function returns a boolean value indicating whether any packets were
397397
/// processed or emitted, and thus, whether the readiness of any socket might
398398
/// have changed.
399-
pub fn poll<D>(
399+
pub fn poll<'socket, D, S>(
400400
&mut self,
401401
timestamp: Instant,
402402
device: &mut D,
403-
sockets: &mut SocketSet<'_>,
403+
sockets: &mut S,
404404
) -> bool
405405
where
406406
D: Device + ?Sized,
407+
S: AnySocketSet<'socket>,
407408
{
408409
self.inner.now = timestamp;
409410

@@ -459,7 +460,10 @@ impl Interface {
459460
///
460461
/// [poll]: #method.poll
461462
/// [Instant]: struct.Instant.html
462-
pub fn poll_at(&mut self, timestamp: Instant, sockets: &SocketSet<'_>) -> Option<Instant> {
463+
pub fn poll_at<'socket, S>(&mut self, timestamp: Instant, sockets: &S) -> Option<Instant>
464+
where
465+
S: AnySocketSet<'socket>,
466+
{
463467
self.inner.now = timestamp;
464468

465469
#[cfg(feature = "_proto-fragmentation")]
@@ -493,17 +497,21 @@ impl Interface {
493497
///
494498
/// [poll]: #method.poll
495499
/// [Duration]: struct.Duration.html
496-
pub fn poll_delay(&mut self, timestamp: Instant, sockets: &SocketSet<'_>) -> Option<Duration> {
500+
pub fn poll_delay<'socket, S>(&mut self, timestamp: Instant, sockets: &S) -> Option<Duration>
501+
where
502+
S: AnySocketSet<'socket>,
503+
{
497504
match self.poll_at(timestamp, sockets) {
498505
Some(poll_at) if timestamp < poll_at => Some(poll_at - timestamp),
499506
Some(_) => Some(Duration::from_millis(0)),
500507
_ => None,
501508
}
502509
}
503510

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

@@ -572,9 +580,10 @@ impl Interface {
572580
processed_any
573581
}
574582

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

@@ -779,13 +788,16 @@ impl InterfaceInner {
779788
}
780789

781790
#[cfg(feature = "medium-ip")]
782-
fn process_ip<'frame>(
791+
fn process_ip<'frame, 'socket, S>(
783792
&mut self,
784-
sockets: &mut SocketSet,
793+
sockets: &mut S,
785794
meta: PacketMeta,
786795
ip_payload: &'frame [u8],
787796
frag: &'frame mut FragmentsBuffer,
788-
) -> Option<Packet<'frame>> {
797+
) -> Option<Packet<'frame>>
798+
where
799+
S: AnySocketSet<'socket>,
800+
{
789801
match IpVersion::of_packet(ip_payload) {
790802
#[cfg(feature = "proto-ipv4")]
791803
Ok(IpVersion::Ipv4) => {
@@ -804,12 +816,15 @@ impl InterfaceInner {
804816
}
805817

806818
#[cfg(feature = "socket-raw")]
807-
fn raw_socket_filter(
819+
fn raw_socket_filter<'socket, S>(
808820
&mut self,
809-
sockets: &mut SocketSet,
821+
sockets: &mut S,
810822
ip_repr: &IpRepr,
811823
ip_payload: &[u8],
812-
) -> bool {
824+
) -> bool
825+
where
826+
S: AnySocketSet<'socket>,
827+
{
813828
let mut handled_by_raw_socket = false;
814829

815830
// Pass every IP packet to all raw sockets we have registered.

src/iface/interface/sixlowpan.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,17 @@ impl InterfaceInner {
5858
tag
5959
}
6060

61-
pub(super) fn process_sixlowpan<'output, 'payload: 'output>(
61+
pub(super) fn process_sixlowpan<'output, 'payload: 'output, 'socket, S>(
6262
&mut self,
63-
sockets: &mut SocketSet,
63+
sockets: &mut S,
6464
meta: PacketMeta,
6565
ieee802154_repr: &Ieee802154Repr,
6666
payload: &'payload [u8],
6767
f: &'output mut FragmentsBuffer,
68-
) -> Option<Packet<'output>> {
68+
) -> Option<Packet<'output>>
69+
where
70+
S: AnySocketSet<'socket>,
71+
{
6972
let payload = match check!(SixlowpanPacket::dispatch(payload)) {
7073
#[cfg(not(feature = "proto-sixlowpan-fragmentation"))]
7174
SixlowpanPacket::FragmentHeader => {

src/iface/interface/tcp.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ use super::*;
33
use crate::socket::tcp::Socket;
44

55
impl InterfaceInner {
6-
pub(crate) fn process_tcp<'frame>(
6+
pub(crate) fn process_tcp<'frame, 'socket, S>(
77
&mut self,
8-
sockets: &mut SocketSet,
8+
sockets: &mut S,
99
ip_repr: IpRepr,
1010
ip_payload: &'frame [u8],
11-
) -> Option<Packet<'frame>> {
11+
) -> Option<Packet<'frame>>
12+
where
13+
S: AnySocketSet<'socket>,
14+
{
1215
let (src_addr, dst_addr) = (ip_repr.src_addr(), ip_repr.dst_addr());
1316
let tcp_packet = check!(TcpPacket::new_checked(ip_payload));
1417
let tcp_repr = check!(TcpRepr::parse(

src/iface/interface/udp.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@ use crate::socket::dns::Socket as DnsSocket;
77
use crate::socket::udp::Socket as UdpSocket;
88

99
impl InterfaceInner {
10-
pub(super) fn process_udp<'frame>(
10+
pub(super) fn process_udp<'frame, 'socket, S>(
1111
&mut self,
12-
sockets: &mut SocketSet,
12+
sockets: &mut S,
1313
meta: PacketMeta,
1414
handled_by_raw_socket: bool,
1515
ip_repr: IpRepr,
1616
ip_payload: &'frame [u8],
17-
) -> Option<Packet<'frame>> {
17+
) -> Option<Packet<'frame>>
18+
where
19+
S: AnySocketSet<'socket>,
20+
{
1821
let (src_addr, dst_addr) = (ip_repr.src_addr(), ip_repr.dst_addr());
1922
let udp_packet = check!(UdpPacket::new_checked(ip_payload));
2023
let udp_repr = check!(UdpRepr::parse(

src/iface/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ pub use self::interface::MulticastError;
2121
pub use self::interface::{Config, Interface, InterfaceInner as Context};
2222

2323
pub use self::route::{Route, RouteTableFull, Routes};
24-
pub use self::socket_set::{SocketHandle, SocketSet, SocketStorage};
24+
pub use self::socket_set::{AnySocketSet, SocketHandle, SocketSet, SocketStorage};

0 commit comments

Comments
 (0)