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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright 2021 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.

use crate::ot::WrongSize;
use crate::prelude_internal::*;
use core::pin::Pin;
use core::task::{Context, Poll};
use fuchsia_sync::Mutex;
use std::sync::Arc;
use std::task::Waker;

/// Iterator type for external routes.
#[allow(missing_debug_implementations)]
pub struct LocalExternalRouteIterator<'a, T: ?Sized> {
    ot_instance: &'a T,
    ot_iter: otNetworkDataIterator,
}

impl<'a, T: ?Sized + BorderRouter> Iterator for LocalExternalRouteIterator<'a, T> {
    type Item = ExternalRouteConfig;
    fn next(&mut self) -> Option<Self::Item> {
        self.ot_instance.iter_next_local_external_route(&mut self.ot_iter)
    }
}

/// Iterator type for on-mesh prefixes.
#[allow(missing_debug_implementations)]
pub struct LocalOnMeshPrefixIterator<'a, T: ?Sized> {
    ot_instance: &'a T,
    ot_iter: otNetworkDataIterator,
}

impl<'a, T: ?Sized + BorderRouter> Iterator for LocalOnMeshPrefixIterator<'a, T> {
    type Item = BorderRouterConfig;
    fn next(&mut self) -> Option<Self::Item> {
        self.ot_instance.iter_next_local_on_mesh_prefix(&mut self.ot_iter)
    }
}

/// Methods from the [OpenThread "Border Router" Module][1].
///
/// [1]: https://openthread.io/reference/group/api-border-router
pub trait BorderRouter {
    /// Functional equivalent of
    /// [`otsys::otBorderRouterAddRoute`](crate::otsys::otBorderRouterAddRoute).
    fn add_external_route(&self, route: &ExternalRouteConfig) -> Result;

    /// Functional equivalent of
    /// [`otsys::otBorderRouterAddOnMeshPrefix`](crate::otsys::otBorderRouterAddOnMeshPrefix).
    fn add_on_mesh_prefix(&self, route: &BorderRouterConfig) -> Result;

    /// Functional equivalent of
    /// [`otsys::otBorderRouterRemoveRoute`](crate::otsys::otBorderRouterRemoveRoute).
    fn remove_external_route(&self, prefix: &Ip6Prefix) -> Result;

    /// Functional equivalent of
    /// [`otsys::otBorderRouterRemoveOnMeshPrefix`](crate::otsys::otBorderRouterRemoveOnMeshPrefix).
    fn remove_on_mesh_prefix(&self, prefix: &Ip6Prefix) -> Result;

    /// Functional equivalent of
    /// [`otsys::otBorderRouterRegister`](crate::otsys::otBorderRouterRegister).
    fn border_router_register(&self) -> Result;

    /// Functional equivalent of
    /// [`otsys::otBorderRoutingInit`](crate::otsys::otBorderRoutingInit).
    fn border_routing_init(&self, infra_if: u32, infra_is_running: bool) -> Result;

    /// Functional equivalent of
    /// [`otsys::otBorderRoutingSetEnabled`](crate::otsys::otBorderRoutingSetEnabled).
    fn border_routing_set_enabled(&self, enabled: bool) -> Result;

    /// Functional equivalent of
    /// [`otsys::otBorderRoutingDhcp6PdSetEnabled`](crate::otsys::otBorderRoutingDhcp6PdSetEnabled).
    fn border_routing_dhcp6_pd_set_enabled(&self, enabled: bool);

    /// Functional equivalent of
    /// [`otsys::otBorderRoutingDhcp6PdGetState`](crate::otsys::otBorderRoutingDhcp6PdGetState).
    fn border_routing_dhcp6_pd_get_state(&self) -> BorderRoutingDhcp6PdState;

