Sha256: 777cd18847534a57711b99be12be60b175a08f215746eafe794bd316c0caf972

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

require 'pio/ethernet_header'
require 'pio/ipv4_header'

module Pio
  # Raw data parser.
  class Parser
    # Ethernet header parser
    class EtherTypeParser < BinData::Record
      endian :big

      mac_address :destination_mac
      mac_address :source_mac
      uint16 :ether_type
    end

    # IPv4 packet parser
    class IPv4Packet < BinData::Record
      include EthernetHeader
      include IPv4Header

      endian :big

      ethernet_header ether_type: EtherType::IPV4
      ipv4_header

      uint16 :transport_source_port
      uint16 :transport_destination_port
      rest :rest
    end

    # rubocop:disable MethodLength
    def self.read(raw_data)
      ethernet_header = EtherTypeParser.read(raw_data)
      case ethernet_header.ether_type
      when EthernetHeader::EtherType::IPV4, EthernetHeader::EtherType::VLAN
        IPv4Packet.read raw_data
      when EthernetHeader::EtherType::ARP
        Pio::Arp.read raw_data
      when EthernetHeader::EtherType::LLDP
        Pio::Lldp.read raw_data
      else
        fail 'Failed to parse packet_in data.'
      end
    end
    # rubocop:enable MethodLength
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pio-0.27.1 lib/pio/parser.rb
pio-0.27.0 lib/pio/parser.rb
pio-0.26.0 lib/pio/parser.rb
pio-0.25.0 lib/pio/parser.rb
pio-0.24.2 lib/pio/parser.rb
pio-0.24.1 lib/pio/parser.rb
pio-0.24.0 lib/pio/parser.rb