Skip to content

Commit 937aeaa

Browse files
committed
Auto merge of #2960 - folkertdev:linux-ifreq, r=JohnTitor
implement ifreq for linux defines the `ifreq` struct, for use with `ioctl`. I used the definition from the [manpages](https://man7.org/linux/man-pages/man7/netdevice.7.html)
2 parents e4e865d + 7266718 commit 937aeaa

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

libc-test/build.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2887,6 +2887,7 @@ fn test_linux(target: &str) {
28872887
"linux/if_ether.h",
28882888
"linux/if_tun.h",
28892889
"linux/input.h",
2890+
"linux/ipv6.h",
28902891
"linux/keyctl.h",
28912892
"linux/magic.h",
28922893
"linux/memfd.h",
@@ -3479,7 +3480,9 @@ fn test_linux(target: &str) {
34793480
(struct_ == "user_fpsimd_struct" && field == "vregs") ||
34803481
// Linux >= 5.11 tweaked the `svm_zero` field of the `sockaddr_vm` struct.
34813482
// https://github.com/torvalds/linux/commit/dc8eeef73b63ed8988224ba6b5ed19a615163a7f
3482-
(struct_ == "sockaddr_vm" && field == "svm_zero")
3483+
(struct_ == "sockaddr_vm" && field == "svm_zero") ||
3484+
// the `ifr_ifru` field is an anonymous union
3485+
(struct_ == "ifreq" && field == "ifr_ifru")
34833486
});
34843487

34853488
cfg.skip_roundtrip(move |s| match s {

libc-test/semver/linux.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,6 +2971,8 @@ idtype_t
29712971
if_freenameindex
29722972
if_nameindex
29732973
ifaddrs
2974+
ifreq
2975+
in6_ifreq
29742976
in6_pktinfo
29752977
in6_rtmsg
29762978
in_pktinfo

src/unix/linux_like/linux/mod.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,21 @@ s! {
603603
pub src_length: ::__u64,
604604
pub dest_offset: ::__u64,
605605
}
606+
607+
pub struct __c_anonymous_ifru_map {
608+
pub mem_start: ::c_ulong,
609+
pub mem_end: ::c_ulong,
610+
pub base_addr: ::c_ushort,
611+
pub irq: ::c_uchar,
612+
pub dma: ::c_uchar,
613+
pub port: ::c_uchar,
614+
}
615+
616+
pub struct in6_ifreq {
617+
pub ifr6_addr: ::in6_addr,
618+
pub ifr6_prefixlen: u32,
619+
pub ifr6_ifindex: ::c_int,
620+
}
606621
}
607622

608623
s_no_extra_traits! {
@@ -690,6 +705,32 @@ s_no_extra_traits! {
690705
#[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))]
691706
pad: [::c_long; 4],
692707
}
708+
709+
#[cfg(libc_union)]
710+
pub union __c_anonymous_ifr_ifru {
711+
pub ifru_addr: ::sockaddr,
712+
pub ifru_dstaddr: ::sockaddr,
713+
pub ifru_broadaddr: ::sockaddr,
714+
pub ifru_netmask: ::sockaddr,
715+
pub ifru_hwaddr: ::sockaddr,
716+
pub ifru_flags: ::c_short,
717+
pub ifru_ifindex: ::c_int,
718+
pub ifru_metric: ::c_int,
719+
pub ifru_mtu: ::c_int,
720+
pub ifru_map: __c_anonymous_ifru_map,
721+
pub ifru_slave: [::c_char; ::IFNAMSIZ],
722+
pub ifru_newname: [::c_char; ::IFNAMSIZ],
723+
pub ifru_data: *mut ::c_char,
724+
}
725+
726+
pub struct ifreq {
727+
/// interface name, e.g. "en0"
728+
pub ifr_name: [::c_char; ::IFNAMSIZ],
729+
#[cfg(libc_union)]
730+
pub ifr_ifru: __c_anonymous_ifr_ifru,
731+
#[cfg(not(libc_union))]
732+
pub ifr_ifru: ::sockaddr,
733+
}
693734
}
694735

695736
s_no_extra_traits! {
@@ -1063,6 +1104,34 @@ cfg_if! {
10631104
self.mq_curmsgs.hash(state);
10641105
}
10651106
}
1107+
#[cfg(libc_union)]
1108+
impl ::fmt::Debug for __c_anonymous_ifr_ifru {
1109+
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
1110+
f.debug_struct("ifr_ifru")
1111+
.field("ifru_addr", unsafe { &self.ifru_addr })
1112+
.field("ifru_dstaddr", unsafe { &self.ifru_dstaddr })
1113+
.field("ifru_broadaddr", unsafe { &self.ifru_broadaddr })
1114+
.field("ifru_netmask", unsafe { &self.ifru_netmask })
1115+
.field("ifru_hwaddr", unsafe { &self.ifru_hwaddr })
1116+
.field("ifru_flags", unsafe { &self.ifru_flags })
1117+
.field("ifru_ifindex", unsafe { &self.ifru_ifindex })
1118+
.field("ifru_metric", unsafe { &self.ifru_metric })
1119+
.field("ifru_mtu", unsafe { &self.ifru_mtu })
1120+
.field("ifru_map", unsafe { &self.ifru_map })
1121+
.field("ifru_slave", unsafe { &self.ifru_slave })
1122+
.field("ifru_newname", unsafe { &self.ifru_newname })
1123+
.field("ifru_data", unsafe { &self.ifru_data })
1124+
.finish()
1125+
}
1126+
}
1127+
impl ::fmt::Debug for ifreq {
1128+
fn fmt(&self, f: &mut ::fmt::Formatter) -> ::fmt::Result {
1129+
f.debug_struct("ifreq")
1130+
.field("ifr_name", &self.ifr_name)
1131+
.field("ifr_ifru", &self.ifr_ifru)
1132+
.finish()
1133+
}
1134+
}
10661135
}
10671136
}
10681137

0 commit comments

Comments
 (0)