Class: PacketGen::Header::Eth

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

Overview

Ethernet header class

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, known_headers

Methods included from HeaderMethods

#header_id, #ip_header, #packet, #packet=

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

  • :proto (Integer)


86
87
88
89
90
91
# File 'lib/packetgen/header/eth.rb', line 86

def initialize(options={})
  super MacAddr.new.parse(options[:dst] || '00:00:00:00:00:00'),
        MacAddr.new.parse(options[:src] || '00:00:00:00:00:00'),
        Int16.new(options[:proto] || 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



6
7
8
# File 'lib/packetgen/header/eth.rb', line 6

def body
  @body
end

#dstString

Get MAC destination address

Returns:



6
7
8
# File 'lib/packetgen/header/eth.rb', line 6

def dst
  @dst
end

#protoInteger

Get protocol field

Returns:

  • (Integer)


6
7
8
# File 'lib/packetgen/header/eth.rb', line 6

def proto
  @proto
end

#srcString

Get MAC source address

Returns:



6
7
8
# File 'lib/packetgen/header/eth.rb', line 6

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:



96
97
98
99
100
101
102
103
104
105
# File 'lib/packetgen/header/eth.rb', line 96

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[:proto].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



149
150
151
152
# File 'lib/packetgen/header/eth.rb', line 149

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