Class: OpenSSL::SSL::SSLServer

Inherits:
Object
  • Object
show all
Includes:
SocketForwarder
Defined in:
lib/openssl/ssl.rb

Overview

SSLServer represents a TCP/IP server socket with Secure Sockets Layer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SocketForwarder

#addr, #closed?, #do_not_reverse_lookup=, #fcntl, #fileno, #getsockopt, #peeraddr, #setsockopt

Constructor Details

#initialize(svr, ctx) ⇒ SSLServer

Creates a new instance of SSLServer.

  • srv is an instance of TCPServer.

  • ctx is an instance of OpenSSL::SSL::SSLContext.



491
492
493
494
495
496
497
498
499
500
501
# File 'lib/openssl/ssl.rb', line 491

def initialize(svr, ctx)
  @svr = svr
  @ctx = ctx
  unless ctx.session_id_context
    # see #6137 - session id may not exceed 32 bytes
    prng = ::Random.new($0.hash)
    session_id = prng.bytes(16).unpack('H*')[0]
    @ctx.session_id_context = session_id
  end
  @start_immediately = true
end

Instance Attribute Details

#start_immediatelyObject

When true then #accept works exactly the same as TCPServer#accept



486
487
488
# File 'lib/openssl/ssl.rb', line 486

def start_immediately
  @start_immediately
end

Instance Method Details

#acceptObject

Works similar to TCPServer#accept.



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/openssl/ssl.rb', line 519

def accept
  # Socket#accept returns [socket, addrinfo].
  # TCPServer#accept returns a socket.
  # The following comma strips addrinfo.
  sock, = @svr.accept
  begin
    ssl = OpenSSL::SSL::SSLSocket.new(sock, @ctx)
    ssl.sync_close = true
    ssl.accept if @start_immediately
    ssl
  rescue Exception => ex
    if ssl
      ssl.close
    else
      sock.close
    end
    raise ex
  end
end

#closeObject

See IO#close for details.



540
541
542
# File 'lib/openssl/ssl.rb', line 540

def close
  @svr.close
end

#listen(backlog = Socket::SOMAXCONN) ⇒ Object

See TCPServer#listen for details.



509
510
511
# File 'lib/openssl/ssl.rb', line 509

def listen(backlog=Socket::SOMAXCONN)
  @svr.listen(backlog)
end

#shutdown(how = Socket::SHUT_RDWR) ⇒ Object

See BasicSocket#shutdown for details.



514
515
516
# File 'lib/openssl/ssl.rb', line 514

def shutdown(how=Socket::SHUT_RDWR)
  @svr.shutdown(how)
end

#to_ioObject

Returns the TCPServer passed to the SSLServer when initialized.



504
505
506
# File 'lib/openssl/ssl.rb', line 504

def to_io
  @svr
end