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