1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use crate::conversion::{to_nb, SocketAddr};
use crate::SocketState;
use embedded_nal::nb;
use embedded_nal::{UdpClientStack, UdpFullStack};
use std::io::{self, Error};
use std::net::{self, IpAddr, Ipv4Addr, Ipv6Addr};

pub struct UdpSocket {
    state: SocketState<net::UdpSocket, net::UdpSocket>,
}

impl UdpSocket {
    fn new() -> Self {
        Self {
            state: SocketState::new(),
        }
    }

    /// Return the raw file descriptor underlying the current socket.
    ///
    /// This is primarily intended for use with `select` style mechanisms: Any of the `nb` methods
    /// of the socket's traits, once returning [`nb::Error::WouldBlock`], will only make progress
    /// if data or buffer is available on that file descriptor.
    ///
    /// If this returns `None`, then the socket is still in a state where it doesn't even have an
    /// underlying operating system socket, and needs further operations ([UdpFullStack::bind] or
    /// [UdpClientStack::connect]) to be performed before it can be waited on. (Then again, a
    /// socket that doesn't return a raw file descriptor should never return `WouldBlock`). Being
    /// fallible, this is a method and not a trait implemntation of [std::os::unix::io::AsRawFd].
    #[cfg(any(unix, target_os = "wasi"))]
    pub fn as_raw_fd(&self) -> Option<std::os::unix::io::RawFd> {
        use std::os::unix::io::AsRawFd;

        Some(self.state.get_any().ok()?.as_raw_fd())
    }
}

impl UdpClientStack for crate::Stack {
    type UdpSocket = UdpSocket;
    type Error = Error;

    fn socket(&mut self) -> Result<Self::UdpSocket, Self::Error> {
        Ok(UdpSocket::new())
    }

    fn connect(
        &mut self,
        socket: &mut Self::UdpSocket,
        remote: embedded_nal::SocketAddr,
    ) -> std::io::Result<()> {
        let any = match remote {
            embedded_nal::SocketAddr::V4(_) => {
                net::SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0)
            }
            embedded_nal::SocketAddr::V6(_) => {
                net::SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0)
            }
        };

        let sock = net::UdpSocket::bind(any)?;

        sock.set_nonblocking(true)?;

        sock.connect(SocketAddr::from(remote))?;
        socket.state = SocketState::Connected(sock);
        Ok(())
    }

    fn send(&mut self, socket: &mut Self::UdpSocket, buffer: &[u8]) -> nb::Result<(), Self::Error> {
        let sock = socket.state.get_running()?;
        sock.send(buffer).map(drop).map_err(to_nb)
    }

    fn receive(
        &mut self,
        socket: &mut Self::UdpSocket,
        buffer: &mut [u8],
    ) -> nb::Result<(usize, embedded_nal::SocketAddr), Self::Error> {
        let sock = socket.state.get_any_mut()?;
        sock.recv_from(buffer)
            .map(|(length, peer_addr)| (length, SocketAddr::from(peer_addr).into()))
            .map_err(to_nb)
    }

    fn close(&mut self, _: Self::UdpSocket) -> io::Result<()> {
        // No-op: Socket gets closed when it is freed
        //
        // Could wrap it in an Option, but really that'll only make things messier; users will
        // probably drop the socket anyway after closing, and can't expect it to be usable with
        // this API.
        Ok(())
    }
}

impl UdpFullStack for crate::Stack {
    fn bind(&mut self, socket: &mut UdpSocket, port: u16) -> Result<(), Error> {
        let anyaddressthisport = net::SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), port);

        let sock = net::UdpSocket::bind(anyaddressthisport)?;

        sock.set_nonblocking(true)?;

        socket.state = SocketState::Bound(sock);
        Ok(())
    }
    fn send_to(
        &mut self,
        socket: &mut UdpSocket,
        remote: embedded_nal::SocketAddr,
        buffer: &[u8],
    ) -> Result<(), nb::Error<Error>> {
        let sock = socket.state.get_bound()?;
        sock.send_to(buffer, SocketAddr::from(remote))
            .map(drop)
            .map_err(to_nb)
    }
}