aboutsummaryrefslogtreecommitdiff
path: root/ssl/error.rs
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2013-12-28 16:57:54 -0700
committerSteven Fackler <[email protected]>2013-12-28 16:57:54 -0700
commit87da25240d71135400f38b6c0dbe666efd3be5a6 (patch)
tree9f428e2f21834891a041e3115ee111847f329898 /ssl/error.rs
parentRelicense under Apache V2 (diff)
downloadrust-openssl-87da25240d71135400f38b6c0dbe666efd3be5a6.tar.xz
rust-openssl-87da25240d71135400f38b6c0dbe666efd3be5a6.zip
Prepare rust-ssl to merge into rust-openssl
Diffstat (limited to 'ssl/error.rs')
-rw-r--r--ssl/error.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/ssl/error.rs b/ssl/error.rs
new file mode 100644
index 00000000..769cc768
--- /dev/null
+++ b/ssl/error.rs
@@ -0,0 +1,59 @@
+use std::libc::c_ulong;
+
+use super::ffi;
+
+/// An SSL error
+#[deriving(ToStr)]
+pub enum SslError {
+ /// The underlying stream has reported an EOF
+ StreamEof,
+ /// The SSL session has been closed by the other end
+ SslSessionClosed,
+ /// An error in the OpenSSL library
+ OpenSslErrors(~[OpensslError])
+}
+
+/// An error from the OpenSSL library
+#[deriving(ToStr)]
+pub enum OpensslError {
+ /// An unknown error
+ UnknownError {
+ /// The library reporting the error
+ library: u8,
+ /// The function reporting the error
+ function: u16,
+ /// The reason for the error
+ reason: u16
+ }
+}
+
+fn get_lib(err: c_ulong) -> u8 {
+ ((err >> 24) & 0xff) as u8
+}
+
+fn get_func(err: c_ulong) -> u16 {
+ ((err >> 12) & 0xfff) as u16
+}
+
+fn get_reason(err: c_ulong) -> u16 {
+ (err & 0xfff) as u16
+}
+
+impl SslError {
+ /// Creates a new `OpenSslErrors` with the current contents of the error
+ /// stack.
+ pub fn get() -> SslError {
+ let mut errs = ~[];
+ loop {
+ match unsafe { ffi::ERR_get_error() } {
+ 0 => break,
+ err => errs.push(UnknownError {
+ library: get_lib(err),
+ function: get_func(err),
+ reason: get_reason(err)
+ })
+ }
+ }
+ OpenSslErrors(errs)
+ }
+}