Class: LibTLS::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/libtls/server.rb

Overview

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configure:) {|self| ... } ⇒ Server

Instantiate and configure a TLS server

Once constructed, a LibTLS::Server instance must be freed with the #finish method. If you pass a block to the constructor it will handle this for you.

Parameters:

  • configure (Hash)

    a mapping from setting name to value. The setting name is any of Config::VALID_SET_CONFIGS; the value is either a scalar value passed through to the C function, or an array of values. For example:

    { ca_file: 'ca.pem', key_mem: [key_ptr, 48] }
    

Yield Parameters:

  • self (Server)

    an initialized and configured instance of self

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/libtls/server.rb', line 53

def initialize(configure:, &block)
  if LibTLS::Raw.tls_init < 0
    raise LibTLS::UnknownCError, "tls_init"
  end

  @config = Config.new(configure)

  if (@ctx = LibTLS::Raw.tls_server) == nil
    raise LibTLS::UnknownCError, "tls_server"
  end

  if LibTLS::Raw::tls_configure(ctx, @config.as_raw) < 0
    raise LibTLS::CError, "tls_configure: #{LibTLS::Raw.tls_error(ctx)}"
  end

  if block
    begin
      block.call(self)
    ensure
      self.finish
    end
  end
end

Instance Attribute Details

#ctxObject (readonly)

The FFI wrapper around the struct tls object

This is only useful for calling any of the Raw methods.



37
38
39
# File 'lib/libtls/server.rb', line 37

def ctx
  @ctx
end

Instance Method Details

#accept(client_socket) {|client| ... } ⇒ Object

Negotiate a TLS handshake on an existing socket

The client socket is assumed to already have an active connection; for example, IO.select or Socket#accept has been called.

The block is run on a connection opened for the client. Once the block finishes, the connection is closed automatically.

Parameters:

  • client_socket (Socket)

    a connected socket

Yield Parameters:

Returns:

  • the result of the block

Raises:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/libtls/server.rb', line 90

def accept(client_socket, &block)
  cctx_ptr = FFI::MemoryPointer.new(:pointer)

  if tls_accept(cctx_ptr, client_socket) == -1
    raise LibTLS::CError, "tls_accept_socket: #{LibTLS::Raw.tls_error(ctx)}"
  end

  cctx = cctx_ptr.read_pointer

  opened_client = OpenedClient.new(cctx)
  block.call(opened_client)
ensure
  opened_client && opened_client.close
end

#finishObject

Release any memory held on to by the C library

This method must be called either implicitly by passing a block to #initialize, or explicitly by you.



110
111
112
113
# File 'lib/libtls/server.rb', line 110

def finish
  @config.free
  LibTLS::Raw.tls_free(ctx)
end