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