Module: IPAddress
- Defined in:
- lib/ipaddress.rb,
lib/ipaddress/ipv4.rb,
lib/ipaddress/ipv6.rb,
lib/ipaddress/prefix.rb
Defined Under Namespace
Classes: IPv4, IPv6, Prefix, Prefix128, Prefix32
Constant Summary collapse
- NAME =
"IPAddress"
- GEM =
"ipaddress"
- AUTHORS =
["Marco Ceresa <[email protected]>"]
Class Method Summary collapse
-
.deprecate(message = nil) ⇒ Object
Deprecate method.
-
.parse(str) ⇒ Object
Parse the argument string to create a new IPv4, IPv6 or Mapped IP object.
-
.valid?(addr) ⇒ Boolean
Checks if the given string is a valid IP address, either IPv4 or IPv6.
-
.valid_ipv4?(addr) ⇒ Boolean
Checks if the given string is a valid IPv4 address.
-
.valid_ipv4_netmask?(addr) ⇒ Boolean
Checks if the argument is a valid IPv4 netmask expressed in dotted decimal format.
-
.valid_ipv6?(addr) ⇒ Boolean
Checks if the given string is a valid IPv6 address.
Instance Method Summary collapse
-
#ipv4? ⇒ Boolean
True if the object is an IPv4 address.
-
#ipv6? ⇒ Boolean
True if the object is an IPv6 address.
Class Method Details
.deprecate(message = nil) ⇒ Object
Deprecate method
149 150 151 152 |
# File 'lib/ipaddress.rb', line 149 def self.deprecate( = nil) # :nodoc: ||= "You are using deprecated behavior which will be removed from the next major or minor release." warn("DEPRECATION WARNING: #{}") end |
.parse(str) ⇒ Object
Parse the argument string to create a new IPv4, IPv6 or Mapped IP object
ip = IPAddress.parse "172.16.10.1/24"
ip6 = IPAddress.parse "2001:db8::8:800:200c:417a/64"
ip_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
ip_mapped.class
#=> IPAddress::IPv6::Mapped
42 43 44 45 46 47 48 49 |
# File 'lib/ipaddress.rb', line 42 def IPAddress::parse(str) case str when /:.+\./ IPAddress::IPv6::Mapped.new(str) else IPAddress::IPv4.new(str) rescue IPAddress::IPv6.new(str) end end |
.valid?(addr) ⇒ Boolean
87 88 89 |
# File 'lib/ipaddress.rb', line 87 def self.valid?(addr) valid_ipv4?(addr) || valid_ipv6?(addr) end |
.valid_ipv4?(addr) ⇒ Boolean
102 103 104 105 106 107 |
# File 'lib/ipaddress.rb', line 102 def self.valid_ipv4?(addr) if /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/ =~ addr return $~.captures.all? {|i| i.to_i < 256} end false end |
.valid_ipv4_netmask?(addr) ⇒ Boolean
Checks if the argument is a valid IPv4 netmask expressed in dotted decimal format.
IPAddress.valid_ipv4_netmask? "255.255.0.0"
#=> true
116 117 118 119 120 121 |
# File 'lib/ipaddress.rb', line 116 def self.valid_ipv4_netmask?(addr) arr = addr.split(".").map{|i| i.to_i}.pack("CCCC").unpack("B*").first.scan(/01/) arr.empty? && valid_ipv4?(addr) rescue return false end |
.valid_ipv6?(addr) ⇒ Boolean
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ipaddress.rb', line 134 def self.valid_ipv6?(addr) # IPv6 (normal) return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*\Z/ =~ addr return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*)?\Z/ =~ addr # IPv6 (IPv4 compat) return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:/ =~ addr && valid_ipv4?($') return true if /\A[\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_ipv4?($') return true if /\A::([\dA-Fa-f]{1,4}(:[\dA-Fa-f]{1,4})*:)?/ =~ addr && valid_ipv4?($') false end |