rust_rsm/net_ext/
rawpacket.rs

1#![allow(unused_variables)]
2#![allow(dead_code)]
3
4#[cfg(unix)]
5use libc;
6
7use std::io::{self,Error, Read, Write};
8#[cfg(unix)]
9use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
10#[cfg(windows)]
11//use std::os::windows::io::{AsRawSocket,RawSocket};
12#[cfg(unix)]
13use libc::{sockaddr_ll, sockaddr_storage, socket, packet_mreq, setsockopt};
14
15//#[cfg(windows)]
16//use windows_sys::Win32::Networking::WinSock::{self,AF_INET,SOCK_RAW};
17
18use std::io::{IoSlice,IoSliceMut};
19#[cfg(unix)]
20use super::unix::rawsocket;
21#[cfg(windows)]
22use super::windows::rawsocket;
23
24use crate::common::{errcode};
25use super::*;
26
27#[derive(Clone,Default,Debug)]
28pub struct Config {
29    pub is_l2_socket:bool,
30    pub recv_outgoing_pkt:bool,
31    pub promiscuous:bool,
32    pub non_blocking:bool,
33    pub read_timeout:u32, //毫秒
34    pub write_timeout:u32, //毫秒
35    pub read_buf_size:i32,
36    pub write_buf_size:i32,//字节
37    pub mtu:u16,
38}
39
40pub struct RawPacket {
41    fd:RawFdType,
42    if_name:String,
43    if_idx:i32,
44    config:Config,
45}
46impl RawPacket {
47    pub fn new(ifname:&str,config:&Config) -> Result<Self,errcode::RESULT> {
48        let fd = rawsocket::create_rawsocket(config.is_l2_socket);
49        if fd <=0 {
50            return Err(errcode::ERROR_BIND_SOCKET);
51        }
52        let idx = match rawsocket::get_netif_index_by_name(ifname) {
53            Ok(id)=>id,
54            Err(ec)=>return Err(ec),
55        };
56        rawsocket::bind_by_index(fd,idx);
57        if config.non_blocking {
58            let _ =rawsocket::set_non_blocking(fd);
59        }
60        if config.promiscuous {
61            rawsocket::set_promisc_mode(fd, idx, config.promiscuous);
62        }
63        let mut rp = Self{
64            fd:fd,
65            if_name:String::from(ifname),
66            if_idx:idx,
67            config:config.clone(),
68        };
69        if rp.config.mtu==0 {
70            rp.config.mtu=DEFAULT_ETHERNET_MTU;
71        }
72        Ok(rp)
73    }
74
75    /// Bind socket to an interface (by name).
76    pub fn bind(&mut self, name: &str) -> errcode::RESULT {
77        self.bind_internal(name)
78    }
79
80    // should take an &mut to unsure not just anyone can call it,
81    // but async wrapper needs this variant
82    pub fn bind_internal(&self, name: &str) -> errcode::RESULT {
83        let idx = match rawsocket::get_netif_index_by_name(name) {
84            Ok(id)=>id,
85            Err(_)=>return errcode::ERROR_NOT_FOUND,
86        };
87        rawsocket::bind_by_index(self.fd,idx)
88    }
89    //等待是否可读
90    pub fn wait_read_event(&self,timeou_msec:i32)->Result<(),errcode::RESULT> {
91        #[cfg(unix)]
92        return rawsocket::wait_for_single_fd_read(self.fd, timeou_msec);
93        #[cfg(windows)]
94        return Ok(());
95    }
96    pub fn recv_packet(&mut self, buf: &mut [u8]) -> io::Result<usize> {
97        return self.read(buf);
98    }
99    pub fn recv_packet_batch(&mut self, bufs: &mut [IoSliceMut]) -> io::Result<usize> {
100        return self.read_vectored(bufs);
101    }
102
103    pub fn send_packet(&mut self, buf: &[u8]) -> io::Result<usize> {
104        return self.write(buf);
105    }
106
107    pub fn send_packet_batch(&mut self, bufs: &mut [IoSlice]) -> io::Result<usize> {
108        return self.write_vectored(bufs);
109    }
110
111    pub fn drain(&mut self) {
112        self.drain_internal()
113    }
114
115    pub(crate) fn drain_internal(&self) {
116        let mut buf = [0u8; 10];
117        loop {
118            match rawsocket::read_fd(self.fd, &mut buf[..], 0)  {
119                Err(_)=>break,
120                Ok(0)=>break,
121                Ok(_)=>(),
122            }
123        }
124    }
125
126}
127
128impl std::fmt::Display for RawPacket{
129    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
130        write!(f,"socket_fd:{:#x},name:{},if_index:{},config:{:?}",self.fd,self.if_name,self.if_idx,self.config)
131    }
132}
133
134impl Read for RawPacket {
135    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
136        match rawsocket::read_fd(self.fd, buf,0) {
137            Ok(len)=>Ok(len),
138            Err(_)=>Err(Error::last_os_error()),
139        }
140    }
141
142    fn read_vectored(&mut self, bufs: &mut [IoSliceMut]) -> io::Result<usize> {
143        match rawsocket::read_fd_vector(self.fd, bufs) {
144            Ok(len)=>Ok(len),
145            Err(_)=>Err(Error::last_os_error()),
146        }
147    }
148}
149
150
151impl<'a> Read for &'a RawPacket {
152    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
153        match rawsocket::read_fd(self.fd, buf,0)  {
154            Ok(len)=>Ok(len),
155            Err(_)=>Err(Error::last_os_error()),
156        }
157    }
158}
159
160
161impl Write for RawPacket {
162    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
163        let mut cur_len:usize = 0;
164        let mut max_len = std::cmp::min(self.config.mtu as usize,buf.len());
165        while cur_len<buf.len() {
166            match rawsocket::write_fd(self.fd, &buf[cur_len..max_len],0)  {
167                Ok(len)=> {
168                    cur_len+=len;
169                    max_len = std::cmp::min(max_len+len as usize,buf.len());
170                },
171                Err(_)=>return Err(Error::last_os_error()),
172            }
173           
174        }
175        return Ok(cur_len)
176       
177    }
178
179    fn write_vectored(&mut self, bufs: &[IoSlice]) -> io::Result<usize> {
180        match rawsocket::write_fd_vertor(self.fd, bufs) {
181            Ok(len)=>Ok(len),
182            Err(_)=>Err(Error::last_os_error()),
183        }
184    }
185
186    fn flush(&mut self) -> io::Result<()> {
187        Ok(())
188    }
189}
190
191impl<'a> Write for &'a RawPacket {
192    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
193        match rawsocket::write_fd(self.fd, buf,0) {
194            Ok(len)=>Ok(len),
195            Err(_)=>Err(Error::last_os_error()),
196        }
197    }
198
199
200    fn flush(&mut self) -> io::Result<()> {
201        Ok(())
202    }
203}
204
205#[cfg(unix)]
206impl IntoRawFd for RawPacket {
207    fn into_raw_fd(self) -> RawFd {
208        self.fd
209    }
210}
211
212#[cfg(unix)]
213impl AsRawFd for RawPacket {
214    fn as_raw_fd(&self) -> RawFd {
215        self.fd
216    }
217}
218
219#[cfg(unix)]
220impl Drop for RawPacket {
221    fn drop(&mut self) {
222      rawsocket::close_fd(self.fd);
223    }
224}