Class: PacketGen::Header::Eth
- Extended by:
- HeaderClassMethods
- Includes:
- HeaderMethods, StructFu
- Defined in:
- lib/packetgen/header/eth.rb
Overview
An Ethernet header consists of:
-
a destination MAC address (MacAddr),
-
a source MAC address (MacAddr),
-
and a body (a StructFu::String or another Header class).
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"
Defined Under Namespace
Classes: MacAddr
Constant Summary collapse
- PCAP_SNAPLEN =
0xffff- PCAP_PROMISC =
false- PCAP_TIMEOUT =
1
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#dst ⇒ String
Get MAC destination address.
-
#ethertype ⇒ Integer
Get ethertype field.
-
#src ⇒ String
Get MAC source address.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Eth
constructor
A new instance of Eth.
-
#read(str) ⇒ self
Read a Eth header from a string.
-
#to_w(iface) ⇒ void
send Eth packet on wire.
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
Constructor Details
#initialize(options = {}) ⇒ Eth
Returns a new instance of Eth.
111 112 113 114 115 116 |
# File 'lib/packetgen/header/eth.rb', line 111 def initialize(={}) super MacAddr.new.from_human([:dst] || '00:00:00:00:00:00'), MacAddr.new.from_human([:src] || '00:00:00:00:00:00'), Int16.new([:ethertype] || 0), StructFu::String.new.read([:body]) end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body
31 32 33 |
# File 'lib/packetgen/header/eth.rb', line 31 def body @body end |
#dst ⇒ String
Get MAC destination address
31 32 33 |
# File 'lib/packetgen/header/eth.rb', line 31 def dst @dst end |
#ethertype ⇒ Integer
Get ethertype field
31 32 33 |
# File 'lib/packetgen/header/eth.rb', line 31 def ethertype @ethertype end |
#src ⇒ String
Get MAC source address
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
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.
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 |