Skip to content

Commit b8fb01c

Browse files
authored
Merge pull request #1003 from ProfFan/fan/add_send_recv_queue
Add `send_queue`/`recv_queue` to UDP/ICMP/Raw Socket
2 parents 032094e + dad57e8 commit b8fb01c

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

src/socket/icmp.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,16 @@ impl<'a> Socket<'a> {
412412
Ok((length, endpoint))
413413
}
414414

415+
/// Return the amount of octets queued in the transmit buffer.
416+
pub fn send_queue(&self) -> usize {
417+
self.tx_buffer.payload_bytes_count()
418+
}
419+
420+
/// Return the amount of octets queued in the receive buffer.
421+
pub fn recv_queue(&self) -> usize {
422+
self.rx_buffer.payload_bytes_count()
423+
}
424+
415425
/// Fitler determining whether the socket accepts a given ICMPv4 packet.
416426
/// Accepted packets are enqueued into the socket's receive buffer.
417427
#[cfg(feature = "proto-ipv4")]

src/socket/raw.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,16 @@ impl<'a> Socket<'a> {
327327
Ok(length)
328328
}
329329

330+
/// Return the amount of octets queued in the transmit buffer.
331+
pub fn send_queue(&self) -> usize {
332+
self.tx_buffer.payload_bytes_count()
333+
}
334+
335+
/// Return the amount of octets queued in the receive buffer.
336+
pub fn recv_queue(&self) -> usize {
337+
self.rx_buffer.payload_bytes_count()
338+
}
339+
330340
pub(crate) fn accepts(&self, ip_repr: &IpRepr) -> bool {
331341
if ip_repr.version() != self.ip_version {
332342
return false;

src/socket/udp.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,22 @@ impl<'a> Socket<'a> {
459459
Ok((length, endpoint))
460460
}
461461

462+
/// Return the amount of octets queued in the transmit buffer.
463+
///
464+
/// Note that the Berkeley sockets interface does not have an equivalent of this API.
465+
pub fn send_queue(&self) -> usize {
466+
self.tx_buffer.payload_bytes_count()
467+
}
468+
469+
/// Return the amount of octets queued in the receive buffer. This value can be larger than
470+
/// the slice read by the next `recv` or `peek` call because it includes all queued octets,
471+
/// and not only the octets that may be returned as a contiguous slice.
472+
///
473+
/// Note that the Berkeley sockets interface does not have an equivalent of this API.
474+
pub fn recv_queue(&self) -> usize {
475+
self.rx_buffer.payload_bytes_count()
476+
}
477+
462478
pub(crate) fn accepts(&self, cx: &mut Context, ip_repr: &IpRepr, repr: &UdpRepr) -> bool {
463479
if self.endpoint.port != repr.dst_port {
464480
return false;

src/storage/packet_buffer.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ impl<'a, H> PacketBuffer<'a, H> {
239239
self.payload_ring.capacity()
240240
}
241241

242+
/// Return the current number of bytes in the payload ring buffer.
243+
pub fn payload_bytes_count(&self) -> usize {
244+
self.payload_ring.len()
245+
}
246+
242247
/// Reset the packet buffer and clear any staged.
243248
#[allow(unused)]
244249
pub(crate) fn reset(&mut self) {

0 commit comments

Comments
 (0)