Parse a string; return an V6 instance if it’s a valid IPv6 address, nil otherwise
# File lib/ip/base.rb, line 303 def self.parse(str) case str when /\A\[?::(ffff:)?(\d+\.\d+\.\d+\.\d+)\]?(?:\/(\d+))?(?:@(.*))?\z/ mapped = $1 pfxlen = ($3 || 128).to_i ctx = $4 return nil if pfxlen > 128 v4 = (V4.parse($2) || return).to_i v4 |= 0xffff00000000 if mapped new(v4, pfxlen, ctx) when /\A\[?([0-9a-f:]+)\]?(?:\/(\d+))?(?:@(.*))?\z/ addr = $1 pfxlen = ($2 || 128).to_i return nil if pfxlen > 128 ctx = $3 return nil if pfxlen > 128 if addr =~ /\A(.*?)::(.*)\z/ left, right = $1, $2 l = left.split(':') r = right.split(':') rest = 8 - l.length - r.length return nil if rest < 0 else l = addr.split(':') r = [] rest = 0 return nil if l.length != 8 end out = "" l.each { |quad| return nil if quad.length>4; out << quad.rjust(4,"0") } rest.times { out << "0000" } r.each { |quad| return nil if quad.length>4; out << quad.rjust(4,"0") } new(out, pfxlen, ctx) else nil end end
# File lib/ip/base.rb, line 364 def ipv4_compat? @addr > 1 && (@addr >> 32) == 0 end
# File lib/ip/base.rb, line 360 def ipv4_mapped? (@addr >> 32) == 0xffff end
Convert an IPv6 mapped/compat address to a V4 native address
# File lib/ip/base.rb, line 369 def native return self unless (ipv4_mapped? || ipv4_compat?) && (@pfxlen >= 96) V4.new(@addr & V4::MASK, @pfxlen - 96, @ctx) end
Return just the address part as a String in compact decimal form
# File lib/ip/base.rb, line 342 def to_addr if ipv4_compat? "::#{native.to_addr}" elsif ipv4_mapped? "::ffff:#{native.to_addr}" elsif @addr.zero? "::" else res = to_hex.scan(/..../).join(':') res.gsub!(/\b0{1,3}/,'') res.sub!(/\b0:0:0:0(:0)*\b/,':') || res.sub!(/\b0:0:0\b/,':') || res.sub!(/\b0:0\b/,':') res.sub!(/:::+/,'::') res end end
Generated with the Darkfish Rdoc Generator 2.