diff options
| author | Steven Fackler <[email protected]> | 2014-11-19 15:48:42 -0500 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2014-11-19 15:48:42 -0500 |
| commit | 3e98880fe8cdeaccd3c08e423bc6ce7a211bae0a (patch) | |
| tree | 383115531b6aa37517495f539ff599ccc3a636e8 | |
| parent | Fix test build (diff) | |
| parent | Baseline server support (diff) | |
| download | rust-openssl-3e98880fe8cdeaccd3c08e423bc6ce7a211bae0a.tar.xz rust-openssl-3e98880fe8cdeaccd3c08e423bc6ce7a211bae0a.zip | |
Merge pull request #54 from jmesmon/server
Server Support
| -rw-r--r-- | openssl-sys/src/lib.rs | 1 | ||||
| -rw-r--r-- | src/ssl/mod.rs | 37 |
2 files changed, 25 insertions, 13 deletions
diff --git a/openssl-sys/src/lib.rs b/openssl-sys/src/lib.rs index 377ae8e5..b4f90fe7 100644 --- a/openssl-sys/src/lib.rs +++ b/openssl-sys/src/lib.rs @@ -403,6 +403,7 @@ extern "C" { pub fn SSL_set_bio(ssl: *mut SSL, rbio: *mut BIO, wbio: *mut BIO); pub fn SSL_get_rbio(ssl: *mut SSL) -> *mut BIO; pub fn SSL_get_wbio(ssl: *mut SSL) -> *mut BIO; + pub fn SSL_accept(ssl: *mut SSL) -> c_int; pub fn SSL_connect(ssl: *mut SSL) -> c_int; pub fn SSL_ctrl(ssl: *mut SSL, cmd: c_int, larg: c_long, parg: *mut c_void) -> c_long; diff --git a/src/ssl/mod.rs b/src/ssl/mod.rs index a3eb5c14..1f0599b4 100644 --- a/src/ssl/mod.rs +++ b/src/ssl/mod.rs @@ -326,6 +326,10 @@ impl Ssl { unsafe { ffi::SSL_connect(self.ssl) } } + fn accept(&self) -> c_int { + unsafe { ffi::SSL_accept(self.ssl) } + } + fn read(&self, buf: &mut [u8]) -> c_int { unsafe { ffi::SSL_read(self.ssl, buf.as_ptr() as *mut c_void, buf.len() as c_int) } @@ -390,31 +394,38 @@ pub struct SslStream<S> { } impl<S: Stream> SslStream<S> { - /// Attempts to create a new SSL stream from a given `Ssl` instance. - pub fn new_from(ssl: Ssl, stream: S) -> Result<SslStream<S>, SslError> { - let mut ssl = SslStream { + fn new_base(ssl:Ssl, stream: S) -> SslStream<S> { + SslStream { stream: stream, ssl: ssl, // Maximum TLS record size is 16k buf: Vec::from_elem(16 * 1024, 0u8) - }; - - match ssl.in_retry_wrapper(|ssl| { ssl.connect() }) { - Ok(_) => Ok(ssl), - Err(err) => Err(err) } } + pub fn new_server_from(ssl: Ssl, stream: S) -> Result<SslStream<S>, SslError> { + let mut ssl = SslStream::new_base(ssl, stream); + ssl.in_retry_wrapper(|ssl| { ssl.accept() }).and(Ok(ssl)) + } + + /// Attempts to create a new SSL stream from a given `Ssl` instance. + pub fn new_from(ssl: Ssl, stream: S) -> Result<SslStream<S>, SslError> { + let mut ssl = SslStream::new_base(ssl, stream); + ssl.in_retry_wrapper(|ssl| { ssl.connect() }).and(Ok(ssl)) + } + /// Creates a new SSL stream pub fn new(ctx: &SslContext, stream: S) -> Result<SslStream<S>, SslError> { - let ssl = match Ssl::new(ctx) { - Ok(ssl) => ssl, - Err(err) => return Err(err) - }; - + let ssl = try!(Ssl::new(ctx)); SslStream::new_from(ssl, stream) } + /// Creates a new SSL server stream + pub fn new_server(ctx: &SslContext, stream: S) -> Result<SslStream<S>, SslError> { + let ssl = try!(Ssl::new(ctx)); + SslStream::new_server_from(ssl, stream) + } + fn in_retry_wrapper(&mut self, blk: |&Ssl| -> c_int) -> Result<c_int, SslError> { loop { |