Class: NetAddr::EUI
- Inherits:
-
Object
- Object
- NetAddr::EUI
- Defined in:
- lib/eui.rb
Overview
EUI - Extended Unique Identifier
A class & series of methods for creating and manipulating Extended Unique Identifier (EUI) addresses. Two types of address formats are supported EUI-48 and EUI-64. The most common use for this class will be to manipulate MAC addresses (which are essentially a type of EUI-48 address).
EUI addresses are separated into two parts, the Organizationally Unique Identifier (OUI) and the Extended Identifier (EI). The OUI is assigned by the IEEE and is used to identify a particular hardware manufacturer. The EI is assigned by the hardware manufacturer as a per device unique address.
Probably the most useful feature of this class, and thus the reason it was created, is to help automate certain address assignments within IP. For example, IPv6 Link Local addresses use MAC addresses for IP auto-assignment and multicast MAC addresses are determined based on the multicast IP address.
Class Method Summary collapse
-
.create(eui) ⇒ Object
Synopsis Create a new EUI48 or EUI64 object.
Instance Method Summary collapse
-
#address(options = nil) ⇒ Object
Synopsis Returns EUI address.
-
#ei(options = nil) ⇒ Object
Synopsis Returns Extended Identifier portion of an EUI address (the vendor assigned ID).
-
#link_local(options = nil) ⇒ Object
Synopsis Provide an IPv6 Link Local address based on the current EUI address.
-
#oui(options = nil) ⇒ Object
Synopsis Returns Organizationally Unique Identifier portion of an EUI address (the vendor ID).
Class Method Details
.create(eui) ⇒ Object
Synopsis
Create a new EUI48 or EUI64 object.
addr = NetAddr::EUI.new(‘aa-bb-cc-dd-ee-ff’) addr = NetAddr::EUI.new(‘aa:bb:cc:dd:ee:ff’) addr = NetAddr::EUI.new(‘aabb.ccdd.eeff’) addr = NetAddr::EUI.new(‘aa-bb-cc-dd-ee-ff-00-01’)
Arguments
-
EUI as a String
Returns
-
EUI48 or EUI64 object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/eui.rb', line 48 def EUI.create(eui) if (!eui.kind_of? String) raise ArgumentError, "Expected String, but #{eui.class} provided." end # validate NetAddr.validate_eui(eui) # remove formatting characters eui.gsub!(/[\.\:\-]/, '') # split into oui & ei, pack, and store if (eui.length == 12) eui = NetAddr::EUI48.new(eui.to_i(16)) else eui = NetAddr::EUI64.new(eui.to_i(16)) end return(eui) end |
Instance Method Details
#address(options = nil) ⇒ Object
Synopsis
Returns EUI address. The default address format is xxxx.xxxx.xxxx
puts addr.address(:Delimiter => ‘.’) –> ‘aabb.ccdd.eeff’
Arguments:
-
Optional Hash with the following fields:
:Delimiter -- delimitation character. valid values are (-,:,and .) (optional)
Returns:
-
String
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/eui.rb', line 85 def address(=nil) known_args = [:Delimiter] delimiter = '-' octets = [] octets.concat(unpack_oui) octets.concat(unpack_ei) if () if (!.kind_of? Hash) raise ArgumentError, "Expected Hash, but #{.class} provided." end NetAddr.validate_args(.keys,known_args) if (.has_key?(:Delimiter)) delimiter = [:Delimiter] delimiter = '-' if (delimiter != '-' && delimiter != ':' && delimiter != '.' ) end end if (delimiter == '-' || delimiter == ':') address = octets.join(delimiter) elsif (delimiter == '.') toggle = 0 octets.each do |x| if (!address) address = x toggle = 1 elsif (toggle == 0) address = address << '.' << x toggle = 1 else address = address << x toggle = 0 end end end return(address) end |
#ei(options = nil) ⇒ Object
Synopsis
Returns Extended Identifier portion of an EUI address (the vendor assigned ID). The default address format is xx-xx-xx
puts addr.ei(:Delimiter => ‘-’)
Arguments:
-
Optional Hash with the following fields:
:Delimiter -- delimitation character. valid values are (-, and :) (optional)
Returns:
-
String
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/eui.rb', line 144 def ei(=nil) known_args = [:Delimiter] octets = unpack_ei() delimiter = '-' if () if (!.kind_of? Hash) raise ArgumentError, "Expected Hash, but #{.class} provided." end NetAddr.validate_args(.keys,known_args) if (.has_key?(:Delimiter)) if ([:Delimiter] == ':') delimiter = [:Delimiter] end end end ei = octets.join(delimiter) return(ei) end |
#link_local(options = nil) ⇒ Object
Synopsis
Provide an IPv6 Link Local address based on the current EUI address.
puts addr.link_local()
Arguments:
-
Optional Hash with the following fields:
:Short -- if true, return IPv6 addresses in short-hand notation (optional) :Objectify -- if true, return CIDR objects (optional)
Returns:
-
CIDR address String or an NetAddr::CIDR object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/eui.rb', line 183 def link_local(=nil) known_args = [:Short, :Objectify] objectify = false short = false if () if (!.kind_of? Hash) raise ArgumentError, "Expected Hash, but #{.class} provided." end NetAddr.validate_args(.keys,known_args) if (.has_key?(:Objectify) && [:Objectify] == true) objectify = true end if (.has_key?(:Short) && [:Short] == true) short = true end end if (self.kind_of?(NetAddr::EUI64)) link_local = @ei | (@oui << 40) else link_local = @ei | 0xfffe000000 | (@oui << 40) end link_local = link_local | (0xfe80 << 112) if (!objectify) link_local = NetAddr.unpack_ip_addr(link_local, :Version => 6) link_local = NetAddr.shorten(link_local) if (short) else link_local = NetAddr::CIDR.create(link_local, :Version => 6) end return(link_local) end |
#oui(options = nil) ⇒ Object
Synopsis
Returns Organizationally Unique Identifier portion of an EUI address (the vendor ID). The default address format is xx-xx-xx.
puts addr.oui(:Delimiter => ‘-’)
Arguments:
-
Optional Hash with the following fields:
:Delimiter -- delimitation character. valid values are (-, and :) (optional)
Returns:
-
String
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/eui.rb', line 237 def oui(=nil) known_args = [:Delimiter] octets = unpack_oui() delimiter = '-' if () if (!.kind_of? Hash) raise ArgumentError, "Expected Hash, but #{.class} provided." end NetAddr.validate_args(.keys,known_args) if (.has_key?(:Delimiter)) if ([:Delimiter] == ':') delimiter = [:Delimiter] end end end oui = octets.join(delimiter) return(oui) end |