Skip to content

Commit 0eb105b

Browse files
committed
NetBSD: Introduce if_.rs, fix the definition of ifreq
The definition of `struct ifreq` didn't match up. Make a new module for `net/if.h` and fix it there. Note that this drops some trait implementations since the correct API involves unions. (backport <#4782>) (cherry picked from commit 8cb1db9)
1 parent b3d3fa1 commit 0eb105b

File tree

5 files changed

+102
-76
lines changed

5 files changed

+102
-76
lines changed

libc-test/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,7 @@ fn test_netbsd(target: &str) {
14901490

14911491
// Anonymous unions
14921492
("ifconf", "ifc_ifcu") => true,
1493+
("ifreq", "ifr_ifru") => true,
14931494
("utmpx", "ut_exit") => true,
14941495
("posix_spawn_file_actions_entry_t", "fae_data") => true,
14951496

src/new/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ cfg_if! {
183183
} else if #[cfg(target_vendor = "apple")] {
184184
pub use signal::*;
185185
} else if #[cfg(target_os = "netbsd")] {
186+
pub use net::if_::*;
186187
pub use sys::ipc::*;
187188
pub use sys::statvfs::*;
188189
pub use sys::time::*;

src/new/netbsd/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
//! * Sys headers: <https://github.com/NetBSD/src/tree/trunk/sys/sys>
55
//! * Manual pages: <https://man.netbsd.org/>
66
7+
pub(crate) mod net {
8+
pub(crate) mod if_;
9+
}
10+
711
pub(crate) mod sys;
812
pub(crate) mod unistd;
913
pub(crate) mod utmp_;

src/new/netbsd/net/if_.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//! Header: `net/if.h`
2+
//!
3+
//! <https://github.com/NetBSD/src/tree/trunk/sys/timex.h>
4+
5+
use crate::prelude::*;
6+
use crate::IFNAMSIZ;
7+
8+
s! {
9+
pub struct if_data {
10+
pub ifi_type: c_uchar,
11+
pub ifi_addrlen: c_uchar,
12+
pub ifi_hdrlen: c_uchar,
13+
pub ifi_link_state: c_int,
14+
pub ifi_mtu: u64,
15+
pub ifi_metric: u64,
16+
pub ifi_baudrate: u64,
17+
pub ifi_ipackets: u64,
18+
pub ifi_ierrors: u64,
19+
pub ifi_opackets: u64,
20+
pub ifi_oerrors: u64,
21+
pub ifi_collisions: u64,
22+
pub ifi_ibytes: u64,
23+
pub ifi_obytes: u64,
24+
pub ifi_imcasts: u64,
25+
pub ifi_omcasts: u64,
26+
pub ifi_iqdrops: u64,
27+
pub ifi_noproto: u64,
28+
pub ifi_lastchange: crate::timespec,
29+
}
30+
}
31+
32+
pub const IFF_UP: c_int = 0x0001; // interface is up
33+
pub const IFF_BROADCAST: c_int = 0x0002; // broadcast address valid
34+
pub const IFF_DEBUG: c_int = 0x0004; // turn on debugging
35+
pub const IFF_LOOPBACK: c_int = 0x0008; // is a loopback net
36+
pub const IFF_POINTOPOINT: c_int = 0x0010; // interface is point-to-point link
37+
pub const IFF_RUNNING: c_int = 0x0040; // resources allocated
38+
pub const IFF_NOARP: c_int = 0x0080; // no address resolution protocol
39+
pub const IFF_PROMISC: c_int = 0x0100; // receive all packets
40+
pub const IFF_ALLMULTI: c_int = 0x0200; // receive all multicast packets
41+
pub const IFF_OACTIVE: c_int = 0x0400; // transmission in progress
42+
pub const IFF_SIMPLEX: c_int = 0x0800; // can't hear own transmissions
43+
pub const IFF_LINK0: c_int = 0x1000; // per link layer defined bit
44+
pub const IFF_LINK1: c_int = 0x2000; // per link layer defined bit
45+
pub const IFF_LINK2: c_int = 0x4000; // per link layer defined bit
46+
pub const IFF_MULTICAST: c_int = 0x8000; // supports multicast
47+
48+
s! {
49+
pub struct if_msghdr {
50+
pub ifm_msglen: c_ushort,
51+
pub ifm_version: c_uchar,
52+
pub ifm_type: c_uchar,
53+
pub ifm_addrs: c_int,
54+
pub ifm_flags: c_int,
55+
pub ifm_index: c_ushort,
56+
pub ifm_data: if_data,
57+
}
58+
}
59+
60+
s_no_extra_traits! {
61+
pub struct ifreq {
62+
pub ifr_name: [c_char; IFNAMSIZ],
63+
pub ifr_ifru: __c_anonymous_ifr_ifru,
64+
}
65+
66+
pub union __c_anonymous_ifr_ifru {
67+
pub ifru_addr: crate::sockaddr,
68+
pub ifru_dstaddr: crate::sockaddr,
69+
pub ifru_broadaddr: crate::sockaddr,
70+
pub space: crate::sockaddr_storage,
71+
pub ifru_flags: c_short,
72+
pub ifru_addrflags: c_int,
73+
pub ifru_metrics: c_int,
74+
pub ifru_mtu: c_int,
75+
pub ifru_dlt: c_int,
76+
pub ifru_value: c_uint,
77+
pub ifru_data: *mut c_void,
78+
// buf and buflen are deprecated but they contribute to union size
79+
ifru_b: __c_anonymous_ifr_ifru_ifru_b,
80+
}
81+
82+
struct __c_anonymous_ifr_ifru_ifru_b {
83+
b_buflen: u32,
84+
b_buf: *mut c_void,
85+
}
86+
87+
pub struct ifconf {
88+
pub ifc_len: c_int,
89+
pub ifc_ifcu: __c_anonymous_ifc_ifcu,
90+
}
91+
92+
pub union __c_anonymous_ifc_ifcu {
93+
pub ifcu_buf: *mut c_void,
94+
pub ifcu_req: *mut ifreq,
95+
}
96+
}

