Sha256: 2b2ed4481729f7fe68c26004c9998c898a3e05a8022b28b97e2b5148162a479c

Contents?: true

Size: 1.35 KB

Versions: 14

Compression:

Stored size: 1.35 KB

Contents

module Ftpd

  # With the commands EPORT and EPSV, the client sends a protocol code
  # to indicate whether it wants an IPV4 or an IPV6 connection.  This
  # class contains functions related to that protocol code.

  class Protocols

    module Codes
      IPV4 = 1
      IPV6 = 2
    end
    include Codes

    # @param socket [TCPSocket, OpenSSL::SSL::SSLSocket] The socket.
    #   It doesn't matter whether it's the server socket (the one on
    #   which #accept is called), or the socket returned by #accept.

    def initialize(socket)
      @socket = socket
    end

    # Can the socket support a connection in the indicated protocol?
    #
    # @param protocol_code [Integer] protocol code

    def supports_protocol?(protocol_code)
      protocol_codes.include?(protocol_code)
    end

    # What protocol codes does the socket support?
    #
    # @return [Array<Integer>] List of protocol codes

    def protocol_codes
      [
        (IPV4 if supports_ipv4?),
        (IPV6 if supports_ipv6?),
      ].compact
    end

    private

    def supports_ipv4?
      @socket.local_address.ipv4? || ipv6_dual_stack?
    end

    def supports_ipv6?
      @socket.local_address.ipv6?
    end

    def ipv6_dual_stack?
      v6only = @socket.getsockopt(Socket::IPPROTO_IPV6,
                                  Socket::IPV6_V6ONLY).unpack('i')
      v6only == [0]
    end

  end

end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
ftpd-1.1.1 lib/ftpd/protocols.rb
ftpd-1.1.0 lib/ftpd/protocols.rb
investtools-ftpd-1.0.1 lib/ftpd/protocols.rb
ftpd-1.0.1 lib/ftpd/protocols.rb
ftpd-1.0.0 lib/ftpd/protocols.rb
ftpd-0.17.0 lib/ftpd/protocols.rb
ftpd-0.16.0 lib/ftpd/protocols.rb
ftpd-0.15.0 lib/ftpd/protocols.rb
ftpd-0.14.0 lib/ftpd/protocols.rb
ftpd-0.13.0 lib/ftpd/protocols.rb
ftpd-0.12.0 lib/ftpd/protocols.rb
ftpd-0.11.0 lib/ftpd/protocols.rb
ftpd-0.10.0 lib/ftpd/protocols.rb
ftpd-0.9.0 lib/ftpd/protocols.rb