Class: IPAddr

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/ipaddr.rb

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

VERSION =
"1.2.7"
IN4MASK =

32 bit mask for IPv4

0xffffffff
IN6MASK =

128 bit mask for IPv6

0xffffffffffffffffffffffffffffffff
IN6FORMAT =

Format string for IPv6

(["%.4x"] * 8).join(':').freeze
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#familyObject (readonly)

Returns the address family of this IP address.



103
104
105
# File 'lib/ipaddr.rb', line 103

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.



107
108
109
# File 'lib/ipaddr.rb', line 107

def self.new_ntoh(addr)
  return new(ntop(addr))
end

.ntop(addr) ⇒ Object

Convert a network byte ordered string form of an IP address into human readable form. It expects the string to be encoded in Encoding::ASCII_8BIT (BINARY).



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ipaddr.rb', line 114

def self.ntop(addr)
  if addr.is_a?(String) && addr.encoding != Encoding::BINARY
    raise InvalidAddressError, "invalid encoding (given #{addr.encoding}, expected BINARY)"
  end

  case addr.bytesize
  when 4
    addr.unpack('C4').join('.')
  when 16
    IN6FORMAT % addr.unpack('n8')
  else
    raise AddressFamilyError, "unsupported address family"
  end
end

Instance Method Details

#&(other) ⇒ Object

Returns a new ipaddr built by bitwise AND.



130
131
132
# File 'lib/ipaddr.rb', line 130

def &(other)
  return self.clone.set(@addr & coerce_other(other).to_i)
end

#<<(num) ⇒ Object

Returns a new ipaddr built by bitwise left shift.



145
146
147
# File 'lib/ipaddr.rb', line 145

def <<(num)
  return self.clone.set(addr_mask(@addr << num))
end

#<=>(other) ⇒ Object

Compares the ipaddr with another.



422
423
424
425
426
427
428
# File 'lib/ipaddr.rb', line 422

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.



155
156
157
158
159
160
161
# File 'lib/ipaddr.rb', line 155

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.



140
141
142
# File 'lib/ipaddr.rb', line 140

def >>(num)
  return self.clone.set(@addr >> num)
end

#as_jsonObject

Returns a string containing the IP address representation with prefix.



231
232
233
234
235
236
237
238
239
# File 'lib/ipaddr.rb', line 231

def as_json(*)
  if ipv4? && prefix == 32
    to_s
  elsif ipv6? && prefix == 128
    to_s
  else
    cidr
  end
end

#cidrObject

Returns a string containing the IP address representation in cidr notation



248
249
250
# File 'lib/ipaddr.rb', line 248

def cidr
  "#{to_s}/#{prefix}"
end

#eql?(other) ⇒ Boolean

Checks equality used by Hash.

Returns:

  • (Boolean)


432
433
434
# File 'lib/ipaddr.rb', line 432

def eql?(other)
  return self.class == other.class && self.hash == other.hash && self == other
end

#hashObject

Returns a hash value used by Hash, Set, and Array classes



437
438
439
# File 'lib/ipaddr.rb', line 437

def hash
  return ([@addr, @mask_addr, @zone_id].hash << 1) | (ipv4? ? 0 : 1)
end

#htonObject

Returns a network byte ordered string form of the IP address.



253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/ipaddr.rb', line 253

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: ===

Returns true if the given ipaddr is in the range.

e.g.:

require 'ipaddr'
net1 = IPAddr.new("192.168.2.0/24")
net2 = IPAddr.new("192.168.2.100")
net3 = IPAddr.new("192.168.3.0")
net4 = IPAddr.new("192.168.2.0/16")
p net1.include?(net2)     #=> true
p net1.include?(net3)     #=> false
p net1.include?(net4)     #=> false
p net4.include?(net1)     #=> true

Returns:

  • (Boolean)


181
182
183
184
185
# File 'lib/ipaddr.rb', line 181

def include?(other)
  other = coerce_other(other)
  return false unless other.family == family
  begin_addr <= other.begin_addr && end_addr >= other.end_addr
end

#inspectObject

