1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

///! Defines the main API entry objects for the exposed API from core.
use lock_order::Unlocked;
use net_types::ip::Ip;
use netstack3_base::{
    ContextPair as _, ContextProvider, CtxPair, TimerBindingsTypes, TimerHandler as _,
};
use netstack3_device::queue::{ReceiveQueueApi, TransmitQueueApi};
use netstack3_device::socket::DeviceSocketApi;
use netstack3_device::{DeviceAnyApi, DeviceApi};
use netstack3_filter::FilterApi;
use netstack3_icmp_echo::IcmpEchoSocketApi;
use netstack3_ip::device::{DeviceIpAnyApi, DeviceIpApi};
use netstack3_ip::multicast_forwarding::MulticastForwardingApi;
use netstack3_ip::nud::NeighborApi;
use netstack3_ip::raw::RawIpSocketApi;
use netstack3_ip::{RoutesAnyApi, RoutesApi};
use netstack3_tcp::TcpApi;
use netstack3_udp::UdpApi;

use crate::context::CoreCtx;
use crate::counters::CountersApi;
use crate::time::TimerId;
use crate::BindingsTypes;

type CoreApiCtxPair<'a, BP> = CtxPair<CoreCtx<'a, <BP as ContextProvider>::Context, Unlocked>, BP>;

/// The single entry point for function calls into netstack3 core.
pub struct CoreApi<'a, BP>(CoreApiCtxPair<'a, BP>)
where
    BP: ContextProvider,
    BP::Context: BindingsTypes;

impl<'a, BP> CoreApi<'a, BP>
where
    BP: ContextProvider,
    BP::Context: BindingsTypes,
{
    pub(crate) fn new(ctx_pair: CoreApiCtxPair<'a, BP>) -> Self {
        Self(ctx_pair)
    }

    /// Gets access to the UDP API for IP version `I`.
    pub fn udp<I: Ip>(self) -> UdpApi<I, CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        UdpApi::new(ctx)
    }

    /// Gets access to the ICMP socket API for IP version `I`.
    pub fn icmp_echo<I: Ip>(self) -> IcmpEchoSocketApi<I, CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        IcmpEchoSocketApi::new(ctx)
    }

    /// Gets access to the TCP API for IP version `I`.
    pub fn tcp<I: Ip>(self) -> TcpApi<I, CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        TcpApi::new(ctx)
    }

    /// Gets access to the raw IP socket API.
    pub fn raw_ip_socket<I: Ip>(self) -> RawIpSocketApi<I, CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        RawIpSocketApi::new(ctx)
    }

    /// Gets access to the device socket API.
    pub fn device_socket(self) -> DeviceSocketApi<CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        DeviceSocketApi::new(ctx)
    }

    /// Gets access to the filtering API.
    pub fn filter(self) -> FilterApi<CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        FilterApi::new(ctx)
    }

    /// Gets access to the routes API for IP version `I`.
    pub fn routes<I: Ip>(self) -> RoutesApi<I, CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        RoutesApi::new(ctx)
    }

    /// Gets access to the routes API for all IP versions.
    pub fn routes_any(self) -> RoutesAnyApi<CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        RoutesAnyApi::new(ctx)
    }

    /// Gets access to the multicast forwarding API for IP version `I`.
    pub fn multicast_forwarding<I: Ip>(self) -> MulticastForwardingApi<I, CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        MulticastForwardingApi::new(ctx)
    }

    /// Gets access to the neighbor API for IP version `I` and device `D`.
    pub fn neighbor<I: Ip, D>(self) -> NeighborApi<I, D, CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        NeighborApi::new(ctx)
    }

    /// Gets access to the device API for device type `D`.
    pub fn device<D>(self) -> DeviceApi<D, CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        DeviceApi::new(ctx)
    }

    /// Gets access to the device API for all device types.
    pub fn device_any(self) -> DeviceAnyApi<CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        DeviceAnyApi::new(ctx)
    }

    /// Gets access to the device IP API for IP version `I``.
    pub fn device_ip<I: Ip>(self) -> DeviceIpApi<I, CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        DeviceIpApi::new(ctx)
    }

    /// Gets access to the device IP API for all IP versions.
    pub fn device_ip_any(self) -> DeviceIpAnyApi<CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        DeviceIpAnyApi::new(ctx)
    }

    /// Gets access to the transmit queue API for device type `D`.
    pub fn transmit_queue<D>(self) -> TransmitQueueApi<D, CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        TransmitQueueApi::new(ctx)
    }

    /// Gets access to the receive queue API for device type `D`.
    pub fn receive_queue<D>(self) -> ReceiveQueueApi<D, CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        ReceiveQueueApi::new(ctx)
    }

    /// Gets access to the counters API.
    pub fn counters(self) -> CountersApi<CoreApiCtxPair<'a, BP>> {
        let Self(ctx) = self;
        CountersApi::new(ctx)
    }

    /// Handles a timer.
    pub fn handle_timer(
        &mut self,
        dispatch: TimerId<BP::Context>,
        timer: <BP::Context as TimerBindingsTypes>::UniqueTimerId,
    ) where
        BP::Context: crate::BindingsContext,
    {
        let Self(ctx) = self;
        let (core_ctx, bindings_ctx) = ctx.contexts();
        core_ctx.handle_timer(bindings_ctx, dispatch, timer)
    }
}