ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - c-rack/cidr-elixir: Classless Inter-Domain Routing (CIDR) for Elixir
Classless Inter-Domain Routing (CIDR) for Elixir. Contribute to c-rack/cidr-elixir development by creating an account on GitHub.
Visit Site

GitHub - c-rack/cidr-elixir: Classless Inter-Domain Routing (CIDR) for Elixir

GitHub - c-rack/cidr-elixir: Classless Inter-Domain Routing (CIDR) for Elixir

CIDR

Downloads Hex.pm Version Build Status Coverage Status Inline docs

Classless Inter-Domain Routing (CIDR) utilities for Elixir

Setup

To use this library, just add :cidr as a dependency to your mix.exs file:

defp deps do
  [
    {:cidr, ">= 1.1.0"}
  ]
end

Usage

Passing an IP address string into CIDR.parse returns a %CIDR{} struct. It contains the first and the last address of the range as Erlang IP tuples, the amount of hosts the range covers and the network mask.

iex(1)> cidr = "1.2.3.4/24" |> CIDR.parse
%CIDR{first: {1, 2, 3, 0}, last: {1, 2, 3, 255}, hosts: 256, mask: 24}

You can query the struct for all of its fields:

iex(2)> cidr.first
{1, 2, 3, 0}
iex(3)> cidr.hosts
256

And use it to see if other IP addresses fall in the same range:

iex(4)> cidr |> CIDR.match!({1,2,3,100})
true
iex(5)> cidr |> CIDR.match!({1,2,4,1})
false

The match!/2 function also supports IP strings:

iex(6)> cidr |> CIDR.match!("1.2.3.100")
true
iex(7)> cidr |> CIDR.match!("1.2.4.1")
false

Please note that match!/2 throws an ArgumentError when you pass in a value that does not represent a valid IP address or when you try to match an IPv4 address with an IPv6 range and vice-versa. We also provide match/2, a non-throwing version that returns tagged tuples:

iex(8)> cidr |> CIDR.match("1.2.3.100")
{:ok, true}
iex(9)> cidr |> CIDR.match("1.2.4.1")
{:ok, false}
iex(10)> cidr |> CIDR.match("1.2.3.1000")
{:error, "Tuple is not a valid IP address."}

IPv4 deaggregation works as well. It returns a list of the largest possible networks for an IPv4 range in the format of $first_ip-$last_ip:

iex(1)> CIDR.parse_range("192.168.1.0-192.168.2.0")
[
  %CIDR{first: {192, 168, 1, 0}, hosts: 256, last: {192, 168, 1, 255}, mask: 24},
  %CIDR{first: {192, 168, 2, 0}, hosts: 1, last: {192, 168, 2, 0}, mask: 32}
]

Contribution

See Collective Code Construction Contract (C4)

License

Mozilla Public License 2.0

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory