EthHeader is a complete Ethernet struct, used in EthPacket. It’s the base header for all other protocols, such as IPHeader, TCPHeader, etc.

For more on the construction on MAC addresses, see en.wikipedia.org/wiki/MAC_address

TODO: Need to come up with a good way of dealing with vlan tagging. Having a usually empty struct member seems weird, but there may not be another way to do it if I want to preserve the Eth-ness of vlan-tagged 802.1Q packets. Also, may as well deal with 0x88a8 as well (en.wikipedia.org/wiki/802.1ad)

Header Definition

 EthMac  :eth_dst                     # See EthMac
 EthMac  :eth_src                     # See EthMac
 Int16   :eth_proto, Default: 0x8000  # IP 0x0800, Arp 0x0806
 String  :body
Methods
E
M
N
R
S
T
Included Modules
Class Public methods
mac2str(mac)

Converts a readable MAC (11:22:33:44:55:66) to a binary string. Readable MAC’s may be split on colons, dots, spaces, or underscores.

irb> PacketFu::EthHeader.mac2str(“11:22:33:44:55:66”)

#=> “021"3DUf“

# File lib/packetfu/protos/eth.rb, line 194
                def self.mac2str(mac)
                        if mac.split(/[:\x2d\x2e\x5f]+/).size == 6
                                ret =       mac.split(/[:\x2d\x2e\x20\x5f]+/).collect {|x| x.to_i(16)}.pack("C6")
                        else
                                raise ArgumentError, "Unkown format for mac address."
                        end
                        return ret
                end
new(args={})
# File lib/packetfu/protos/eth.rb, line 150
                def initialize(args={})
                        super(
                                EthMac.new.read(args[:eth_dst]),
                                EthMac.new.read(args[:eth_src]),
                                Int16.new(args[:eth_proto] || 0x0800),
                                StructFu::String.new.read(args[:body])
                        )
                end
str2mac(mac='')

Converts a binary string to a readable MAC (11:22:33:44:55:66).

irb> PacketFu::EthHeader.str2mac(“x11x22x33x44x55x66”)

#=> “11:22:33:44:55:66“

# File lib/packetfu/protos/eth.rb, line 208
                def self.str2mac(mac='')
                        if mac.to_s.size == 6 && mac.kind_of?(::String)
                                ret = mac.unpack("C6").map {|x| sprintf("%02x",x)}.join(":")
                        end
                end
Instance Public methods
eth_daddr()

Gets the destination MAC address in a more readable way.

This method is also aliased as eth_dst_readable
# File lib/packetfu/protos/eth.rb, line 234
                def eth_daddr
                        EthHeader.str2mac(self[:eth_dst].to_s)
                end
eth_daddr=(mac)

Set the destination MAC address in a more readable way.

# File lib/packetfu/protos/eth.rb, line 227
                def eth_daddr=(mac)
                        mac = EthHeader.mac2str(mac)
                        self[:eth_dst].read mac
                        self[:eth_dst]
                end
eth_dst()

Getter for the Ethernet destination address.

# File lib/packetfu/protos/eth.rb, line 162
                def eth_dst; self[:eth_dst].to_s; end
eth_dst=(i)

Setter for the Ethernet destination address.

# File lib/packetfu/protos/eth.rb, line 160
                def eth_dst=(i); typecast(i); end
eth_dst_readable()

Alias for eth_daddr

eth_proto()

Getter for the Ethernet protocol number.

# File lib/packetfu/protos/eth.rb, line 170
                def eth_proto; self[:eth_proto].to_i; end
eth_proto=(i)

Setter for the Ethernet protocol number.

# File lib/packetfu/protos/eth.rb, line 168
                def eth_proto=(i); typecast(i); end
eth_proto_readable()
# File lib/packetfu/protos/eth.rb, line 243
                def eth_proto_readable
                        "0x%04x" % eth_proto
                end
eth_saddr()

Gets the source MAC address in a more readable way.

This method is also aliased as eth_src_readable
# File lib/packetfu/protos/eth.rb, line 222
                def eth_saddr
                        EthHeader.str2mac(self[:eth_src].to_s)
                end
eth_saddr=(mac)

Sets the source MAC address in a more readable way.

# File lib/packetfu/protos/eth.rb, line 215
                def eth_saddr=(mac)
                        mac = EthHeader.mac2str(mac)
                        self[:eth_src].read mac
                        self[:eth_src]
                end
eth_src()

Getter for the Ethernet source address.

# File lib/packetfu/protos/eth.rb, line 166
                def eth_src; self[:eth_src].to_s; end
eth_src=(i)

Setter for the Ethernet source address.

# File lib/packetfu/protos/eth.rb, line 164
                def eth_src=(i); typecast(i); end
eth_src_readable()

Alias for eth_saddr

read(str)

Reads a string to populate the object.

# File lib/packetfu/protos/eth.rb, line 178
                def read(str)
                        force_binary(str)
                        return self if str.nil?
                        self[:eth_dst].read str[0,6]
                        self[:eth_src].read str[6,6]
                        self[:eth_proto].read str[12,2]
                        self[:body].read str[14,str.size]
                        self
                end
to_s()

Returns the object in string form.

# File lib/packetfu/protos/eth.rb, line 173
                def to_s
                        self.to_a.map {|x| x.to_s}.join
                end