Skip to content

Commit

Permalink
Remove usage of IO internals.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed May 29, 2023
1 parent cb8f4ee commit 725d32c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
2 changes: 2 additions & 0 deletions ext/openssl/extconf.rb
Expand Up @@ -18,6 +18,8 @@

Logging::message "=== OpenSSL for Ruby configurator ===\n"

have_func("rb_io_descriptor")

##
# Adds -DOSSL_DEBUG for compilation and some more targets when GCC is used
# To turn it on, use: --with-debug or --enable-debug
Expand Down
49 changes: 31 additions & 18 deletions ext/openssl/ossl_ssl.c
Expand Up @@ -1653,6 +1653,16 @@ ossl_ssl_initialize(int argc, VALUE *argv, VALUE self)
return self;
}

#ifndef HAVE_RB_IO_DESCRIPTOR
static int
rb_io_descriptor(VALUE io)
{
rb_io_t *fptr;
GetOpenFile(io, fptr);
return fptr->fd;
}
#endif

static VALUE
ossl_ssl_setup(VALUE self)
{
Expand All @@ -1668,8 +1678,8 @@ ossl_ssl_setup(VALUE self)
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
rb_io_check_writable(fptr);
if (!SSL_set_fd(ssl, TO_SOCKET(fptr->fd)))
ossl_raise(eSSLError, "SSL_set_fd");
if (!SSL_set_fd(ssl, TO_SOCKET(rb_io_descriptor(io))))
ossl_raise(eSSLError, "SSL_set_fd");

return Qtrue;
}
Expand Down Expand Up @@ -1709,21 +1719,25 @@ no_exception_p(VALUE opts)
#endif

static void
io_wait_writable(rb_io_t *fptr)
io_wait_writable(VALUE io)
{
#ifdef HAVE_RB_IO_MAYBE_WAIT
rb_io_maybe_wait_writable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT);
rb_io_maybe_wait_writable(errno, io, RUBY_IO_TIMEOUT_DEFAULT);
#else
rb_io_t *fptr;
GetOpenFile(io, fptr);
rb_io_wait_writable(fptr->fd);
#endif
}

static void
io_wait_readable(rb_io_t *fptr)
io_wait_readable(VALUE io)
{
#ifdef HAVE_RB_IO_MAYBE_WAIT
rb_io_maybe_wait_readable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT);
rb_io_maybe_wait_readable(errno, io, RUBY_IO_TIMEOUT_DEFAULT);
#else
rb_io_t *fptr;
GetOpenFile(io, fptr);
rb_io_wait_readable(fptr->fd);
#endif
}
Expand All @@ -1744,7 +1758,7 @@ ossl_start_ssl(VALUE self, int (*func)(SSL *), const char *funcname, VALUE opts)

GetSSL(self, ssl);

GetOpenFile(rb_attr_get(self, id_i_io), fptr);
VALUE io = rb_attr_get(self, id_i_io);
for(;;){
ret = func(ssl);

Expand All @@ -1762,12 +1776,12 @@ ossl_start_ssl(VALUE self, int (*func)(SSL *), const char *funcname, VALUE opts)
case SSL_ERROR_WANT_WRITE:
if (no_exception_p(opts)) { return sym_wait_writable; }
write_would_block(nonblock);
io_wait_writable(fptr);
io_wait_writable(io);
continue;
case SSL_ERROR_WANT_READ:
if (no_exception_p(opts)) { return sym_wait_readable; }
read_would_block(nonblock);
io_wait_readable(fptr);
io_wait_readable(io);
continue;
case SSL_ERROR_SYSCALL:
#ifdef __APPLE__
Expand Down Expand Up @@ -1907,7 +1921,7 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
int ilen;
VALUE len, str;
rb_io_t *fptr;
VALUE io, opts = Qnil;
VALUE opts = Qnil;

if (nonblock) {
rb_scan_args(argc, argv, "11:", &len, &str, &opts);
Expand All @@ -1932,8 +1946,7 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
if (ilen == 0)
return str;

io = rb_attr_get(self, id_i_io);
GetOpenFile(io, fptr);
VALUE io = rb_attr_get(self, id_i_io);

rb_str_locktmp(str);
for (;;) {
Expand All @@ -1953,15 +1966,15 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
if (no_exception_p(opts)) { return sym_wait_writable; }
write_would_block(nonblock);
}
io_wait_writable(fptr);
io_wait_writable(io);
continue;
case SSL_ERROR_WANT_READ:
if (nonblock) {
rb_str_unlocktmp(str);
if (no_exception_p(opts)) { return sym_wait_readable; }
read_would_block(nonblock);
}
io_wait_readable(fptr);
io_wait_readable(io);
continue;
case SSL_ERROR_SYSCALL:
if (!ERR_peek_error()) {
Expand Down Expand Up @@ -2027,14 +2040,14 @@ ossl_ssl_write_internal(VALUE self, VALUE str, VALUE opts)
SSL *ssl;
rb_io_t *fptr;
int num, nonblock = opts != Qfalse;
VALUE tmp, io;
VALUE tmp;

GetSSL(self, ssl);
if (!ssl_started(ssl))
rb_raise(eSSLError, "SSL session is not started yet");

tmp = rb_str_new_frozen(StringValue(str));
io = rb_attr_get(self, id_i_io);
VALUE io = rb_attr_get(self, id_i_io);
GetOpenFile(io, fptr);

/* SSL_write(3ssl) manpage states num == 0 is undefined */
Expand All @@ -2050,12 +2063,12 @@ ossl_ssl_write_internal(VALUE self, VALUE str, VALUE opts)
case SSL_ERROR_WANT_WRITE:
if (no_exception_p(opts)) { return sym_wait_writable; }
write_would_block(nonblock);
io_wait_writable(fptr);
io_wait_writable(io);
continue;
case SSL_ERROR_WANT_READ:
if (no_exception_p(opts)) { return sym_wait_readable; }
read_would_block(nonblock);
io_wait_readable(fptr);
io_wait_readable(io);
continue;
case SSL_ERROR_SYSCALL:
#ifdef __APPLE__
Expand Down
10 changes: 10 additions & 0 deletions lib/openssl/ssl.rb
Expand Up @@ -374,6 +374,16 @@ class SSLSocket
# connection is shut down. This defaults to +false+.
attr_accessor :sync_close

def close_write
stop
end

def shutdown(flags)
if flags == ???
stop
to_io.shutdown(flags)
end

# call-seq:
# ssl.sysclose => nil
#
Expand Down

0 comments on commit 725d32c

Please sign in to comment.