Class: PacketGen::Header::Eth

Inherits:
Struct
  • Object
show all
Extended by:
HeaderClassMethods
Includes:
HeaderMethods, StructFu
Defined in:
lib/packetgen/header/eth.rb

Overview

An Ethernet header consists of:

Create a Ethernet header

# standalone
eth = PacketGen::Header::Eth.new
# in a packet
pkt = PacketGen.gen('Eth')
# access to Ethernet header
pkt.eth   # => PacketGen::Header::Eth

Ethernet attributes

eth.dst = "00:01:02:03:04:05'
eth.src        # => "00:01:01:01:01:01"
eth[:src]      # => PacketGen::Header::Eth::MacAddr
eth.ethertype  # => 16-bit Integer
eth.body = "This is a body"

Author:

  • Sylvain Daubert

Defined Under Namespace

Classes: MacAddr

Constant Summary collapse

PCAP_SNAPLEN =
0xffff
PCAP_PROMISC =
false
PCAP_TIMEOUT =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HeaderClassMethods

bind_header, define_bit_fields_on, known_headers

Methods included from HeaderMethods

#header_id, #inspect, #ip_header, #packet, #packet=, #protocol_name

Methods included from StructFu

#clone, #set_endianness, #sz, #to_s, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(options = {}) ⇒ Eth

Returns a new instance of Eth.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :dst (String)

    MAC destination address

  • :src (String)

    MAC source address

  • :ethertype (Integer)


111
112
113
114
115
116
# File 'lib/packetgen/header/eth.rb', line 111

def initialize(options={})
  super MacAddr.new.from_human(options[:dst] || '00:00:00:00:00:00'),
        MacAddr.new.from_human(options[:src] || '00:00:00:00:00:00'),
        Int16.new(options[:ethertype] || 0),
        StructFu::String.new.read(options[:body])
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



31
32
33
# File 'lib/packetgen/header/eth.rb', line 31

def body
  @body
end

#dstString

Get MAC destination address

Returns:



31
32
33
# File 'lib/packetgen/header/eth.rb', line 31

def dst
  @dst
end

#ethertypeInteger

Get ethertype field

Returns:

  • (Integer)


31
32
33
# File 'lib/packetgen/header/eth.rb', line 31

def ethertype
  @ethertype
end

#srcString

Get MAC source address

Returns:



31
32
33
# File 'lib/packetgen/header/eth.rb', line 31

def src
  @src
end

Instance Method Details

#read(str) ⇒ self

Read a Eth header from a string

Parameters:

  • str (String)

    binary string

Returns:

  • (self)

Raises:



121
122
123
124
125
126
127
128
129
130
# File 'lib/packetgen/header/eth.rb', line 121

def read(str)
  return self if str.nil?
  raise ParseError, 'string too short for Eth' if str.size < self.sz
  force_binary str
  self[:dst].read str[0, 6]
  self[:src].read str[6, 6]
  self[:ethertype].read str[12, 2]
  self[:body].read str[14..-1]
  self
end

#to_w(iface) ⇒ void

This method returns an undefined value.

send Eth packet on wire.

Parameters:

  • iface (String)

    interface name



174
175
176
177
# File 'lib/packetgen/header/eth.rb', line 174

def to_w(iface)
  pcap = PCAPRUB::Pcap.open_live(iface, PCAP_SNAPLEN, PCAP_PROMISC, PCAP_TIMEOUT)
  pcap.inject self.to_s
end