Skip to content

Commit 9c96179

Browse files
Add Rust type to represent a valid libc::sockaddr_ll
1 parent 047d662 commit 9c96179

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/sys/socket/addr.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,64 @@ mod datalink {
18641864
}
18651865
}
18661866

1867+
/// A Rust representation of [libc::sockaddr_ll] with the additional
1868+
/// guarantee of valid data, so that this can be made into a [LinkAddr]
1869+
/// without the need of `unsafe` code.
1870+
///
1871+
/// # Examples
1872+
///
1873+
/// ```edition2021
1874+
/// use nix::sys::socket::{LinkAddr, SockAddrLl};
1875+
///
1876+
/// let addr = LinkAddr::from(SockAddrLl {
1877+
/// sll_family: libc::AF_PACKET as u16,
1878+
/// sll_ifindex: 0,
1879+
/// sll_protocol: 0,
1880+
/// sll_addr: [0; 8],
1881+
/// sll_halen: 0,
1882+
/// sll_hatype: 0,
1883+
/// sll_pkttype: 0,
1884+
/// });
1885+
/// ```
1886+
#[derive(Copy, Clone, Debug)]
1887+
pub struct SockAddrLl {
1888+
/// See [libc::sockaddr_ll::sll_family]
1889+
pub sll_family: u16,
1890+
/// See [libc::sockaddr_ll::sll_protocol]
1891+
pub sll_protocol: u16,
1892+
/// See [libc::sockaddr_ll::sll_ifindex]
1893+
pub sll_ifindex: i32,
1894+
/// See [libc::sockaddr_ll::sll_hatype]
1895+
pub sll_hatype: u16,
1896+
/// See [libc::sockaddr_ll::sll_pkttype]
1897+
pub sll_pkttype: u8,
1898+
/// See [libc::sockaddr_ll::sll_halen]
1899+
pub sll_halen: u8,
1900+
/// See [libc::sockaddr_ll::sll_addr]
1901+
pub sll_addr: [u8; 8]
1902+
}
1903+
1904+
1905+
impl From<SockAddrLl> for libc::sockaddr_ll {
1906+
fn from(sll: SockAddrLl) -> Self {
1907+
Self {
1908+
sll_family: sll.sll_family,
1909+
sll_protocol: sll.sll_protocol,
1910+
sll_ifindex: sll.sll_ifindex,
1911+
sll_hatype: sll.sll_hatype,
1912+
sll_pkttype: sll.sll_pkttype,
1913+
sll_halen: sll.sll_halen,
1914+
sll_addr: sll.sll_addr,
1915+
}
1916+
}
1917+
}
1918+
1919+
impl From<SockAddrLl> for LinkAddr {
1920+
fn from(sll: SockAddrLl) -> Self {
1921+
Self(sll.into())
1922+
}
1923+
}
1924+
18671925
impl fmt::Display for LinkAddr {
18681926
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18691927
if let Some(addr) = self.addr() {

0 commit comments

Comments
 (0)