fix releasebuffers

Change-Id: I2aaeb8c5a411ef089b3a52a2ec7f0c8353d7dfd3
This commit is contained in:
JT Olds 2014-04-30 14:47:50 -06:00
parent 1b3b1e773b
commit b364999a65
3 changed files with 30 additions and 7 deletions

22
bio.go
View File

@ -90,9 +90,10 @@ func cbioFree(b *C.BIO) C.int {
}
type writeBio struct {
data_mtx sync.Mutex
op_mtx sync.Mutex
buf []byte
data_mtx sync.Mutex
op_mtx sync.Mutex
buf []byte
release_buffers bool
}
func loadWritePtr(b *C.BIO) *writeBio {
@ -174,6 +175,9 @@ func (b *writeBio) WriteTo(w io.Writer) (rv int64, err error) {
// subtract however much data we wrote from the buffer
b.data_mtx.Lock()
b.buf = b.buf[:copy(b.buf, b.buf[n:])]
if b.release_buffers && len(b.buf) == 0 {
b.buf = nil
}
b.data_mtx.Unlock()
return int64(n), err
@ -192,10 +196,11 @@ func (b *writeBio) MakeCBIO() *C.BIO {
}
type readBio struct {
data_mtx sync.Mutex
op_mtx sync.Mutex
buf []byte
eof bool
data_mtx sync.Mutex
op_mtx sync.Mutex
buf []byte
eof bool
release_buffers bool
}
func loadReadPtr(b *C.BIO) *readBio {
@ -229,6 +234,9 @@ func readBioRead(b *C.BIO, data *C.char, size C.int) (rc C.int) {
}
n := copy(nonCopyCString(data, size), ptr.buf)
ptr.buf = ptr.buf[:copy(ptr.buf, ptr.buf[n:])]
if ptr.release_buffers && len(ptr.buf) == 0 {
ptr.buf = nil
}
return C.int(n)
}

View File

@ -65,6 +65,11 @@ func newConn(conn net.Conn, ctx *Ctx) (*Conn, error) {
into_ssl := &readBio{}
from_ssl := &writeBio{}
if ctx.GetMode()&ReleaseBuffers > 0 {
into_ssl.release_buffers = true
from_ssl.release_buffers = true
}
into_ssl_cbio := into_ssl.MakeCBIO()
from_ssl_cbio := from_ssl.MakeCBIO()
if into_ssl_cbio == nil || from_ssl_cbio == nil {

10
ctx.go
View File

@ -17,6 +17,10 @@ static long SSL_CTX_set_mode_not_a_macro(SSL_CTX* ctx, long modes) {
return SSL_CTX_set_mode(ctx, modes);
}
static long SSL_CTX_get_mode_not_a_macro(SSL_CTX* ctx) {
return SSL_CTX_get_mode(ctx);
}
static long SSL_CTX_set_session_cache_mode_not_a_macro(SSL_CTX* ctx, long modes) {
return SSL_CTX_set_session_cache_mode(ctx, modes);
}
@ -316,6 +320,12 @@ func (c *Ctx) SetMode(modes Modes) Modes {
return Modes(C.SSL_CTX_set_mode_not_a_macro(c.ctx, C.long(modes)))
}
// GetMode returns context modes. See
// http://www.openssl.org/docs/ssl/SSL_CTX_set_mode.html
func (c *Ctx) GetMode() Modes {
return Modes(C.SSL_CTX_get_mode_not_a_macro(c.ctx))
}
type VerifyOptions int
const (