module Pio::IPv4

IP Version 4 Header Format

Public Class Methods

included(klass) click to toggle source

rubocop:disable MethodLength This method smells of :reek:TooManyStatements

# File lib/pio/ipv4_header.rb, line 22
def self.included(klass)
  def klass.ipv4_header(options = {})
    bit4 :ip_version, value: 0x4
    bit4 :ip_header_length, initial_value: 0x5
    uint8 :ip_type_of_service, initial_value: 0
    uint16be :ip_total_length, initial_value: :calculate_ip_length
    uint16be :ip_identifier, initial_value: 0
    bit3 :ip_flag, initial_value: 0
    bit13 :ip_fragment, initial_value: 0
    uint8 :ip_ttl, initial_value: 128
    uint8 :ip_protocol, initial_value: options[:ip_protocol] || 0
    uint16be :ip_header_checksum, initial_value: :calculate_ip_checksum
    ip_address :source_ip_address
    ip_address :destination_ip_address
    string :ip_option, read_length: :ip_option_length
  end
end

Public Instance Methods

ip_header_length_in_bytes() click to toggle source

rubocop:enable MethodLength

# File lib/pio/ipv4_header.rb, line 61
def ip_header_length_in_bytes
  ip_header_length * 4
end
to_exact_match(in_port) click to toggle source

rubocop:disable MethodLength

# File lib/pio/ipv4_header.rb, line 42
def to_exact_match(in_port)
  match_options = {
    in_port: in_port,
    source_mac_address: source_mac,
    destination_mac_address: destination_mac,
    vlan_vid: vlan? ? vlan_vid : 0xffff,
    vlan_priority: vlan_pcp,
    ether_type: ether_type,
    tos: ip_type_of_service,
    ip_protocol: ip_protocol,
    source_ip_address: source_ip_address,
    destination_ip_address: destination_ip_address,
    transport_source_port: transport_source_port,
    transport_destination_port: transport_destination_port
  }
  Pio::OpenFlow10::Match.new match_options
end

Private Instance Methods

calculate_ip_checksum() click to toggle source

rubocop:disable AbcSize

# File lib/pio/ipv4_header.rb, line 72
def calculate_ip_checksum
  sum = [ip_version << 12 | ip_header_length << 8 | ip_type_of_service,
         ip_total_length,
         ip_identifier,
         ip_flag << 13 | ip_fragment,
         ip_ttl << 8 | ip_protocol,
         source_ip_address >> 16,
         source_ip_address & 0xffff,
         destination_ip_address >> 16,
         destination_ip_address & 0xffff].reduce(:+)
  ~((sum & 0xffff) + (sum >> 16)) & 0xffff
end
calculate_ip_length() click to toggle source
# File lib/pio/ipv4_header.rb, line 67
def calculate_ip_length
  ip_header_length * 4 + ip_payload_binary.length
end
ip_option_length() click to toggle source
# File lib/pio/ipv4_header.rb, line 90
def ip_option_length
  20 - ip_header_length * 4
end
ip_payload_binary() click to toggle source

rubocop:enable AbcSize

# File lib/pio/ipv4_header.rb, line 86
def ip_payload_binary
  binary_after(:ip_option)
end