Sha256: 65103780474e0cdbb5e3658fe90b1515bac96d9a5804eaa4cfdadefee7859d43

Contents?: true

Size: 1.56 KB

Versions: 11

Compression:

Stored size: 1.56 KB

Contents

require 'bindata'
require 'forwardable'
require 'pio/monkey_patch/integer'

module Pio
  # An action to modify the source/destination TCP/UDP port of a packet.
  class SetTransportPort
    # rubocop:disable MethodLength
    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
    # rubocop:enable MethodLength

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

    extend Forwardable

    def_delegators :@format, :type
    def_delegators :@format, :message_length
    def_delegators :@format, :port_number
    def_delegator :@format, :to_binary_s, :to_binary

    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
  end

  # An action to modify the source TCP/UDP port of a packet.
  class SetTransportSourcePort < SetTransportPort
    def_format 9
  end

  # An action to modify the source TCP/UDP port of a packet.
  class SetTransportDestinationPort < SetTransportPort
    def_format 10
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
pio-0.25.0 lib/pio/open_flow10/set_transport_port.rb
pio-0.24.2 lib/pio/open_flow10/set_transport_port.rb
pio-0.24.1 lib/pio/open_flow10/set_transport_port.rb
pio-0.24.0 lib/pio/open_flow10/set_transport_port.rb
pio-0.23.1 lib/pio/open_flow10/set_transport_port.rb
pio-0.23.0 lib/pio/open_flow10/set_transport_port.rb
pio-0.22.0 lib/pio/open_flow10/set_transport_port.rb
pio-0.21.1 lib/pio/open_flow10/set_transport_port.rb
pio-0.21.0 lib/pio/set_transport_port.rb
pio-0.20.1 lib/pio/set_transport_port.rb
pio-0.20.0 lib/pio/set_transport_port.rb