Sha256: ec0bf20a354cb86e2b5646bfc79297a378f11fcdf5641c16e4dd13e305011dc6

Contents?: true

Size: 1.96 KB

Versions: 17

Compression:

Stored size: 1.96 KB

Contents

//! The BSD sockets API requires us to read the `ss_family` field before
//! we can interpret the rest of a `sockaddr` produced by the kernel.
#![allow(unsafe_code)]

use super::super::c;
use crate::net::{SocketAddrAny, SocketAddrStorage, SocketAddrUnix, SocketAddrV4, SocketAddrV6};
use core::mem::size_of;

pub(crate) unsafe fn write_sockaddr(
    addr: &SocketAddrAny,
    storage: *mut SocketAddrStorage,
) -> usize {
    match addr {
        SocketAddrAny::V4(v4) => write_sockaddr_v4(v4, storage),
        SocketAddrAny::V6(v6) => write_sockaddr_v6(v6, storage),
        SocketAddrAny::Unix(unix) => write_sockaddr_unix(unix, storage),
    }
}

pub(crate) unsafe fn encode_sockaddr_v4(v4: &SocketAddrV4) -> c::sockaddr_in {
    c::sockaddr_in {
        sin_family: c::AF_INET as _,
        sin_port: u16::to_be(v4.port()),
        sin_addr: c::in_addr {
            s_addr: u32::from_ne_bytes(v4.ip().octets()),
        },
        __pad: [0_u8; 8],
    }
}

unsafe fn write_sockaddr_v4(v4: &SocketAddrV4, storage: *mut SocketAddrStorage) -> usize {
    let encoded = encode_sockaddr_v4(v4);
    core::ptr::write(storage.cast(), encoded);
    size_of::<c::sockaddr_in>()
}

pub(crate) unsafe fn encode_sockaddr_v6(v6: &SocketAddrV6) -> c::sockaddr_in6 {
    c::sockaddr_in6 {
        sin6_family: c::AF_INET6 as _,
        sin6_port: u16::to_be(v6.port()),
        sin6_flowinfo: u32::to_be(v6.flowinfo()),
        sin6_addr: c::in6_addr {
            in6_u: linux_raw_sys::general::in6_addr__bindgen_ty_1 {
                u6_addr8: v6.ip().octets(),
            },
        },
        sin6_scope_id: v6.scope_id(),
    }
}

unsafe fn write_sockaddr_v6(v6: &SocketAddrV6, storage: *mut SocketAddrStorage) -> usize {
    let encoded = encode_sockaddr_v6(v6);
    core::ptr::write(storage.cast(), encoded);
    size_of::<c::sockaddr_in6>()
}

unsafe fn write_sockaddr_unix(unix: &SocketAddrUnix, storage: *mut SocketAddrStorage) -> usize {
    core::ptr::write(storage.cast(), unix.unix);
    unix.len()
}

Version data entries

17 entries across 15 versions & 1 rubygems

Version Path
wasmtime-13.0.0 ./ext/cargo-vendor/rustix-0.37.23/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-12.0.1 ./ext/cargo-vendor/rustix-0.37.23/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-12.0.0 ./ext/cargo-vendor/rustix-0.37.23/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-11.0.0 ./ext/cargo-vendor/rustix-0.37.23/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-10.0.1 ./ext/cargo-vendor/rustix-0.37.23/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-10.0.0 ./ext/cargo-vendor/rustix-0.37.23/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-9.0.4 ./ext/cargo-vendor/rustix-0.37.23/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-9.0.1 ./ext/cargo-vendor/rustix-0.37.19/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-8.0.0 ./ext/cargo-vendor/rustix-0.36.12/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-8.0.0 ./ext/cargo-vendor/rustix-0.37.13/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-7.0.0 ./ext/cargo-vendor/rustix-0.36.11/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-7.0.0 ./ext/cargo-vendor/rustix-0.37.5/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-6.0.1 ./ext/cargo-vendor/rustix-0.36.8/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-6.0.0 ./ext/cargo-vendor/rustix-0.36.8/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-5.0.0 ./ext/cargo-vendor/rustix-0.36.7/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-0.4.1 ./ext/cargo-vendor/rustix-0.36.5/src/backend/linux_raw/net/write_sockaddr.rs
wasmtime-0.4.0 ./ext/cargo-vendor/rustix-0.36.5/src/backend/linux_raw/net/write_sockaddr.rs