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