Class IP::V6
In: lib/ip/base.rb
lib/ip/socket.rb
Parent: IP

Methods

Constants

PROTO = "v6".freeze
ADDR_BITS = 128
MASK = (1 << ADDR_BITS) - 1
AF = Socket::AF_INET6

External Aliases

orig_new -> new

Public Class methods

Parse a string; return an V6 instance if it‘s a valid IPv6 address, nil otherwise

[Source]

     # File lib/ip/base.rb, line 284
284:     def self.parse(str)
285:       case str
286:       when /\A\[?::(ffff:)?(\d+\.\d+\.\d+\.\d+)\]?(?:\/(\d+))?(?:@(.*))?\z/i
287:         mapped = $1
288:         pfxlen = ($3 || 128).to_i
289:         ctx = $4
290:         return nil if pfxlen > 128
291:         v4 = (V4.parse($2) || return).to_i
292:         v4 |= 0xffff00000000 if mapped
293:         new(v4, pfxlen, ctx)
294:       when /\A\[?([0-9a-f:]+)\]?(?:\/(\d+))?(?:@(.*))?\z/i
295:         addr = $1
296:         pfxlen = ($2 || 128).to_i
297:         return nil if pfxlen > 128
298:         ctx = $3
299:         return nil if pfxlen > 128
300:         if addr =~ /\A(.*?)::(.*)\z/
301:           left, right = $1, $2
302:           l = left.split(':')
303:           r = right.split(':')
304:           rest = 8 - l.length - r.length
305:           return nil if rest < 0
306:         else
307:           l = addr.split(':')
308:           r = []
309:           rest = 0
310:           return nil if l.length != 8
311:         end
312:         out = ""
313:         l.each { |quad| return nil if quad.length>4; out << quad.rjust(4,"0") }
314:         rest.times { out << "0000" }
315:         r.each { |quad| return nil if quad.length>4; out << quad.rjust(4,"0") }
316:         new(out, pfxlen, ctx)
317:       else
318:         nil
319:       end
320:     end

Public Instance methods

[Source]

     # File lib/ip/base.rb, line 341
341:     def ipv4_compat?
342:       @addr > 1 && (@addr >> 32) == 0
343:     end

[Source]

     # File lib/ip/base.rb, line 337
337:     def ipv4_mapped?
338:       (@addr >> 32) == 0xffff
339:     end

Convert an IPv6 mapped/compat address to a V4 native address

[Source]

     # File lib/ip/base.rb, line 346
346:     def native
347:       return self unless (ipv4_mapped? || ipv4_compat?) && (@pfxlen >= 96)
348:       V4.new(@addr & V4::MASK, @pfxlen - 96, @ctx)
349:     end

Return just the address part as a String in compact decimal form

[Source]

     # File lib/ip/base.rb, line 323
323:     def to_addr
324:       if ipv4_compat?
325:         "::#{native.to_addr}"
326:       elsif ipv4_mapped?
327:         "::ffff:#{native.to_addr}"
328:       else
329:         res = to_hex.scan(/..../).join(':')
330:         res.gsub!(/\b0{1,3}/,'')
331:         res.gsub!(/(\A|:)(0:)+/,'::')
332:         res.gsub!(/::0\z/, '::')
333:         res
334:       end
335:     end

[Validate]