Class: IPAddr
Overview
IPAddr provides a set of methods to manipulate an IP address. Both IPv4 and IPv6 are supported.
Example
require 'ipaddr'
ipaddr1 = IPAddr.new "3ffe:505:2::1"
p ipaddr1 #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0001/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
p ipaddr1.to_s #=> "3ffe:505:2::1"
ipaddr2 = ipaddr1.mask(48) #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0000/ffff:ffff:ffff:0000:0000:0000:0000:0000>
p ipaddr2.to_s #=> "3ffe:505:2::"
ipaddr3 = IPAddr.new "192.168.2.0/24"
p ipaddr3 #=> #<IPAddr: IPv4:192.168.2.0/255.255.255.0>
Defined Under Namespace
Classes: AddressFamilyError, Error, InvalidAddressError, InvalidPrefixError
Constant Summary collapse
- IN4MASK =
32 bit mask for IPv4
0xffffffff
- IN6MASK =
128 bit mask for IPv6
0xffffffffffffffffffffffffffffffff
- IN6FORMAT =
Format string for IPv6
(["%.4x"] * 8).join(':')
- RE_IPV4ADDRLIKE =
Regexp internally used for parsing IPv4 address.
%r{ \A (\d+) \. (\d+) \. (\d+) \. (\d+) \z }x
- RE_IPV6ADDRLIKE_FULL =
Regexp internally used for parsing IPv6 address.
%r{ \A (?: (?: [\da-f]{1,4} : ){7} [\da-f]{1,4} | ( (?: [\da-f]{1,4} : ){6} ) (\d+) \. (\d+) \. (\d+) \. (\d+) ) \z }xi
- RE_IPV6ADDRLIKE_COMPRESSED =
Regexp internally used for parsing IPv6 address.
%r{ \A ( (?: (?: [\da-f]{1,4} : )* [\da-f]{1,4} )? ) :: ( (?: ( (?: [\da-f]{1,4} : )* ) (?: [\da-f]{1,4} | (\d+) \. (\d+) \. (\d+) \. (\d+) ) )? ) \z }xi
Instance Attribute Summary collapse
-
#family ⇒ Object
readonly
Returns the address family of this IP address.
Class Method Summary collapse
-
.new_ntoh(addr) ⇒ Object
Creates a new ipaddr containing the given network byte ordered string form of an IP address.
-
.ntop(addr) ⇒ Object
Convert a network byte ordered string form of an IP address into human readable form.
Instance Method Summary collapse
-
#&(other) ⇒ Object
Returns a new ipaddr built by bitwise AND.
-
#<<(num) ⇒ Object
Returns a new ipaddr built by bitwise left shift.
-
#<=>(other) ⇒ Object
Compares the ipaddr with another.
-
#==(other) ⇒ Object
Returns true if two ipaddrs are equal.
-
#>>(num) ⇒ Object
Returns a new ipaddr built by bitwise right-shift.
-
#eql?(other) ⇒ Boolean
Checks equality used by Hash.
-
#hash ⇒ Object
Returns a hash value used by Hash, Set, and Array classes.
-
#hton ⇒ Object
Returns a network byte ordered string form of the IP address.
-
#include?(other) ⇒ Boolean
(also: #===)
Returns true if the given ipaddr is in the range.
-
#inspect ⇒ Object
Returns a string containing a human-readable representation of the ipaddr.
-
#ip6_arpa ⇒ Object
Returns a string for DNS reverse lookup compatible with RFC3172.
-
#ip6_int ⇒ Object
Returns a string for DNS reverse lookup compatible with RFC1886.
-
#ipv4? ⇒ Boolean
Returns true if the ipaddr is an IPv4 address.
-
#ipv4_compat ⇒ Object
Returns a new ipaddr built by converting the native IPv4 address into an IPv4-compatible IPv6 address.
-
#ipv4_compat? ⇒ Boolean
Returns true if the ipaddr is an IPv4-compatible IPv6 address.
-
#ipv4_mapped ⇒ Object
Returns a new ipaddr built by converting the native IPv4 address into an IPv4-mapped IPv6 address.
-
#ipv4_mapped? ⇒ Boolean
Returns true if the ipaddr is an IPv4-mapped IPv6 address.
-
#ipv6? ⇒ Boolean
Returns true if the ipaddr is an IPv6 address.
-
#mask(prefixlen) ⇒ Object
Returns a new ipaddr built by masking IP address with the given prefixlen/netmask.
-
#native ⇒ Object
Returns a new ipaddr built by converting the IPv6 address into a native IPv4 address.
-
#prefix ⇒ Object
Returns the prefix length in bits for the ipaddr.
-
#prefix=(prefix) ⇒ Object
Sets the prefix length in bits.
-
#reverse ⇒ Object
Returns a string for DNS reverse lookup.
-
#succ ⇒ Object
Returns the successor to the ipaddr.
-
#to_i ⇒ Object
Returns the integer representation of the ipaddr.
-
#to_range ⇒ Object
Creates a Range object for the network address.
-
#to_s ⇒ Object
Returns a string containing the IP address representation.
-
#to_string ⇒ Object
Returns a string containing the IP address representation in canonical form.
-
#|(other) ⇒ Object
Returns a new ipaddr built by bitwise OR.
-
#~ ⇒ Object
Returns a new ipaddr built by bitwise negation.
Instance Attribute Details
#family ⇒ Object (readonly)
Returns the address family of this IP address.
102 103 104 |
# File 'lib/ipaddr.rb', line 102 def family @family end |
Class Method Details
.new_ntoh(addr) ⇒ Object
Creates a new ipaddr containing the given network byte ordered string form of an IP address.
106 107 108 |
# File 'lib/ipaddr.rb', line 106 def IPAddr::new_ntoh(addr) return IPAddr.new(IPAddr::ntop(addr)) end |
.ntop(addr) ⇒ Object
Convert a network byte ordered string form of an IP address into human readable form.
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/ipaddr.rb', line 112 def IPAddr::ntop(addr) case addr.size when 4 s = addr.unpack('C4').join('.') when 16 s = IN6FORMAT % addr.unpack('n8') else raise AddressFamilyError, "unsupported address family" end return s end |
Instance Method Details
#&(other) ⇒ Object
Returns a new ipaddr built by bitwise AND.
125 126 127 |
# File 'lib/ipaddr.rb', line 125 def &(other) return self.clone.set(@addr & coerce_other(other).to_i) end |
#<<(num) ⇒ Object
Returns a new ipaddr built by bitwise left shift.
140 141 142 |
# File 'lib/ipaddr.rb', line 140 def <<(num) return self.clone.set(addr_mask(@addr << num)) end |
#<=>(other) ⇒ Object
Compares the ipaddr with another.
339 340 341 342 343 344 345 |
# File 'lib/ipaddr.rb', line 339 def <=>(other) other = coerce_other(other) rescue nil else @addr <=> other.to_i if other.family == @family end |
#==(other) ⇒ Object
Returns true if two ipaddrs are equal.
150 151 152 153 154 155 156 |
# File 'lib/ipaddr.rb', line 150 def ==(other) other = coerce_other(other) rescue false else @family == other.family && @addr == other.to_i end |
#>>(num) ⇒ Object
Returns a new ipaddr built by bitwise right-shift.
135 136 137 |
# File 'lib/ipaddr.rb', line 135 def >>(num) return self.clone.set(@addr >> num) end |
#eql?(other) ⇒ Boolean
Checks equality used by Hash.
349 350 351 |
# File 'lib/ipaddr.rb', line 349 def eql?(other) return self.class == other.class && self.hash == other.hash && self == other end |
#hash ⇒ Object
Returns a hash value used by Hash, Set, and Array classes
354 355 356 |
# File 'lib/ipaddr.rb', line 354 def hash return ([@addr, @mask_addr].hash << 1) | (ipv4? ? 0 : 1) end |
#hton ⇒ Object
Returns a network byte ordered string form of the IP address.
239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/ipaddr.rb', line 239 def hton case @family when Socket::AF_INET return [@addr].pack('N') when Socket::AF_INET6 return (0..7).map { |i| (@addr >> (112 - 16 * i)) & 0xffff }.pack('n8') else raise AddressFamilyError, "unsupported address family" end end |
#include?(other) ⇒ Boolean Also known as: ===
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/ipaddr.rb', line 173 def include?(other) other = coerce_other(other) if ipv4_mapped? if (@mask_addr >> 32) != 0xffffffffffffffffffffffff return false end mask_addr = (@mask_addr & IN4MASK) addr = (@addr & IN4MASK) family = Socket::AF_INET else mask_addr = @mask_addr addr = @addr family = @family end if other.ipv4_mapped? other_addr = (other.to_i & IN4MASK) other_family = Socket::AF_INET else other_addr = other.to_i other_family = other.family end if family != other_family return false end return ((addr & mask_addr) == (other_addr & mask_addr)) end |
#inspect ⇒ Object
Returns a string containing a human-readable representation of the ipaddr. (“#<IPAddr: family:address/mask>”)
405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/ipaddr.rb', line 405 def inspect case @family when Socket::AF_INET af = "IPv4" when Socket::AF_INET6 af = "IPv6" else raise AddressFamilyError, "unsupported address family" end return sprintf("#<%s: %s:%s/%s>", self.class.name, af, _to_string(@addr), _to_string(@mask_addr)) end |
#ip6_arpa ⇒ Object
Returns a string for DNS reverse lookup compatible with RFC3172.
318 319 320 321 322 323 |
# File 'lib/ipaddr.rb', line 318 def ip6_arpa if !ipv6? raise InvalidAddressError, "not an IPv6 address" end return _reverse + ".ip6.arpa" end |
#ip6_int ⇒ Object
Returns a string for DNS reverse lookup compatible with RFC1886.
326 327 328 329 330 331 |
# File 'lib/ipaddr.rb', line 326 def ip6_int if !ipv6? raise InvalidAddressError, "not an IPv6 address" end return _reverse + ".ip6.int" end |
#ipv4? ⇒ Boolean
Returns true if the ipaddr is an IPv4 address.
253 254 255 |
# File 'lib/ipaddr.rb', line 253 def ipv4? return @family == Socket::AF_INET end |
#ipv4_compat ⇒ Object
Returns a new ipaddr built by converting the native IPv4 address into an IPv4-compatible IPv6 address.
287 288 289 290 291 292 |
# File 'lib/ipaddr.rb', line 287 def ipv4_compat if !ipv4? raise InvalidAddressError, "not an IPv4 address" end return self.clone.set(@addr, Socket::AF_INET6) end |
#ipv4_compat? ⇒ Boolean
Returns true if the ipaddr is an IPv4-compatible IPv6 address.
268 269 270 271 272 273 274 |
# File 'lib/ipaddr.rb', line 268 def ipv4_compat? if !ipv6? || (@addr >> 32) != 0 return false end a = (@addr & IN4MASK) return a != 0 && a != 1 end |
#ipv4_mapped ⇒ Object
Returns a new ipaddr built by converting the native IPv4 address into an IPv4-mapped IPv6 address.
278 279 280 281 282 283 |
# File 'lib/ipaddr.rb', line 278 def ipv4_mapped if !ipv4? raise InvalidAddressError, "not an IPv4 address" end return self.clone.set(@addr | 0xffff00000000, Socket::AF_INET6) end |
#ipv4_mapped? ⇒ Boolean
Returns true if the ipaddr is an IPv4-mapped IPv6 address.
263 264 265 |
# File 'lib/ipaddr.rb', line 263 def ipv4_mapped? return ipv6? && (@addr >> 32) == 0xffff end |
#ipv6? ⇒ Boolean
Returns true if the ipaddr is an IPv6 address.
258 259 260 |
# File 'lib/ipaddr.rb', line 258 def ipv6? return @family == Socket::AF_INET6 end |
#mask(prefixlen) ⇒ Object
Returns a new ipaddr built by masking IP address with the given prefixlen/netmask. (e.g. 8, 64, “255.255.255.0”, etc.)
160 161 162 |
# File 'lib/ipaddr.rb', line 160 def mask(prefixlen) return self.clone.mask!(prefixlen) end |
#native ⇒ Object
Returns a new ipaddr built by converting the IPv6 address into a native IPv4 address. If the IP address is not an IPv4-mapped or IPv4-compatible IPv6 address, returns self.
297 298 299 300 301 302 |
# File 'lib/ipaddr.rb', line 297 def native if !ipv4_mapped? && !ipv4_compat? return self end return self.clone.set(@addr & IN4MASK, Socket::AF_INET) end |
#prefix ⇒ Object
Returns the prefix length in bits for the ipaddr.
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/ipaddr.rb', line 375 def prefix case @family when Socket::AF_INET n = IN4MASK ^ @mask_addr i = 32 when Socket::AF_INET6 n = IN6MASK ^ @mask_addr i = 128 else raise AddressFamilyError, "unsupported address family" end while n.positive? n >>= 1 i -= 1 end i end |
#prefix=(prefix) ⇒ Object
Sets the prefix length in bits
394 395 396 397 398 399 400 401 |
# File 'lib/ipaddr.rb', line 394 def prefix=(prefix) case prefix when Integer mask!(prefix) else raise InvalidPrefixError, "prefix must be an integer" end end |
#reverse ⇒ Object
Returns a string for DNS reverse lookup. It returns a string in RFC3172 form for an IPv6 address.
306 307 308 309 310 311 312 313 314 315 |
# File 'lib/ipaddr.rb', line 306 def reverse case @family when Socket::AF_INET return _reverse + ".in-addr.arpa" when Socket::AF_INET6 return ip6_arpa else raise AddressFamilyError, "unsupported address family" end end |
#succ ⇒ Object
Returns the successor to the ipaddr.
334 335 336 |
# File 'lib/ipaddr.rb', line 334 def succ return self.clone.set(@addr + 1, @family) end |
#to_i ⇒ Object
Returns the integer representation of the ipaddr.
203 204 205 |
# File 'lib/ipaddr.rb', line 203 def to_i return @addr end |
#to_range ⇒ Object
Creates a Range object for the network address.
359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/ipaddr.rb', line 359 def to_range begin_addr = (@addr & @mask_addr) case @family when Socket::AF_INET end_addr = (@addr | (IN4MASK ^ @mask_addr)) when Socket::AF_INET6 end_addr = (@addr | (IN6MASK ^ @mask_addr)) else raise AddressFamilyError, "unsupported address family" end return clone.set(begin_addr, @family)..clone.set(end_addr, @family) end |
#to_s ⇒ Object
Returns a string containing the IP address representation.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/ipaddr.rb', line 208 def to_s str = to_string return str if ipv4? str.gsub!(/\b0{1,3}([\da-f]+)\b/i, '\1') loop do break if str.sub!(/\A0:0:0:0:0:0:0:0\z/, '::') break if str.sub!(/\b0:0:0:0:0:0:0\b/, ':') break if str.sub!(/\b0:0:0:0:0:0\b/, ':') break if str.sub!(/\b0:0:0:0:0\b/, ':') break if str.sub!(/\b0:0:0:0\b/, ':') break if str.sub!(/\b0:0:0\b/, ':') break if str.sub!(/\b0:0\b/, ':') break end str.sub!(/:{3,}/, '::') if /\A::(ffff:)?([\da-f]{1,4}):([\da-f]{1,4})\z/i =~ str str = sprintf('::%s%d.%d.%d.%d', $1, $2.hex / 256, $2.hex % 256, $3.hex / 256, $3.hex % 256) end str end |
#to_string ⇒ Object
Returns a string containing the IP address representation in canonical form.
234 235 236 |
# File 'lib/ipaddr.rb', line 234 def to_string return _to_string(@addr) end |
#|(other) ⇒ Object
Returns a new ipaddr built by bitwise OR.
130 131 132 |
# File 'lib/ipaddr.rb', line 130 def |(other) return self.clone.set(@addr | coerce_other(other).to_i) end |
#~ ⇒ Object
Returns a new ipaddr built by bitwise negation.
145 146 147 |
# File 'lib/ipaddr.rb', line 145 def ~ return self.clone.set(addr_mask(~@addr)) end |