Class: PacketGen::Header::IGMP

Inherits:
Base
  • Object
show all
Defined in:
lib/packetgen/header/igmp.rb

Overview

This class supports IGMPv2 (RFC 2236).

From RFC 2236, a IGMP header has the following format:

0                   1                   2                   3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|      Type     | Max Resp Time |           Checksum            |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                         Group Address                         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

A IGMP header consists of:

After adding a IGMP header to a packet, you have to call #igmpize to ensure resulting packet conforms to RFC 2236.

Examples:

Create a IGMP header

# standalone
igmp = PacketGen::Header::IGMP.new
# in a packet
pkt = PacketGen.gen('IP').add('IGMP')
# access to IGMP header
pkt.igmp.class  # => PacketGen::Header::IGMP

IGMP attributes

igmp = PacketGen::Header::IGMP.new
igmp.type = 'MembershipQuery'   # or 0x11
igmp.max_resp_time = 20
igmp.checksum = 0x248a
igmp.group_addr = '224.0.0.1'

Author:

  • Sylvain Daubert

Since:

  • 2.4.0

Direct Known Subclasses

IGMPv3

Constant Summary collapse

IP_PROTOCOL =

IGMP internet protocol number

Since:

  • 2.4.0

2
TYPES =

Known types

Since:

  • 2.4.0

{
  'MembershipQuery' => 0x11,
  'MembershipReportv1' => 0x12,
  'MembershipReportv2' => 0x16,
  'LeaveGroup' => 0x17,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

bind, calculate_and_set_length, #header_id, inherited, #initialize, #ip_header, #ll_header

Methods included from PacketGen::Headerable

included, #method_name, #packet, #packet=, #parse?, #protocol_name, #read, #to_s

Constructor Details

This class inherits a constructor from PacketGen::Header::Base

Instance Attribute Details

#bodyString, Base

IGMP body (not used in IGMPv2)

Returns:


79
# File 'lib/packetgen/header/igmp.rb', line 79

define_attr :body, BinStruct::String

#checksumInteger

16-bit IGMP Checksum

Returns:

  • (Integer)

71
# File 'lib/packetgen/header/igmp.rb', line 71

define_attr :checksum, BinStruct::Int16

#group_addrIP::Addr

IP Group address

Returns:


75
# File 'lib/packetgen/header/igmp.rb', line 75

define_attr :group_addr, IP::Addr, default: '0.0.0.0'

#max_resp_timeInteger

8-bit IGMP Max Response Time

Returns:

  • (Integer)

67
# File 'lib/packetgen/header/igmp.rb', line 67

define_attr :max_resp_time, BinStruct::Int8

#typeInteger

8-bit IGMP Type

Returns:

  • (Integer)

63
# File 'lib/packetgen/header/igmp.rb', line 63

define_attr :type, BinStruct::Int8Enum, enum: TYPES

Instance Method Details

#added_to_packet(packet) ⇒ Object

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.

Note:

This method is used internally by PacketGen and should not be directly called

Define #igmpize method onto packet. This method calls #igmpize.

Since:

  • 2.4.0


85
86
87
88
# File 'lib/packetgen/header/igmp.rb', line 85

def added_to_packet(packet)
  igmp_idx = packet.headers.size
  packet.instance_eval "def igmpize() @headers[#{igmp_idx}].igmpize; end" # def igmpize() @headers[2].igmpize; end
end

#calc_checksumInteger

Compute checksum and set checksum field

Returns:

  • (Integer)

Since:

  • 2.4.0


98
99
100
101
# File 'lib/packetgen/header/igmp.rb', line 98

def calc_checksum
  sum = IP.sum16(self)
  self.checksum = IP.reduce_checksum(sum)
end

#human_typeString

Get human readbale type

Returns:

  • (String)

Since:

  • 2.4.0


92
93
94
# File 'lib/packetgen/header/igmp.rb', line 92

def human_type
  self[:type].to_human
end

#igmpizevoid

This method returns an undefined value.

Fixup IP header according to RFC 2236:

  • set TTL to 1,

  • add Router Alert option,

  • recalculate checksum and length.

This method may be called as:

# first method
pkt.igmp.igmpize
# second method
pkt.igmpize

Examples:

pkt = PacketGen.gen('IP').add('IGMP', type: 'MembershipQuery', max_resp_time: 20, group_addr: '1.2.3.4')
pkt.igmpize
pkt.ip.ttl     #=> 1
pkt.ip.options.map(&:class) #=> [PacketGen::Header::IP::RA]

Since:

  • 2.4.0


118
119
120
121
122
123
# File 'lib/packetgen/header/igmp.rb', line 118

def igmpize
  iph = ip_header(self)
  iph.ttl = 1
  iph.options << IP::RA.new
  packet.calc
end