Giter Site home page Giter Site logo

cidr-elixir's Introduction

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."}

Contribution

See Collective Code Construction Contract (C4)

License

Mozilla Public License 2.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.