From 9b53a9d1a61115a328ca43955153d09cc2e969ef Mon Sep 17 00:00:00 2001 From: Quang Luong Date: Fri, 18 Sep 2020 11:20:06 +0700 Subject: Fix some clippy warnings Signed-off-by: Quang Luong --- src/platform/linux/tun.rs | 14 ++++---- src/platform/linux/udp.rs | 92 ++++++++++++++++++++++++----------------------- 2 files changed, 53 insertions(+), 53 deletions(-) (limited to 'src/platform') diff --git a/src/platform/linux/tun.rs b/src/platform/linux/tun.rs index 15ca1ec..9157b38 100644 --- a/src/platform/linux/tun.rs +++ b/src/platform/linux/tun.rs @@ -1,7 +1,5 @@ use super::super::tun::*; -use libc; - use std::error::Error; use std::fmt; use std::mem; @@ -9,7 +7,7 @@ use std::os::raw::c_short; use std::os::unix::io::RawFd; const TUNSETIFF: u64 = 0x4004_54ca; -const CLONE_DEVICE_PATH: &'static [u8] = b"/dev/net/tun\0"; +const CLONE_DEVICE_PATH: &[u8] = b"/dev/net/tun\0"; #[repr(C)] struct Ifreq { @@ -75,11 +73,11 @@ impl fmt::Display for LinuxTunError { } impl Error for LinuxTunError { - fn description(&self) -> &str { + fn source(&self) -> Option<&(dyn Error + 'static)> { unimplemented!() } - fn source(&self) -> Option<&(dyn Error + 'static)> { + fn description(&self) -> &str { unimplemented!() } } @@ -156,7 +154,7 @@ fn get_mtu(name: &[u8; libc::IFNAMSIZ]) -> Result { mtu: 0, }; let err = unsafe { - let ptr: &libc::c_void = mem::transmute(&buf); + let ptr: &libc::c_void = &*(&buf as *const _ as *const libc::c_void); libc::ioctl(fd, libc::SIOCGIFMTU, ptr) }; @@ -312,9 +310,9 @@ impl LinuxTunStatus { } impl Tun for LinuxTun { - type Error = LinuxTunError; - type Reader = LinuxTunReader; type Writer = LinuxTunWriter; + type Reader = LinuxTunReader; + type Error = LinuxTunError; } impl PlatformTun for LinuxTun { diff --git a/src/platform/linux/udp.rs b/src/platform/linux/udp.rs index e76c2a8..e1ceb73 100644 --- a/src/platform/linux/udp.rs +++ b/src/platform/linux/udp.rs @@ -1,8 +1,6 @@ use super::super::udp::*; use super::super::Endpoint; -use log; - use std::convert::TryInto; use std::io; use std::mem; @@ -132,19 +130,6 @@ fn safe_cast(v: &mut T) -> *mut D { } impl Endpoint for LinuxEndpoint { - fn clear_src(&mut self) { - match self { - LinuxEndpoint::V4(EndpointV4 { ref mut info, .. }) => { - info.ipi_ifindex = 0; - info.ipi_spec_dst = libc::in_addr { s_addr: 0 }; - } - LinuxEndpoint::V6(EndpointV6 { ref mut info, .. }) => { - info.ipi6_addr = libc::in6_addr { s6_addr: [0; 16] }; - info.ipi6_ifindex = 0; - } - }; - } - fn from_address(addr: SocketAddr) -> Self { match addr { SocketAddr::V4(addr) => LinuxEndpoint::V4(EndpointV4 { @@ -196,6 +181,19 @@ impl Endpoint for LinuxEndpoint { )), } } + + fn clear_src(&mut self) { + match self { + LinuxEndpoint::V4(EndpointV4 { ref mut info, .. }) => { + info.ipi_ifindex = 0; + info.ipi_spec_dst = libc::in_addr { s_addr: 0 }; + } + LinuxEndpoint::V6(EndpointV6 { ref mut info, .. }) => { + info.ipi6_addr = libc::in6_addr { s6_addr: [0; 16] }; + info.ipi6_ifindex = 0; + } + }; + } } impl LinuxUDPReader { @@ -206,7 +204,7 @@ impl LinuxUDPReader { buf.len() ); - debug_assert!(buf.len() > 0, "reading into empty buffer (will fail)"); + debug_assert!(!buf.is_empty(), "reading into empty buffer (will fail)"); let mut iovs: [libc::iovec; 1] = [libc::iovec { iov_base: buf.as_mut_ptr() as *mut core::ffi::c_void, @@ -260,7 +258,7 @@ impl LinuxUDPReader { buf.len() ); - debug_assert!(buf.len() > 0, "reading into empty buffer (will fail)"); + debug_assert!(!buf.is_empty(), "reading into empty buffer (will fail)"); let mut iovs: [libc::iovec; 1] = [libc::iovec { iov_base: buf.as_mut_ptr() as *mut core::ffi::c_void, @@ -366,14 +364,14 @@ impl LinuxUDPWriter { hdr.msg_control = ptr::null_mut(); hdr.msg_controllen = 0; dst.info = unsafe { mem::zeroed() }; - if unsafe { libc::sendmsg(fd, &hdr, 0) } < 0 { - return Err(io::Error::new( + return if unsafe { libc::sendmsg(fd, &hdr, 0) } < 0 { + Err(io::Error::new( io::ErrorKind::NotConnected, "failed to send IPv6 packet", - )); + )) } else { - return Ok(()); - } + Ok(()) + }; } return Err(io::Error::new( io::ErrorKind::NotConnected, @@ -431,14 +429,14 @@ impl LinuxUDPWriter { hdr.msg_control = ptr::null_mut(); hdr.msg_controllen = 0; dst.info = unsafe { mem::zeroed() }; - if unsafe { libc::sendmsg(fd, &hdr, 0) } < 0 { - return Err(io::Error::new( + return if unsafe { libc::sendmsg(fd, &hdr, 0) } < 0 { + Err(io::Error::new( io::ErrorKind::NotConnected, "failed to send IPv4 packet", - )); + )) } else { - return Ok(()); - } + Ok(()) + }; } return Err(io::Error::new( io::ErrorKind::NotConnected, @@ -485,22 +483,26 @@ impl Owner for LinuxOwner { impl Drop for LinuxOwner { fn drop(&mut self) { log::debug!("closing the bind (port = {})", self.port); - self.sock4.as_ref().map(|fd| unsafe { + if let Some(fd) = &self.sock4 { log::debug!("shutdown IPv4 (fd = {})", fd.0); - libc::shutdown(fd.0, libc::SHUT_RDWR); - }); - self.sock6.as_ref().map(|fd| unsafe { + unsafe { + libc::shutdown(fd.0, libc::SHUT_RDWR); + } + }; + if let Some(fd) = &self.sock6 { log::debug!("shutdown IPv6 (fd = {})", fd.0); - libc::shutdown(fd.0, libc::SHUT_RDWR); - }); + unsafe { + libc::shutdown(fd.0, libc::SHUT_RDWR); + } + }; } } impl UDP for LinuxUDP { type Error = io::Error; type Endpoint = LinuxEndpoint; - type Reader = LinuxUDPReader; type Writer = LinuxUDPWriter; + type Reader = LinuxUDPReader; } impl LinuxUDP { @@ -580,7 +582,7 @@ impl LinuxUDP { debug_assert_eq!(sockaddr.sin6_family, libc::AF_INET6 as libc::sa_family_t); debug_assert_eq!(new_port, if port != 0 { port } else { new_port }); log::trace!("bound IPv6 socket (port {}, fd {})", new_port, fd); - return Ok((new_port, fd)); + Ok((new_port, fd)) } /* Bind on all IPv4 interfaces. @@ -657,7 +659,7 @@ impl LinuxUDP { debug_assert_eq!(sockaddr.sin_family, libc::AF_INET as libc::sa_family_t); debug_assert_eq!(new_port, if port != 0 { port } else { new_port }); log::trace!("bound IPv4 socket (port {}, fd {})", new_port, fd); - return Ok((new_port, fd)); + Ok((new_port, fd)) } } @@ -697,18 +699,18 @@ impl PlatformUDP for LinuxUDP { // create readers let mut readers: Vec = Vec::with_capacity(2); - sock6 - .clone() - .map(|sock| readers.push(LinuxUDPReader::V6(sock))); - sock4 - .clone() - .map(|sock| readers.push(LinuxUDPReader::V4(sock))); - debug_assert!(readers.len() > 0); + if let Some(sock) = sock6.clone() { + readers.push(LinuxUDPReader::V6(sock)) + } + if let Some(sock) = sock4.clone() { + readers.push(LinuxUDPReader::V4(sock)) + } + debug_assert!(!readers.is_empty()); // create writer let writer = LinuxUDPWriter { - sock4: sock4.unwrap_or(Arc::new(FD(-1))), - sock6: sock6.unwrap_or(Arc::new(FD(-1))), + sock4: sock4.unwrap_or_else(|| Arc::new(FD(-1))), + sock6: sock6.unwrap_or_else(|| Arc::new(FD(-1))), }; Ok((readers, writer, owner)) -- cgit v1.2.3-59-g8ed1b