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

adding ifreq struct for openbsd #3384

Merged
merged 1 commit into from
Oct 14, 2023
Merged
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
14 changes: 14 additions & 0 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,19 @@ fn test_openbsd(target: &str) {
"sys/param.h",
}

cfg.skip_type(move |ty| {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code seems not required (it built fine without it)

if ty.starts_with("__c_anonymous_") {
return true;
}
match ty {
_ => false,
}
});

cfg.skip_struct(move |ty| {
if ty.starts_with("__c_anonymous_") {
return true;
}
match ty {
// FIXME: actually a union
"sigval" => true,
Expand Down Expand Up @@ -596,6 +608,8 @@ fn test_openbsd(target: &str) {
// conflicting with `p_type` macro from <resolve.h>.
("Elf32_Phdr", "p_type") => true,
("Elf64_Phdr", "p_type") => true,
// ifr_ifru is defined is an union
("ifreq", "ifr_ifru") => true,
_ => false,
}
});
Expand Down
1 change: 1 addition & 0 deletions libc-test/semver/openbsd.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,7 @@ if_freenameindex
if_msghdr
if_nameindex
ifaddrs
ifreq
in6_pktinfo
initgroups
ip_mreqn
Expand Down
74 changes: 74 additions & 0 deletions src/unix/bsd/netbsdlike/openbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@ s! {
pub key: *mut ::c_char,
pub data: *mut ::c_void,
}

pub struct ifreq {
pub ifr_name: [::c_char; ::IFNAMSIZ],
#[cfg(libc_union)]
pub ifr_ifru: __c_anonymous_ifr_ifru,
#[cfg(not(libc_union))]
pub ifr_ifru: ::sockaddr,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in not libc_union case, the struct size is wrong and could lead to have ioctl(2) write outside the struct.

}

impl siginfo_t {
Expand Down Expand Up @@ -608,6 +616,18 @@ s_no_extra_traits! {
align: [::c_char; 160],
}

#[cfg(libc_union)]
pub union __c_anonymous_ifr_ifru {
pub ifru_addr: ::sockaddr,
pub ifru_dstaddr: ::sockaddr,
pub ifru_broadaddr: ::sockaddr,
pub ifru_flags: ::c_short,
pub ifru_metric: ::c_int,
pub ifru_vnetid: i64,
pub ifru_media: u64,
pub ifru_data: *mut ::c_char,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use ::caddr_t as the C struct. *mut ::c_char is valid, but matching the C file is specially important here (as the struct isn't check at all by tests due to __c_anonymous_ name prefix).

pub ifru_index: ::c_uint,
}
}

cfg_if! {
Expand Down Expand Up @@ -814,6 +834,60 @@ cfg_if! {
unsafe { self.align.hash(state) };
}
}

#[cfg(libc_union)]
impl PartialEq for __c_anonymous_ifr_ifru {
fn eq(&self, other: &__c_anonymous_ifr_ifru) -> bool {
unsafe {
self.ifru_addr == other.ifru_addr
&& self.ifru_dstaddr == other.ifru_dstaddr
&& self.ifru_broadaddr == other.ifru_broadaddr
&& self.ifru_flags == other.ifru_flags
&& self.ifru_metric == other.ifru_metric
&& self.ifru_vnetid == other.ifru_vnetid
&& self.ifru_media == other.ifru_media
&& self.ifru_data == other.ifru_data
&& self.ifru_index == other.ifru_index
}
}
}

#[cfg(libc_union)]
impl Eq for __c_anonymous_ifr_ifru {}

#[cfg(libc_union)]
impl ::fmt::Debug for __c_anonymous_ifr_ifru {
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
f.debug_struct("__c_anonymous_ifr_ifru")
.field("ifru_addr", unsafe { &self.ifru_addr })
.field("ifru_dstaddr", unsafe { &self.ifru_dstaddr })
.field("ifru_broadaddr", unsafe { &self.ifru_broadaddr })
.field("ifru_flags", unsafe { &self.ifru_flags })
.field("ifru_metric", unsafe { &self.ifru_metric })
.field("ifru_vnetid", unsafe { &self.ifru_vnetid })
.field("ifru_media", unsafe { &self.ifru_media })
.field("ifru_data", unsafe { &self.ifru_data })
.field("ifru_index", unsafe { &self.ifru_index })
.finish()
}
}

#[cfg(libc_union)]
impl ::hash::Hash for __c_anonymous_ifr_ifru {
fn hash<H: ::hash::Hasher>(&self, state: &mut H) {
unsafe {
self.ifru_addr.hash(state);
self.ifru_dstaddr.hash(state);
self.ifru_broadaddr.hash(state);
self.ifru_flags.hash(state);
self.ifru_metric.hash(state);
self.ifru_vnetid.hash(state);
self.ifru_media.hash(state);
self.ifru_data.hash(state);
self.ifru_index.hash(state);
}
}
}
}
}

Expand Down