diff options
| author | Steven Fackler <[email protected]> | 2013-10-05 10:56:36 -0700 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2013-10-05 10:56:36 -0700 |
| commit | 58eb7ab5c4e29922e6cee4c21ca5420e3504a654 (patch) | |
| tree | a278e977833589e4a934f749e2d57f92155c2bd5 /src/ssl/lib.rs | |
| parent | Basic library initialization (diff) | |
| download | rust-openssl-58eb7ab5c4e29922e6cee4c21ca5420e3504a654.tar.xz rust-openssl-58eb7ab5c4e29922e6cee4c21ca5420e3504a654.zip | |
Create contexts
Diffstat (limited to 'src/ssl/lib.rs')
| -rw-r--r-- | src/ssl/lib.rs | 55 |
1 files changed, 47 insertions, 8 deletions
diff --git a/src/ssl/lib.rs b/src/ssl/lib.rs index d52a0cfa..a7c5588f 100644 --- a/src/ssl/lib.rs +++ b/src/ssl/lib.rs @@ -1,18 +1,57 @@ +use std::unstable::atomics::{AtomicBool, INIT_ATOMIC_BOOL, Acquire, Release}; +use std::task; -mod ffi { - use std::libc::{c_int}; +mod ffi; - #[link_args = "-lssl"] - extern "C" { - fn SSL_library_init() -> c_int; - fn SSL_load_error_strings(); - } -} +static mut STARTED_INIT: AtomicBool = INIT_ATOMIC_BOOL; +static mut FINISHED_INIT: AtomicBool = INIT_ATOMIC_BOOL; #[fixed_stack_segment] pub fn init() { unsafe { + if STARTED_INIT.swap(true, Acquire) { + while !FINISHED_INIT.load(Release) { + task::deschedule(); + } + return; + } + ffi::SSL_library_init(); ffi::SSL_load_error_strings(); + FINISHED_INIT.store(true, Release); + } +} + +pub enum SslMethod { + Sslv23 +} + +impl SslMethod { + #[fixed_stack_segment] + unsafe fn to_raw(&self) -> *ffi::SSL_METHOD { + match *self { + Sslv23 => ffi::SSLv23_method() + } + } +} + +pub struct SslCtx { + priv ctx: *ffi::SSL_CTX +} + +impl Drop for SslCtx { + #[fixed_stack_segment] + fn drop(&mut self) { + unsafe { ffi::SSL_CTX_free(self.ctx); } + } +} + +impl SslCtx { + #[fixed_stack_segment] + pub fn new(method: SslMethod) -> SslCtx { + init(); + SslCtx { + ctx: unsafe { ffi::SSL_CTX_new(method.to_raw()) } + } } } |