Code Monkey home page Code Monkey logo

crystal-ipaddr's Introduction

IPAddr

IP address handling library for Crystal language.

Installation

Add this to your application's shard.yml:

dependencies:
  ipaddr:
    github: arcage/crystal-ipaddr

Usage

require "ipaddr"

IP address object

ipv4 = IPAddr.new("169.254.10.1")
# => #<IPAddr:IPv4 "169.254.10.1">

ipv6 = IPAddr.new("2001:db8::deca:face")
# => #<IPAddr:IPv6 "2001:0db8:0000:0000:0000:0000:deca:face">

IPv4 compatible/mapped IPv6 address

v4compat = ipv4.to_ipv4_compat
# => #<IPAddr:IPv6 "0000:0000:0000:0000:0000:0000:a9fe:0a01">

v4mapped = ipv4.to_ipv4_mapped
# => #<IPAddr:IPv6 "0000:0000:0000:0000:0000:ffff:a9fe:0a01">

v4compat.native_ipv4
# => #<IPAddr:IPv4 "169.254.10.1">

v4mapped.native_ipv4
# => #<IPAddr:IPv4 "169.254.10.1">

Network address

nw  = IPAddr::NetworkAddr.new(ipv4, 16)
# => #<IPAddr::NetworkAddr:IPv4 "169.254.0.0/16">

nw2 = IPAddr::NetworkAddr.new("2001:db8::beaf:cafe", 120)
# => #<IPAddr::NetworkAddr:IPv6 "2001:0db8:0000:0000:0000:0000:beaf:ca00/120">

nw3 = IPAddr::NetworkAddr.new("192.168.20.2/255.255.255.0")
# => #<IPAddr::NetworkAddr:IPv4 "192.168.20.0/24">

nw4 = IPAddr::NetworkAddr.new("2001:db8::beaf:cafe/120")
# => #<IPAddr::NetworkAddr:IPv6 "2001:0db8:0000:0000:0000:0000:beaf:ca00/120">


nw.begin
# => #<IPAddr:IPv4 "169.254.0.0">

nw.end
# => #<IPAddr:IPv4 "169.254.255.255">

nw.includes?(ipv4)
# => true

Contributing

  1. Fork it ( https://github.com/arcage/ipaddr/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • arcage ʕ·ᴥ·ʔAKJ - creator, maintainer

crystal-ipaddr's People

Contributors

arcage avatar foxxx0 avatar

Watchers

 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.