diff options
| author | Steven Fackler <[email protected]> | 2013-12-08 22:29:37 -0800 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2013-12-08 22:29:37 -0800 |
| commit | e72290b056cf753ffdab6e0a07c944d469f59c8c (patch) | |
| tree | 375660bf027762ce655ce6f2acbe0c6a6f3a9f6c | |
| parent | Add pointers to enforce lifetimes (diff) | |
| download | rust-openssl-e72290b056cf753ffdab6e0a07c944d469f59c8c.tar.xz rust-openssl-e72290b056cf753ffdab6e0a07c944d469f59c8c.zip | |
Make MemBio interface safe
| -rw-r--r-- | lib.rs | 46 |
1 files changed, 30 insertions, 16 deletions
@@ -332,23 +332,22 @@ impl Ssl { Ok(ssl) } - fn get_rbio<'a>(&'a self) -> MemBio<'a> { - let bio = unsafe { ffi::SSL_get_rbio(self.ssl) }; - assert!(bio != ptr::null()); + fn get_rbio<'a>(&'a self) -> MemBioRef<'a> { + unsafe { self.wrap_bio(ffi::SSL_get_rbio(self.ssl)) } + } - MemBio { - bio: bio, - owned: false - } + fn get_wbio<'a>(&'a self) -> MemBioRef<'a> { + unsafe { self.wrap_bio(ffi::SSL_get_wbio(self.ssl)) } } - fn get_wbio<'a>(&'a self) -> MemBio<'a> { - let bio = unsafe { ffi::SSL_get_wbio(self.ssl) }; + fn wrap_bio<'a>(&'a self, bio: *ffi::BIO) -> MemBioRef<'a> { assert!(bio != ptr::null()); - - MemBio { - bio: bio, - owned: false + MemBioRef { + ssl: self, + bio: MemBio { + bio: bio, + owned: false + } } } @@ -388,12 +387,27 @@ enum LibSslError { ErrorWantAccept = ffi::SSL_ERROR_WANT_ACCEPT, } -struct MemBio<'ssl> { +struct MemBioRef<'ssl> { + ssl: &'ssl Ssl, + bio: MemBio, +} + +impl<'ssl> MemBioRef<'ssl> { + fn read(&self, buf: &mut [u8]) -> Option<uint> { + self.bio.read(buf) + } + + fn write(&self, buf: &[u8]) { + self.bio.write(buf) + } +} + +struct MemBio { bio: *ffi::BIO, owned: bool } -impl<'ssl> Drop for MemBio<'ssl> { +impl Drop for MemBio { fn drop(&mut self) { if self.owned { unsafe { @@ -403,7 +417,7 @@ impl<'ssl> Drop for MemBio<'ssl> { } } -impl<'self> MemBio<'self> { +impl MemBio { fn read(&self, buf: &mut [u8]) -> Option<uint> { let ret = unsafe { ffi::BIO_read(self.bio, vec::raw::to_ptr(buf) as *c_void, |