From 71fbf6b3daef7e3178738ba9beb052315ef5e109 Mon Sep 17 00:00:00 2001 From: Josh Guilfoyle Date: Thu, 13 Jul 2023 20:11:03 -0700 Subject: [PATCH 1/4] Support for the ESP-IDF framework Smoke tested on esp32c3 dev board. I've also tested a similar patch backported to v0.4.9 with much greater functionality including tokio + mio with other patches I've been working on and it's fully working. Closes #379 --- src/lib.rs | 15 +++++++++++---- src/socket.rs | 17 +++++++++++++---- src/sys/unix.rs | 33 +++++++++++++++++++++++++++------ 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ae64ecf2..879a2e72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -270,13 +270,16 @@ impl Type { pub const DCCP: Type = Type(sys::SOCK_DCCP); /// Type corresponding to `SOCK_SEQPACKET`. - #[cfg(feature = "all")] - #[cfg_attr(docsrs, doc(cfg(feature = "all")))] + #[cfg(all(feature = "all", not(target_os = "espidf")))] + #[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(target_os = "espidf")))))] pub const SEQPACKET: Type = Type(sys::SOCK_SEQPACKET); /// Type corresponding to `SOCK_RAW`. - #[cfg(all(feature = "all", not(target_os = "redox")))] - #[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(target_os = "redox")))))] + #[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))] + #[cfg_attr( + docsrs, + doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))) + )] pub const RAW: Type = Type(sys::SOCK_RAW); } @@ -428,6 +431,7 @@ pub struct TcpKeepalive { target_os = "redox", target_os = "solaris", target_os = "nto", + target_os = "espidf", )))] interval: Option, #[cfg(not(any( @@ -436,6 +440,7 @@ pub struct TcpKeepalive { target_os = "solaris", target_os = "windows", target_os = "nto", + target_os = "espidf", )))] retries: Option, } @@ -450,6 +455,7 @@ impl TcpKeepalive { target_os = "redox", target_os = "solaris", target_os = "nto", + target_os = "espidf", )))] interval: None, #[cfg(not(any( @@ -458,6 +464,7 @@ impl TcpKeepalive { target_os = "solaris", target_os = "windows", target_os = "nto", + target_os = "espidf", )))] retries: None, } diff --git a/src/socket.rs b/src/socket.rs index fe34bea0..80f278c2 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -790,6 +790,7 @@ fn set_common_flags(socket: Socket) -> io::Result { target_os = "linux", target_os = "netbsd", target_os = "openbsd", + target_os = "espidf", )) ))] socket._set_cloexec(true)?; @@ -1108,8 +1109,8 @@ impl Socket { /// For more information about this option, see [`set_header_included`]. /// /// [`set_header_included`]: Socket::set_header_included - #[cfg(all(feature = "all", not(target_os = "redox")))] - #[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(target_os = "redox")))))] + #[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))] + #[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))))] pub fn header_included(&self) -> io::Result { unsafe { getsockopt::(self.as_raw(), sys::IPPROTO_IP, sys::IP_HDRINCL) @@ -1132,8 +1133,8 @@ impl Socket { any(target_os = "fuchsia", target_os = "illumos", target_os = "solaris"), allow(rustdoc::broken_intra_doc_links) )] - #[cfg(all(feature = "all", not(target_os = "redox")))] - #[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(target_os = "redox")))))] + #[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))] + #[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))))] pub fn set_header_included(&self, included: bool) -> io::Result<()> { unsafe { setsockopt( @@ -1237,6 +1238,7 @@ impl Socket { target_os = "redox", target_os = "solaris", target_os = "nto", + target_os = "espidf", )))] pub fn join_multicast_v4_n( &self, @@ -1268,6 +1270,7 @@ impl Socket { target_os = "redox", target_os = "solaris", target_os = "nto", + target_os = "espidf", )))] pub fn leave_multicast_v4_n( &self, @@ -1300,6 +1303,7 @@ impl Socket { target_os = "redox", target_os = "fuchsia", target_os = "nto", + target_os = "espidf", )))] pub fn join_ssm_v4( &self, @@ -1335,6 +1339,7 @@ impl Socket { target_os = "redox", target_os = "fuchsia", target_os = "nto", + target_os = "espidf", )))] pub fn leave_ssm_v4( &self, @@ -1512,6 +1517,7 @@ impl Socket { target_os = "solaris", target_os = "haiku", target_os = "nto", + target_os = "espidf", )))] pub fn set_recv_tos(&self, recv_tos: bool) -> io::Result<()> { unsafe { @@ -1540,6 +1546,7 @@ impl Socket { target_os = "solaris", target_os = "haiku", target_os = "nto", + target_os = "espidf", )))] pub fn recv_tos(&self) -> io::Result { unsafe { @@ -1755,6 +1762,7 @@ impl Socket { target_os = "redox", target_os = "solaris", target_os = "haiku", + target_os = "espidf", )))] pub fn recv_tclass_v6(&self) -> io::Result { unsafe { @@ -1777,6 +1785,7 @@ impl Socket { target_os = "redox", target_os = "solaris", target_os = "haiku", + target_os = "espidf", )))] pub fn set_recv_tclass_v6(&self, recv_tclass: bool) -> io::Result<()> { unsafe { diff --git a/src/sys/unix.rs b/src/sys/unix.rs index 808c6ef1..be979e1b 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -81,9 +81,9 @@ pub(crate) use libc::{AF_INET, AF_INET6, AF_UNIX}; // Used in `Type`. #[cfg(all(feature = "all", target_os = "linux"))] pub(crate) use libc::SOCK_DCCP; -#[cfg(all(feature = "all", not(target_os = "redox")))] +#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))] pub(crate) use libc::SOCK_RAW; -#[cfg(feature = "all")] +#[cfg(all(feature = "all", not(target_os = "espidf")))] pub(crate) use libc::SOCK_SEQPACKET; pub(crate) use libc::{SOCK_DGRAM, SOCK_STREAM}; // Used in `Protocol`. @@ -111,11 +111,19 @@ pub(crate) use libc::{ sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, sockaddr_storage, socklen_t, }; // Used in `RecvFlags`. +#[cfg(not(any(target_os = "redox", target_os = "espidf")))] +pub(crate) use libc::MSG_TRUNC; #[cfg(not(target_os = "redox"))] -pub(crate) use libc::{MSG_TRUNC, SO_OOBINLINE}; +pub(crate) use libc::SO_OOBINLINE; +#[cfg(target_os = "espidf")] +pub(crate) const MSG_TRUNC: libc::c_int = 4; // TODO: Expose in libc for ESP-IDF/LwIP + // Used in `Socket`. + // Used in `Socket`. // Used in `Socket`. #[cfg(not(target_os = "nto"))] pub(crate) use libc::ipv6_mreq as Ipv6Mreq; +#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))] +pub(crate) use libc::IP_HDRINCL; #[cfg(not(any( target_os = "dragonfly", target_os = "fuchsia", @@ -125,6 +133,7 @@ pub(crate) use libc::ipv6_mreq as Ipv6Mreq; target_os = "redox", target_os = "solaris", target_os = "haiku", + target_os = "espidf", )))] pub(crate) use libc::IPV6_RECVTCLASS; #[cfg(all(feature = "all", not(target_os = "redox")))] @@ -140,6 +149,7 @@ pub(crate) use libc::IP_HDRINCL; target_os = "solaris", target_os = "haiku", target_os = "nto", + target_os = "espidf", )))] pub(crate) use libc::IP_RECVTOS; #[cfg(not(any( @@ -178,6 +188,7 @@ pub(crate) use libc::{ target_os = "redox", target_os = "fuchsia", target_os = "nto", + target_os = "espidf", )))] pub(crate) use libc::{ ip_mreq_source as IpMreqSource, IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP, @@ -329,6 +340,7 @@ type IovLen = usize; target_os = "solaris", target_os = "tvos", target_os = "watchos", + target_os = "espidf", ))] type IovLen = c_int; @@ -471,10 +483,11 @@ impl_debug!( libc::SOCK_DGRAM, #[cfg(all(feature = "all", target_os = "linux"))] libc::SOCK_DCCP, - #[cfg(not(target_os = "redox"))] + #[cfg(not(any(target_os = "redox", target_os = "espidf")))] libc::SOCK_RAW, - #[cfg(not(any(target_os = "redox", target_os = "haiku")))] + #[cfg(not(any(target_os = "redox", target_os = "haiku", target_os = "espidf")))] libc::SOCK_RDM, + #[cfg(not(target_os = "espidf"))] libc::SOCK_SEQPACKET, /* TODO: add these optional bit OR-ed flags: #[cfg(any( @@ -539,7 +552,14 @@ impl RecvFlags { /// /// [`SEQPACKET`]: Type::SEQPACKET pub const fn is_end_of_record(self) -> bool { - self.0 & libc::MSG_EOR != 0 + // TODO: Expose this constant in libc for the ESP-IDF/LwIP framework + #[cfg(target_os = "espidf")] + const MSG_EOR: libc::c_int = 8; + + #[cfg(not(target_os = "espidf"))] + use libc::MSG_EOR; + + self.0 & MSG_EOR != 0 } /// Check if the message contains out-of-band data. @@ -1264,6 +1284,7 @@ pub(crate) fn from_in6_addr(addr: in6_addr) -> Ipv6Addr { target_os = "redox", target_os = "solaris", target_os = "nto", + target_os = "espidf", )))] pub(crate) const fn to_mreqn( multiaddr: &Ipv4Addr, From d55d2f411948d768a9369100688c9ac305f233e9 Mon Sep 17 00:00:00 2001 From: Josh Guilfoyle Date: Thu, 13 Jul 2023 21:13:25 -0700 Subject: [PATCH 2/4] Fix merge error causing multiple definitions of IP_HDRINCL --- src/sys/unix.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/sys/unix.rs b/src/sys/unix.rs index be979e1b..bd27c25c 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -122,8 +122,6 @@ pub(crate) const MSG_TRUNC: libc::c_int = 4; // TODO: Expose in libc for ESP-IDF // Used in `Socket`. #[cfg(not(target_os = "nto"))] pub(crate) use libc::ipv6_mreq as Ipv6Mreq; -#[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))] -pub(crate) use libc::IP_HDRINCL; #[cfg(not(any( target_os = "dragonfly", target_os = "fuchsia", From 10a88c04d4d3e1ad69ed7fd28904e56f36433827 Mon Sep 17 00:00:00 2001 From: Josh Guilfoyle Date: Thu, 13 Jul 2023 21:16:03 -0700 Subject: [PATCH 3/4] cargo fmt and fix a minor merge error added superfluous docs --- src/socket.rs | 10 ++++++++-- src/sys/unix.rs | 5 ++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/socket.rs b/src/socket.rs index 80f278c2..73b806ba 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -1110,7 +1110,10 @@ impl Socket { /// /// [`set_header_included`]: Socket::set_header_included #[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))] - #[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))))] + #[cfg_attr( + docsrs, + doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))) + )] pub fn header_included(&self) -> io::Result { unsafe { getsockopt::(self.as_raw(), sys::IPPROTO_IP, sys::IP_HDRINCL) @@ -1134,7 +1137,10 @@ impl Socket { allow(rustdoc::broken_intra_doc_links) )] #[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))] - #[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))))] + #[cfg_attr( + docsrs, + doc(cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))) + )] pub fn set_header_included(&self, included: bool) -> io::Result<()> { unsafe { setsockopt( diff --git a/src/sys/unix.rs b/src/sys/unix.rs index bd27c25c..88d31559 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -115,10 +115,9 @@ pub(crate) use libc::{ pub(crate) use libc::MSG_TRUNC; #[cfg(not(target_os = "redox"))] pub(crate) use libc::SO_OOBINLINE; +// TODO: Expose in libc for ESP-IDF/LwIP #[cfg(target_os = "espidf")] -pub(crate) const MSG_TRUNC: libc::c_int = 4; // TODO: Expose in libc for ESP-IDF/LwIP - // Used in `Socket`. - // Used in `Socket`. +pub(crate) const MSG_TRUNC: libc::c_int = 4; // Used in `Socket`. #[cfg(not(target_os = "nto"))] pub(crate) use libc::ipv6_mreq as Ipv6Mreq; From a607e6bf8ca23e7846866743266c8353b4782fbe Mon Sep 17 00:00:00 2001 From: Josh Guilfoyle Date: Fri, 14 Jul 2023 18:16:18 -0700 Subject: [PATCH 4/4] espidf: Remove dummy support for is_truncated and is_end_of_record --- src/lib.rs | 1 + src/sys/unix.rs | 25 +++++++++---------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 879a2e72..7ed91054 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -377,6 +377,7 @@ impl RecvFlags { /// /// On Unix this corresponds to the `MSG_TRUNC` flag. /// On Windows this corresponds to the `WSAEMSGSIZE` error code. + #[cfg(not(target_os = "espidf"))] pub const fn is_truncated(self) -> bool { self.0 & sys::MSG_TRUNC != 0 } diff --git a/src/sys/unix.rs b/src/sys/unix.rs index 88d31559..788d9fdb 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -115,9 +115,6 @@ pub(crate) use libc::{ pub(crate) use libc::MSG_TRUNC; #[cfg(not(target_os = "redox"))] pub(crate) use libc::SO_OOBINLINE; -// TODO: Expose in libc for ESP-IDF/LwIP -#[cfg(target_os = "espidf")] -pub(crate) const MSG_TRUNC: libc::c_int = 4; // Used in `Socket`. #[cfg(not(target_os = "nto"))] pub(crate) use libc::ipv6_mreq as Ipv6Mreq; @@ -548,15 +545,9 @@ impl RecvFlags { /// On Unix this corresponds to the `MSG_EOR` flag. /// /// [`SEQPACKET`]: Type::SEQPACKET + #[cfg(not(target_os = "espidf"))] pub const fn is_end_of_record(self) -> bool { - // TODO: Expose this constant in libc for the ESP-IDF/LwIP framework - #[cfg(target_os = "espidf")] - const MSG_EOR: libc::c_int = 8; - - #[cfg(not(target_os = "espidf"))] - use libc::MSG_EOR; - - self.0 & MSG_EOR != 0 + self.0 & libc::MSG_EOR != 0 } /// Check if the message contains out-of-band data. @@ -573,11 +564,13 @@ impl RecvFlags { #[cfg(not(target_os = "redox"))] impl std::fmt::Debug for RecvFlags { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("RecvFlags") - .field("is_end_of_record", &self.is_end_of_record()) - .field("is_out_of_band", &self.is_out_of_band()) - .field("is_truncated", &self.is_truncated()) - .finish() + let mut s = f.debug_struct("RecvFlags"); + #[cfg(not(target_os = "espidf"))] + s.field("is_end_of_record", &self.is_end_of_record()); + s.field("is_out_of_band", &self.is_out_of_band()); + #[cfg(not(target_os = "espidf"))] + s.field("is_truncated", &self.is_truncated()); + s.finish() } }