Ssl errors may return a stack

This commit is contained in:
Steven Fackler 2013-10-23 21:28:08 -07:00
parent eea07ef137
commit 7ea442be94
2 changed files with 23 additions and 14 deletions

View File

@ -6,6 +6,11 @@ use super::ffi;
pub enum SslError { pub enum SslError {
StreamEof, StreamEof,
SslSessionClosed, SslSessionClosed,
OpenSslErrors(~[OpensslError])
}
#[deriving(ToStr)]
pub enum OpensslError {
UnknownError { UnknownError {
library: u8, library: u8,
function: u16, function: u16,
@ -26,14 +31,18 @@ fn get_reason(err: c_ulong) -> u16 {
} }
impl SslError { impl SslError {
pub fn get() -> Option<SslError> { pub fn get() -> SslError {
match unsafe { ffi::ERR_get_error() } { let mut errs = ~[];
0 => None, loop {
err => Some(UnknownError { match unsafe { ffi::ERR_get_error() } {
library: get_lib(err), 0 => break,
function: get_func(err), err => errs.push(UnknownError {
reason: get_reason(err) library: get_lib(err),
}) function: get_func(err),
reason: get_reason(err)
})
}
} }
OpenSslErrors(errs)
} }
} }

12
lib.rs
View File

@ -98,7 +98,7 @@ impl SslContext {
let ctx = unsafe { ffi::SSL_CTX_new(method.to_raw()) }; let ctx = unsafe { ffi::SSL_CTX_new(method.to_raw()) };
if ctx == ptr::null() { if ctx == ptr::null() {
return Err(SslError::get().unwrap()); return Err(SslError::get());
} }
Ok(SslContext { ctx: ctx }) Ok(SslContext { ctx: ctx })
@ -130,7 +130,7 @@ impl SslContext {
}; };
if ret == 0 { if ret == 0 {
Some(SslError::get().unwrap()) Some(SslError::get())
} else { } else {
None None
} }
@ -151,19 +151,19 @@ impl Ssl {
fn try_new(ctx: &SslContext) -> Result<Ssl, SslError> { fn try_new(ctx: &SslContext) -> Result<Ssl, SslError> {
let ssl = unsafe { ffi::SSL_new(ctx.ctx) }; let ssl = unsafe { ffi::SSL_new(ctx.ctx) };
if ssl == ptr::null() { if ssl == ptr::null() {
return Err(SslError::get().unwrap()); return Err(SslError::get());
} }
let ssl = Ssl { ssl: ssl }; let ssl = Ssl { ssl: ssl };
let rbio = unsafe { ffi::BIO_new(ffi::BIO_s_mem()) }; let rbio = unsafe { ffi::BIO_new(ffi::BIO_s_mem()) };
if rbio == ptr::null() { if rbio == ptr::null() {
return Err(SslError::get().unwrap()); return Err(SslError::get());
} }
let wbio = unsafe { ffi::BIO_new(ffi::BIO_s_mem()) }; let wbio = unsafe { ffi::BIO_new(ffi::BIO_s_mem()) };
if wbio == ptr::null() { if wbio == ptr::null() {
unsafe { ffi::BIO_free_all(rbio) } unsafe { ffi::BIO_free_all(rbio) }
return Err(SslError::get().unwrap()); return Err(SslError::get());
} }
unsafe { ffi::SSL_set_bio(ssl.ssl, rbio, wbio) } unsafe { ffi::SSL_set_bio(ssl.ssl, rbio, wbio) }
@ -307,7 +307,7 @@ impl<S: Stream> SslStream<S> {
} }
ErrorWantWrite => self.flush(), ErrorWantWrite => self.flush(),
ErrorZeroReturn => return Err(SslSessionClosed), ErrorZeroReturn => return Err(SslSessionClosed),
ErrorSsl => return Err(SslError::get().unwrap()), ErrorSsl => return Err(SslError::get()),
_ => unreachable!() _ => unreachable!()
} }
} }