rsipstack 0.8.2

SIP Stack Rust library for building SIP applications
Documentation
//! Backend-agnostic UDP socket seam for the transport layer (WP3).
//!
//! `transport::udp::UdpConnection` stores `Arc<dyn UdpSocket>`; the tokio
//! backend implements the trait for `tokio::net::UdpSocket`. Embedded
//! backends implement it over embassy-net (wrapping the `&mut self` recv in
//! an internal mutex to provide the shared-`&self` model the connection
//! clones need).

use crate::Result;
use alloc::boxed::Box;
use core::net::SocketAddr;

/// `addr` with an IPv4-mapped IPv6 address (`::ffff:a.b.c.d`) turned into
/// the IPv4 address. A dual-stack socket (bound to `::`) reports its IPv4
/// peers in the mapped form; everything above the socket sees them as IPv4,
/// the form they have in SIP headers and in the addresses we send to.
pub fn canonical_socket_addr(addr: SocketAddr) -> SocketAddr {
    SocketAddr::new(addr.ip().to_canonical(), addr.port())
}

/// A new socket of `local`'s family, not bound yet. An IPv6 socket has
/// `IPV6_V6ONLY` set explicitly, so the `net.ipv6.bindv6only` sysctl (or
/// the OS default, on in BSD and Windows) does not decide it: one bound to
/// `::` is dual-stack and serves IPv4 too, one bound to a given IPv6
/// address serves only IPv6. To listen on IPv4 and IPv6 with separate
/// sockets on one port, bind given addresses, not `::` and `0.0.0.0`.
#[cfg(feature = "platform-tokio")]
pub fn socket_for(
    local: SocketAddr,
    ty: socket2::Type,
    protocol: socket2::Protocol,
) -> std::io::Result<socket2::Socket> {
    let domain = if local.is_ipv6() {
        socket2::Domain::IPV6
    } else {
        socket2::Domain::IPV4
    };
    let socket = socket2::Socket::new(domain, ty, Some(protocol))?;
    if local.is_ipv6() {
        socket.set_only_v6(!local.ip().is_unspecified())?;
    }
    Ok(socket)
}

/// A UDP socket shared across connection clones: all async methods take
/// `&self`, so implementors must be internally synchronised.
#[async_trait::async_trait]
pub trait UdpSocket: Send + Sync + 'static {
    /// Receives one datagram into `buf`, returning `(len, source)`.
    async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>;

    /// Sends one datagram; for UDP this always equals `buf.len()`.
    async fn send_to(&self, buf: &[u8], addr: SocketAddr) -> Result<usize>;

    fn local_addr(&self) -> Result<SocketAddr>;
}

#[cfg(feature = "platform-tokio")]
pub mod tokio_impl {
    use super::UdpSocket;
    use crate::Result;
    use core::net::SocketAddr;

    #[async_trait::async_trait]
    impl UdpSocket for tokio::net::UdpSocket {
        async fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)> {
            let (len, addr) = tokio::net::UdpSocket::recv_from(self, buf).await?;
            Ok((len, super::canonical_socket_addr(addr)))
        }
        async fn send_to(&self, buf: &[u8], addr: SocketAddr) -> Result<usize> {
            Ok(tokio::net::UdpSocket::send_to(self, buf, addr).await?)
        }
        fn local_addr(&self) -> Result<SocketAddr> {
            Ok(tokio::net::UdpSocket::local_addr(self)?)
        }
    }
}