Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby31/share/ruby
File: ipaddr.rb
# frozen_string_literal: true
[0] Fix | Delete
#
[1] Fix | Delete
# ipaddr.rb - A class to manipulate an IP address
[2] Fix | Delete
#
[3] Fix | Delete
# Copyright (c) 2002 Hajimu UMEMOTO <ume@mahoroba.org>.
[4] Fix | Delete
# Copyright (c) 2007, 2009, 2012 Akinori MUSHA <knu@iDaemons.org>.
[5] Fix | Delete
# All rights reserved.
[6] Fix | Delete
#
[7] Fix | Delete
# You can redistribute and/or modify it under the same terms as Ruby.
[8] Fix | Delete
#
[9] Fix | Delete
# $Id$
[10] Fix | Delete
#
[11] Fix | Delete
# Contact:
[12] Fix | Delete
# - Akinori MUSHA <knu@iDaemons.org> (current maintainer)
[13] Fix | Delete
#
[14] Fix | Delete
# TODO:
[15] Fix | Delete
# - scope_id support
[16] Fix | Delete
#
[17] Fix | Delete
require 'socket'
[18] Fix | Delete
[19] Fix | Delete
# IPAddr provides a set of methods to manipulate an IP address. Both IPv4 and
[20] Fix | Delete
# IPv6 are supported.
[21] Fix | Delete
#
[22] Fix | Delete
# == Example
[23] Fix | Delete
#
[24] Fix | Delete
# require 'ipaddr'
[25] Fix | Delete
#
[26] Fix | Delete
# ipaddr1 = IPAddr.new "3ffe:505:2::1"
[27] Fix | Delete
#
[28] Fix | Delete
# p ipaddr1 #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0001/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
[29] Fix | Delete
#
[30] Fix | Delete
# p ipaddr1.to_s #=> "3ffe:505:2::1"
[31] Fix | Delete
#
[32] Fix | Delete
# ipaddr2 = ipaddr1.mask(48) #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0000/ffff:ffff:ffff:0000:0000:0000:0000:0000>
[33] Fix | Delete
#
[34] Fix | Delete
# p ipaddr2.to_s #=> "3ffe:505:2::"
[35] Fix | Delete
#
[36] Fix | Delete
# ipaddr3 = IPAddr.new "192.168.2.0/24"
[37] Fix | Delete
#
[38] Fix | Delete
# p ipaddr3 #=> #<IPAddr: IPv4:192.168.2.0/255.255.255.0>
[39] Fix | Delete
[40] Fix | Delete
class IPAddr
[41] Fix | Delete
VERSION = "1.2.4"
[42] Fix | Delete
[43] Fix | Delete
# 32 bit mask for IPv4
[44] Fix | Delete
IN4MASK = 0xffffffff
[45] Fix | Delete
# 128 bit mask for IPv6
[46] Fix | Delete
IN6MASK = 0xffffffffffffffffffffffffffffffff
[47] Fix | Delete
# Format string for IPv6
[48] Fix | Delete
IN6FORMAT = (["%.4x"] * 8).join(':')
[49] Fix | Delete
[50] Fix | Delete
# Regexp _internally_ used for parsing IPv4 address.
[51] Fix | Delete
RE_IPV4ADDRLIKE = %r{
[52] Fix | Delete
\A
[53] Fix | Delete
(\d+) \. (\d+) \. (\d+) \. (\d+)
[54] Fix | Delete
\z
[55] Fix | Delete
}x
[56] Fix | Delete
[57] Fix | Delete
# Regexp _internally_ used for parsing IPv6 address.
[58] Fix | Delete
RE_IPV6ADDRLIKE_FULL = %r{
[59] Fix | Delete
\A
[60] Fix | Delete
(?:
[61] Fix | Delete
(?: [\da-f]{1,4} : ){7} [\da-f]{1,4}
[62] Fix | Delete
|
[63] Fix | Delete
( (?: [\da-f]{1,4} : ){6} )
[64] Fix | Delete
(\d+) \. (\d+) \. (\d+) \. (\d+)
[65] Fix | Delete
)
[66] Fix | Delete
\z
[67] Fix | Delete
}xi
[68] Fix | Delete
[69] Fix | Delete
# Regexp _internally_ used for parsing IPv6 address.
[70] Fix | Delete
RE_IPV6ADDRLIKE_COMPRESSED = %r{
[71] Fix | Delete
\A
[72] Fix | Delete
( (?: (?: [\da-f]{1,4} : )* [\da-f]{1,4} )? )
[73] Fix | Delete
::
[74] Fix | Delete
( (?:
[75] Fix | Delete
( (?: [\da-f]{1,4} : )* )
[76] Fix | Delete
(?:
[77] Fix | Delete
[\da-f]{1,4}
[78] Fix | Delete
|
[79] Fix | Delete
(\d+) \. (\d+) \. (\d+) \. (\d+)
[80] Fix | Delete
)
[81] Fix | Delete
)? )
[82] Fix | Delete
\z
[83] Fix | Delete
}xi
[84] Fix | Delete
[85] Fix | Delete
# Generic IPAddr related error. Exceptions raised in this class should
[86] Fix | Delete
# inherit from Error.
[87] Fix | Delete
class Error < ArgumentError; end
[88] Fix | Delete
[89] Fix | Delete
# Raised when the provided IP address is an invalid address.
[90] Fix | Delete
class InvalidAddressError < Error; end
[91] Fix | Delete
[92] Fix | Delete
# Raised when the address family is invalid such as an address with an
[93] Fix | Delete
# unsupported family, an address with an inconsistent family, or an address
[94] Fix | Delete
# who's family cannot be determined.
[95] Fix | Delete
class AddressFamilyError < Error; end
[96] Fix | Delete
[97] Fix | Delete
# Raised when the address is an invalid length.
[98] Fix | Delete
class InvalidPrefixError < InvalidAddressError; end
[99] Fix | Delete
[100] Fix | Delete
# Returns the address family of this IP address.
[101] Fix | Delete
attr_reader :family
[102] Fix | Delete
[103] Fix | Delete
# Creates a new ipaddr containing the given network byte ordered
[104] Fix | Delete
# string form of an IP address.
[105] Fix | Delete
def self.new_ntoh(addr)
[106] Fix | Delete
return new(ntop(addr))
[107] Fix | Delete
end
[108] Fix | Delete
[109] Fix | Delete
# Convert a network byte ordered string form of an IP address into
[110] Fix | Delete
# human readable form.
[111] Fix | Delete
def self.ntop(addr)
[112] Fix | Delete
case addr.size
[113] Fix | Delete
when 4
[114] Fix | Delete
addr.unpack('C4').join('.')
[115] Fix | Delete
when 16
[116] Fix | Delete
IN6FORMAT % addr.unpack('n8')
[117] Fix | Delete
else
[118] Fix | Delete
raise AddressFamilyError, "unsupported address family"
[119] Fix | Delete
end
[120] Fix | Delete
end
[121] Fix | Delete
[122] Fix | Delete
# Returns a new ipaddr built by bitwise AND.
[123] Fix | Delete
def &(other)
[124] Fix | Delete
return self.clone.set(@addr & coerce_other(other).to_i)
[125] Fix | Delete
end
[126] Fix | Delete
[127] Fix | Delete
# Returns a new ipaddr built by bitwise OR.
[128] Fix | Delete
def |(other)
[129] Fix | Delete
return self.clone.set(@addr | coerce_other(other).to_i)
[130] Fix | Delete
end
[131] Fix | Delete
[132] Fix | Delete
# Returns a new ipaddr built by bitwise right-shift.
[133] Fix | Delete
def >>(num)
[134] Fix | Delete
return self.clone.set(@addr >> num)
[135] Fix | Delete
end
[136] Fix | Delete
[137] Fix | Delete
# Returns a new ipaddr built by bitwise left shift.
[138] Fix | Delete
def <<(num)
[139] Fix | Delete
return self.clone.set(addr_mask(@addr << num))
[140] Fix | Delete
end
[141] Fix | Delete
[142] Fix | Delete
# Returns a new ipaddr built by bitwise negation.
[143] Fix | Delete
def ~
[144] Fix | Delete
return self.clone.set(addr_mask(~@addr))
[145] Fix | Delete
end
[146] Fix | Delete
[147] Fix | Delete
# Returns true if two ipaddrs are equal.
[148] Fix | Delete
def ==(other)
[149] Fix | Delete
other = coerce_other(other)
[150] Fix | Delete
rescue
[151] Fix | Delete
false
[152] Fix | Delete
else
[153] Fix | Delete
@family == other.family && @addr == other.to_i
[154] Fix | Delete
end
[155] Fix | Delete
[156] Fix | Delete
# Returns a new ipaddr built by masking IP address with the given
[157] Fix | Delete
# prefixlen/netmask. (e.g. 8, 64, "255.255.255.0", etc.)
[158] Fix | Delete
def mask(prefixlen)
[159] Fix | Delete
return self.clone.mask!(prefixlen)
[160] Fix | Delete
end
[161] Fix | Delete
[162] Fix | Delete
# Returns true if the given ipaddr is in the range.
[163] Fix | Delete
#
[164] Fix | Delete
# e.g.:
[165] Fix | Delete
# require 'ipaddr'
[166] Fix | Delete
# net1 = IPAddr.new("192.168.2.0/24")
[167] Fix | Delete
# net2 = IPAddr.new("192.168.2.100")
[168] Fix | Delete
# net3 = IPAddr.new("192.168.3.0")
[169] Fix | Delete
# net4 = IPAddr.new("192.168.2.0/16")
[170] Fix | Delete
# p net1.include?(net2) #=> true
[171] Fix | Delete
# p net1.include?(net3) #=> false
[172] Fix | Delete
# p net1.include?(net4) #=> false
[173] Fix | Delete
# p net4.include?(net1) #=> true
[174] Fix | Delete
def include?(other)
[175] Fix | Delete
other = coerce_other(other)
[176] Fix | Delete
return false unless other.family == family
[177] Fix | Delete
range = to_range
[178] Fix | Delete
other = other.to_range
[179] Fix | Delete
range.begin <= other.begin && range.end >= other.end
[180] Fix | Delete
end
[181] Fix | Delete
alias === include?
[182] Fix | Delete
[183] Fix | Delete
# Returns the integer representation of the ipaddr.
[184] Fix | Delete
def to_i
[185] Fix | Delete
return @addr
[186] Fix | Delete
end
[187] Fix | Delete
[188] Fix | Delete
# Returns a string containing the IP address representation.
[189] Fix | Delete
def to_s
[190] Fix | Delete
str = to_string
[191] Fix | Delete
return str if ipv4?
[192] Fix | Delete
[193] Fix | Delete
str.gsub!(/\b0{1,3}([\da-f]+)\b/i, '\1')
[194] Fix | Delete
loop do
[195] Fix | Delete
break if str.sub!(/\A0:0:0:0:0:0:0:0\z/, '::')
[196] Fix | Delete
break if str.sub!(/\b0:0:0:0:0:0:0\b/, ':')
[197] Fix | Delete
break if str.sub!(/\b0:0:0:0:0:0\b/, ':')
[198] Fix | Delete
break if str.sub!(/\b0:0:0:0:0\b/, ':')
[199] Fix | Delete
break if str.sub!(/\b0:0:0:0\b/, ':')
[200] Fix | Delete
break if str.sub!(/\b0:0:0\b/, ':')
[201] Fix | Delete
break if str.sub!(/\b0:0\b/, ':')
[202] Fix | Delete
break
[203] Fix | Delete
end
[204] Fix | Delete
str.sub!(/:{3,}/, '::')
[205] Fix | Delete
[206] Fix | Delete
if /\A::(ffff:)?([\da-f]{1,4}):([\da-f]{1,4})\z/i =~ str
[207] Fix | Delete
str = sprintf('::%s%d.%d.%d.%d', $1, $2.hex / 256, $2.hex % 256, $3.hex / 256, $3.hex % 256)
[208] Fix | Delete
end
[209] Fix | Delete
[210] Fix | Delete
str
[211] Fix | Delete
end
[212] Fix | Delete
[213] Fix | Delete
# Returns a string containing the IP address representation in
[214] Fix | Delete
# canonical form.
[215] Fix | Delete
def to_string
[216] Fix | Delete
str = _to_string(@addr)
[217] Fix | Delete
[218] Fix | Delete
if @family == Socket::AF_INET6
[219] Fix | Delete
str << zone_id.to_s
[220] Fix | Delete
end
[221] Fix | Delete
[222] Fix | Delete
return str
[223] Fix | Delete
end
[224] Fix | Delete
[225] Fix | Delete
# Returns a network byte ordered string form of the IP address.
[226] Fix | Delete
def hton
[227] Fix | Delete
case @family
[228] Fix | Delete
when Socket::AF_INET
[229] Fix | Delete
return [@addr].pack('N')
[230] Fix | Delete
when Socket::AF_INET6
[231] Fix | Delete
return (0..7).map { |i|
[232] Fix | Delete
(@addr >> (112 - 16 * i)) & 0xffff
[233] Fix | Delete
}.pack('n8')
[234] Fix | Delete
else
[235] Fix | Delete
raise AddressFamilyError, "unsupported address family"
[236] Fix | Delete
end
[237] Fix | Delete
end
[238] Fix | Delete
[239] Fix | Delete
# Returns true if the ipaddr is an IPv4 address.
[240] Fix | Delete
def ipv4?
[241] Fix | Delete
return @family == Socket::AF_INET
[242] Fix | Delete
end
[243] Fix | Delete
[244] Fix | Delete
# Returns true if the ipaddr is an IPv6 address.
[245] Fix | Delete
def ipv6?
[246] Fix | Delete
return @family == Socket::AF_INET6
[247] Fix | Delete
end
[248] Fix | Delete
[249] Fix | Delete
# Returns true if the ipaddr is a loopback address.
[250] Fix | Delete
def loopback?
[251] Fix | Delete
case @family
[252] Fix | Delete
when Socket::AF_INET
[253] Fix | Delete
@addr & 0xff000000 == 0x7f000000
[254] Fix | Delete
when Socket::AF_INET6
[255] Fix | Delete
@addr == 1
[256] Fix | Delete
else
[257] Fix | Delete
raise AddressFamilyError, "unsupported address family"
[258] Fix | Delete
end
[259] Fix | Delete
end
[260] Fix | Delete
[261] Fix | Delete
# Returns true if the ipaddr is a private address. IPv4 addresses
[262] Fix | Delete
# in 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 as defined in RFC
[263] Fix | Delete
# 1918 and IPv6 Unique Local Addresses in fc00::/7 as defined in RFC
[264] Fix | Delete
# 4193 are considered private.
[265] Fix | Delete
def private?
[266] Fix | Delete
case @family
[267] Fix | Delete
when Socket::AF_INET
[268] Fix | Delete
@addr & 0xff000000 == 0x0a000000 || # 10.0.0.0/8
[269] Fix | Delete
@addr & 0xfff00000 == 0xac100000 || # 172.16.0.0/12
[270] Fix | Delete
@addr & 0xffff0000 == 0xc0a80000 # 192.168.0.0/16
[271] Fix | Delete
when Socket::AF_INET6
[272] Fix | Delete
@addr & 0xfe00_0000_0000_0000_0000_0000_0000_0000 == 0xfc00_0000_0000_0000_0000_0000_0000_0000
[273] Fix | Delete
else
[274] Fix | Delete
raise AddressFamilyError, "unsupported address family"
[275] Fix | Delete
end
[276] Fix | Delete
end
[277] Fix | Delete
[278] Fix | Delete
# Returns true if the ipaddr is a link-local address. IPv4
[279] Fix | Delete
# addresses in 169.254.0.0/16 reserved by RFC 3927 and Link-Local
[280] Fix | Delete
# IPv6 Unicast Addresses in fe80::/10 reserved by RFC 4291 are
[281] Fix | Delete
# considered link-local.
[282] Fix | Delete
def link_local?
[283] Fix | Delete
case @family
[284] Fix | Delete
when Socket::AF_INET
[285] Fix | Delete
@addr & 0xffff0000 == 0xa9fe0000 # 169.254.0.0/16
[286] Fix | Delete
when Socket::AF_INET6
[287] Fix | Delete
@addr & 0xffc0_0000_0000_0000_0000_0000_0000_0000 == 0xfe80_0000_0000_0000_0000_0000_0000_0000
[288] Fix | Delete
else
[289] Fix | Delete
raise AddressFamilyError, "unsupported address family"
[290] Fix | Delete
end
[291] Fix | Delete
end
[292] Fix | Delete
[293] Fix | Delete
# Returns true if the ipaddr is an IPv4-mapped IPv6 address.
[294] Fix | Delete
def ipv4_mapped?
[295] Fix | Delete
return ipv6? && (@addr >> 32) == 0xffff
[296] Fix | Delete
end
[297] Fix | Delete
[298] Fix | Delete
# Returns true if the ipaddr is an IPv4-compatible IPv6 address.
[299] Fix | Delete
def ipv4_compat?
[300] Fix | Delete
warn "IPAddr\##{__callee__} is obsolete", uplevel: 1 if $VERBOSE
[301] Fix | Delete
_ipv4_compat?
[302] Fix | Delete
end
[303] Fix | Delete
[304] Fix | Delete
def _ipv4_compat?
[305] Fix | Delete
if !ipv6? || (@addr >> 32) != 0
[306] Fix | Delete
return false
[307] Fix | Delete
end
[308] Fix | Delete
a = (@addr & IN4MASK)
[309] Fix | Delete
return a != 0 && a != 1
[310] Fix | Delete
end
[311] Fix | Delete
[312] Fix | Delete
private :_ipv4_compat?
[313] Fix | Delete
[314] Fix | Delete
# Returns a new ipaddr built by converting the native IPv4 address
[315] Fix | Delete
# into an IPv4-mapped IPv6 address.
[316] Fix | Delete
def ipv4_mapped
[317] Fix | Delete
if !ipv4?
[318] Fix | Delete
raise InvalidAddressError, "not an IPv4 address: #{@addr}"
[319] Fix | Delete
end
[320] Fix | Delete
clone = self.clone.set(@addr | 0xffff00000000, Socket::AF_INET6)
[321] Fix | Delete
clone.instance_variable_set(:@mask_addr, @mask_addr | 0xffffffffffffffffffffffff00000000)
[322] Fix | Delete
clone
[323] Fix | Delete
end
[324] Fix | Delete
[325] Fix | Delete
# Returns a new ipaddr built by converting the native IPv4 address
[326] Fix | Delete
# into an IPv4-compatible IPv6 address.
[327] Fix | Delete
def ipv4_compat
[328] Fix | Delete
warn "IPAddr\##{__callee__} is obsolete", uplevel: 1 if $VERBOSE
[329] Fix | Delete
if !ipv4?
[330] Fix | Delete
raise InvalidAddressError, "not an IPv4 address: #{@addr}"
[331] Fix | Delete
end
[332] Fix | Delete
return self.clone.set(@addr, Socket::AF_INET6)
[333] Fix | Delete
end
[334] Fix | Delete
[335] Fix | Delete
# Returns a new ipaddr built by converting the IPv6 address into a
[336] Fix | Delete
# native IPv4 address. If the IP address is not an IPv4-mapped or
[337] Fix | Delete
# IPv4-compatible IPv6 address, returns self.
[338] Fix | Delete
def native
[339] Fix | Delete
if !ipv4_mapped? && !_ipv4_compat?
[340] Fix | Delete
return self
[341] Fix | Delete
end
[342] Fix | Delete
return self.clone.set(@addr & IN4MASK, Socket::AF_INET)
[343] Fix | Delete
end
[344] Fix | Delete
[345] Fix | Delete
# Returns a string for DNS reverse lookup. It returns a string in
[346] Fix | Delete
# RFC3172 form for an IPv6 address.
[347] Fix | Delete
def reverse
[348] Fix | Delete
case @family
[349] Fix | Delete
when Socket::AF_INET
[350] Fix | Delete
return _reverse + ".in-addr.arpa"
[351] Fix | Delete
when Socket::AF_INET6
[352] Fix | Delete
return ip6_arpa
[353] Fix | Delete
else
[354] Fix | Delete
raise AddressFamilyError, "unsupported address family"
[355] Fix | Delete
end
[356] Fix | Delete
end
[357] Fix | Delete
[358] Fix | Delete
# Returns a string for DNS reverse lookup compatible with RFC3172.
[359] Fix | Delete
def ip6_arpa
[360] Fix | Delete
if !ipv6?
[361] Fix | Delete
raise InvalidAddressError, "not an IPv6 address: #{@addr}"
[362] Fix | Delete
end
[363] Fix | Delete
return _reverse + ".ip6.arpa"
[364] Fix | Delete
end
[365] Fix | Delete
[366] Fix | Delete
# Returns a string for DNS reverse lookup compatible with RFC1886.
[367] Fix | Delete
def ip6_int
[368] Fix | Delete
if !ipv6?
[369] Fix | Delete
raise InvalidAddressError, "not an IPv6 address: #{@addr}"
[370] Fix | Delete
end
[371] Fix | Delete
return _reverse + ".ip6.int"
[372] Fix | Delete
end
[373] Fix | Delete
[374] Fix | Delete
# Returns the successor to the ipaddr.
[375] Fix | Delete
def succ
[376] Fix | Delete
return self.clone.set(@addr + 1, @family)
[377] Fix | Delete
end
[378] Fix | Delete
[379] Fix | Delete
# Compares the ipaddr with another.
[380] Fix | Delete
def <=>(other)
[381] Fix | Delete
other = coerce_other(other)
[382] Fix | Delete
rescue
[383] Fix | Delete
nil
[384] Fix | Delete
else
[385] Fix | Delete
@addr <=> other.to_i if other.family == @family
[386] Fix | Delete
end
[387] Fix | Delete
include Comparable
[388] Fix | Delete
[389] Fix | Delete
# Checks equality used by Hash.
[390] Fix | Delete
def eql?(other)
[391] Fix | Delete
return self.class == other.class && self.hash == other.hash && self == other
[392] Fix | Delete
end
[393] Fix | Delete
[394] Fix | Delete
# Returns a hash value used by Hash, Set, and Array classes
[395] Fix | Delete
def hash
[396] Fix | Delete
return ([@addr, @mask_addr, @zone_id].hash << 1) | (ipv4? ? 0 : 1)
[397] Fix | Delete
end
[398] Fix | Delete
[399] Fix | Delete
# Creates a Range object for the network address.
[400] Fix | Delete
def to_range
[401] Fix | Delete
begin_addr = (@addr & @mask_addr)
[402] Fix | Delete
[403] Fix | Delete
case @family
[404] Fix | Delete
when Socket::AF_INET
[405] Fix | Delete
end_addr = (@addr | (IN4MASK ^ @mask_addr))
[406] Fix | Delete
when Socket::AF_INET6
[407] Fix | Delete
end_addr = (@addr | (IN6MASK ^ @mask_addr))
[408] Fix | Delete
else
[409] Fix | Delete
raise AddressFamilyError, "unsupported address family"
[410] Fix | Delete
end
[411] Fix | Delete
[412] Fix | Delete
self.class.new(begin_addr, @family)..self.class.new(end_addr, @family)
[413] Fix | Delete
end
[414] Fix | Delete
[415] Fix | Delete
# Returns the prefix length in bits for the ipaddr.
[416] Fix | Delete
def prefix
[417] Fix | Delete
case @family
[418] Fix | Delete
when Socket::AF_INET
[419] Fix | Delete
n = IN4MASK ^ @mask_addr
[420] Fix | Delete
i = 32
[421] Fix | Delete
when Socket::AF_INET6
[422] Fix | Delete
n = IN6MASK ^ @mask_addr
[423] Fix | Delete
i = 128
[424] Fix | Delete
else
[425] Fix | Delete
raise AddressFamilyError, "unsupported address family"
[426] Fix | Delete
end
[427] Fix | Delete
while n.positive?
[428] Fix | Delete
n >>= 1
[429] Fix | Delete
i -= 1
[430] Fix | Delete
end
[431] Fix | Delete
i
[432] Fix | Delete
end
[433] Fix | Delete
[434] Fix | Delete
# Sets the prefix length in bits
[435] Fix | Delete
def prefix=(prefix)
[436] Fix | Delete
case prefix
[437] Fix | Delete
when Integer
[438] Fix | Delete
mask!(prefix)
[439] Fix | Delete
else
[440] Fix | Delete
raise InvalidPrefixError, "prefix must be an integer: #{@addr}"
[441] Fix | Delete
end
[442] Fix | Delete
end
[443] Fix | Delete
[444] Fix | Delete
# Returns a string containing a human-readable representation of the
[445] Fix | Delete
# ipaddr. ("#<IPAddr: family:address/mask>")
[446] Fix | Delete
def inspect
[447] Fix | Delete
case @family
[448] Fix | Delete
when Socket::AF_INET
[449] Fix | Delete
af = "IPv4"
[450] Fix | Delete
when Socket::AF_INET6
[451] Fix | Delete
af = "IPv6"
[452] Fix | Delete
zone_id = @zone_id.to_s
[453] Fix | Delete
else
[454] Fix | Delete
raise AddressFamilyError, "unsupported address family"
[455] Fix | Delete
end
[456] Fix | Delete
return sprintf("#<%s: %s:%s%s/%s>", self.class.name,
[457] Fix | Delete
af, _to_string(@addr), zone_id, _to_string(@mask_addr))
[458] Fix | Delete
end
[459] Fix | Delete
[460] Fix | Delete
# Returns the netmask in string format e.g. 255.255.0.0
[461] Fix | Delete
def netmask
[462] Fix | Delete
_to_string(@mask_addr)
[463] Fix | Delete
end
[464] Fix | Delete
[465] Fix | Delete
# Returns the IPv6 zone identifier, if present.
[466] Fix | Delete
# Raises InvalidAddressError if not an IPv6 address.
[467] Fix | Delete
def zone_id
[468] Fix | Delete
if @family == Socket::AF_INET6
[469] Fix | Delete
@zone_id
[470] Fix | Delete
else
[471] Fix | Delete
raise InvalidAddressError, "not an IPv6 address"
[472] Fix | Delete
end
[473] Fix | Delete
end
[474] Fix | Delete
[475] Fix | Delete
# Returns the IPv6 zone identifier, if present.
[476] Fix | Delete
# Raises InvalidAddressError if not an IPv6 address.
[477] Fix | Delete
def zone_id=(zid)
[478] Fix | Delete
if @family == Socket::AF_INET6
[479] Fix | Delete
case zid
[480] Fix | Delete
when nil, /\A%(\w+)\z/
[481] Fix | Delete
@zone_id = zid
[482] Fix | Delete
else
[483] Fix | Delete
raise InvalidAddressError, "invalid zone identifier for address"
[484] Fix | Delete
end
[485] Fix | Delete
else
[486] Fix | Delete
raise InvalidAddressError, "not an IPv6 address"
[487] Fix | Delete
end
[488] Fix | Delete
end
[489] Fix | Delete
[490] Fix | Delete
protected
[491] Fix | Delete
[492] Fix | Delete
# Set +@addr+, the internal stored ip address, to given +addr+. The
[493] Fix | Delete
# parameter +addr+ is validated using the first +family+ member,
[494] Fix | Delete
# which is +Socket::AF_INET+ or +Socket::AF_INET6+.
[495] Fix | Delete
def set(addr, *family)
[496] Fix | Delete
case family[0] ? family[0] : @family
[497] Fix | Delete
when Socket::AF_INET
[498] Fix | Delete
if addr < 0 || addr > IN4MASK
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function