Returns a string containing a human-readable representation of the ipaddr. (“#<IPAddr: family:address/mask>”)



477
478
479
480
481
482
483
484
485
486
487
488
489
# File 'lib/ipaddr.rb', line 477

def inspect
  case @family
  when Socket::AF_INET
    af = "IPv4"
  when Socket::AF_INET6
    af = "IPv6"
    zone_id = @zone_id.to_s
  else
    raise AddressFamilyError, "unsupported address family"
  end
  return sprintf("#<%s: %s:%s%s/%s>", self.class.name,
                 af, _to_string(@addr), zone_id, _to_string(@mask_addr))
end

#ip6_arpaObject

Returns a string for DNS reverse lookup compatible with RFC3172.



401
402
403
404
405
406
# File 'lib/ipaddr.rb', line 401

def ip6_arpa
  if !ipv6?
    raise InvalidAddressError, "not an IPv6 address: #{@addr}"
  end
  return _reverse + ".ip6.arpa"
end

#ip6_intObject

Returns a string for DNS reverse lookup compatible with RFC1886.



409
410
411
412
413
414
# File 'lib/ipaddr.rb', line 409

def ip6_int
  if !ipv6?
    raise InvalidAddressError, "not an IPv6 address: #{@addr}"
  end
  return _reverse + ".ip6.int"
end

#ipv4?Boolean

Returns true if the ipaddr is an IPv4 address.

Returns:

  • (Boolean)


267
268
269
# File 'lib/ipaddr.rb', line 267

def ipv4?
  return @family == Socket::AF_INET
end

#ipv4_compatObject

Returns a new ipaddr built by converting the native IPv4 address into an IPv4-compatible IPv6 address.



369
370
371
372
373
374
375
# File 'lib/ipaddr.rb', line 369

def ipv4_compat
  warn "IPAddr\##{__callee__} is obsolete", uplevel: 1 if $VERBOSE
  if !ipv4?
    raise InvalidAddressError, "not an IPv4 address: #{@addr}"
  end
  return self.clone.set(@addr, Socket::AF_INET6)
end

#ipv4_compat?Boolean

Returns true if the ipaddr is an IPv4-compatible IPv6 address.

Returns:

  • (Boolean)


341
342
343
344
# File 'lib/ipaddr.rb', line 341

def ipv4_compat?
  warn "IPAddr\##{__callee__} is obsolete", uplevel: 1 if $VERBOSE
  _ipv4_compat?
end

#ipv4_mappedObject

Returns a new ipaddr built by converting the native IPv4 address into an IPv4-mapped IPv6 address.



358
359
360
361
362
363
364
365
# File 'lib/ipaddr.rb', line 358

def ipv4_mapped
  if !ipv4?
    raise InvalidAddressError, "not an IPv4 address: #{@addr}"
  end
  clone = self.clone.set(@addr | 0xffff00000000, Socket::AF_INET6)
  clone.instance_variable_set(:@mask_addr, @mask_addr | 0xffffffffffffffffffffffff00000000)
  clone
end

#ipv4_mapped?Boolean

Returns true if the ipaddr is an IPv4-mapped IPv6 address.

Returns:

  • (Boolean)


336
337
338
# File 'lib/ipaddr.rb', line 336

def ipv4_mapped?
  return ipv6? && (@addr >> 32) == 0xffff
end

#ipv6?Boolean

Returns true if the ipaddr is an IPv6 address.

Returns:

  • (Boolean)


272
273
274
# File 'lib/ipaddr.rb', line 272

def ipv6?
  return @family == Socket::AF_INET6
end

Returns true if the ipaddr is a link-local address. IPv4 addresses in 169.254.0.0/16 reserved by RFC 3927 and link-local IPv6 Unicast Addresses in fe80::/10 reserved by RFC 4291 are considered link-local. Link-local IPv4 addresses in the IPv4-mapped IPv6 address range are also considered link-local.

Returns:

  • (Boolean)


321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/ipaddr.rb', line 321

def link_local?
  case @family
  when Socket::AF_INET
    @addr & 0xffff0000 == 0xa9fe0000 # 169.254.0.0/16
  when Socket::AF_INET6
    @addr & 0xffc0_0000_0000_0000_0000_0000_0000_0000 == 0xfe80_0000_0000_0000_0000_0000_0000_0000 || # fe80::/10
      (@addr & 0xffff_0000_0000 == 0xffff_0000_0000 && (
        @addr & 0xffff0000 == 0xa9fe0000 # ::ffff:169.254.0.0/16
      ))
  else
    raise AddressFamilyError, "unsupported address family"
  end
end

#loopback?Boolean

Returns true if the ipaddr is a loopback address. Loopback IPv4 addresses in the IPv4-mapped IPv6 address range are also considered as loopback addresses.

Returns:

  • (Boolean)


279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/ipaddr.rb', line 279

def loopback?
  case @family
  when Socket::AF_INET
    @addr & 0xff000000 == 0x7f000000 # 127.0.0.1/8
  when Socket::AF_INET6
    @addr == 1 || # ::1
      (@addr & 0xffff_0000_0000 == 0xffff_0000_0000 && (
        @addr & 0xff000000 == 0x7f000000 # ::ffff:127.0.0.1/8
      ))
  else
    raise AddressFamilyError, "unsupported address family"
  end
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.)



165
166
167
# File 'lib/ipaddr.rb', line 165

def mask(prefixlen)
  return self.clone.mask!(prefixlen)
end

#nativeObject

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.



380
381
382
383
384
385
# File 'lib/ipaddr.rb', line 380

def native
  if !ipv4_mapped? && !_ipv4_compat?
    return self
  end
  return self.clone.set(@addr & IN4MASK, Socket::AF_INET)
end

#netmaskObject

Returns the netmask in string format e.g. 255.255.0.0