src/unix/bsd/netbsdlike/netbsd/mod.rs

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -342,38 +342,6 @@ s! {
342342
pub int_n_sign_posn: c_char,
343343
}
344344

345-
pub struct if_data {
346-
pub ifi_type: c_uchar,
347-
pub ifi_addrlen: c_uchar,
348-
pub ifi_hdrlen: c_uchar,
349-
pub ifi_link_state: c_int,
350-
pub ifi_mtu: u64,
351-
pub ifi_metric: u64,
352-
pub ifi_baudrate: u64,
353-
pub ifi_ipackets: u64,
354-
pub ifi_ierrors: u64,
355-
pub ifi_opackets: u64,
356-
pub ifi_oerrors: u64,
357-
pub ifi_collisions: u64,
358-
pub ifi_ibytes: u64,
359-
pub ifi_obytes: u64,
360-
pub ifi_imcasts: u64,
361-
pub ifi_omcasts: u64,
362-
pub ifi_iqdrops: u64,
363-
pub ifi_noproto: u64,
364-
pub ifi_lastchange: crate::timespec,
365-
}
366-
367-
pub struct if_msghdr {
368-
pub ifm_msglen: c_ushort,
369-
pub ifm_version: c_uchar,
370-
pub ifm_type: c_uchar,
371-
pub ifm_addrs: c_int,
372-
pub ifm_flags: c_int,
373-
pub ifm_index: c_ushort,
374-
pub ifm_data: if_data,
375-
}
376-
377345
pub struct sockcred {
378346
pub sc_pid: crate::pid_t,
379347
pub sc_uid: crate::uid_t,
@@ -685,15 +653,6 @@ s! {
685653
pub descr_str: [c_char; 1],
686654
}
687655

688-
pub struct ifreq {
689-
pub _priv: [[c_char; 6]; 24],
690-
}
691-
692-
pub struct ifconf {
693-
pub ifc_len: c_int,
694-
pub ifc_ifcu: __c_anonymous_ifc_ifcu,
695-
}
696-
697656
pub struct tcp_info {
698657
pub tcpi_state: u8,
699658
pub __tcpi_ca_state: u8,
@@ -797,11 +756,6 @@ s_no_extra_traits! {
797756
pub open: __c_anonymous_posix_spawn_fae_open,
798757
pub dup2: __c_anonymous_posix_spawn_fae_dup2,
799758
}
800-
801-
pub union __c_anonymous_ifc_ifcu {
802-
pub ifcu_buf: *mut c_void,
803-
pub ifcu_req: *mut ifreq,
804-
}
805759
}
806760

807761
cfg_if! {
@@ -820,21 +774,6 @@ cfg_if! {
820774
}
821775
}
822776
}
823-
824-
impl Eq for __c_anonymous_ifc_ifcu {}
825-
impl PartialEq for __c_anonymous_ifc_ifcu {
826-
fn eq(&self, other: &__c_anonymous_ifc_ifcu) -> bool {
827-
unsafe { self.ifcu_buf == other.ifcu_buf || self.ifcu_req == other.ifcu_req }
828-
}
829-
}
830-
impl hash::Hash for __c_anonymous_ifc_ifcu {
831-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
832-
unsafe {
833-
self.ifcu_buf.hash(state);
834-
self.ifcu_req.hash(state);
835-
}
836-
}
837-
}
838777
}
839778
}
840779

@@ -1000,21 +939,6 @@ pub const LOCAL_PEEREID: c_int = 0x0003; // get peer identification
1000939
pub const LOCAL_CREDS: c_int = 0x0004; // pass credentials to receiver
1001940

1002941
// https://github.com/NetBSD/src/blob/trunk/sys/net/if.h#L373
1003-
pub const IFF_UP: c_int = 0x0001; // interface is up
1004-
pub const IFF_BROADCAST: c_int = 0x0002; // broadcast address valid
1005-
pub const IFF_DEBUG: c_int = 0x0004; // turn on debugging
1006-
pub const IFF_LOOPBACK: c_int = 0x0008; // is a loopback net
1007-
pub const IFF_POINTOPOINT: c_int = 0x0010; // interface is point-to-point link
1008-
pub const IFF_RUNNING: c_int = 0x0040; // resources allocated
1009-
pub const IFF_NOARP: c_int = 0x0080; // no address resolution protocol
1010-
pub const IFF_PROMISC: c_int = 0x0100; // receive all packets
1011-
pub const IFF_ALLMULTI: c_int = 0x0200; // receive all multicast packets
1012-
pub const IFF_OACTIVE: c_int = 0x0400; // transmission in progress
1013-
pub const IFF_SIMPLEX: c_int = 0x0800; // can't hear own transmissions
1014-
pub const IFF_LINK0: c_int = 0x1000; // per link layer defined bit
1015-
pub const IFF_LINK1: c_int = 0x2000; // per link layer defined bit
1016-
pub const IFF_LINK2: c_int = 0x4000; // per link layer defined bit
1017-
pub const IFF_MULTICAST: c_int = 0x8000; // supports multicast
1018942

1019943
// sys/netinet/in.h
1020944
// Protocols (RFC 1700)

0 commit comments

Comments
 (0)