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 30, 2021
1 parent fdd1a9f commit 0751b23
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Raised bitflags to 1.3.0 and the MSRV to 1.46.0.
([#1492](https://github.com/nix-rust/nix/pull/1492))

- Changed `SockAddr::as_ffi_pair` to return a raw pointer to the `sockaddr`
instead of a reference.
(#[1504](https://github.com/nix-rust/nix/pull/1504))

### Fixed

- `posix_fadvise` now returns errors in the conventional way, rather than as a
Expand Down
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 0751b23

Please sign in to comment.