Class: NetworkUtils::Port

Inherits:
Object
  • Object
show all
Defined in:
lib/network_utils/port.rb

Overview

Simple class to work with ports Allows to get random port number, check availability, etc.

Constant Summary collapse

PORT_LOOKUP_RETRY_LIMIT =

The max limit for port lookup retries

50
IANA_PORT_RANGE =

Internet Assigned Numbers Authority suggested range

(49_152..65_535).freeze

Class Method Summary collapse

Class Method Details

.available?(port, host = '127.0.0.1', timeout = 1) ⇒ Boolean Also known as: free?

Checks if the port is available (free) on the host

Examples:

NetworkUtils::Port.available?(9292)
NetworkUtils::Port.available?(80, 'google.com', 100)
NetworkUtils::Port.free?(80, 'google.com', 100)
NetworkUtils::Port.free?(80, 'google.com', 100)

Parameters:

  • port (Integer)

    the port we want to check availability of

  • host (String) (defaults to: '127.0.0.1')

    the host we want to check on (default: 127.0.0.1)

  • timeout (Timeout) (defaults to: 1)

    the time (seconds) we ready to wait (default: 1)

Returns:

  • (Boolean)

    result of the check (true — port is free to use, false — the port is occupied)



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/network_utils/port.rb', line 33

def self.available?(port, host = '127.0.0.1', timeout = 1)
  return false unless port && host && timeout && timeout.positive?

  Timeout.timeout(timeout) do
    TCPSocket.new(host, port).close
    false
  end
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
  true
rescue SocketError, Timeout::Error, Errno::EADDRNOTAVAIL
  false
end

.opened?(port, host = '127.0.0.1', timeout = 1) ⇒ Boolean Also known as: occupied?

Note:

Just the opposite of ‘available?`

Checks if the port is opened (occupied / being listened) on the host

Examples:

NetworkUtils::Port.opened?(443, 'google.com')
NetworkUtils::Port.opened?(80, 'google.com', 1)
NetworkUtils::Port.occupied?(80, 'google.com', 1)
NetworkUtils::Port.occupied?(80, 'google.com', 1)

Parameters:

  • port (Integer)

    the port we want to check availability of

  • host (String) (defaults to: '127.0.0.1')

    the host we want to check on (default: 127.0.0.1)

  • timeout (Timeout) (defaults to: 1)

    the time (seconds) we ready to wait (default: 1)

Returns:

  • (Boolean)

    result of the check (true — the port is being listened, false — the port is free)



61
62
63
# File 'lib/network_utils/port.rb', line 61

def self.opened?(port, host = '127.0.0.1', timeout = 1)
  !available?(port, host, timeout)
end

.randomBoolean

Note:

The Internet Assigned Numbers Authority (IANA) suggests the range 49152 to 65535 (215+214 to 216−1) for dynamic or private ports.

Generates random port from IANA recommended range

Returns:

  • (Boolean)

    port the port from the IANA suggested range



72
73
74
# File 'lib/network_utils/port.rb', line 72

def self.random
  rand(IANA_PORT_RANGE)
end

.random_freeBoolean

Note:

The Internet Assigned Numbers Authority (IANA) suggests the range 49152 to 65535 (215+214 to 216−1) for dynamic or private ports.

Generates random port from IANA recommended range which is free on the localhost

Returns:

  • (Boolean)

    port the port from the IANA suggested range which is also free on the current machine



83
84
85
86
87
88
89
90
# File 'lib/network_utils/port.rb', line 83

def self.random_free
  PORT_LOOKUP_RETRY_LIMIT.times do
    port = random
    return port if available?(port)
  end

  nil
end