    /// Functional equivalent of
    /// [`otsys::otBorderRoutingDhcp6PdSetRequestCallback`](crate::otsys::otBorderRoutingDhcp6PdSetRequestCallback).
    fn border_routing_dhcp6_pd_set_request_fn<'a, F>(&'a self, f: Option<F>)
    where
        F: FnMut(BorderRoutingDhcp6PdState) + 'a;

    /// Get the DHCPv6 PD state change stream
    fn border_routing_dhcp6_pd_state_change_stream(&self)
        -> BorderRoutingDhcp6PdStateChangedStream;

    /// Functional equivalent of
    /// [`otsys::otBorderRoutingGetPdOmrPrefix`](crate::otsys::otBorderRoutingGetPdOmrPrefix).
    fn border_routing_get_pd_omr_prefix(&self) -> Result<ot::BorderRoutingPrefixTableEntry>;

    /// Functional equivalent of
    /// [`otsys::otBorderRoutingGetOmrPrefix`](crate::otsys::otBorderRoutingGetOmrPrefix).
    fn border_routing_get_omr_prefix(&self) -> Result<ot::Ip6Prefix>;

    /// Functional equivalent of
    /// [`otsys::otBorderRoutingGetOnLinkPrefix`](crate::otsys::otBorderRoutingGetOnLinkPrefix).
    fn border_routing_get_on_link_prefix(&self) -> Result<ot::Ip6Prefix>;

    /// Functional equivalent of
    /// [`otsys::otPlatBorderRoutingProcessIcmp6Ra`](crate::otsys::otPlatBorderRoutingProcessIcmp6Ra).
    fn border_routing_process_icmp6_ra(&self, message: &[u8]) -> Result<(), WrongSize>;

    /// Functional equivalent of
    /// [`otsys::otBorderRoutingGetPdProcessedRaInfo`](crate::otsys::otBorderRoutingGetPdProcessedRaInfo).
    fn border_routing_get_pd_processed_ra_info(&self) -> PdProcessedRaInfo;

    /// Functional equivalent of
    /// [`otsys::otBorderRouterGetNextRoute`](crate::otsys::otBorderRouterGetNextRoute).
    // TODO: Determine if the underlying implementation of
    //       this method has undefined behavior when network data
    //       is being mutated while iterating. If it is undefined,
    //       we may need to make it unsafe and provide a safe method
    //       that collects the results.
    fn iter_next_local_external_route(
        &self,
        ot_iter: &mut otNetworkDataIterator,
    ) -> Option<ExternalRouteConfig>;

    /// Functional equivalent of
    /// [`otsys::otBorderRouterGetNextOnMeshPrefix`](crate::otsys::otBorderRouterGetNextOnMeshPrefix).
    // TODO: Determine if the underlying implementation of
    //       this method has undefined behavior when network data
    //       is being mutated while iterating. If it is undefined,
    //       we may need to make it unsafe and provide a safe method
    //       that collects the results.
    fn iter_next_local_on_mesh_prefix(
        &self,
        ot_iter: &mut otNetworkDataIterator,
    ) -> Option<BorderRouterConfig>;

    /// Returns an iterator for iterating over external routes.
    fn iter_local_external_routes(&self) -> LocalExternalRouteIterator<'_, Self> {
        LocalExternalRouteIterator { ot_instance: self, ot_iter: OT_NETWORK_DATA_ITERATOR_INIT }
    }

    /// Returns an iterator for iterating over on-mesh prefixes
    fn iter_local_on_mesh_prefixes(&self) -> LocalOnMeshPrefixIterator<'_, Self> {
        LocalOnMeshPrefixIterator { ot_instance: self, ot_iter: OT_NETWORK_DATA_ITERATOR_INIT }
    }
}

impl<T: BorderRouter + Boxable> BorderRouter for ot::Box<T> {
    fn add_external_route(&self, route: &ExternalRouteConfig) -> Result {
        self.as_ref().add_external_route(route)
    }

    fn add_on_mesh_prefix(&self, route: &BorderRouterConfig) -> Result {
        self.as_ref().add_on_mesh_prefix(route)
    }

