lib/macaddr.rb in macaddr-1.7.0 vs lib/macaddr.rb in macaddr-1.7.1

- old
+ new

@@ -22,11 +22,11 @@ require 'systemu' require 'socket' module Mac - VERSION = '1.7.0' + VERSION = '1.7.1' def Mac.version ::Mac::VERSION end @@ -73,19 +73,33 @@ raise "all of #{ cmds.join ' ' } failed" unless output @mac_address = parse(output) end + link = Socket::PF_LINK if Socket.const_defined? :PF_LINK + packet = Socket::PF_PACKET if Socket.const_defined? :PF_PACKET + INTERFACE_PACKET_FAMILY = link || packet # :nodoc: + def from_getifaddrs return unless Socket.respond_to? :getifaddrs + interfaces = Socket.getifaddrs.select do |addr| + addr.addr.pfamily == INTERFACE_PACKET_FAMILY + end + mac, = - Socket.getifaddrs.select do |addr| - addr.addr.pfamily == Socket::PF_LINK - end.map do |addr| - addr.addr.getnameinfo - end.find do |m,| - !m.empty? + if Socket.const_defined? :PF_LINK then + interfaces.map do |addr| + addr.addr.getnameinfo + end.find do |m,| + !m.empty? + end + elsif Socket.const_defined? :PF_PACKET then + interfaces.map do |addr| + addr.addr.inspect_sockaddr[/hwaddr=([\h:]+)/, 1] + end.find do |mac_addr| + mac_addr != '00:00:00:00:00:00' + end end @mac_address = mac if mac end