Class: FFI::Packets::Eth::EthAddr

Inherits:
Struct
  • Object
show all
Includes:
DRY::StructHelper
Defined in:
lib/ffi/packets/eth.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ EthAddr

Adds the ability to initialize a new EthAddr with a mac address string such as ‘de:ad:be:ef:ba:be’. This argument is only parsed if it is passed as the only String argument.



14
15
16
17
18
19
20
21
# File 'lib/ffi/packets/eth.rb', line 14

def initialize(*args)
  if args.size == 1 and (s=args[0]).is_a? String
    super()
    self.addr = s
  else
    super(*args)
  end
end

Instance Method Details

#addr=(val) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/ffi/packets/eth.rb', line 23

def addr=(val)
  unless val.to_s =~ /^#{Util::RX_MAC_ADDR}$/
    raise(ArgumentError, "invalid mac address #{val.inspect}")
  end
  raw = Util.unhexify(val, /[:-]/)
  self[:data].to_ptr.write_string(raw, ETH_ADDR_LEN)
end

#charsObject

Returns the MAC address as an array of unsigned char values.



32
# File 'lib/ffi/packets/eth.rb', line 32

def chars; self[:data].to_a ; end

#stringObject

Returns the MAC address as a string with colon-separated hex-bytes.



35
# File 'lib/ffi/packets/eth.rb', line 35

def string; chars.map {|x| "%0.2x" % x }.join(':'); end