    fn remove_external_route(&self, prefix: &Ip6Prefix) -> Result {
        self.as_ref().remove_external_route(prefix)
    }

    fn remove_on_mesh_prefix(&self, prefix: &Ip6Prefix) -> Result {
        self.as_ref().remove_on_mesh_prefix(prefix)
    }

    fn border_router_register(&self) -> Result {
        self.as_ref().border_router_register()
    }

    fn border_routing_init(&self, infra_if: u32, infra_is_running: bool) -> Result {
        self.as_ref().border_routing_init(infra_if, infra_is_running)
    }

    fn border_routing_set_enabled(&self, enabled: bool) -> Result {
        self.as_ref().border_routing_set_enabled(enabled)
    }

    fn border_routing_dhcp6_pd_set_enabled(&self, enabled: bool) {
        self.as_ref().border_routing_dhcp6_pd_set_enabled(enabled)
    }

    fn border_routing_dhcp6_pd_get_state(&self) -> BorderRoutingDhcp6PdState {
        self.as_ref().border_routing_dhcp6_pd_get_state()
    }

    fn border_routing_dhcp6_pd_set_request_fn<'a, F>(&'a self, f: Option<F>)
    where
        F: FnMut(BorderRoutingDhcp6PdState) + 'a,
    {
        self.as_ref().border_routing_dhcp6_pd_set_request_fn(f)
    }

    fn border_routing_dhcp6_pd_state_change_stream(
        &self,
    ) -> BorderRoutingDhcp6PdStateChangedStream {
        self.as_ref().border_routing_dhcp6_pd_state_change_stream()
    }

    fn border_routing_get_pd_omr_prefix(&self) -> Result<ot::BorderRoutingPrefixTableEntry> {
        self.as_ref().border_routing_get_pd_omr_prefix()
    }

    fn border_routing_get_omr_prefix(&self) -> Result<ot::Ip6Prefix> {
        self.as_ref().border_routing_get_omr_prefix()
    }

    fn border_routing_get_on_link_prefix(&self) -> Result<ot::Ip6Prefix> {
        self.as_ref().border_routing_get_on_link_prefix()
    }

    fn border_routing_process_icmp6_ra(&self, message: &[u8]) -> Result<(), WrongSize> {
        self.as_ref().border_routing_process_icmp6_ra(message)
    }

    fn border_routing_get_pd_processed_ra_info(&self) -> PdProcessedRaInfo {
        self.as_ref().border_routing_get_pd_processed_ra_info()
    }

    fn iter_next_local_external_route(
        &self,
        ot_iter: &mut otNetworkDataIterator,
    ) -> Option<ExternalRouteConfig> {
        self.as_ref().iter_next_local_external_route(ot_iter)
    }

    fn iter_next_local_on_mesh_prefix(
        &self,
        ot_iter: &mut otNetworkDataIterator,
    ) -> Option<BorderRouterConfig> {
        self.as_ref().iter_next_local_on_mesh_prefix(ot_iter)
    }
}

impl BorderRouter for Instance {
    fn add_external_route(&self, route: &ExternalRouteConfig) -> Result {
        Error::from(unsafe { otBorderRouterAddRoute(self.as_ot_ptr(), route.as_ot_ptr()) }).into()
    }

    fn add_on_mesh_prefix(&self, route: &BorderRouterConfig) -> Result {
        Error::from(unsafe { otBorderRouterAddOnMeshPrefix(self.as_ot_ptr(), route.as_ot_ptr()) })
            .into()
    }

    fn remove_external_route(&self, prefix: &Ip6Prefix) -> Result {
        Error::from(unsafe { otBorderRouterRemoveRoute(self.as_ot_ptr(), prefix.as_ot_ptr()) })
            .into()
    }

