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
use super::{Result, ReusablePacketWriter};

use nix::sys::socket::{
    sendto, socket, AddressFamily, MsgFlags, SockFlag, SockProtocol, SockType, SockaddrIn,
};
use nix::unistd::close;
use std::os::fd::{AsRawFd, RawFd};

/// Wrapper around low-level `socket(AF_INET, SOCK_RAW, IPPROTO_RAW)`
#[derive(Debug)]
pub struct RawSocket {
    fd: RawFd,
}

impl RawSocket {
    /// Trying to create raw socket
    #[inline]
    pub fn new() -> Result<Self> {
        let fd = socket(
            AddressFamily::Inet,
            SockType::Raw,
            SockFlag::empty(),
            Some(SockProtocol::Raw),
        )?;
        Ok(Self { fd })
    }

    /// Wraps `sendto` call with `nix` crate and other extra security checks
    #[inline]
    pub fn sendto(&self, buf: &[u8], addr: [u8; 4]) -> Result<usize> {
        let [a, b, c, d] = addr;
        let addr = SockaddrIn::new(a, b, c, d, 0);
        let len = sendto(self.fd, buf, &addr, MsgFlags::empty())?;
        Ok(len)
    }

    /// Sends fake UDP packet with given parameters
    #[inline]
    pub fn send_fake_udp_packet(
        &self,
        writer: &mut ReusablePacketWriter,
        source: [u8; 4],
        source_port: u16,
        destination: [u8; 4],
        destination_port: u16,
        payload: &[u8],
        time_to_live: u8,
    ) -> Result<usize> {
        writer.write_udp_packet(
            source,
            source_port,
            destination,
            destination_port,
            payload,
            time_to_live,
        )?;
        self.sendto(writer.as_slice(), destination)
    }

    /// Sends fake TCP-SYN packet with given parameters
    #[inline]
    pub fn send_fake_tcp_syn_packet(
        &self,
        writer: &mut ReusablePacketWriter,
        source: [u8; 4],
        source_port: u16,
        destination: [u8; 4],
        destination_port: u16,
        payload: &[u8],
        time_to_live: u8,
        sequence_number: u32,
        window_size: u16,
    ) -> Result<usize> {
        writer.write_tcp_syn_packet(
            source,
            source_port,
            destination,
            destination_port,
            payload,
            time_to_live,
            sequence_number,
            window_size,
        )?;
        self.sendto(writer.as_slice(), destination)
    }
}

/// Implementation that converts `RawSocket` into `fd` (file descriptor)
impl AsRawFd for RawSocket {
    fn as_raw_fd(&self) -> RawFd {
        self.fd
    }
}

/// Destructor implementation for `RawSocket`
impl Drop for RawSocket {
    fn drop(&mut self) {
        let _ = close(self.fd);
    }
}