| Module | Racket::L2::Misc |
| In: |
lib/racket/l2/misc.rb
(CVS)
|
Miscelaneous L2 helper methods
given a long representing a MAC address print it out in human readable form of a given length, defaulting to 6 (ethernet)
# File lib/racket/l2/misc.rb, line 56 def Misc.long2mac(long, len=6) long.to_s(16).rjust(len*2, '0').unpack("a2"*len).join(":") end
given a MAC address, return the long representation
# File lib/racket/l2/misc.rb, line 45 def Misc.mac2long(addr) long = 0 addr.split(':').map { |s| s.to_i(16) }.each do |o| long = (long << 8) ^ o end long end
given a MAC address, return the string representation
# File lib/racket/l2/misc.rb, line 40 def Misc.mac2string(mac) mac.split(":").map { |i| i.hex.chr }.join end
Return a random MAC, defaults to 6 bytes (ethernet)
# File lib/racket/l2/misc.rb, line 61 def Misc.randommac(len=6) long2mac(rand(2**(8*len)), len) end