netstack_smoltcp/
filter.rs1use std::net::IpAddr;
2
3pub type IpFilter<'a> = Box<dyn Fn(&IpAddr, &IpAddr) -> bool + Send + Sync + 'a>;
4
5pub struct IpFilters<'a> {
6 filters: Vec<IpFilter<'a>>,
7}
8
9impl<'a> Default for IpFilters<'a> {
10 fn default() -> Self {
11 Self::new()
12 }
13}
14
15impl<'a> IpFilters<'a> {
16 pub fn new() -> Self {
17 Self {
18 filters: Default::default(),
19 }
20 }
21
22 pub fn with_non_broadcast() -> Self {
23 macro_rules! non_broadcast {
24 ($addr:ident) => {
25 match $addr {
26 IpAddr::V4(a) => !(a.is_broadcast() || a.is_multicast() || a.is_unspecified()),
27 IpAddr::V6(a) => !(a.is_multicast() || a.is_unspecified()),
28 }
29 };
30 }
31 Self {
32 filters: vec![Box::new(|src, dst| {
33 non_broadcast!(src) && non_broadcast!(dst)
34 })],
35 }
36 }
37
38 pub fn add(&mut self, filter: IpFilter<'a>) {
39 self.filters.push(filter);
40 }
41
42 pub fn add_fn<F>(&mut self, filter: F)
43 where
44 F: Fn(&IpAddr, &IpAddr) -> bool + Send + Sync + 'a,
45 {
46 self.filters.push(Box::new(filter));
47 }
48
49 pub fn add_all<I: IntoIterator<Item = IpFilter<'a>>>(&mut self, filters: I) {
50 self.filters.extend(filters);
51 }
52
53 pub fn is_allowed(&self, src: &IpAddr, dst: &IpAddr) -> bool {
54 self.filters.iter().all(|filter| filter(src, dst))
55 }
56}