Class: Pio::SetTransportPort

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pio/set_transport_port.rb

Overview

An action to modify the source/destination TCP/UDP port of a packet.

Direct Known Subclasses

SetTransportDstPort, SetTransportSrcPort

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ SetTransportPort

Returns a new instance of SetTransportPort.



38
39
40
41
42
43
44
45
46
# File 'lib/pio/set_transport_port.rb', line 38

def initialize(number)
  port_number = number.to_i
  unless port_number.unsigned_16bit?
    fail ArgumentError, 'TCP/UDP port must be an unsigned 16-bit integer.'
  end
  @format = self.class.const_get(:Format).new(port_number: port_number)
rescue NoMethodError
  raise TypeError, 'TCP/UDP port must be an unsigned 16-bit integer.'
end

Class Method Details

.def_format(action_type) ⇒ Object

rubocop:disable MethodLength



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pio/set_transport_port.rb', line 9

def self.def_format(action_type)
  str = %(
    class Format < BinData::Record
      endian :big

      uint16 :type, value: #{action_type}
      uint16 :message_length, value: 8
      uint16 :port_number
      uint16 :padding
      hide :padding
    end
  )
  module_eval str
end

.read(raw_data) ⇒ Object

rubocop:enable MethodLength



25
26
27
28
29
# File 'lib/pio/set_transport_port.rb', line 25

def self.read(raw_data)
  action = allocate
  action.instance_variable_set(:@format, const_get(:Format).read(raw_data))
  action
end