In Files

  • ext/openssl_nonblock.c

OpenSSL::SSL::SSLSocket

Public Instance Methods

accept_nonblock => self click to toggle source
 
               static VALUE
ossl_nonblock_accept_nonblock(VALUE self)
{
#if RUBY_VERSION_CODE >= 190
  rb_rescue(ossl_nonblock_ssl_setup, self, 0, 0);
#else
  ossl_nonblock_ssl_setup(self);
#endif

  return ossl_nonblock_start_ssl(self, SSL_accept, "SSL_accept");
}
            
connect_nonblock => self click to toggle source
 
               static VALUE
ossl_nonblock_connect_nonblock(VALUE self)
{
#if RUBY_VERSION_CODE >= 190
  rb_rescue(ossl_nonblock_ssl_setup, self, ossl_nonblock_ssl_setup_check, Qnil);
#else
  ossl_nonblock_ssl_setup(self);
#endif
  
  return ossl_nonblock_start_ssl(self, SSL_connect, "SSL_connect");
}
            
read_nonblock(length) => string click to toggle source
read_nonblock(length, buffer) => buffer

Parameters

  • length is a positive integer.

  • buffer is a string used to store the result.

 
               static VALUE
ossl_nonblock_read_nonblock(int argc, VALUE *argv, VALUE self)
{
  SSL *ssl;
  int ilen, nread = 0;
  VALUE len, str;

  rb_scan_args(argc, argv, "11", &len, &str);
  ilen = NUM2INT(len);
  
  if(NIL_P(str)) 
    str = rb_str_new(0, ilen);
  else {
    StringValue(str);
    rb_str_modify(str);
    rb_str_resize(str, ilen);
  }
  
  if(ilen == 0) return str;

  Data_Get_Struct(self, SSL, ssl);

  if (ssl) {
    nread = SSL_read(ssl, RSTRING_PTR(str), RSTRING_LEN(str));
    switch(SSL_get_error(ssl, nread)){
    case SSL_ERROR_NONE:
      goto end;
    case SSL_ERROR_ZERO_RETURN:
      rb_eof_error();
    case SSL_ERROR_WANT_WRITE:
      rb_raise(eWriteAgain, "write again");
    case SSL_ERROR_WANT_READ:
      rb_raise(eReadAgain, "read again");
    case SSL_ERROR_SYSCALL:
      if(ERR_peek_error() == 0 && nread == 0) rb_eof_error();
      rb_sys_fail(0);
    default:
      rb_raise(eSSLError, "SSL_read:");
    }
  } else
    rb_raise(rb_eRuntimeError, "SSL session is not started yet.");

end:
  rb_str_set_len(str, nread);
  OBJ_TAINT(str);

  return str;
}
            
write_nonblock(string) => integer click to toggle source
 
               static VALUE
ossl_nonblock_write_nonblock(VALUE self, VALUE str)
{
  SSL *ssl;
  int nwrite = 0;

  StringValue(str);
  Data_Get_Struct(self, SSL, ssl);

  if (ssl) {
    nwrite = SSL_write(ssl, RSTRING_PTR(str), RSTRING_LEN(str));
    switch(SSL_get_error(ssl, nwrite)){
    case SSL_ERROR_NONE:
      goto end;
    case SSL_ERROR_WANT_WRITE:
      rb_raise(eWriteAgain, "write again");
    case SSL_ERROR_WANT_READ:
      rb_raise(eReadAgain, "read again");
    case SSL_ERROR_SYSCALL:
      if (errno) rb_sys_fail(0);
    default:
      rb_raise(eSSLError, "SSL_write:");
    }
  } else
    rb_raise(rb_eRuntimeError, "SSL session is not started yet.");

end:
  return INT2NUM(nwrite);
}