    fn remove_on_mesh_prefix(&self, prefix: &Ip6Prefix) -> Result {
        Error::from(unsafe {
            otBorderRouterRemoveOnMeshPrefix(self.as_ot_ptr(), prefix.as_ot_ptr())
        })
        .into()
    }

    fn border_router_register(&self) -> Result {
        Error::from(unsafe { otBorderRouterRegister(self.as_ot_ptr()) }).into()
    }

    fn border_routing_init(&self, infra_if: u32, infra_is_running: bool) -> Result {
        Error::from(unsafe { otBorderRoutingInit(self.as_ot_ptr(), infra_if, infra_is_running) })
            .into()
    }

    fn border_routing_set_enabled(&self, enabled: bool) -> Result {
        Error::from(unsafe { otBorderRoutingSetEnabled(self.as_ot_ptr(), enabled) }).into()
    }

    fn border_routing_dhcp6_pd_set_enabled(&self, enabled: bool) {
        unsafe { otBorderRoutingDhcp6PdSetEnabled(self.as_ot_ptr(), enabled) }
    }

    fn border_routing_dhcp6_pd_get_state(&self) -> BorderRoutingDhcp6PdState {
        BorderRoutingDhcp6PdState::from_isize(unsafe {
            otBorderRoutingDhcp6PdGetState(self.as_ot_ptr())
        } as isize)
        .unwrap_or(BorderRoutingDhcp6PdState::Disabled)
    }

