Create contexts
This commit is contained in:
parent
6afafafe60
commit
58eb7ab5c4
|
|
@ -0,0 +1,14 @@
|
||||||
|
use std::libc::{c_int, c_void};
|
||||||
|
|
||||||
|
pub type SSL_CTX = c_void;
|
||||||
|
pub type SSL_METHOD = c_void;
|
||||||
|
|
||||||
|
#[link_args = "-lssl"]
|
||||||
|
extern "C" {
|
||||||
|
fn SSL_library_init() -> c_int;
|
||||||
|
fn SSL_load_error_strings();
|
||||||
|
|
||||||
|
fn SSL_CTX_new(method: *SSL_METHOD) -> *SSL_CTX;
|
||||||
|
fn SSLv23_method() -> *SSL_METHOD;
|
||||||
|
fn SSL_CTX_free(ctx: *SSL_CTX);
|
||||||
|
}
|
||||||
|
|
@ -1,18 +1,57 @@
|
||||||
|
use std::unstable::atomics::{AtomicBool, INIT_ATOMIC_BOOL, Acquire, Release};
|
||||||
|
use std::task;
|
||||||
|
|
||||||
mod ffi {
|
mod ffi;
|
||||||
use std::libc::{c_int};
|
|
||||||
|
|
||||||
#[link_args = "-lssl"]
|
static mut STARTED_INIT: AtomicBool = INIT_ATOMIC_BOOL;
|
||||||
extern "C" {
|
static mut FINISHED_INIT: AtomicBool = INIT_ATOMIC_BOOL;
|
||||||
fn SSL_library_init() -> c_int;
|
|
||||||
fn SSL_load_error_strings();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[fixed_stack_segment]
|
#[fixed_stack_segment]
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
if STARTED_INIT.swap(true, Acquire) {
|
||||||
|
while !FINISHED_INIT.load(Release) {
|
||||||
|
task::deschedule();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ffi::SSL_library_init();
|
ffi::SSL_library_init();
|
||||||
ffi::SSL_load_error_strings();
|
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()) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
extern mod ssl;
|
extern mod ssl;
|
||||||
|
|
||||||
|
use ssl::{Sslv23, SslCtx};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_init_works() {
|
fn test_new_ctx() {
|
||||||
ssl::init();
|
SslCtx::new(Sslv23);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue