Giter Site home page Giter Site logo

mrinalwadhwa / inet_cidr Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cobenian/inet_cidr

0.0 2.0 0.0 131 KB

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

License: Apache License 2.0

Elixir 100.00%

inet_cidr's Introduction

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

inet_cidr's People

Contributors

brweber2 avatar alco avatar

Watchers

Mrinal Wadhwa avatar James Cloos avatar

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.