netstack3_ip/raw/
state.rsuse derivative::Derivative;
use lock_order::lock::{OrderedLockAccess, OrderedLockRef};
use netstack3_base::sync::RwLock;
use netstack3_base::{IpExt, WeakDeviceIdentifier};
use crate::internal::raw::counters::RawIpSocketCounters;
use crate::internal::raw::filter::RawIpSocketIcmpFilter;
use crate::internal::raw::protocol::RawIpSocketProtocol;
use crate::internal::raw::RawIpSocketsBindingsTypes;
use crate::internal::routing::rules::Marks;
use crate::internal::socket::SocketHopLimits;
#[derive(Derivative)]
#[derivative(Default(bound = ""))]
pub struct RawIpSocketLockedState<I: IpExt, D: WeakDeviceIdentifier> {
pub(crate) bound_device: Option<D>,
pub(crate) icmp_filter: Option<RawIpSocketIcmpFilter<I>>,
pub(crate) hop_limits: SocketHopLimits<I>,
#[derivative(Default(value = "true"))]
pub(crate) multicast_loop: bool,
pub(crate) marks: Marks,
pub(crate) system_checksums: bool,
}
pub struct RawIpSocketState<I: IpExt, D: WeakDeviceIdentifier, BT: RawIpSocketsBindingsTypes> {
external_state: BT::RawIpSocketState<I>,
protocol: RawIpSocketProtocol<I>,
locked_state: RwLock<RawIpSocketLockedState<I, D>>,
counters: RawIpSocketCounters<I>,
}
impl<I: IpExt, D: WeakDeviceIdentifier, BT: RawIpSocketsBindingsTypes> RawIpSocketState<I, D, BT> {
pub(super) fn new(
protocol: RawIpSocketProtocol<I>,
external_state: BT::RawIpSocketState<I>,
) -> RawIpSocketState<I, D, BT> {
RawIpSocketState {
external_state,
protocol,
locked_state: Default::default(),
counters: Default::default(),
}
}
pub(super) fn protocol(&self) -> &RawIpSocketProtocol<I> {
&self.protocol
}
pub(super) fn external_state(&self) -> &BT::RawIpSocketState<I> {
&self.external_state
}
pub(super) fn into_external_state(self) -> BT::RawIpSocketState<I> {
let RawIpSocketState { protocol: _, locked_state: _, counters: _, external_state } = self;
external_state
}
pub fn counters(&self) -> &RawIpSocketCounters<I> {
&self.counters
}
#[cfg(test)]
pub(super) fn locked_state(&self) -> &RwLock<RawIpSocketLockedState<I, D>> {
&self.locked_state
}
}
impl<I: IpExt, D: WeakDeviceIdentifier, BT: RawIpSocketsBindingsTypes>
OrderedLockAccess<RawIpSocketLockedState<I, D>> for RawIpSocketState<I, D, BT>
{
type Lock = RwLock<RawIpSocketLockedState<I, D>>;
fn ordered_lock_access(&self) -> OrderedLockRef<'_, Self::Lock> {
OrderedLockRef::new(&self.locked_state)
}
}