Skip to main content

dope_sys/io/socket/msg/
source.rs

1use std::{net, os::fd::AsFd as _};
2
3use crate::{
4    Errno,
5    io::socket::{self, Domain, Kind, addr, gso},
6};
7
8/// A concrete source IP and scope with the endpoint's requested port.
9#[derive(Clone, Copy, Debug)]
10pub struct Address(net::SocketAddr);
11
12impl Address {
13    pub const fn get(self) -> net::SocketAddr {
14        self.0
15    }
16}
17
18/// The exact cold source-selection failure.
19#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20pub enum Error {
21    Family {
22        bind: net::SocketAddr,
23        peer: net::SocketAddr,
24    },
25    Open(socket::Create),
26    Bind(Errno),
27    Connect(Errno),
28    Local(addr::Local<Errno>),
29    Address(addr::Error),
30    Unspecified(net::SocketAddr),
31}
32
33/// Selects the native ancillary encoding for a fixed datagram source.
34pub trait Source: socket::Prepare + addr::Source + gso::Source + Sized {
35    type Control: Copy + AsRef<[u8]> + From<Address>;
36    /// Owns the selected source and exactly the optional segmentation capability supplied.
37    type Combined: AsRef<[u8]>;
38
39    fn compose(source: Self::Control, gso: Option<<Self as gso::Source>::Mode>) -> Self::Combined;
40    fn release(control: Self::Combined) -> (Self::Control, Option<<Self as gso::Source>::Mode>);
41
42    /// Preserves an explicit source; resolves wildcard binds once on a separate socket.
43    fn source(
44        bind: net::SocketAddr,
45        peer: net::SocketAddr,
46    ) -> Result<(Address, Self::Control), Error> {
47        if bind.is_ipv4() != peer.is_ipv4() {
48            return Err(Error::Family { bind, peer });
49        }
50        let local = if bind.ip().to_canonical().is_unspecified() {
51            let file = Self::open(Domain::for_addr(&peer), Kind::Dgram).map_err(Error::Open)?;
52            let mut local = bind;
53            local.set_port(0);
54            addr::Internet::from_std::<Self>(&local)
55                .as_addr()
56                .try_bind::<Self, Errno>(file.as_fd())
57                .map_err(Error::Bind)?;
58            addr::Internet::from_std::<Self>(&peer)
59                .as_addr()
60                .try_connect::<Self, Errno>(file.as_fd())
61                .map_err(Error::Connect)?;
62            let mut local =
63                addr::Addr::local::<Self, Errno>(file.as_fd(), &mut addr::Storage::empty())
64                    .map_err(Error::Local)?
65                    .into_std()
66                    .map_err(Error::Address)?;
67            if local.ip().to_canonical().is_unspecified() {
68                return Err(Error::Unspecified(local));
69            }
70            local.set_port(bind.port());
71            Address(local)
72        } else {
73            Address(bind)
74        };
75        Ok((local, Self::Control::from(local)))
76    }
77}