module

IPAddress

A Crystal library to manipulate IPv4 and IPv6 addresses.

require "ipaddress"

Constants

VERSION = {{ (`shards version \"/tmp/tmp.nnoGnp/src/src/ipaddress\"`).chomp.stringify }}

Constructors

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

Source
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

Source

Class methods

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
Source
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"
Source
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
Source
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?

Source
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?

Source
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?

Source
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?

Source

Instance methods

ipv4?

Returns true if the object is an IPv4 address.

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

Returns true if the object is an IPv6 address.

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

Nested types