Module: PacketGen::Header::HeaderMethods

Included in:
ARP, ESP, Eth, ICMP, IP, IPv6, TCP, UDP
Defined in:
lib/packetgen/header/header_methods.rb

Overview

Mixin for various headers

Author:

  • Sylvain Daubert

Instance Method Summary collapse

Instance Method Details

#header_id(header) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get header id in packet headers array

Parameters:

Returns:

  • (Integer)

Raises:

  • FormatError header not in a packet



33
34
35
36
37
38
39
40
# File 'lib/packetgen/header/header_methods.rb', line 33

def header_id(header)
  raise FormatError, "header of type #{header.class} not in a packet" if packet.nil?
  id = packet.headers.index(header)
  if id.nil?
    raise FormatError, "header of type #{header.class} not in packet #{packet}"
  end
  id
end

#inspectString

Common inspect method for headers

Returns:

  • (String)


63
64
65
66
67
68
69
70
# File 'lib/packetgen/header/header_methods.rb', line 63

def inspect
  str = Inspect.dashed_line(self.class, 2)
  to_h.each do |attr, value|
    next if attr == :body
    str << Inspect.inspect_attribute(attr, value, 2)
  end
  str
end

#ip_header(header) ⇒ Header

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get IP or IPv6 previous header from header

Parameters:

Returns:

Raises:

  • FormatError no IP or IPv6 header previous header in packet

  • FormatError header not in a packet



48
49
50
51
52
53
# File 'lib/packetgen/header/header_methods.rb', line 48

def ip_header(header)
  hid = header_id(header)
  iph = packet.headers[0...hid].reverse.find { |h| h.is_a? IP or h.is_a? IPv6 }
  raise FormatError, 'no IP or IPv6 header in packet' if iph.nil?
  iph
end

#packetPacket

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get rference on packet which owns this header

Returns:



24
25
26
# File 'lib/packetgen/header/header_methods.rb', line 24

def packet
  @packet
end

#packet=(packet) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Set reference of packet which owns this header

Parameters:



17
18
19
# File 'lib/packetgen/header/header_methods.rb', line 17

def packet=(packet)
  @packet = packet
end

#protocol_nameString

Return header protocol name

Returns:

  • (String)


57
58
59
# File 'lib/packetgen/header/header_methods.rb', line 57

def protocol_name
  self.class.to_s.sub(/.*::/, '')
end