    fn border_routing_dhcp6_pd_set_request_fn<'a, F>(&'a self, f: Option<F>)
    where
        F: FnMut(BorderRoutingDhcp6PdState) + 'a,
    {
        unsafe extern "C" fn _ot_border_routing_dhcp6_pd_state_change_callback<
            'a,
            F: FnMut(BorderRoutingDhcp6PdState) + 'a,
        >(
            state: otBorderRoutingDhcp6PdState,
            context: *mut ::std::os::raw::c_void,
        ) {
            trace!("_ot_border_routing_dhcp6_pd_state_change_callback");

            // Convert `otBorderRoutingDhcp6PdState` to `BorderRoutingDhcp6PdState`
            let state = BorderRoutingDhcp6PdState::from(state);

            // Reconstitute a reference to our closure.
            let sender = &mut *(context as *mut F);

            sender(state)
        }

        let (fn_ptr, fn_box, cb): (_, _, otBorderRoutingRequestDhcp6PdCallback) = if let Some(f) = f
        {
            let mut x = Box::new(f);

            (
                x.as_mut() as *mut F as *mut ::std::os::raw::c_void,
                Some(x as Box<dyn FnMut(BorderRoutingDhcp6PdState) + 'a>),
                Some(_ot_border_routing_dhcp6_pd_state_change_callback::<F>),
            )
        } else {
            (std::ptr::null_mut() as *mut ::std::os::raw::c_void, None, None)
        };

        unsafe {
            otBorderRoutingDhcp6PdSetRequestCallback(self.as_ot_ptr(), cb, fn_ptr);

            // Make sure our object eventually gets cleaned up.
            // Here we must also transmute our closure to have a 'static lifetime.
            // We need to do this because the borrow checker cannot infer the
            // proper lifetime for the singleton instance backing, but
            // this is guaranteed by the API.
            self.borrow_backing().dhcp6pd_state_change_callback_fn.set(std::mem::transmute::<
                Option<Box<dyn FnMut(BorderRoutingDhcp6PdState) + 'a>>,
                Option<Box<dyn FnMut(BorderRoutingDhcp6PdState) + 'static>>,
            >(fn_box));
        }
    }

    fn border_routing_dhcp6_pd_state_change_stream(
        &self,
    ) -> BorderRoutingDhcp6PdStateChangedStream {
        let state = Arc::new(Mutex::new((None, futures::task::noop_waker())));

        let state_copy = state.clone();

        self.border_routing_dhcp6_pd_set_request_fn(Some(
            move |pd_state: BorderRoutingDhcp6PdState| {
                let mut borrowed = state_copy.lock();
                borrowed.0 = Some(pd_state);
                borrowed.1.wake_by_ref();
            },
        ));

        BorderRoutingDhcp6PdStateChangedStream(state)
    }

    fn border_routing_get_pd_omr_prefix(&self) -> Result<ot::BorderRoutingPrefixTableEntry> {
        let mut ret: BorderRoutingPrefixTableEntry = Default::default();
        Error::from(unsafe {
            otBorderRoutingGetPdOmrPrefix(self.as_ot_ptr(), ret.as_ot_mut_ptr())
        })
        .into_result()?;
        Ok(ret)
    }

    fn border_routing_get_omr_prefix(&self) -> Result<ot::Ip6Prefix> {
        let mut ret: Ip6Prefix = Default::default();
        Error::from(unsafe { otBorderRoutingGetOmrPrefix(self.as_ot_ptr(), ret.as_ot_mut_ptr()) })
            .into_result()?;
        Ok(ret)
    }

    fn border_routing_get_on_link_prefix(&self) -> Result<ot::Ip6Prefix> {
        let mut ret: Ip6Prefix = Default::default();
        Error::from(unsafe {
            otBorderRoutingGetOnLinkPrefix(self.as_ot_ptr(), ret.as_ot_mut_ptr())
        })
        .into_result()?;
        Ok(ret)
    }

    fn border_routing_process_icmp6_ra(&self, message: &[u8]) -> Result<(), WrongSize> {
        unsafe {
            otPlatBorderRoutingProcessIcmp6Ra(
                self.as_ot_ptr(),
                message.as_ptr(),
                message.len().try_into().map_err(|_| WrongSize)?,
            )
        }
        Ok(())
    }

    fn border_routing_get_pd_processed_ra_info(&self) -> PdProcessedRaInfo {
        let mut info = PdProcessedRaInfo::default();
        unsafe {
            otBorderRoutingGetPdProcessedRaInfo(self.as_ot_ptr(), info.as_ot_mut_ptr());
        }
        info
    }

    fn iter_next_local_external_route(
        &self,
        ot_iter: &mut otNetworkDataIterator,
    ) -> Option<ExternalRouteConfig> {
        unsafe {
            let mut ret = ExternalRouteConfig::default();
            match Error::from(otBorderRouterGetNextRoute(
                self.as_ot_ptr(),
                ot_iter as *mut otNetworkDataIterator,
                ret.as_ot_mut_ptr(),
            )) {
                Error::NotFound => None,
                Error::None => Some(ret),
                err => panic!("Unexpected error from otBorderRouterGetNextRoute: {err:?}"),
            }
        }
    }

    fn iter_next_local_on_mesh_prefix(
        &self,
        ot_iter: &mut otNetworkDataIterator,
    ) -> Option<BorderRouterConfig> {
        unsafe {
            let mut ret = BorderRouterConfig::default();
            match Error::from(otBorderRouterGetNextOnMeshPrefix(
                self.as_ot_ptr(),
                ot_iter as *mut otNetworkDataIterator,
                ret.as_ot_mut_ptr(),
            )) {
                Error::NotFound => None,
                Error::None => Some(ret),
                err => panic!("Unexpected error from otBorderRouterGetNextOnMeshPrefix: {err:?}"),
            }
        }
    }
}

/// Stream for getting state changed events.
#[derive(Debug, Clone)]
pub struct BorderRoutingDhcp6PdStateChangedStream(
    Arc<Mutex<(Option<BorderRoutingDhcp6PdState>, Waker)>>,
);

impl Stream for BorderRoutingDhcp6PdStateChangedStream {
    type Item = BorderRoutingDhcp6PdState;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let mut state = self.0.lock();

        state.1 = cx.waker().clone();

        if let Some(pd_state) = state.0.take() {
            Poll::Ready(Some(pd_state))
        } else {
            Poll::Pending
        }
    }
}