Skip to main content

network/
error.rs

1//! Network errors are the shared I/O vocabulary plus a typed resolver error.
2//!
3//! The translations from smoltcp live here, one per operation, because the same
4//! smoltcp error means different things for a bind, a send and a lookup.
5
6pub use file_system::{IntoIoErrorKind, IoErrorKind, Result, ResultExt, io_error};
7pub use io_error::{GetAddrInfoError, ResolveError};
8
9use smoltcp::socket::{dns, icmp, udp};
10
11/// Result of a name resolution.
12pub type ResolveResult<T> = core::result::Result<T, ResolveError>;
13
14/// A DNS query could not be started.
15pub(crate) fn dns_start_error(error: dns::StartQueryError) -> ResolveError {
16    match error {
17        dns::StartQueryError::InvalidName => ResolveError::InvalidName,
18        dns::StartQueryError::NameTooLong => ResolveError::NameTooLong,
19        dns::StartQueryError::NoFreeSlot => ResolveError::NoQuerySlots,
20    }
21}
22
23/// A finished DNS query failed. smoltcp reports no reason, so this stays the
24/// generic failure: it is never reported as a confirmed negative answer.
25pub(crate) fn dns_result_error(error: dns::GetQueryResultError) -> ResolveError {
26    match error {
27        dns::GetQueryResultError::Pending | dns::GetQueryResultError::Failed => {
28            ResolveError::Failure
29        }
30    }
31}
32
33/// An ICMP or UDP bind failed: the socket is already open, or the endpoint is
34/// refused. smoltcp reports `Unaddressable` only for UDP port 0 and an
35/// unspecified ICMP endpoint, which are invalid arguments, not an address the
36/// host lacks.
37pub(crate) fn icmp_bind_error(error: icmp::BindError) -> IoErrorKind {
38    match error {
39        icmp::BindError::InvalidState => IoErrorKind::InvalidState,
40        icmp::BindError::Unaddressable => IoErrorKind::InvalidArgument,
41    }
42}
43
44/// See [`icmp_bind_error`].
45pub(crate) fn udp_bind_error(error: udp::BindError) -> IoErrorKind {
46    match error {
47        udp::BindError::InvalidState => IoErrorKind::InvalidState,
48        udp::BindError::Unaddressable => IoErrorKind::InvalidArgument,
49    }
50}
51
52/// An ICMP send was refused by smoltcp.
53///
54/// `bound` says whether the socket has a local binding and `destination_given`
55/// whether the caller supplied a usable destination: smoltcp reports both
56/// problems as the same `Unaddressable`.
57pub(crate) fn icmp_send_error(
58    error: icmp::SendError,
59    bound: bool,
60    destination_given: bool,
61) -> IoErrorKind {
62    match error {
63        icmp::SendError::BufferFull => IoErrorKind::WouldBlock,
64        icmp::SendError::Unaddressable => unaddressable_send(bound, destination_given),
65    }
66}
67
68/// A UDP send was refused by smoltcp. See [`icmp_send_error`].
69pub(crate) fn udp_send_error(
70    error: udp::SendError,
71    bound: bool,
72    destination_given: bool,
73) -> IoErrorKind {
74    match error {
75        udp::SendError::BufferFull => IoErrorKind::WouldBlock,
76        udp::SendError::Unaddressable => unaddressable_send(bound, destination_given),
77    }
78}
79
80fn unaddressable_send(bound: bool, destination_given: bool) -> IoErrorKind {
81    if !destination_given {
82        IoErrorKind::DestinationRequired
83    } else if !bound {
84        IoErrorKind::InvalidState
85    } else {
86        IoErrorKind::HostUnreachable
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use super::*;
93
94    #[test]
95    fn dns_failures_stay_distinct() {
96        assert_eq!(
97            dns_start_error(dns::StartQueryError::InvalidName),
98            ResolveError::InvalidName
99        );
100        assert_eq!(
101            dns_start_error(dns::StartQueryError::NameTooLong),
102            ResolveError::NameTooLong
103        );
104        assert_eq!(
105            dns_start_error(dns::StartQueryError::NoFreeSlot),
106            ResolveError::NoQuerySlots
107        );
108        // The opaque smoltcp failure is never a confirmed negative answer.
109        assert_eq!(
110            dns_result_error(dns::GetQueryResultError::Failed),
111            ResolveError::Failure
112        );
113    }
114
115    #[test]
116    fn bind_errors_split_state_and_argument() {
117        assert_eq!(
118            icmp_bind_error(icmp::BindError::InvalidState),
119            IoErrorKind::InvalidState
120        );
121        assert_eq!(
122            icmp_bind_error(icmp::BindError::Unaddressable),
123            IoErrorKind::InvalidArgument
124        );
125        assert_eq!(
126            udp_bind_error(udp::BindError::InvalidState),
127            IoErrorKind::InvalidState
128        );
129        assert_eq!(
130            udp_bind_error(udp::BindError::Unaddressable),
131            IoErrorKind::InvalidArgument
132        );
133    }
134
135    #[test]
136    fn unaddressable_send_depends_on_the_cause() {
137        let error = udp::SendError::Unaddressable;
138        assert_eq!(
139            udp_send_error(error, true, false),
140            IoErrorKind::DestinationRequired
141        );
142        assert_eq!(
143            udp_send_error(error, false, true),
144            IoErrorKind::InvalidState
145        );
146        assert_eq!(
147            udp_send_error(error, true, true),
148            IoErrorKind::HostUnreachable
149        );
150        assert_eq!(
151            icmp_send_error(icmp::SendError::BufferFull, true, true),
152            IoErrorKind::WouldBlock
153        );
154    }
155}