From 3fe3d57976664c76212ac9b8abcf876baeb3d1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Sch=C3=B6lling?= Date: Fri, 14 Aug 2015 20:16:10 +0200 Subject: Add get_state_string() --- openssl/src/ssl/mod.rs | 27 +++++++++++++++++++++++++++ openssl/src/ssl/tests.rs | 8 ++++++++ 2 files changed, 35 insertions(+) (limited to 'openssl/src') diff --git a/openssl/src/ssl/mod.rs b/openssl/src/ssl/mod.rs index 0d7351dc..35180d3a 100644 --- a/openssl/src/ssl/mod.rs +++ b/openssl/src/ssl/mod.rs @@ -6,6 +6,7 @@ use std::fmt; use std::io; use std::io::prelude::*; use std::mem; +use std::str; use std::net; use std::path::Path; use std::ptr; @@ -690,6 +691,24 @@ impl Ssl { Ok(ssl) } + pub fn get_state_string(&self) -> &'static str { + let state = unsafe { + let ptr = ffi::SSL_state_string(self.ssl); + CStr::from_ptr(ptr) + }; + + str::from_utf8(state.to_bytes()).unwrap() + } + + pub fn get_state_string_long(&self) -> &'static str { + let state = unsafe { + let ptr = ffi::SSL_state_string_long(self.ssl); + CStr::from_ptr(ptr) + }; + + str::from_utf8(state.to_bytes()).unwrap() + } + fn get_rbio<'a>(&'a self) -> MemBioRef<'a> { unsafe { self.wrap_bio(ffi::SSL_get_rbio(self.ssl)) } } @@ -1316,6 +1335,14 @@ impl SslStream { pub fn pending(&self) -> usize { self.kind.ssl().pending() } + + pub fn get_state_string(&self) -> &'static str { + self.kind.ssl().get_state_string() + } + + pub fn get_state_string_long(&self) -> &'static str { + self.kind.ssl().get_state_string_long() + } } impl Read for SslStream { diff --git a/openssl/src/ssl/tests.rs b/openssl/src/ssl/tests.rs index 3a8ffa2b..9198a642 100644 --- a/openssl/src/ssl/tests.rs +++ b/openssl/src/ssl/tests.rs @@ -395,6 +395,14 @@ fn test_pending() { assert_eq!(pending, len); } +#[test] +fn test_state() { + let tcp = TcpStream::connect("127.0.0.1:15418").unwrap(); + let stream = SslStream::connect_generic(&SslContext::new(Sslv23).unwrap(), tcp).unwrap(); + assert_eq!(stream.get_state_string(), "SSLOK "); + assert_eq!(stream.get_state_string_long(), "SSL negotiation finished successfully"); +} + /// Tests that connecting with the client using NPN, but the server not does not /// break the existing connection behavior. #[test] -- cgit v1.2.3