module IPAddress

Overview

A Crystal library to manipulate IPv4 and IPv6 addresses.

require "ipaddress"

Direct including types

Defined in:

ipaddress.cr
ipaddress/ipv4.cr
ipaddress/ipv6.cr
ipaddress/ipv6_loopback.cr
ipaddress/ipv6_mapped.cr
ipaddress/ipv6_unspecified.cr
ipaddress/prefix.cr
ipaddress/version.cr

Constant Summary

VERSION = {{ (`shards version \"/srv/crystaldoc.info/github-xtokio-ipaddress-master/src/ipaddress\"`).chomp.stringify }}

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(addr : String | Int) : IPAddress #

IPAddress.new is a wrapper method built around IPAddress's library classes. Its purpose is to make you independent from the type of IP address you're going to use.

For example, instead of creating the three types of IP addresses using their own constructors:

ip = IPAddress::IPv4.new "172.16.10.1/24"
ip6 = IPAddress::IPv6.new "2001:db8::8:800:200c:417a/64"
ip6_mapped = IPAddress::IPv6::Mapped.new "::ffff:172.16.10.1/128"

you can just use the IPAddress.new wrapper:

ip = IPAddress.new "172.16.10.1/24"
ip6 = IPAddress.new "2001:db8::8:800:200c:417a/64"
ip6_mapped = IPAddress.new "::ffff:172.16.10.1/128"

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

ip.class         # => IPAddress::IPv4
ip6.class        # => IPAddress::IPv6
ip6_mapped.class # => IPAddress::IPv6::Mapped

See also #parse


[View source]
def self.parse(addr : String | Int) : IPAddress #

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

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

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

ip.class         # => IPAddress::IPv4
ip6.class        # => IPAddress::IPv6
ip6_mapped.class # => IPAddress::IPv6::Mapped

See also #new, #ntoa


[View source]

Class Method Detail

def self.aton(addr : String) : UInt32 #

Converts an IPv4 string to UInt32.

IPAddress.aton "10.1.1.1" # => 167837953_u32
IPAddress.aton "0.0.0.0"  # => 0_u32

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

Converts an UInt32 to IPv4 string.

IPAddress.ntoa 167837953_u32 # => "10.1.1.1"
IPAddress.ntoa 0_u32         # => "0.0.0.0"

[View source]
def self.ntoa(int : Int) : String #

Converts an Int to IPv4 string, raises otherwise.

IPAddress.ntoa 167837953 # => "10.1.1.1"
IPAddress.ntoa 0         # => "0.0.0.0"
IPAddress.ntoa -1        # raises ArgumentError

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

Returns true if the given string is a valid IP address, either IPv4 or IPv6.

IPAddress.valid? "10.0.0.256" # => false
IPAddress.valid? "2002::1"    # => true

See also #valid_ipv4?, #valid_ipv6?


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

Returns true if the given string is a valid IPv4 address.

IPAddress.valid_ipv4? "172.16.10.1" # => true
IPAddress.valid_ipv4? "2002::1"     # => false

NOTE Alias for IPAddress::IPv4.valid?


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

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

IPAddress.valid_ipv4_netmask? "255.255.0.0" # => true

NOTE Alias for IPAddress::IPv4.valid_netmask?


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

Returns true if the given string is a valid IPv6 address.

IPAddress.valid_ipv6? "2002::1"          # => true
IPAddress.valid_ipv6? "2002::DEAD::BEEF" # => false

NOTE Alias for IPAddress::IPv6.valid?


[View source]

Instance Method Detail

def ipv4? #

Returns true if the object is an IPv4 address.

ip = IPAddress.new "192.168.10.100/24"
ip.ipv4? # => true

[View source]
def ipv6? #

Returns true if the object is an IPv6 address.

ip = IPAddress.new "192.168.10.100/24"
ip.ipv6? # => false

[View source]