From b8b0369cc224b51796dedd0d93b41a72e01ac08c Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 13 Jul 2023 09:40:57 +0300 Subject: [PATCH 1/2] Remove TCPSocket hard-coding in SSLStream --- src/ssl.jl | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/ssl.jl b/src/ssl.jl index 3f94309..952aa49 100644 --- a/src/ssl.jl +++ b/src/ssl.jl @@ -35,13 +35,22 @@ bio_clear_flags(bio::BIO) = bio_set_flags(bio, 0x00) function on_bio_stream_read(bio::BIO, out::Ptr{Cchar}, outlen::Cint) try bio_clear_flags(bio) - io = bio_get_data(bio)::TCPSocket - n = bytesavailable(io) - if n == 0 - bio_set_read_retry(bio) - return Cint(0) + io = bio_get_data(bio) + if io isa TCPSocket + n = bytesavailable(io) + if n == 0 + bio_set_read_retry(bio) + return Cint(0) + end + unsafe_read(io, out, min(UInt(n), outlen)) + else + n = bytesavailable(io) + if n == 0 + bio_set_read_retry(bio) + return Cint(0) + end + unsafe_read(io, out, min(UInt(n), outlen)) end - unsafe_read(io, out, min(UInt(n), outlen)) return Cint(min(n, outlen)) catch e # we don't want to throw a Julia exception from a C callback @@ -51,8 +60,18 @@ end function on_bio_stream_write(bio::BIO, in::Ptr{Cchar}, inlen::Cint)::Cint try +<<<<<<< Updated upstream io = bio_get_data(bio)::TCPSocket written = unsafe_write(io, in, inlen) +======= + bio_clear_flags(bio) + io = bio_get_data(bio) + if io isa TCPSocket + written = unsafe_write(io, in, inlen) + else + written = unsafe_write(io, in, inlen) + end +>>>>>>> Stashed changes return Cint(written) catch e # we don't want to throw a Julia exception from a C callback @@ -373,12 +392,12 @@ end """ SSLStream. """ -mutable struct SSLStream <: IO +mutable struct SSLStream{T} <: IO ssl::SSL ssl_context::SSLContext rbio::BIO wbio::BIO - io::TCPSocket + io::T # used in `eof` where we want the call to `eof` on the underlying # socket and the SSL_peek call that processes bytes to be seen # as one "operation" @@ -395,7 +414,7 @@ mutable struct SSLStream <: IO peekbytes::Base.RefValue{Csize_t} closed::Bool - function SSLStream(ssl_context::SSLContext, io::TCPSocket) + function SSLStream(ssl_context::SSLContext, io::T) where {T <: IO} # Create a read and write BIOs. bio_read::BIO = BIO(io; finalize=false) bio_write::BIO = BIO(io; finalize=false) @@ -404,7 +423,7 @@ mutable struct SSLStream <: IO end end -SSLStream(tcp::TCPSocket) = SSLStream(SSLContext(OpenSSL.TLSClientMethod()), tcp) +SSLStream(tcp::IO) = SSLStream(SSLContext(OpenSSL.TLSClientMethod()), tcp) # backwards compat Base.getproperty(ssl::SSLStream, nm::Symbol) = nm === :bio_read_stream ? ssl : getfield(ssl, nm) From 6aa00bd2c391961c78367eb82f01a8b0ef53f6b0 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Thu, 13 Jul 2023 09:42:09 +0300 Subject: [PATCH 2/2] Fix merge --- src/ssl.jl | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/ssl.jl b/src/ssl.jl index 952aa49..a0a9a0f 100644 --- a/src/ssl.jl +++ b/src/ssl.jl @@ -60,10 +60,6 @@ end function on_bio_stream_write(bio::BIO, in::Ptr{Cchar}, inlen::Cint)::Cint try -<<<<<<< Updated upstream - io = bio_get_data(bio)::TCPSocket - written = unsafe_write(io, in, inlen) -======= bio_clear_flags(bio) io = bio_get_data(bio) if io isa TCPSocket @@ -71,7 +67,6 @@ function on_bio_stream_write(bio::BIO, in::Ptr{Cchar}, inlen::Cint)::Cint else written = unsafe_write(io, in, inlen) end ->>>>>>> Stashed changes return Cint(written) catch e # we don't want to throw a Julia exception from a C callback @@ -419,7 +414,7 @@ mutable struct SSLStream{T} <: IO bio_read::BIO = BIO(io; finalize=false) bio_write::BIO = BIO(io; finalize=false) ssl = SSL(ssl_context, bio_read, bio_write) - return new(ssl, ssl_context, bio_read, bio_write, io, ReentrantLock(), ReentrantLock(), Ref{Csize_t}(0), Ref{Csize_t}(0), Ref{UInt8}(0x00), Ref{Csize_t}(0), false) + return new{T}(ssl, ssl_context, bio_read, bio_write, io, ReentrantLock(), ReentrantLock(), Ref{Csize_t}(0), Ref{Csize_t}(0), Ref{UInt8}(0x00), Ref{Csize_t}(0), false) end end