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
use prelude::{Protocol, SockAddr, Endpoint};
use ffi::SOCK_RAW;
use raw_socket::RawSocket;
use generic::GenericEndpoint;

#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
pub struct GenericRaw {
    family: i32,
    protocol: i32,
    capacity: usize,
}

impl Protocol for GenericRaw {
    type Endpoint = GenericEndpoint<Self>;

    fn family_type(&self) -> i32 {
        self.family
    }

    fn socket_type(&self) -> i32 {
        SOCK_RAW
    }

    fn protocol_type(&self) -> i32 {
        self.protocol
    }

    unsafe fn uninitialized(&self) -> Self::Endpoint {
        GenericEndpoint::default(self.capacity, self.protocol)
    }
}

impl Endpoint<GenericRaw> for GenericEndpoint<GenericRaw> {
    fn protocol(&self) -> GenericRaw {
        GenericRaw {
            family: self.as_ref().sa_family as i32,
            protocol: self.protocol,
            capacity: self.capacity(),
        }
    }
}

pub type GenericRawEndpoint = GenericEndpoint<GenericRaw>;

pub type GenericRawSocket = RawSocket<GenericRaw>;