Skip to content

CIDR library for Elixir that is compatible with Erlang's :inet and supports both IPv4 and IPv6

License

Notifications You must be signed in to change notification settings

mrinalwadhwa/inet_cidr

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

InetCidr

Classless Inter-Domain Routing (CIDR) library for Elixir

This library has the following features:

  • Compatible with Erlang's :inet module
  • Supports IPv4 and IPv6

Install

defp deps do
  [
    {:inet_cidr, "~> 1.0.0"}
  ]
end

Usage

Parsing a CIDR string

iex(1)> InetCidr.parse("192.168.0.0/16")
{{192,168,0,0}, {192,168,255,255}, 16}

iex(2)> InetCidr.parse("2001:abcd::/32")
{{8193, 43981, 0, 0, 0, 0, 0, 0}, {8193, 43981, 65535, 65535, 65535, 65535, 65535, 65535}, 32}

Printing a CIDR block to string

iex(1)> "192.168.0.0/16" |> InetCidr.parse |> InetCidr.to_string
"192.168.0.0/16"

iex(2)> "2001:abcd::/32" |> InetCidr.parse |> InetCidr.to_string
"2001:ABCD::/32"

Check whether a CIDR block contains an IP address

iex(1)> cidr = InetCidr.parse("192.168.0.0/16")
{{192,168,0,0}, {192,168,255,255}, 16}

iex(2)> address1 = InetCidr.parse_address!("192.168.15.20")
{192,168,15,20}

iex(3)> InetCidr.contains?(cidr, address1)
true

iex(4)> address2 = InetCidr.parse_address!("10.168.15.20")
{10,168,15,20}

iex(5)> InetCidr.contains?(cidr, address2)
false
iex(1)> cidr = InetCidr.parse("2001:abcd::/32")
{{8193, 43981, 0, 0, 0, 0, 0, 0}, {8193, 43981, 65535, 65535, 65535, 65535, 65535, 65535}, 32}

iex(2)> address1 = InetCidr.parse_address!("2001:abcd::")
{8193, 43981, 0, 0, 0, 0, 0, 0}

iex(3)> InetCidr.contains?(cidr, address1)
true

iex(4)> address2 = InetCidr.parse_address!("abcd:2001::")
{43981, 8193, 0, 0, 0, 0, 0, 0}

iex(5)> InetCidr.contains?(cidr, address2)
false

License

LICENSE

About

CIDR library for Elixir that is compatible with Erlang's :inet and supports both IPv4 and IPv6

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Elixir 100.0%