module Subnet

Overview

TODO Write documentation for Subnet

Direct including types

Defined in:

subnet.cr
subnet/ipv4.cr
subnet/ipv6.cr
subnet/prefix.cr
subnet/version.cr

Constant Summary

VERSION = "0.1.0"

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(value : JSON::PullParser) : Subnet #

[View source]
def self.parse(str) : Subnet #

Parse the argument string to create a new IPv4, IPv6 or Mapped IP object

ip = Subnet.parse 167837953 # 10.1.1.1
ip = Subnet.parse "172.16.10.1/24"
ip6 = Subnet.parse "2001:db8::8:800:200c:417a/64"
ip_mapped = Subnet.parse "::ffff:172.16.10.1/128"

All the object created will be instances of the correct class:

ip.class
# => Subnet::IPv4
ip6.class
# => Subnet::IPv6
ip_mapped.class
# => Subnet::IPv6::Mapped

[View source]

Class Method Detail

def self.deprecate(message = nil) #

Deprecate method :nodoc:


[View source]
def self.ntoa(uint) #

Converts a unit32 to IPv4

Subnet::ntoa(167837953)
  # => "10.1.1.1"

[View source]
def self.valid?(addr) #

Checks if the given string is either a valid IP, either a valid IPv4 subnet

Example:

Subnet::valid? "10.0.0.0/24"
  # => true

Subnet::valid? "2002::1"
  # => true

Subnet::valid? "10.0.0.256"
  # => false

Subnet::valid? "10.0.0.0/999"
  # => false

[View source]
def self.valid_ip?(addr) #

Checks if the given string is a valid IP address, either IPv4 or IPv6

Example:

Subnet::valid_ip? "2002::1"
  # => true

Subnet::valid_ip? "10.0.0.256"
  # => false

[View source]
def self.valid_ipv4?(addr) #

Checks if the given string is a valid IPv4 address

Example:

Subnet::valid_ipv4? "2002::1"
  # => false

Subnet::valid_ipv4? "172.16.10.1"
  # => true

[View source]
def self.valid_ipv4_netmask?(addr) #

Checks if the argument is a valid IPv4 netmask expressed in dotted decimal format.

Subnet.valid_ipv4_netmask? "255.255.0.0"
# => true

[View source]
def self.valid_ipv4_subnet?(addr) #

Checks if the given string is a valid IPv4 subnet

Example:

Subnet::valid_ipv4_subnet? "10.0.0.0/24"
  # => true

Subnet::valid_ipv4_subnet? "10.0.0.0/255.255.255.0"
  # => true

Subnet::valid_ipv4_subnet? "10.0.0.0/64"
  # => false

[View source]
def self.valid_ipv6?(addr) #

Checks if the given string is a valid IPv6 address

Example:

Subnet::valid_ipv6? "2002::1"
  # => true

Subnet::valid_ipv6? "2002::DEAD::BEEF"
  # => false

[View source]
def self.valid_ipv6_subnet?(addr) #

Checks if the given string is a valid IPv6 subnet

Example:

Subnet::valid_ipv6_subnet? "::/0"
  # => true

Subnet::valid_ipv6_subnet? "dead:beef:cafe:babe::/64"
  # => true

Subnet::valid_ipv6_subnet? "2001::1/129"
  # => false

[View source]

Instance Method Detail

def ipv4? #

True if the object is an IPv4 address

ip = Subnet.parse("192.168.10.100/24")

ip.ipv4?
# => true

[View source]
def ipv6? #

True if the object is an IPv6 address

ip = Subnet.parse("192.168.10.100/24")

ip.ipv6?
# => false

[View source]
def to_json(json : JSON::Builder) #

[View source]