| 1 |
use std::ffi; |
| 2 |
use std::io; |
| 3 |
#[cfg(unix)] |
| 4 |
use std::str; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#[derive(Debug)] |
| 9 |
pub struct LookupError { |
| 10 |
kind: LookupErrorKind, |
| 11 |
err_num: i32, |
| 12 |
inner: io::Error, |
| 13 |
} |
| 14 |
|
| 15 |
impl LookupError { |
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
|
| 27 |
|
| 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 |
|
| 36 |
|
| 37 |
pub fn kind(&self) -> LookupErrorKind { |
| 38 |
self.kind |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
pub fn error_num(&self) -> i32 { |
| 45 |
self.err_num |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
#[derive(Copy, Clone, Debug)] |
| 53 |
pub enum LookupErrorKind { |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
Again, |
| 58 |
|
| 59 |
Badflags, |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
NoName, |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
NoData, |
| 71 |
|
| 72 |
Fail, |
| 73 |
|
| 74 |
Family, |
| 75 |
|
| 76 |
Socktype, |
| 77 |
|
| 78 |
Service, |
| 79 |
|
| 80 |
Memory, |
| 81 |
|
| 82 |
System, |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
Unknown, |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
IO, |
| 94 |
} |
| 95 |
|
| 96 |
impl LookupErrorKind { |
| 97 |
#[cfg(all(not(windows), not(unix)))] |
| 98 |
|
| 99 |
pub fn new(err: i32) -> Self { |
| 100 |
LookupErrorKind::IO |
| 101 |
} |
| 102 |
|
| 103 |
#[cfg(unix)] |
| 104 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 168 |
|
| 169 |
|
| 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 |
|
| 185 |
|
| 186 |
|
| 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 |
|
| 210 |
|
| 211 |
|
| 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 |
} |