module IPAddress
Overview
A Crystal library to manipulate IPv4 and IPv6 addresses.
require "ipaddress"
Direct including types
Defined in:
ipaddress.cripaddress/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
-
.new(addr : String | Int) : IPAddress
IPAddress.new
is a wrapper method built around IPAddress's library classes. - .parse(addr : String | Int) : IPAddress
Class Method Summary
-
.aton(addr : String) : UInt32
Converts an IPv4 string to
UInt32
. -
.ntoa(uint : UInt32) : String
Converts an
UInt32
to IPv4 string. -
.ntoa(int : Int) : String
Converts an
Int
to IPv4 string, raises otherwise. -
.valid?(addr : String)
Returns
true
if the given string is a valid IP address, either IPv4 or IPv6. -
.valid_ipv4?(addr : String)
Returns
true
if the given string is a valid IPv4 address. -
.valid_ipv4_netmask?(addr : String)
Returns
true
if the argument is a valid IPv4 netmask expressed in dotted decimal format. -
.valid_ipv6?(addr : String)
Returns
true
if the given string is a valid IPv6 address.
Instance Method Summary
-
#ipv4?
Returns
true
if the object is anIPv4
address. -
#ipv6?
Returns
true
if the object is anIPv6
address.
Constructor Detail
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
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
Class Method Detail
Converts an IPv4 string to UInt32
.
IPAddress.aton "10.1.1.1" # => 167837953_u32
IPAddress.aton "0.0.0.0" # => 0_u32
Converts an UInt32
to IPv4 string.
IPAddress.ntoa 167837953_u32 # => "10.1.1.1"
IPAddress.ntoa 0_u32 # => "0.0.0.0"
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
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?
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?
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?
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?
Instance Method Detail
Returns true
if the object is an IPv4
address.
ip = IPAddress.new "192.168.10.100/24"
ip.ipv4? # => true