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