Mac (net_address v0.3.1) View Source

Mac Address value tool module.

Link to this section Summary

Types

t()

general mac addresses are six octets.

Functions

converts a mac address string and turns it into a proper mac address datatype. Supports standard colon format, BMC hyphen format, and arista/cisco switch dot format.

checks to see if a value is a local mac address, by convention it means it has been assigned by a VM system.

checks to see if a value is a mac address.

checks to see if a value is a local mac address, by convention it means it has been assigned by the hardware manufacturer

generates a mask for the first n bits of the mac address.

generates a random mac address from another, with a mask value

allows you to use the convenient ~m sigil to declare a Mac address in its normal string form, instead of using the tuple form.

Converts a mac address to a string

Link to this section Types

Specs

t() :: {byte(), byte(), byte(), byte(), byte(), byte()}

general mac addresses are six octets.

Link to this section Functions

Specs

from_string(String.t()) :: {:ok, t()} | {:error, term()}

converts a mac address string and turns it into a proper mac address datatype. Supports standard colon format, BMC hyphen format, and arista/cisco switch dot format.

iex> Mac.from_string!("06:AA:07:FB:B6:1E")
{0x06, 0xAA, 0x07, 0xFB, 0xB6, 0x1E}

iex> Mac.from_string!("06:aa:07:fb:b6:1e")
{0x06, 0xAA, 0x07, 0xFB, 0xB6, 0x1E}

iex> Mac.from_string!("06-AA-07-FB-B6-1E")
{0x06, 0xAA, 0x07, 0xFB, 0xB6, 0x1E}

iex> Mac.from_string!("06aa.07fb.b61e")
{0x06, 0xAA, 0x07, 0xFB, 0xB6, 0x1E}
Link to this function

from_string!(mac_string)

View Source

Specs

from_string!(String.t()) :: t()
Link to this macro

is_local_mac(mac_addr)

View Source (macro)

Specs

is_local_mac(any()) :: Macro.t()

checks to see if a value is a local mac address, by convention it means it has been assigned by a VM system.

usable in guards.

iex> Mac.is_local_mac({0xF2, 0, 0, 1, 1, 1})
true
iex> Mac.is_local_mac({0xF0, 0, 0, 1, 1, 1})
false
Link to this macro

is_mac(mac_addr)

View Source (macro)

Specs

is_mac(any()) :: Macro.t()

checks to see if a value is a mac address.

usable in guards.

iex> Mac.is_mac({10, 0, 0, 1, 1, 1})
true
iex> Mac.is_mac(:foo)
false
iex> Mac.is_mac({0x100, 0, 0, 1, 1, 1})
false
iex> Mac.is_mac({10, 0, 0, 1, 1, 1, 1})
false
Link to this macro

is_universal_mac(mac_addr)

View Source (macro)

Specs

is_universal_mac(any()) :: Macro.t()

checks to see if a value is a local mac address, by convention it means it has been assigned by the hardware manufacturer

usable in guards.

iex> Mac.is_universal_mac({0xF2, 0, 0, 1, 1, 1})
false
iex> Mac.is_universal_mac({0xF0, 0, 0, 1, 1, 1})
true
Link to this function

mask(bits, mode \\ :mac)

View Source

Specs

mask(0..48, :integer) :: 0..281_474_976_710_655
mask(0..48, :binary) :: <<_::48>>
mask(0..48, :mac) :: t()

generates a mask for the first n bits of the mac address.

iex> Mac.mask(1)
{0x80, 0, 0, 0, 0, 0}
iex> Mac.mask(16)
{0xFF, 0xFF, 0, 0, 0, 0}

you may pass another mode to the second parameter for other formats.

iex> Mac.mask(16, :binary)
<<0xFF, 0xFF, 0, 0, 0, 0>>
iex> Mac.mask(16, :integer)
0xFFFF_0000_0000
Link to this function

random(src \\ nil, bits \\ 48)

View Source

Specs

random(nil, 48) :: t()
random(t(), 0..48) :: t()

generates a random mac address from another, with a mask value

Link to this macro

sigil_m(arg, _)

View Source (macro)

Specs

sigil_m(Macro.t(), [byte()]) :: Macro.t()

allows you to use the convenient ~m sigil to declare a Mac address in its normal string form, instead of using the tuple form.

iex> import Mac
iex> ~m"06:66:F4:12:34:56"
{0x06, 0x66, 0xF4, 0x12, 0x34, 0x56}

Specs

to_string(t()) :: String.t()

Converts a mac address to a string

iex> Mac.to_string({255, 255, 255, 255, 255, 255})
"FF:FF:FF:FF:FF:FF"

iex> Mac.to_string({6, 1, 2, 3, 4, 5})
"06:01:02:03:04:05"