From e85b49d3755375b3e535cbd4b07d4fbf953948cb Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 18 Dec 2015 21:19:16 -0800 Subject: Work around the worst of clone bogusness SslStream::{clone,try_clone} are inherently broken since the Ssl object shared by both streams is only going to be talking to one stream. Stuff like hyper depends on try_clone, so we'll leave it here for now but minimize the brokenness to "no worse than what it used to be like". They'll be removed in 0.8. cc #325 --- openssl/src/ssl/bio.rs | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) (limited to 'openssl/src/ssl/bio.rs') diff --git a/openssl/src/ssl/bio.rs b/openssl/src/ssl/bio.rs index ef63d146..99d9af0a 100644 --- a/openssl/src/ssl/bio.rs +++ b/openssl/src/ssl/bio.rs @@ -6,35 +6,20 @@ use std::io::prelude::*; use std::mem; use std::slice; use std::ptr; +use std::sync::Arc; use ssl::error::SslError; // "rust" const NAME: [c_char; 5] = [114, 117, 115, 116, 0]; -// we use this after removing the stream from the BIO so that we don't have to -// worry about freeing the heap allocated BIO_METHOD after freeing the BIO. -static DESTROY_METHOD: BIO_METHOD = BIO_METHOD { - type_: BIO_TYPE_NONE, - name: &NAME[0], - bwrite: None, - bread: None, - bputs: None, - bgets: None, - ctrl: None, - create: None, - destroy: Some(destroy), - callback_ctrl: None, -}; - pub struct StreamState { pub stream: S, pub error: Option, } -pub fn new(stream: S) -> Result<(*mut BIO, Box), SslError> { - - let method = Box::new(BIO_METHOD { +pub fn new(stream: S) -> Result<(*mut BIO, Arc), SslError> { + let method = Arc::new(BIO_METHOD { type_: BIO_TYPE_NONE, name: &NAME[0], bwrite: Some(bwrite::), @@ -43,7 +28,7 @@ pub fn new(stream: S) -> Result<(*mut BIO, Box), Ss bgets: None, ctrl: Some(ctrl::), create: Some(create), - destroy: None, // covered in the replacement BIO_METHOD + destroy: Some(destroy::), callback_ctrl: None, }); @@ -66,14 +51,6 @@ pub unsafe fn take_error(bio: *mut BIO) -> Option { state.error.take() } -pub unsafe fn take_stream(bio: *mut BIO) -> S { - let state: Box> = Box::from_raw((*bio).ptr as *mut _); - (*bio).ptr = ptr::null_mut(); - (*bio).method = &DESTROY_METHOD as *const _ as *mut _; - (*bio).init = 0; - state.stream -} - pub unsafe fn get_ref<'a, S: 'a>(bio: *mut BIO) -> &'a S { let state: &'a StreamState = mem::transmute((*bio).ptr); &state.stream @@ -159,11 +136,14 @@ unsafe extern "C" fn create(bio: *mut BIO) -> c_int { 1 } -unsafe extern "C" fn destroy(bio: *mut BIO) -> c_int { +unsafe extern "C" fn destroy(bio: *mut BIO) -> c_int { if bio.is_null() { return 0; } - assert!((*bio).ptr.is_null()); + assert!(!(*bio).ptr.is_null()); + Box::>::from_raw((*bio).ptr as *mut _); + (*bio).ptr = ptr::null_mut(); + (*bio).init = 0; 1 } -- cgit v1.2.3 From 11129aa5214b7ed70027368d52715c7d4e2247c2 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Fri, 18 Dec 2015 22:34:30 -0800 Subject: Rustfmt --- openssl/src/ssl/bio.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'openssl/src/ssl/bio.rs') diff --git a/openssl/src/ssl/bio.rs b/openssl/src/ssl/bio.rs index 99d9af0a..a361ae81 100644 --- a/openssl/src/ssl/bio.rs +++ b/openssl/src/ssl/bio.rs @@ -101,7 +101,7 @@ unsafe extern "C" fn bread(bio: *mut BIO, buf: *mut c_char, len: c_int) fn retriable_error(err: &io::Error) -> bool { match err.kind() { io::ErrorKind::WouldBlock | io::ErrorKind::NotConnected => true, - _ => false + _ => false, } } -- cgit v1.2.3