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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use crate::platform::posix::Fd;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use crate::PACKET_INFORMATION_LENGTH as PIL;
#[cfg(any(target_os = "macos", target_os = "ios"))]
use bytes::BufMut;
use std::io::{self, Read, Write};
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
#[cfg(any(target_os = "macos", target_os = "ios"))]
use std::sync::atomic::{AtomicBool, Ordering};

/// Infer the protocol based on the first nibble in the packet buffer.
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) fn is_ipv6(buf: &[u8]) -> std::io::Result<bool> {
    use std::io::{Error, ErrorKind::InvalidData};
    if buf.is_empty() {
        return Err(Error::new(InvalidData, "Zero-length data"));
    }
    match buf[0] >> 4 {
        4 => Ok(false),
        6 => Ok(true),
        p => Err(Error::new(InvalidData, format!("IP version {}", p))),
    }
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) fn generate_packet_information(_ipv6: bool) -> Option<[u8; PIL]> {
    #[cfg(any(target_os = "linux", target_os = "android"))]
    const TUN_PROTO_IP6: [u8; PIL] = (libc::ETH_P_IPV6 as u32).to_be_bytes();
    #[cfg(any(target_os = "linux", target_os = "android"))]
    const TUN_PROTO_IP4: [u8; PIL] = (libc::ETH_P_IP as u32).to_be_bytes();

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    const TUN_PROTO_IP6: [u8; PIL] = (libc::AF_INET6 as u32).to_be_bytes();
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    const TUN_PROTO_IP4: [u8; PIL] = (libc::AF_INET as u32).to_be_bytes();

    // FIXME: Currently, the FreeBSD we test (FreeBSD-14.0-RELEASE) seems to have no PI. Here just a dummy.
    #[cfg(target_os = "freebsd")]
    const TUN_PROTO_IP6: [u8; PIL] = 0x86DD_u32.to_be_bytes();
    #[cfg(target_os = "freebsd")]
    const TUN_PROTO_IP4: [u8; PIL] = 0x0800_u32.to_be_bytes();

    if _ipv6 {
        Some(TUN_PROTO_IP6)
    } else {
        Some(TUN_PROTO_IP4)
    }
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[rustversion::since(1.79)]
macro_rules! local_buf_util {
	($e:expr,$size:expr) => {
		if $e{
			&mut vec![0u8; $size][..]
		}else{
			const STACK_BUF_LEN: usize = crate::DEFAULT_MTU as usize + PIL;
			&mut [0u8; STACK_BUF_LEN]
		}
	};
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[rustversion::before(1.79)]
macro_rules! local_buf_util {
	($e:expr,$size:expr) =>{
		{
			#[allow(clippy::large_enum_variant)]
			pub(crate) enum OptBuf{
				Heap(Vec<u8>),
				Stack([u8;crate::DEFAULT_MTU as usize + PIL])
			}
			impl OptBuf{
				pub(crate) fn as_mut(& mut self)->& mut [u8]{
					match self{
						OptBuf::Heap(v)=>v.as_mut(),
						OptBuf::Stack(v)=>v.as_mut()
					}
				}
			}

			fn get_local_buf(cond:bool,in_buf_len:usize)-> OptBuf{
				if cond{
					OptBuf::Heap(vec![0u8; in_buf_len])
				}else{
					const STACK_BUF_LEN: usize = crate::DEFAULT_MTU as usize + PIL;
					OptBuf::Stack([0u8; STACK_BUF_LEN])
				}
			}
			get_local_buf($e,$size)
		}
	}
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[rustversion::since(1.79)]
macro_rules! need_mut {
    ($id:ident, $e:expr) => {
        let $id = $e;
    };
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[rustversion::before(1.79)]
macro_rules! need_mut {
    ($id:ident, $e:expr) => {
        let mut $id = $e;
    };
}

pub struct Tun {
    pub(crate) fd: Fd,
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    ignore_packet_information: AtomicBool,
}

impl Tun {
    pub(crate) fn new(fd: Fd) -> Self {
        Self {
            fd,
            #[cfg(any(target_os = "macos", target_os = "ios"))]
            ignore_packet_information: AtomicBool::new(false),
        }
    }

    pub fn set_nonblock(&self) -> io::Result<()> {
        self.fd.set_nonblock()
    }
    #[cfg(not(any(target_os = "macos", target_os = "ios")))]
    #[inline]
    pub(crate) fn send(&self, in_buf: &[u8]) -> io::Result<usize> {
        self.fd.write(in_buf)
    }
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub(crate) fn send(&self, in_buf: &[u8]) -> io::Result<usize> {
        if self.ignore_packet_info() {
            let ipv6 = is_ipv6(in_buf)?;
            if let Some(header) = generate_packet_information(ipv6) {
                const STACK_BUF_LEN: usize = crate::DEFAULT_MTU as usize + PIL;
                let in_buf_len = in_buf.len() + PIL;

                // The following logic is to prevent dynamically allocating Vec on every send
                // As long as the MTU is set to value lesser than 1500, this api uses `stack_buf`
                // and avoids `Vec` allocation
                let local_buf_v0 = local_buf_util!(in_buf_len > STACK_BUF_LEN, in_buf_len);
                need_mut! {local_buf_v1,local_buf_v0};
                #[allow(clippy::useless_asref)]
                let local_buf = local_buf_v1.as_mut();

                (&mut local_buf[..PIL]).put_slice(header.as_ref());
                (&mut local_buf[PIL..in_buf_len]).put_slice(in_buf);
                let amount = self.fd.write(&local_buf[..in_buf_len])?;
                return Ok(amount - PIL);
            }
        }
        self.fd.write(in_buf)
    }
    #[cfg(not(any(target_os = "macos", target_os = "ios")))]
    #[inline]
    pub(crate) fn recv(&self, in_buf: &mut [u8]) -> io::Result<usize> {
        self.fd.read(in_buf)
    }
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub(crate) fn recv(&self, mut in_buf: &mut [u8]) -> io::Result<usize> {
        if self.ignore_packet_info() {
            const STACK_BUF_LEN: usize = crate::DEFAULT_MTU as usize + PIL;
            let in_buf_len = in_buf.len() + PIL;

            // The following logic is to prevent dynamically allocating Vec on every recv
            // As long as the MTU is set to value lesser than 1500, this api uses `stack_buf`
            // and avoids `Vec` allocation

            let local_buf_v0 = local_buf_util!(in_buf_len > STACK_BUF_LEN, in_buf_len);
            need_mut! {local_buf_v1,local_buf_v0};
            #[allow(clippy::useless_asref)]
            let local_buf = local_buf_v1.as_mut();
            let amount = self.fd.read(local_buf)?;
            in_buf.put_slice(&local_buf[PIL..amount]);
            Ok(amount - PIL)
        } else {
            self.fd.read(in_buf)
        }
    }
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    #[inline]
    pub(crate) fn ignore_packet_info(&self) -> bool {
        self.ignore_packet_information.load(Ordering::Relaxed)
    }
    #[cfg(any(target_os = "macos", target_os = "ios"))]
    pub(crate) fn set_ignore_packet_info(&self, ign: bool) {
        self.ignore_packet_information.store(ign, Ordering::Relaxed)
    }
    #[cfg(feature = "experimental")]
    pub(crate) fn shutdown(&self) -> io::Result<()> {
        self.fd.shutdown()
    }
}

impl Read for Tun {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.recv(buf)
    }
}

impl Write for Tun {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.send(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

impl AsRawFd for Tun {
    fn as_raw_fd(&self) -> RawFd {
        self.fd.as_raw_fd()
    }
}

impl IntoRawFd for Tun {
    fn into_raw_fd(self) -> RawFd {
        self.fd.into_raw_fd()
    }
}