formatted
This commit is contained in:
parent
0e4cd32027
commit
97b957742f
3 changed files with 140 additions and 107 deletions
|
|
@ -21,9 +21,10 @@ defmodule Tres.IPv4Address do
|
|||
[^addr] ->
|
||||
{:ok, ipaddr} = addr |> to_charlist |> :inet.parse_address()
|
||||
{ipaddr, {255, 255, 255, 255}}
|
||||
|
||||
[netaddr, cidr_str] ->
|
||||
cidr = String.to_integer(cidr_str)
|
||||
mask = (0xFFFFFFFF >>> (32 - cidr)) <<< (32 - cidr)
|
||||
mask = 0xFFFFFFFF >>> (32 - cidr) <<< (32 - cidr)
|
||||
<<m1, m2, m3, m4>> = <<mask::32>>
|
||||
{:ok, ipaddr} = netaddr |> to_charlist |> :inet.parse_address()
|
||||
{ipaddr, {m1, m2, m3, m4}}
|
||||
|
|
@ -38,7 +39,7 @@ defmodule Tres.IPv4Address do
|
|||
def to_network({{a1, a2, a3, a4}, {m1, m2, m3, m4}}) do
|
||||
addr_int = :binary.decode_unsigned(<<a1, a2, a3, a4>>, :big)
|
||||
mask_int = :binary.decode_unsigned(<<m1, m2, m3, m4>>, :big)
|
||||
<<n1, n2, n3, n4>> = <<(addr_int &&& mask_int)::32>>
|
||||
<<n1, n2, n3, n4>> = <<addr_int &&& mask_int::32>>
|
||||
{n1, n2, n3, n4}
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ defmodule Tres.MacAddress do
|
|||
|> check_format()
|
||||
|> to_a()
|
||||
|> Enum.at(0)
|
||||
|> Kernel.==(0xff)
|
||||
|> Kernel.==(0xFF)
|
||||
rescue
|
||||
_e in ArgumentError ->
|
||||
{:error, :invalid_format}
|
||||
|
|
@ -63,6 +63,7 @@ defmodule Tres.MacAddress do
|
|||
# private functions
|
||||
|
||||
defp to_a(<<>>, acc), do: Enum.reverse(acc)
|
||||
|
||||
defp to_a(<<octet::2-bytes, rest::bytes>>, acc) do
|
||||
to_a(rest, [String.to_integer(octet, 16) | acc])
|
||||
end
|
||||
|
|
@ -81,6 +82,6 @@ defmodule Tres.MacAddress do
|
|||
defp check_format(mac) when is_binary(mac) do
|
||||
if String.match?(mac, @mac_addr_pattern),
|
||||
do: mac,
|
||||
else: raise ArgumentError, message: "MAC address should be 12 letters hex string format"
|
||||
else: raise(ArgumentError, message: "MAC address should be 12 letters hex string format")
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue