Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge sockaddr_storage_to_addr and SockAddr::from_raw_sockaddr #1504

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Added read/write accessors for 'events' on `PollFd`.
(#[1517](https://github.com/nix-rust/nix/pull/1517))

- Deprecated `nix::sys::socket::sockaddr_storage_to_addr` in favor of the new
`SockAddr::from_raw_sockaddr`.
(#[1504](https://github.com/nix-rust/nix/pull/1504))

### Changed

- `FdSet::{contains, highest, fds}` no longer require a mutable reference.
Expand Down Expand Up @@ -81,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
40 changes: 32 additions & 8 deletions src/ifaddrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use std::iter::Iterator;
use std::mem;
use std::option::Option;

use crate::{Result, Errno};
use crate::sys::socket::SockAddr;
use crate::net::if_::*;
use crate::sys::socket::SockAddr;
use crate::{Errno, Result};

/// Describes a single address for an interface as returned by `getifaddrs`.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
Expand Down Expand Up @@ -46,8 +46,31 @@ impl InterfaceAddress {
/// Create an `InterfaceAddress` from the libc struct.
fn from_libc_ifaddrs(info: &libc::ifaddrs) -> InterfaceAddress {
let ifname = unsafe { ffi::CStr::from_ptr(info.ifa_name) };
let address = unsafe { SockAddr::from_libc_sockaddr(info.ifa_addr) };
let netmask = unsafe { SockAddr::from_libc_sockaddr(info.ifa_netmask) };
let get_sockaddr = |sa: *const libc::sockaddr| {
if sa.is_null() {
return None;
}
// TODO: there's gotta be a better way to do this but this is basically
// what the man pages recommend
let len = match unsafe { (*sa).sa_family } as _ {
libc::AF_INET => mem::size_of::<libc::sockaddr_in>(),
libc::AF_INET6 => mem::size_of::<libc::sockaddr_in6>(),
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "illumos",
target_os = "fuchsia",
target_os = "solaris"
))]
libc::AF_PACKET => mem::size_of::<libc::sockaddr_in>(),
#[cfg(any(target_os = "android", target_os = "linux"))]
libc::AF_NETLINK => mem::size_of::<libc::sockaddr_nl>(),
_ => return None,
};
unsafe { SockAddr::from_raw_sockaddr(sa, len as libc::socklen_t) }.ok()
};
let address = get_sockaddr(info.ifa_addr);
let netmask = get_sockaddr(info.ifa_netmask);
let mut addr = InterfaceAddress {
interface_name: ifname.to_string_lossy().to_string(),
flags: InterfaceFlags::from_bits_truncate(info.ifa_flags as i32),
Expand All @@ -59,9 +82,9 @@ impl InterfaceAddress {

let ifu = get_ifu_from_sockaddr(info);
if addr.flags.contains(InterfaceFlags::IFF_POINTOPOINT) {
addr.destination = unsafe { SockAddr::from_libc_sockaddr(ifu) };
addr.destination = get_sockaddr(ifu);
} else if addr.flags.contains(InterfaceFlags::IFF_BROADCAST) {
addr.broadcast = unsafe { SockAddr::from_libc_sockaddr(ifu) };
addr.broadcast = get_sockaddr(ifu);
}

addr
Expand Down Expand Up @@ -127,9 +150,10 @@ pub fn getifaddrs() -> Result<InterfaceAddressIterator> {
let mut addrs = mem::MaybeUninit::<*mut libc::ifaddrs>::uninit();
unsafe {
Errno::result(libc::getifaddrs(addrs.as_mut_ptr())).map(|_| {
let addrs = addrs.assume_init();
InterfaceAddressIterator {
base: addrs.assume_init(),
next: addrs.assume_init(),
base: addrs,
next: addrs,
}
})
}
Expand Down
Loading