Skip to content

Commit

Permalink
Add Rust type to represent a valid libc::sockaddr_ll
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatanzeidler committed May 6, 2024
1 parent 047d662 commit 9c96179
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,64 @@ mod datalink {
}
}

/// A Rust representation of [libc::sockaddr_ll] with the additional
/// guarantee of valid data, so that this can be made into a [LinkAddr]
/// without the need of `unsafe` code.
///
/// # Examples
///
/// ```edition2021
/// use nix::sys::socket::{LinkAddr, SockAddrLl};
///
/// let addr = LinkAddr::from(SockAddrLl {
/// sll_family: libc::AF_PACKET as u16,
/// sll_ifindex: 0,
/// sll_protocol: 0,
/// sll_addr: [0; 8],
/// sll_halen: 0,
/// sll_hatype: 0,
/// sll_pkttype: 0,
/// });
/// ```
#[derive(Copy, Clone, Debug)]
pub struct SockAddrLl {
/// See [libc::sockaddr_ll::sll_family]
pub sll_family: u16,
/// See [libc::sockaddr_ll::sll_protocol]
pub sll_protocol: u16,
/// See [libc::sockaddr_ll::sll_ifindex]
pub sll_ifindex: i32,
/// See [libc::sockaddr_ll::sll_hatype]
pub sll_hatype: u16,
/// See [libc::sockaddr_ll::sll_pkttype]
pub sll_pkttype: u8,
/// See [libc::sockaddr_ll::sll_halen]
pub sll_halen: u8,
/// See [libc::sockaddr_ll::sll_addr]
pub sll_addr: [u8; 8]
}


impl From<SockAddrLl> for libc::sockaddr_ll {
fn from(sll: SockAddrLl) -> Self {
Self {
sll_family: sll.sll_family,
sll_protocol: sll.sll_protocol,
sll_ifindex: sll.sll_ifindex,
sll_hatype: sll.sll_hatype,
sll_pkttype: sll.sll_pkttype,
sll_halen: sll.sll_halen,
sll_addr: sll.sll_addr,
}
}
}

impl From<SockAddrLl> for LinkAddr {
fn from(sll: SockAddrLl) -> Self {
Self(sll.into())
}
}

impl fmt::Display for LinkAddr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(addr) = self.addr() {
Expand Down

0 comments on commit 9c96179

Please sign in to comment.