Debian Codegraph @

src/err.rs

raw symbols SCIP
1 use std::ffi;
2 use std::io;
3 #[cfg(unix)]
4 use std::str;
5
6 /// Struct that stores a lookup error from `getaddrinfo`
7 /// or `getnameinfo`. Can automatically be coerced to an io::Error using `?`.
8 #[derive(Debug)]
9 pub struct LookupError {
10 kind: LookupErrorKind,
11 err_num: i32,
12 inner: io::Error,
13 }
14
15 impl LookupError {
16 /// Match a `gai` error, returning Ok() if it's
17 /// `0`. Otherwise return Err(LookupError) with
18 /// the specific error details.
19 pub fn match_gai_error(err: i32) -> Result<(), Self> {
20 match err {
21 0 => Ok(()),
22 _ => Err(LookupError::new(err)),
23 }
24 }
25
26 /// Create a new LookupError from a `gai` error,
27 /// returned by `getaddrinfo` and `getnameinfo`.
28 pub fn new(err: i32) -> Self {
29 LookupError {
30 kind: LookupErrorKind::new(err),
31 err_num: err,
32 inner: gai_err_to_io_err(err),
33 }
34 }
35 /// Get the error kind explicitly. If this is an
36 /// io::Error, use From/Into to convert it.
37 pub fn kind(&self) -> LookupErrorKind {
38 self.kind
39 }
40
41 /// Get the actual error number. This can be used
42 /// to find non-standard return codes from some
43 /// implementations (be careful of portability here).
44 pub fn error_num(&self) -> i32 {
45 self.err_num
46 }
47 }
48
49 /// Different kinds of lookup errors that `getaddrinfo` and
50 /// `getnameinfo` can return. These can be a little inconsitant
51 /// between platforms, so it's recommended not to rely on them.
52 #[derive(Copy, Clone, Debug)]
53 pub enum LookupErrorKind {
54 /// Temporary failure in name resolution.
55 ///
56 /// May also be returend when DNS server returns a SERVFAIL.
57 Again,
58 /// Invalid value for `ai_flags' field.
59 Badflags,
60 /// NAME or SERVICE is unknown.
61 ///
62 /// May also be returned when domain doesn't exist (NXDOMAIN) or domain
63 /// exists but contains no address records (NODATA).
64 NoName,
65 /// The specified network host exists, but has no data defined.
66 ///
67 /// This is no longer a POSIX standard, however it's still returned by
68 /// some platforms. Be warned that FreeBSD does not include the corresponding
69 /// `EAI_NODATA` symbol.
70 NoData,
71 /// Non-recoverable failure in name resolution.
72 Fail,
73 /// `ai_family' not supported.
74 Family,
75 /// `ai_socktype' not supported.
76 Socktype,
77 /// SERVICE not supported for `ai_socktype'.
78 Service,
79 /// Memory allocation failure.
80 Memory,
81 /// System error returned in `errno'.
82 System,
83 /// An unknown result code was returned.
84 ///
85 /// For some platforms, you may wish to match on an unknown value directly.
86 /// Note that `gai_strerr` is used to get error messages, so the generated IO
87 /// error should contain the correct error message for the platform.
88 Unknown,
89 /// A generic C error or IO error occured.
90 ///
91 /// You should convert this `LookupError` into an IO error directly. Note
92 /// that the error code is set to 0 in the case this is returned.
93 IO,
94 }
95
96 impl LookupErrorKind {
97 #[cfg(all(not(windows), not(unix)))]
98 /// Create a `LookupErrorKind` from a `gai` error.
99 pub fn new(err: i32) -> Self {
100 LookupErrorKind::IO
101 }
102
103 #[cfg(unix)]
104 /// Create a `LookupErrorKind` from a `gai` error.
105 pub fn new(err: i32) -> Self {
106 use libc as c;
107 match err {
108 c::EAI_AGAIN => LookupErrorKind::Again,
109 c::EAI_BADFLAGS => LookupErrorKind::Badflags,
110 c::EAI_FAIL => LookupErrorKind::Fail,
111 c::EAI_FAMILY => LookupErrorKind::Family,
112 c::EAI_MEMORY => LookupErrorKind::Memory,
113 c::EAI_NONAME => LookupErrorKind::NoName,
114 // FreeBSD has no EAI_NODATA, so don't match it on that platform.
115 #[cfg(not(any(target_os="freebsd", target_os="emscripten")))]
116 c::EAI_NODATA => LookupErrorKind::NoData,
117 c::EAI_SERVICE => LookupErrorKind::Service,
118 c::EAI_SOCKTYPE => LookupErrorKind::Socktype,
119 c::EAI_SYSTEM => LookupErrorKind::System,
120 _ => LookupErrorKind::IO,
121 }
122 }
123
124 #[cfg(windows)]
125 /// Create a `LookupErrorKind` from a `gai` error.
126 pub fn new(err: i32) -> Self {
127 use winapi::shared::winerror as e;
128 match err as u32 {
129 e::WSATRY_AGAIN => LookupErrorKind::Again,
130 e::WSAEINVAL => LookupErrorKind::Badflags,
131 e::WSANO_RECOVERY => LookupErrorKind::Fail,
132 e::WSAEAFNOSUPPORT => LookupErrorKind::Family,
133 e::ERROR_NOT_ENOUGH_MEMORY => LookupErrorKind::Memory,
134 e::WSAHOST_NOT_FOUND => LookupErrorKind::NoName,
135 e::WSANO_DATA => LookupErrorKind::NoData,
136 e::WSATYPE_NOT_FOUND => LookupErrorKind::Service,
137 e::WSAESOCKTNOSUPPORT => LookupErrorKind::Socktype,
138 _ => LookupErrorKind::IO,
139 }
140 }
141 }
142
143 impl From<LookupError> for io::Error {
144 fn from(err: LookupError) -> io::Error {
145 err.inner
146 }
147 }
148
149 impl From<io::Error> for LookupError {
150 fn from(err: io::Error) -> LookupError {
151 LookupError {
152 kind: LookupErrorKind::IO,
153 err_num: 0,
154 inner: err,
155 }
156 }
157 }
158
159 impl From<ffi::NulError> for LookupError {
160 fn from(err: ffi::NulError) -> LookupError {
161 let err: io::Error = err.into();
162 err.into()
163 }
164 }
165
166 #[cfg(all(not(windows), not(unix)))]
167 /// Given a gai error, return an `std::io::Error` with
168 /// the appropriate error message. Note `0` is not an
169 /// error, but will still map to an error
170 pub(crate) fn gai_err_to_io_err(err: i32) -> io::Error {
171 match (err) {
172 0 => io::Error::new(
173 io::ErrorKind::Other,
174 "address information lookup success"
175 ),
176 _ => io::Error::new(
177 io::ErrorKind::Other,
178 "failed to lookup address information"
179 ),
180 }
181 }
182
183 #[cfg(unix)]
184 /// Given a gai error, return an `std::io::Error` with
185 /// the appropriate error message. Note `0` is not an
186 /// error, but will still map to an error
187 pub(crate) fn gai_err_to_io_err(err: i32) -> io::Error {
188 use libc::{EAI_SYSTEM, gai_strerror};
189
190 match err {
191 0 => return io::Error::new(
192 io::ErrorKind::Other,
193 "address information lookup success"
194 ),
195 EAI_SYSTEM => return io::Error::last_os_error(),
196 _ => {},
197 }
198
199 let detail = unsafe {
200 str::from_utf8(ffi::CStr::from_ptr(gai_strerror(err)).to_bytes()).unwrap()
201 .to_owned()
202 };
203 io::Error::new(io::ErrorKind::Other,
204 &format!("failed to lookup address information: {}", detail)[..]
205 )
206 }
207
208 #[cfg(windows)]
209 /// Given a gai error, return an `std::io::Error` with
210 /// the appropriate error message. Note `0` is not an
211 /// error, but will still map to an error
212 pub(crate) fn gai_err_to_io_err(err: i32) -> io::Error {
213 use winapi::um::winsock2::WSAGetLastError;
214 match err {
215 0 => io::Error::new(
216 io::ErrorKind::Other,
217 "address information lookup success"
218 ),
219 _ => {
220 io::Error::from_raw_os_error(
221 unsafe { WSAGetLastError() }
222 )
223 }
224 }
225 }