class ROM::LDAP::Socket

Builds either TCP or UNIX.

@api private

Public Instance Methods

call() click to toggle source

@return [::Socket, ::OpenSSL::SSL::SSLSocket]

# File lib/rom/ldap/socket.rb, line 85
def call
  socket.do_not_reverse_lookup = true
  socket.sync = buffered
  socket.setsockopt(:SOCKET, :KEEPALIVE, keep_alive)
  socket.setsockopt(:TCP, :NODELAY, buffered) unless path

  connect!
end

Private Instance Methods

addrinfo() click to toggle source

Performs DNS lookup

@return [Addrinfo]

@api private

# File lib/rom/ldap/socket.rb, line 188
def addrinfo
  return Addrinfo.unix(path) if path

  Addrinfo.tcp(host, port)
end
connect!() click to toggle source

@return [::Socket]

@raise [ConnectionError]

# File lib/rom/ldap/socket.rb, line 100
def connect!
  @counter ||= 1

  if ssl
    require 'openssl'

    ctx = OpenSSL::SSL::SSLContext.new
    ctx.set_params(ssl)
    @socket = OpenSSL::SSL::SSLSocket.new(socket, ctx)
    # socket.extend(GetbyteForSSLSocket) unless socket.respond_to?(:getbyte)
    # socket.extend(FixSSLSocketSyncClose)
    socket.connect_nonblock
  else
    socket.connect_nonblock(sockaddr)
  end

  socket
rescue Errno::EISCONN
  socket
rescue IO::WaitWritable
  # OpenSSL::SSL::SSLErrorWaitWritable
  if writable?
    connect!
  else
    disconnect!
    raise ConnectionError, "Connection write timeout - #{host}:#{port}"
  end
rescue IO::WaitReadable
  if readable?
    @counter += 1
    retry unless @counter > retry_count
  else
    raise ConnectionError, "Connection read timeout - #{host}:#{port}"
  end
rescue Errno::EADDRNOTAVAIL
  raise ConnectionError, "Host or port is invalid - #{host}:#{port}"
rescue SocketError
  raise ConnectionError, "Host could not be resolved - #{host}:#{port}"
rescue Errno::ENOENT
  raise ConnectionError, "Path to unix socket is invalid - #{path}"
rescue Errno::EHOSTDOWN
  raise ConnectionError, "Server is down - #{host}:#{port}"
rescue Errno::ECONNREFUSED
  raise ConnectionError, "Connection refused - #{host}:#{port}"
rescue Errno::EAFNOSUPPORT
  raise ConnectionError, "Connection is not supported - #{host}:#{port}"
rescue
  disconnect!
  raise ConnectionError, "Connection failed - #{host}:#{port}"
end
disconnect!() click to toggle source

@return [NilClass]

# File lib/rom/ldap/socket.rb, line 161
def disconnect!
  socket.close
  @socket = nil
end
readable?() click to toggle source

@return [TrueClass,FalseClass]

@api private

# File lib/rom/ldap/socket.rb, line 204
def readable?
  !IO.select([socket], nil, nil, read_timeout).nil?
end
sockaddr() click to toggle source

IPV4 or UNIX socket address

@return [String] ASCII-8BIT

@api private

# File lib/rom/ldap/socket.rb, line 171
def sockaddr
  if addrinfo.unix?
    ::Socket.pack_sockaddr_un(addrinfo.unix_path)

  elsif addrinfo.ipv4?
    ::Socket.pack_sockaddr_in(addrinfo.ip_port, addrinfo.ip_address)

  elsif addrinfo.ipv6_loopback?
    ::Socket.pack_sockaddr_in(addrinfo.ip_port, '127.0.0.1')
  end
end
socket() click to toggle source

@return [::Socket]

# File lib/rom/ldap/socket.rb, line 155
def socket
  @socket ||= ::Socket.new((path ? :UNIX : :INET), :STREAM)
end
writable?() click to toggle source

@return [TrueClass,FalseClass]

@api private

# File lib/rom/ldap/socket.rb, line 197
def writable?
  !IO.select(nil, [socket], nil, write_timeout).nil?
end