twizzler_net/
addr.rs

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
use std::fmt::Display;

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub struct Ipv4Addr {
    addr: u32,
}

impl Ipv4Addr {
    pub fn is_localhost(&self) -> bool {
        self.addr == 0x7F000001
    }

    pub fn localhost() -> Self {
        Self { addr: 0x7F000001 }
    }
}

impl From<Ipv4Addr> for u32 {
    fn from(x: Ipv4Addr) -> Self {
        x.addr
    }
}

impl From<u32> for Ipv4Addr {
    fn from(x: u32) -> Self {
        Self { addr: x }
    }
}

impl Display for Ipv4Addr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}.{}.{}.{}",
            (self.addr >> 24) & 0xff,
            (self.addr >> 16) & 0xff,
            (self.addr >> 8) & 0xff,
            (self.addr) & 0xff,
        )
    }
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub enum NodeAddr {
    Ipv4(Ipv4Addr),
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub enum ServiceAddr {
    Null,
    Icmp,
    Tcp(u16),
    Udp(u16),
}

impl ServiceAddr {
    pub fn any(&self) -> Self {
        match self {
            ServiceAddr::Tcp(_) => ServiceAddr::Tcp(0),
            ServiceAddr::Udp(_) => ServiceAddr::Udp(0),
            _ => *self,
        }
    }
}