Skip to content

Commit 861e605

Browse files
committed
refactor: make internal APIs public for external use
- Change visibility of many internal structs, enums, and methods from `pub(crate)`to`pub` - This includes fragmentation components, interface internals, neighbor cache, socket structures, and wire protocol modules - Add new public methods to neighbor cache for iteration and inspection - Update test utilities to be publicly accessible - No functional changes, only visibility modifications to enable external library usage Signed-off-by: longjin <longjin@dragonos.org>
1 parent d2d6470 commit 861e605

35 files changed

+211
-182
lines changed

src/iface/fragmentation.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ impl<K> PacketAssembler<K> {
7676
}
7777
}
7878

79-
pub(crate) fn reset(&mut self) {
79+
pub fn reset(&mut self) {
8080
self.key = None;
8181
self.assembler.clear();
8282
self.total_size = None;
8383
self.expires_at = Instant::ZERO;
8484
}
8585

8686
/// Set the total size of the packet assembler.
87-
pub(crate) fn set_total_size(&mut self, size: usize) -> Result<(), AssemblerError> {
87+
pub fn set_total_size(&mut self, size: usize) -> Result<(), AssemblerError> {
8888
if let Some(old_size) = self.total_size {
8989
if old_size != size {
9090
return Err(AssemblerError);
@@ -106,11 +106,11 @@ impl<K> PacketAssembler<K> {
106106
}
107107

108108
/// Return the instant when the assembler expires.
109-
pub(crate) fn expires_at(&self) -> Instant {
109+
pub fn expires_at(&self) -> Instant {
110110
self.expires_at
111111
}
112112

113-
pub(crate) fn add_with(
113+
pub fn add_with(
114114
&mut self,
115115
offset: usize,
116116
f: impl Fn(&mut [u8]) -> Result<usize, AssemblerError>,
@@ -138,7 +138,7 @@ impl<K> PacketAssembler<K> {
138138
///
139139
/// - Returns [`Error::PacketAssemblerBufferTooSmall`] when trying to add data into the buffer at a non-existing
140140
/// place.
141-
pub(crate) fn add(&mut self, data: &[u8], offset: usize) -> Result<(), AssemblerError> {
141+
pub fn add(&mut self, data: &[u8], offset: usize) -> Result<(), AssemblerError> {
142142
#[cfg(not(feature = "alloc"))]
143143
if self.buffer.len() < offset + data.len() {
144144
return Err(AssemblerError);
@@ -164,7 +164,7 @@ impl<K> PacketAssembler<K> {
164164

165165
/// Get an immutable slice of the underlying packet data, if reassembly complete.
166166
/// This will mark the assembler as empty, so that it can be reused.
167-
pub(crate) fn assemble(&mut self) -> Option<&'_ [u8]> {
167+
pub fn assemble(&mut self) -> Option<&'_ [u8]> {
168168
if !self.is_complete() {
169169
return None;
170170
}
@@ -176,7 +176,7 @@ impl<K> PacketAssembler<K> {
176176
}
177177

178178
/// Returns `true` when all fragments have been received, otherwise `false`.
179-
pub(crate) fn is_complete(&self) -> bool {
179+
pub fn is_complete(&self) -> bool {
180180
self.total_size == Some(self.assembler.peek_front())
181181
}
182182

@@ -207,7 +207,7 @@ impl<K: Eq + Copy> PacketAssemblerSet<K> {
207207
/// If it doesn't exist, it is created, with the `expires_at` timestamp.
208208
///
209209
/// If the assembler set is full, in which case an error is returned.
210-
pub(crate) fn get(
210+
pub fn get(
211211
&mut self,
212212
key: &K,
213213
expires_at: Instant,
@@ -240,19 +240,19 @@ impl<K: Eq + Copy> PacketAssemblerSet<K> {
240240

241241
// Max len of non-fragmented packets after decompression (including ipv6 header and payload)
242242
// TODO: lower. Should be (6lowpan mtu) - (min 6lowpan header size) + (max ipv6 header size)
243-
pub(crate) const MAX_DECOMPRESSED_LEN: usize = 1500;
243+
pub const MAX_DECOMPRESSED_LEN: usize = 1500;
244244

245245
#[cfg(feature = "_proto-fragmentation")]
246246
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, Copy)]
247247
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
248-
pub(crate) enum FragKey {
248+
pub enum FragKey {
249249
#[cfg(feature = "proto-ipv4-fragmentation")]
250250
Ipv4(Ipv4FragKey),
251251
#[cfg(feature = "proto-sixlowpan-fragmentation")]
252252
Sixlowpan(SixlowpanFragKey),
253253
}
254254

255-
pub(crate) struct FragmentsBuffer {
255+
pub struct FragmentsBuffer {
256256
#[cfg(feature = "proto-sixlowpan")]
257257
pub decompress_buf: [u8; MAX_DECOMPRESSED_LEN],
258258

@@ -264,17 +264,17 @@ pub(crate) struct FragmentsBuffer {
264264
}
265265

266266
#[cfg(not(feature = "_proto-fragmentation"))]
267-
pub(crate) struct Fragmenter {}
267+
pub struct Fragmenter {}
268268

269269
#[cfg(not(feature = "_proto-fragmentation"))]
270270
impl Fragmenter {
271-
pub(crate) fn new() -> Self {
271+
pub fn new() -> Self {
272272
Self {}
273273
}
274274
}
275275

276276
#[cfg(feature = "_proto-fragmentation")]
277-
pub(crate) struct Fragmenter {
277+
pub struct Fragmenter {
278278
/// The buffer that holds the unfragmented 6LoWPAN packet.
279279
pub buffer: [u8; FRAGMENTATION_BUFFER_SIZE],
280280
/// The size of the packet without the IEEE802.15.4 header and the fragmentation headers.
@@ -289,7 +289,7 @@ pub(crate) struct Fragmenter {
289289
}
290290

291291
#[cfg(feature = "proto-ipv4-fragmentation")]
292-
pub(crate) struct Ipv4Fragmenter {
292+
pub struct Ipv4Fragmenter {
293293
/// The IPv4 representation.
294294
pub repr: Ipv4Repr,
295295
/// The destination hardware address.
@@ -302,7 +302,7 @@ pub(crate) struct Ipv4Fragmenter {
302302
}
303303

304304
#[cfg(feature = "proto-sixlowpan-fragmentation")]
305-
pub(crate) struct SixlowpanFragmenter {
305+
pub struct SixlowpanFragmenter {
306306
/// The datagram size that is used for the fragmentation headers.
307307
pub datagram_size: u16,
308308
/// The datagram tag that is used for the fragmentation headers.
@@ -320,7 +320,7 @@ pub(crate) struct SixlowpanFragmenter {
320320

321321
#[cfg(feature = "_proto-fragmentation")]
322322
impl Fragmenter {
323-
pub(crate) fn new() -> Self {
323+
pub fn new() -> Self {
324324
Self {
325325
buffer: [0u8; FRAGMENTATION_BUFFER_SIZE],
326326
packet_len: 0,
@@ -355,18 +355,18 @@ impl Fragmenter {
355355

356356
/// Return `true` when everything is transmitted.
357357
#[inline]
358-
pub(crate) fn finished(&self) -> bool {
358+
pub fn finished(&self) -> bool {
359359
self.packet_len == self.sent_bytes
360360
}
361361

362362
/// Returns `true` when there is nothing to transmit.
363363
#[inline]
364-
pub(crate) fn is_empty(&self) -> bool {
364+
pub fn is_empty(&self) -> bool {
365365
self.packet_len == 0
366366
}
367367

368368
// Reset the buffer.
369-
pub(crate) fn reset(&mut self) {
369+
pub fn reset(&mut self) {
370370
self.packet_len = 0;
371371
self.sent_bytes = 0;
372372

src/iface/interface/ipv4.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl InterfaceInner {
4141
/// **NOTE**: unlike for IPv6, no specific selection algorithm is implemented. The first IPv4
4242
/// address from the interface is returned.
4343
#[allow(unused)]
44-
pub(crate) fn get_source_address_ipv4(&self, _dst_addr: &Ipv4Address) -> Option<Ipv4Address> {
44+
pub fn get_source_address_ipv4(&self, _dst_addr: &Ipv4Address) -> Option<Ipv4Address> {
4545
for cidr in self.ip_addrs.iter() {
4646
#[allow(irrefutable_let_patterns)] // if only ipv4 is enabled
4747
if let IpCidr::Ipv4(cidr) = cidr {
@@ -53,7 +53,7 @@ impl InterfaceInner {
5353

5454
/// Checks if an address is broadcast, taking into account ipv4 subnet-local
5555
/// broadcast addresses.
56-
pub(crate) fn is_broadcast_v4(&self, address: Ipv4Address) -> bool {
56+
pub fn is_broadcast_v4(&self, address: Ipv4Address) -> bool {
5757
if address.is_broadcast() {
5858
return true;
5959
}

src/iface/interface/ipv6.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl InterfaceInner {
2525
/// # Panics
2626
/// This function panics if the destination address is unspecified.
2727
#[allow(unused)]
28-
pub(crate) fn get_source_address_ipv6(&self, dst_addr: &Ipv6Address) -> Ipv6Address {
28+
pub fn get_source_address_ipv6(&self, dst_addr: &Ipv6Address) -> Ipv6Address {
2929
assert!(!dst_addr.is_unspecified());
3030

3131
// See RFC 6724 Section 4: Candidate source address

src/iface/interface/mod.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod ipv6;
1818
mod sixlowpan;
1919

2020
#[cfg(feature = "multicast")]
21-
pub(crate) mod multicast;
21+
pub mod multicast;
2222
#[cfg(feature = "socket-tcp")]
2323
mod tcp;
2424
#[cfg(any(feature = "socket-udp", feature = "socket-dns"))]
@@ -109,7 +109,7 @@ pub enum PollIngressSingleResult {
109109
/// a dependency on heap allocation, it instead owns a `BorrowMut<[T]>`, which can be
110110
/// a `&mut [T]`, or `Vec<T>` if a heap is available.
111111
pub struct Interface {
112-
pub(crate) inner: InterfaceInner,
112+
pub inner: InterfaceInner,
113113
fragments: FragmentsBuffer,
114114
fragmenter: Fragmenter,
115115
}
@@ -122,8 +122,8 @@ pub struct Interface {
122122
/// methods on the `Interface` in this time (since its `device` field is borrowed
123123
/// exclusively). However, it is still possible to call methods on its `inner` field.
124124
pub struct InterfaceInner {
125-
caps: DeviceCapabilities,
126-
now: Instant,
125+
pub caps: DeviceCapabilities,
126+
pub now: Instant,
127127
rand: Rand,
128128

129129
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
@@ -762,33 +762,33 @@ impl Interface {
762762

763763
impl InterfaceInner {
764764
#[allow(unused)] // unused depending on which sockets are enabled
765-
pub(crate) fn now(&self) -> Instant {
765+
pub fn now(&self) -> Instant {
766766
self.now
767767
}
768768

769769
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
770770
#[allow(unused)] // unused depending on which sockets are enabled
771-
pub(crate) fn hardware_addr(&self) -> HardwareAddress {
771+
pub fn hardware_addr(&self) -> HardwareAddress {
772772
self.hardware_addr
773773
}
774774

775775
#[allow(unused)] // unused depending on which sockets are enabled
776-
pub(crate) fn checksum_caps(&self) -> ChecksumCapabilities {
776+
pub fn checksum_caps(&self) -> ChecksumCapabilities {
777777
self.caps.checksum.clone()
778778
}
779779

780780
#[allow(unused)] // unused depending on which sockets are enabled
781-
pub(crate) fn ip_mtu(&self) -> usize {
781+
pub fn ip_mtu(&self) -> usize {
782782
self.caps.ip_mtu()
783783
}
784784

785785
#[allow(unused)] // unused depending on which sockets are enabled, and in tests
786-
pub(crate) fn rand(&mut self) -> &mut Rand {
786+
pub fn rand(&mut self) -> &mut Rand {
787787
&mut self.rand
788788
}
789789

790790
#[allow(unused)] // unused depending on which sockets are enabled
791-
pub(crate) fn get_source_address(&self, dst_addr: &IpAddress) -> Option<IpAddress> {
791+
pub fn get_source_address(&self, dst_addr: &IpAddress) -> Option<IpAddress> {
792792
match dst_addr {
793793
#[cfg(feature = "proto-ipv4")]
794794
IpAddress::Ipv4(addr) => self.get_source_address_ipv4(addr).map(|a| a.into()),
@@ -799,7 +799,7 @@ impl InterfaceInner {
799799

800800
#[cfg(test)]
801801
#[allow(unused)] // unused depending on which sockets are enabled
802-
pub(crate) fn set_now(&mut self, now: Instant) {
802+
pub fn set_now(&mut self, now: Instant) {
803803
self.now = now
804804
}
805805

@@ -895,7 +895,7 @@ impl InterfaceInner {
895895

896896
/// Checks if an address is broadcast, taking into account ipv4 subnet-local
897897
/// broadcast addresses.
898-
pub(crate) fn is_broadcast(&self, address: &IpAddress) -> bool {
898+
pub fn is_broadcast(&self, address: &IpAddress) -> bool {
899899
match address {
900900
#[cfg(feature = "proto-ipv4")]
901901
IpAddress::Ipv4(address) => self.is_broadcast_v4(*address),
@@ -938,11 +938,11 @@ impl InterfaceInner {
938938
}
939939
}
940940

941-
fn in_same_network(&self, addr: &IpAddress) -> bool {
941+
pub fn in_same_network(&self, addr: &IpAddress) -> bool {
942942
self.ip_addrs.iter().any(|cidr| cidr.contains_addr(addr))
943943
}
944944

945-
fn route(&self, addr: &IpAddress, timestamp: Instant) -> Option<IpAddress> {
945+
pub fn route(&self, addr: &IpAddress, timestamp: Instant) -> Option<IpAddress> {
946946
// Send directly.
947947
// note: no need to use `self.is_broadcast()` to check for subnet-local broadcast addrs
948948
// here because `in_same_network` will already return true.
@@ -1121,6 +1121,12 @@ impl InterfaceInner {
11211121
self.neighbor_cache.flush()
11221122
}
11231123

1124+
/// Get the neighbor cache.
1125+
#[cfg(any(feature = "medium-ethernet", feature = "medium-ieee802154"))]
1126+
pub fn neighbor_cache(&self) -> &NeighborCache {
1127+
&self.neighbor_cache
1128+
}
1129+
11241130
fn dispatch_ip<Tx: TxToken>(
11251131
&mut self,
11261132
// NOTE(unused_mut): tx_token isn't always mutated, depending on

src/iface/interface/multicast.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum MulticastError {
1919
}
2020

2121
#[cfg(feature = "proto-ipv4")]
22-
pub(crate) enum IgmpReportState {
22+
pub enum IgmpReportState {
2323
Inactive,
2424
ToGeneralQuery {
2525
version: IgmpVersion,
@@ -56,7 +56,7 @@ enum GroupState {
5656
Leaving,
5757
}
5858

59-
pub(crate) struct State {
59+
pub struct State {
6060
groups: LinearMap<IpAddress, GroupState, IFACE_MAX_MULTICAST_GROUP_COUNT>,
6161
/// When to report for (all or) the next multicast group membership via IGMP
6262
#[cfg(feature = "proto-ipv4")]
@@ -66,7 +66,7 @@ pub(crate) struct State {
6666
}
6767

6868
impl State {
69-
pub(crate) fn new() -> Self {
69+
pub fn new() -> Self {
7070
Self {
7171
groups: LinearMap::new(),
7272
#[cfg(feature = "proto-ipv4")]
@@ -76,7 +76,7 @@ impl State {
7676
}
7777
}
7878

79-
pub(crate) fn has_multicast_group<T: Into<IpAddress>>(&self, addr: T) -> bool {
79+
pub fn has_multicast_group<T: Into<IpAddress>>(&self, addr: T) -> bool {
8080
// Return false if we don't have the multicast group,
8181
// or we're leaving it.
8282
match self.groups.get(&addr.into()) {
@@ -184,7 +184,7 @@ impl Interface {
184184
/// - Send join/leave packets according to the multicast group state.
185185
/// - Depending on `igmp_report_state` and the therein contained
186186
/// timeouts, send IGMP membership reports.
187-
pub(crate) fn multicast_egress(&mut self, device: &mut (impl Device + ?Sized)) {
187+
pub fn multicast_egress(&mut self, device: &mut (impl Device + ?Sized)) {
188188
// Process multicast joins.
189189
while let Some((&addr, _)) = self
190190
.inner

src/iface/interface/sixlowpan.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::wire::Result;
33

44
// Max len of non-fragmented packets after decompression (including ipv6 header and payload)
55
// TODO: lower. Should be (6lowpan mtu) - (min 6lowpan header size) + (max ipv6 header size)
6-
pub(crate) const MAX_DECOMPRESSED_LEN: usize = 1500;
6+
pub const MAX_DECOMPRESSED_LEN: usize = 1500;
77

88
impl Interface {
99
/// Process fragments that still need to be sent for 6LoWPAN packets.

src/iface/interface/tcp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use super::*;
33
use crate::socket::tcp::Socket;
44

55
impl InterfaceInner {
6-
pub(crate) fn process_tcp<'frame>(
6+
pub fn process_tcp<'frame>(
77
&mut self,
88
sockets: &mut SocketSet,
99
ip_repr: IpRepr,

src/iface/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod rpl;
1414
mod socket_meta;
1515
mod socket_set;
1616

17-
mod packet;
17+
pub mod packet;
1818

1919
#[cfg(feature = "multicast")]
2020
pub use self::interface::multicast::MulticastError;

0 commit comments

Comments
 (0)