492
493
494
# File 'lib/ipaddr.rb', line 492

def netmask
  _to_string(@mask_addr)
end

#prefixObject

Returns the prefix length in bits for the ipaddr.



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/ipaddr.rb', line 447

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



466
467
468
469
470
471
472
473
# File 'lib/ipaddr.rb', line 466

def prefix=(prefix)
  case prefix
  when Integer
    mask!(prefix)
  else
    raise InvalidPrefixError, "prefix must be an integer"
  end
end

#private?Boolean

Returns true if the ipaddr is a private address. IPv4 addresses in 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 as defined in RFC 1918 and IPv6 Unique Local Addresses in fc00::/7 as defined in RFC 4193 are considered private. Private IPv4 addresses in the IPv4-mapped IPv6 address range are also considered private.

Returns:

  • (Boolean)


298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/ipaddr.rb', line 298

def private?
  case @family
  when Socket::AF_INET
    @addr & 0xff000000 == 0x0a000000 ||    # 10.0.0.0/8
      @addr & 0xfff00000 == 0xac100000 ||  # 172.16.0.0/12
      @addr & 0xffff0000 == 0xc0a80000     # 192.168.0.0/16
  when Socket::AF_INET6
    @addr & 0xfe00_0000_0000_0000_0000_0000_0000_0000 == 0xfc00_0000_0000_0000_0000_0000_0000_0000 ||
      (@addr & 0xffff_0000_0000 == 0xffff_0000_0000 && (
        @addr & 0xff000000 == 0x0a000000 ||  # ::ffff:10.0.0.0/8
        @addr & 0xfff00000 == 0xac100000 ||  # ::ffff::172.16.0.0/12
        @addr & 0xffff0000 == 0xc0a80000     # ::ffff::192.168.0.0/16
      ))
  else
    raise AddressFamilyError, "unsupported address family"
  end
end

#reverseObject

Returns a string for DNS reverse lookup. It returns a string in RFC3172 form for an IPv6 address.



389
390
391
392
393
394
395
396
397
398
# File 'lib/ipaddr.rb', line 389

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

#succObject

Returns the successor to the ipaddr.



417
418
419
# File 'lib/ipaddr.rb', line 417

def succ
  return self.clone.set(@addr + 1, @family)
end

#to_iObject

Returns the integer representation of the ipaddr.



189
190
191
# File 'lib/ipaddr.rb', line 189

def to_i
  return @addr
end

#to_json(*a) ⇒ Object

Returns a json string containing the IP address representation.



242
243
244
# File 'lib/ipaddr.rb', line 242

def to_json(*a)
  %Q{"#{as_json(*a)}"}
end

#to_rangeObject

Creates a Range object for the network address.



442
443
444
# File 'lib/ipaddr.rb', line 442

def to_range
  self.class.new(begin_addr, @family)..self.class.new(end_addr, @family)
end

#to_sObject

Returns a string containing the IP address representation.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/ipaddr.rb', line 194

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_stringObject

Returns a string containing the IP address representation in canonical form.



220
221
222
223
224
225
226
227
228
# File 'lib/ipaddr.rb', line 220

def to_string
  str = _to_string(@addr)

  if @family == Socket::AF_INET6
    str << zone_id.to_s
  end

  return str
end

#wildcard_maskObject

Returns the wildcard mask in string format e.g. 0.0.255.255



497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/ipaddr.rb', line 497

def wildcard_mask
  case @family
  when Socket::AF_INET
    mask = IN4MASK ^ @mask_addr
  when Socket::AF_INET6
    mask = IN6MASK ^ @mask_addr
  else
    raise AddressFamilyError, "unsupported address family"
  end

  _to_string(mask)
end

#zone_idObject

Returns the IPv6 zone identifier, if present. Raises InvalidAddressError if not an IPv6 address.



512
513
514
515
516
517
518
# File 'lib/ipaddr.rb', line 512

def zone_id
  if @family == Socket::AF_INET6
    @zone_id
  else
    raise InvalidAddressError, "not an IPv6 address"
  end
end

#zone_id=(zid) ⇒ Object

Returns the IPv6 zone identifier, if present. Raises InvalidAddressError if not an IPv6 address.



522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/ipaddr.rb', line 522

def zone_id=(zid)
  if @family == Socket::AF_INET6
    case zid
    when nil, /\A%(\w+)\z/
      @zone_id = zid
    else
      raise InvalidAddressError, "invalid zone identifier for address"
    end
  else
    raise InvalidAddressError, "not an IPv6 address"
  end
end

#|(other) ⇒ Object

Returns a new ipaddr built by bitwise OR.



135
136
137
# File 'lib/ipaddr.rb', line 135

def |(other)
  return self.clone.set(@addr | coerce_other(other).to_i)
end

#~Object

Returns a new ipaddr built by bitwise negation.



150
151
152
# File 'lib/ipaddr.rb', line 150

def ~
  return self.clone.set(addr_mask(~@addr))
end