2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-11-22 01:16:06 +08:00
|
|
|
class IPAddr
|
2015-02-11 02:38:59 +08:00
|
|
|
def self.handle_wildcards(val)
|
|
|
|
return if val.blank?
|
|
|
|
|
|
|
|
num_wildcards = val.count("*")
|
|
|
|
|
|
|
|
return val if num_wildcards == 0
|
|
|
|
|
|
|
|
# strip ranges like "/16" from the end if present
|
|
|
|
v = val.gsub(%r{/.*}, "")
|
|
|
|
|
|
|
|
return if v[v.index("*")..-1] =~ /[^\.\*]/
|
|
|
|
|
|
|
|
parts = v.split(".")
|
|
|
|
(4 - parts.size).times { parts << "*" } # support strings like 192.*
|
|
|
|
v = parts.join(".")
|
|
|
|
|
2016-06-11 10:37:33 +08:00
|
|
|
"#{v.tr("*", "0")}/#{32 - (v.count("*") * 8)}"
|
2015-02-11 02:38:59 +08:00
|
|
|
end
|
|
|
|
|
2014-11-22 01:16:06 +08:00
|
|
|
def to_cidr_s
|
|
|
|
if @addr
|
|
|
|
mask = @mask_addr.to_s(2).count("1")
|
|
|
|
if mask == 32
|
|
|
|
to_s
|
|
|
|
else
|
2023-12-07 06:25:00 +08:00
|
|
|
"#{self}/#{mask}"
|
2014-11-22 01:16:06 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|