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
#![allow(missing_docs)]
#![allow(dead_code, unused_variables)]
use ipnet::IpNet;
use std::{io, net};
enum AddrSet {
Net(IpNet),
NameWildcard(String),
}
impl AddrSet {
fn contains(&self, addr: net::IpAddr) -> bool {
match self {
Self::Net(ip_net) => ip_net.contains(&addr),
Self::NameWildcard(name) => false,
}
}
}
struct IpGrant {
set: AddrSet,
port: u16,
}
impl IpGrant {
fn contains(&self, addr: &net::SocketAddr) -> bool {
self.set.contains(addr.ip()) && addr.port() == self.port
}
}
pub struct Catalog {
grants: Vec<IpGrant>,
}
impl Catalog {
pub fn check_addr(&self, addr: &net::SocketAddr) -> io::Result<()> {
if self.grants.iter().any(|grant| grant.contains(addr)) {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"An address led outside the catalog",
))
}
}
}
pub const NO_SOCKET_ADDRS: &[net::SocketAddr] = &[];