Skip to content

Commit

Permalink
as_ffi_pair returns a raw pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
coolreader18 committed Sep 23, 2021
1 parent 720853a commit 32b2bd7
Showing 1 changed file with 10 additions and 37 deletions.
47 changes: 10 additions & 37 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -907,60 +907,39 @@ impl SockAddr {
/// with the size of the actual data type. sockaddr is commonly used as a proxy for
/// a superclass as C doesn't support inheritance, so many functions that take
/// a sockaddr * need to take the size of the underlying type as well and then internally cast it back.
pub fn as_ffi_pair(&self) -> (&libc::sockaddr, libc::socklen_t) {
pub fn as_ffi_pair(&self) -> (*const libc::sockaddr, libc::socklen_t) {
match *self {
SockAddr::Inet(InetAddr::V4(ref addr)) => (
// This cast is always allowed in C
unsafe {
&*(addr as *const libc::sockaddr_in as *const libc::sockaddr)
},
addr as *const libc::sockaddr_in as *const libc::sockaddr,
mem::size_of_val(addr) as libc::socklen_t
),
SockAddr::Inet(InetAddr::V6(ref addr)) => (
// This cast is always allowed in C
unsafe {
&*(addr as *const libc::sockaddr_in6 as *const libc::sockaddr)
},
addr as *const libc::sockaddr_in6 as *const libc::sockaddr,
mem::size_of_val(addr) as libc::socklen_t
),
SockAddr::Unix(UnixAddr { ref sun, path_len }) => (
// This cast is always allowed in C
unsafe {
&*(sun as *const libc::sockaddr_un as *const libc::sockaddr)
},
sun as *const libc::sockaddr_un as *const libc::sockaddr,
(path_len + offset_of!(libc::sockaddr_un, sun_path)) as libc::socklen_t
),
#[cfg(any(target_os = "android", target_os = "linux"))]
SockAddr::Netlink(NetlinkAddr(ref sa)) => (
// This cast is always allowed in C
unsafe {
&*(sa as *const libc::sockaddr_nl as *const libc::sockaddr)
},
sa as *const libc::sockaddr_nl as *const libc::sockaddr,
mem::size_of_val(sa) as libc::socklen_t
),
#[cfg(any(target_os = "android", target_os = "linux"))]
SockAddr::Alg(AlgAddr(ref sa)) => (
// This cast is always allowed in C
unsafe {
&*(sa as *const libc::sockaddr_alg as *const libc::sockaddr)
},
sa as *const libc::sockaddr_alg as *const libc::sockaddr,
mem::size_of_val(sa) as libc::socklen_t
),
#[cfg(any(target_os = "ios", target_os = "macos"))]
SockAddr::SysControl(SysControlAddr(ref sa)) => (
// This cast is always allowed in C
unsafe {
&*(sa as *const libc::sockaddr_ctl as *const libc::sockaddr)
},
sa as *const libc::sockaddr_ctl as *const libc::sockaddr,
mem::size_of_val(sa) as libc::socklen_t

),
#[cfg(any(target_os = "android", target_os = "linux"))]
SockAddr::Link(LinkAddr(ref addr)) => (
// This cast is always allowed in C
unsafe {
&*(addr as *const libc::sockaddr_ll as *const libc::sockaddr)
},
addr as *const libc::sockaddr_ll as *const libc::sockaddr,
mem::size_of_val(addr) as libc::socklen_t
),
#[cfg(any(target_os = "dragonfly",
Expand All @@ -971,18 +950,12 @@ impl SockAddr {
target_os = "netbsd",
target_os = "openbsd"))]
SockAddr::Link(LinkAddr(ref addr)) => (
// This cast is always allowed in C
unsafe {
&*(addr as *const libc::sockaddr_dl as *const libc::sockaddr)
},
addr as *const libc::sockaddr_dl as *const libc::sockaddr,
mem::size_of_val(addr) as libc::socklen_t
),
#[cfg(any(target_os = "android", target_os = "linux"))]
SockAddr::Vsock(VsockAddr(ref sa)) => (
// This cast is always allowed in C
unsafe {
&*(sa as *const libc::sockaddr_vm as *const libc::sockaddr)
},
sa as *const libc::sockaddr_vm as *const libc::sockaddr,
mem::size_of_val(sa) as libc::socklen_t
),
}
Expand Down

0 comments on commit 32b2bd7

Please sign in to comment.