socket9 0.1.0-alpha.1

Extended untilities for the networking/unix sockets and raw network sockets
Documentation
/*-
 * socket9 - A RAW networking sockets manipulation and configration basing on 
 * strong types.
 * 
 * Copyright (C) 2021 Aleksandr Morozov, Lucia Hoffmann
 * Copyright (C) 2025 Aleksandr Morozov
 * 
 * The syslog-rs crate can be redistributed and/or modified
 * under the terms of either of the following licenses:
 *
 *   1. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016 OR
 *
 *   2. the Mozilla Public License Version 2.0 (the “MPL”) OR
 *                     
 *   3. The MIT License (MIT)
 */



use std::
{
    fmt, io::{self, IoSlice, IoSliceMut}, net::Shutdown, time::Duration
};


#[cfg(unix)]
use crate::Credentials;
use crate::
{
    AsOsDescr, So9MsgFlags, So9SockDomain, So9SockDwFlags, So9SockProtocol, So9SockType, SockOptMarker, Socket9ExtSo, address::{So9AddrIntoRaw, So9SocketAddr}, op_sol_socket::SoProtocol
};

#[cfg(windows)]
use std::os::windows::io::FromRawSocket;

#[cfg(windows)]
pub trait SockTypeFromRaw: FromRawSocket {}

#[cfg(unix)]
use std::os::fd::{FromRawFd, OwnedFd};

/// A cover trait for the [FromRawFd].
#[cfg(unix)]
pub trait SockTypeFromRaw: FromRawFd {}

/// A trait which standartisize the behaviour of the sockets for the [crate::Socket9].
/// 
/// `windows` OS has another representation of the socket, so it requires to implement
/// a type above the windows `owned` socket.
pub trait SocketTypeImps: AsOsDescr + Socket9ExtSo<Self> + SockTypeFromRaw + fmt::Debug
{
    /// A raw type which represents the `owned` type. For unix alike systems it is
    /// `RawFd` and for windows `RawSocket`.
    type RawType: fmt::Debug + PartialEq + Clone + Copy;

    /// Returns the raw socket.
    fn get_raw(&self) -> Self::RawType;

    fn new_sock(so_dom: So9SockDomain, so_type: So9SockType, so_proto: So9SockProtocol, dwflags: So9SockDwFlags) -> io::Result<Self>
    where 
        Self: Sized;

    fn bind_sock<A: So9AddrIntoRaw>(&self, addr: &A) -> io::Result<()>;

    fn listen_sock(&self, backlog: i32) -> io::Result<()>;

    fn accept_with_flags<A, CAST>(&self, flags: i32) -> io::Result<(CAST, A)>
    where 
        Self: Sized,
        A: TryFrom<So9SocketAddr, Error = io::Error>,
        CAST: SocketTypeImps;

    fn connect_sock<A>(&self, addr: &A) -> io::Result<()>
    where 
        A: So9AddrIntoRaw;

    fn poll_connect_sock(&self, timeout: Duration) -> io::Result<()>;


    fn poll_sock(&self, ev: i16, timeout: Duration) -> io::Result<()>;

    fn get_socket_peer_addr<A>(&self) -> io::Result<A>
    where 
        A: TryFrom<So9SocketAddr, Error = io::Error>;

    fn get_socket_local_addr<A>(&self) -> io::Result<A>
    where 
        A: TryFrom<So9SocketAddr, Error = io::Error>;

    fn get_socket_addr_type(&self) -> io::Result<So9SockDomain>;

    fn get_socket_proto(&self) -> io::Result<Option<<SoProtocol as SockOptMarker>::DataType>>;

    fn send_with_flags(&self, data: &[u8], flags: So9MsgFlags) -> Result<usize, io::Error>;

    #[cfg(unix)]
    fn send_vect_with_flags<A>(&self, slices: &[IoSlice], dest: Option<&A>, snd_flags: i32) -> io::Result<usize>
    where A: So9AddrIntoRaw;

    #[cfg(unix)]
    fn send_fd_with_flags(&self, fd_to_send: OwnedFd, snd_flags: i32) -> io::Result<usize>;

    #[cfg(unix)]
    fn send_cred_with_flags(&self, creds: Credentials, snd_flags: i32) -> io::Result<usize>;

    #[cfg(unix)]
    fn recv_fd(&self, rcv_flags: i32) -> io::Result<(OwnedFd, So9MsgFlags)>;

    #[cfg(unix)]
    fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize>;

    fn recv_with_flags(&self, buf: &mut [u8], flags: i32) -> io::Result<usize>;

    fn recv_from<A>(&self, buf: &mut [u8], rcv_flags: i32) -> io::Result<(usize, A)>
    where 
        A: TryFrom<So9SocketAddr, Error = io::Error>;

    /// Peeks a sender address
    fn recv_addr_from<A>(&self) -> io::Result<A>
    where 
        A: TryFrom<So9SocketAddr, Error = io::Error>;

   
    fn recv_vectored_flags(&self, bufs: &mut [IoSliceMut<'_>], flags: i32) -> io::Result<(usize, So9MsgFlags)>;

    fn recv_vect_from_flags<A>(&self, buffers: &mut[IoSliceMut], recv_from: bool, snd_flags: i32) -> io::Result<(usize, So9MsgFlags, Option<A>)>
    where 
        A: TryFrom<So9SocketAddr, Error = io::Error>;

    fn send_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize>;

    #[cfg(windows)]
    fn send_vectored_flags(&self, bufs: &[IoSlice<'_>], snd_flags: i32) -> io::Result<usize>;

    fn send_to_sock<A>(&self, addr: &A, buf: &[u8], flags: i32) -> io::Result<usize>
    where 
        A: So9AddrIntoRaw;

    fn send_to_sock_vect<A>(&self, addr: &A, bufs: &[IoSlice<'_>], flags: i32) -> io::Result<usize>
    where 
        A: So9AddrIntoRaw;

    fn try_clone_sock(&self, dwflags: So9SockDwFlags) -> io::Result<Self> 
    where 
        Self: Sized;

    #[cfg(unix)]
    fn is_nonblocking_sock(&self) -> io::Result<bool>;

    fn set_nonblocking_sock(&self, nonblk: bool) -> io::Result<()>;

    #[cfg(unix)]
    fn is_cloexec_sock(&self) -> io::Result<bool>;

    fn set_cloexec_sock(&self, cloexec: bool) -> io::Result<()>;

    #[cfg(any(
        target_os = "ios",
        target_os = "visionos",
        target_os = "macos",
        target_os = "tvos",
        target_os = "watchos",
    ))]
    fn set_nosigpipe_sock(&self, nosigpipe: bool) -> io::Result<()>;

    #[cfg(any(
        target_os = "ios",
        target_os = "macos",
        target_os = "tvos",
        target_os = "watchos",
        target_os = "visionos",
    ))]
    fn set_nosigpipe_sock<FD: AsRawFd>(fd: &FD, nosigpipe: bool) -> io::Result<()>;

    fn shutdown_sock(&self, how: Shutdown) -> io::Result<()>;

    #[cfg(unix)]
    fn pair(so_dom: So9SockDomain, so_type: So9SockType, so_proto: Option<So9SockProtocol>, dwflags: So9SockDwFlags) -> io::Result<(Self, Self)>
    where Self: Sized;

}