Parent

Included Modules

Class/Module Index [+]

Quicksearch

PacketFu::EthHeader

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

Public Class Methods

mac2str(mac) click to toggle source

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/header.rb, line 193
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={}) click to toggle source
# File lib/packetfu/protos/eth/header.rb, line 149
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='') click to toggle source

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/header.rb, line 207
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

Public Instance Methods

eth_daddr() click to toggle source

Gets the destination MAC address in a more readable way.

# File lib/packetfu/protos/eth/header.rb, line 233
def eth_daddr
        EthHeader.str2mac(self[:eth_dst].to_s)
end
Also aliased as: eth_dst_readable
eth_daddr=(mac) click to toggle source

Set the destination MAC address in a more readable way.

# File lib/packetfu/protos/eth/header.rb, line 226
def eth_daddr=(mac)
        mac = EthHeader.mac2str(mac)
        self[:eth_dst].read mac
        self[:eth_dst]
end
eth_dst() click to toggle source

Getter for the Ethernet destination address.

# File lib/packetfu/protos/eth/header.rb, line 161
def eth_dst; self[:eth_dst].to_s; end
eth_dst=(i) click to toggle source

Setter for the Ethernet destination address.

# File lib/packetfu/protos/eth/header.rb, line 159
def eth_dst=(i); typecast(i); end
eth_dst_readable() click to toggle source

Readability aliases

Alias for: eth_daddr
eth_proto() click to toggle source

Getter for the Ethernet protocol number.

# File lib/packetfu/protos/eth/header.rb, line 169
def eth_proto; self[:eth_proto].to_i; end
eth_proto=(i) click to toggle source

Setter for the Ethernet protocol number.

# File lib/packetfu/protos/eth/header.rb, line 167
def eth_proto=(i); typecast(i); end
eth_proto_readable() click to toggle source
# File lib/packetfu/protos/eth/header.rb, line 242
def eth_proto_readable
        "0x%04x" % eth_proto
end
eth_saddr() click to toggle source

Gets the source MAC address in a more readable way.

# File lib/packetfu/protos/eth/header.rb, line 221
def eth_saddr
        EthHeader.str2mac(self[:eth_src].to_s)
end
Also aliased as: eth_src_readable
eth_saddr=(mac) click to toggle source

Sets the source MAC address in a more readable way.

# File lib/packetfu/protos/eth/header.rb, line 214
def eth_saddr=(mac)
        mac = EthHeader.mac2str(mac)
        self[:eth_src].read mac
        self[:eth_src]
end
eth_src() click to toggle source

Getter for the Ethernet source address.

# File lib/packetfu/protos/eth/header.rb, line 165
def eth_src; self[:eth_src].to_s; end
eth_src=(i) click to toggle source

Setter for the Ethernet source address.

# File lib/packetfu/protos/eth/header.rb, line 163
def eth_src=(i); typecast(i); end
eth_src_readable() click to toggle source
Alias for: eth_saddr
read(str) click to toggle source

Reads a string to populate the object.

# File lib/packetfu/protos/eth/header.rb, line 177
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() click to toggle source

Returns the object in string form.

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.