Module: Kanrisuru::Core::Socket

Extended by:
OsPackage::Define
Defined in:
lib/kanrisuru/core/socket.rb,
lib/kanrisuru/core/socket/types.rb,
lib/kanrisuru/core/socket/constants.rb,
lib/kanrisuru/core/socket/parsers/ss.rb,
lib/kanrisuru/core/socket/commands/ss.rb

Defined Under Namespace

Modules: Parser Classes: Statistics, StatisticsMemory

Constant Summary collapse

TCP_STATES =
%w[
  established syn-sent syn-recv
  fin-wait-1 fin-wait-2 time-wait
  closed close-wait last-ack listening closing
].freeze
OTHER_STATES =
%w[
  all connected synchronized bucket syn-recv
  big
].freeze
TCP_STATE_ABBR =
{
  'ESTAB' => 'established', 'LISTEN' => 'listening', 'UNCONN' => 'unconnected',
  'SYN-SENT' => 'syn-sent', 'SYN-RECV' => 'syn-recv', 'FIN-WAIT-1' => 'fin-wait-1',
  'FIN-WAIT-2' => 'fin-wait-2', 'TIME-WAIT' => 'time-wait', 'CLOSE-WAIT' => 'close-wait',
  'LAST-ACK' => 'last-ack', 'CLOSING' => 'closing'
}.freeze
NETWORK_FAMILIES =
%w[
  unix inet inet6 link netlink vsock xdp
].freeze

Instance Method Summary collapse

Methods included from OsPackage::Define

extended, os_define

Instance Method Details

#ss(opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kanrisuru/core/socket/commands/ss.rb', line 6

def ss(opts = {})
  state = opts[:state]
  expression = opts[:expression]
  family = opts[:family]

  command = Kanrisuru::Command.new('ss')

  command.append_flag('-a')
  command.append_flag('-m')

  command.append_flag('-n', opts[:numeric])
  command.append_flag('-t', opts[:tcp])
  command.append_flag('-u', opts[:udp])
  command.append_flag('-x', opts[:unix])
  command.append_flag('-w', opts[:raw])

  if Kanrisuru::Util.present?(family)
    raise ArgumentError, 'invalid family type' unless NETWORK_FAMILIES.include?(family)

    command.append_arg('-f', family)
  end

  if Kanrisuru::Util.present?(state)
    raise ArgumentError, 'invalid filter state' if !TCP_STATES.include?(state) && !OTHER_STATES.include?(state)

    command.append_arg('state', state)
  end

  command << expression if Kanrisuru::Util.present?(expression)

  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::Ss.parse(cmd, state, opts)
  end
end