Class: Procfs2::ProcNetTcpSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/procfs2/proc_net_tcp_socket.rb

Constant Summary collapse

HEADERS =
%i[
  id
  local_address_port
  remote_address_port
  state
  transmit_receive_queue
  timer_and_jiffies
  retransmit
  uid
  timeout
  inode
  socket_reference_count
  socket_location
  retransmit_timeout
  soft_clock_predicted_tick
  ack_pingpong
  sending_congestion_window
  slow_start_size_threshold
].freeze
STATE_STR =
{
  0 => 'UNKNOWN',
  1 => 'ESTABLISHED',
  2 => 'SYN',
  3 => 'SYN_RECV',
  4 => 'FIN_WAIT1',
  5 => 'FIN_WAIT2',
  6 => 'TIME_WAIT',
  7 => 'CLOSE',
  8 => 'CLOSE_WAIT',
  9 => 'LAST_ACK',
  10 => 'LISTEN',
  11 => 'CLOSING',
  12 => 'NEW_SYN_RECV',
  13 => 'MAX_STATE'
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, local_address:, local_port:, remote_address:, remote_port:, state:, transmit_queue:, receive_queue:, uid:, inode:, **extra) ⇒ ProcNetTcpSocket

Returns a new instance of ProcNetTcpSocket.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 45

def initialize(id:, local_address:, local_port:, remote_address:, remote_port:, state:,
               transmit_queue:, receive_queue:, uid:, inode:, **extra)
  @id = id
  @local_address = local_address
  @local_port = local_port
  @remote_address = remote_address
  @remote_port = remote_port
  @state = state
  @transmit_queue = transmit_queue
  @receive_queue = receive_queue
  @uid = uid
  @inode = inode

  @extra = extra
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



42
43
44
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 42

def id
  @id
end

#inodeObject (readonly)

Returns the value of attribute inode.



42
43
44
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 42

def inode
  @inode
end

#local_addressObject (readonly)

Returns the value of attribute local_address.



42
43
44
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 42

def local_address
  @local_address
end

#local_portObject (readonly)

Returns the value of attribute local_port.



42
43
44
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 42

def local_port
  @local_port
end

#receive_queueObject (readonly)

Returns the value of attribute receive_queue.



42
43
44
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 42

def receive_queue
  @receive_queue
end

#remote_addressObject (readonly)

Returns the value of attribute remote_address.



42
43
44
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 42

def remote_address
  @remote_address
end

#remote_portObject (readonly)

Returns the value of attribute remote_port.



42
43
44
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 42

def remote_port
  @remote_port
end

#stateObject (readonly)

Returns the value of attribute state.



42
43
44
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 42

def state
  @state
end

#transmit_queueObject (readonly)

Returns the value of attribute transmit_queue.



42
43
44
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 42

def transmit_queue
  @transmit_queue
end

#uidObject (readonly)

Returns the value of attribute uid.



42
43
44
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 42

def uid
  @uid
end

Class Method Details

.build_from_line(line) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 70

def build_from_line(line)
  info = HEADERS.zip(line.strip.split).to_h
  options = {}

  options[:id] = info[:id].chomp(':').to_i
  options[:local_address], options[:local_port] = parse_address_hex(info[:local_address_port])
  options[:remote_address], options[:remote_port] = parse_address_hex(info[:remote_address_port])
  options[:state] = info[:state].hex
  options[:transmit_queue], options[:receive_queue] = info[:transmit_receive_queue].split(':').map(&:hex)
  options[:timer_active], options[:jiffries] = info[:timer_and_jiffies].split(':').map(&:hex)
  options[:retransmit] = info[:retransmit].hex
  options[:uid] = info[:uid].to_i
  options[:timeout] = info[:timeout].to_i
  options[:inode] = info[:inode].to_i
  options[:socket_reference_count] = info[:socket_reference_count].to_i
  options[:socket_location] = info[:socket_location]
  options[:retransmit_timeout] = info[:retransmit_timeout].to_i
  options[:soft_clock_predicted_tick] = info[:soft_clock_predicted_tick].to_i
  options[:ack_pingpong] = info[:ack_pingpong].to_i
  options[:sending_congestion_window] = info[:sending_congestion_window].to_i
  options[:slow_start_size_threshold] = info[:slow_start_size_threshold].to_i

  new(**options)
end

.parse_address_hex(address_port_hex) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 95

def parse_address_hex(address_port_hex)
  address_hex, port_hex = address_port_hex.split(':')
  address = [address_hex[6..7],
             address_hex[4..5],
             address_hex[2..3],
             address_hex[0..1]].map(&:hex).join('.')
  port = port_hex.hex
  [address, port]
end

Instance Method Details

#state_strObject



65
66
67
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 65

def state_str
  STATE_STR[state]
end

#typeObject



61
62
63
# File 'lib/procfs2/proc_net_tcp_socket.rb', line 61

def type
  'tcp'
end