class 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
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> ::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
# 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
Converts a binary string to a readable MAC (11:22:33:44:55:66).
irb> ::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
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
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
Getter for the Ethernet destination address.
# File lib/packetfu/protos/eth/header.rb, line 161 def eth_dst; self[:eth_dst].to_s; end
Setter for the Ethernet destination address.
# File lib/packetfu/protos/eth/header.rb, line 159 def eth_dst=(i); typecast(i); end
Getter for the Ethernet protocol number.
# File lib/packetfu/protos/eth/header.rb, line 169 def eth_proto; self[:eth_proto].to_i; end
Setter for the Ethernet protocol number.
# File lib/packetfu/protos/eth/header.rb, line 167 def eth_proto=(i); typecast(i); end
# File lib/packetfu/protos/eth/header.rb, line 242 def eth_proto_readable "0x%04x" % eth_proto end
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
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
Getter for the Ethernet source address.
# File lib/packetfu/protos/eth/header.rb, line 165 def eth_src; self[:eth_src].to_s; end
Setter for the Ethernet source address.
# File lib/packetfu/protos/eth/header.rb, line 163 def eth_src=(i); typecast(i); end
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
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