class PacketGen::Header::OSPFv3::IPv6Prefix

This class handles IPv6 prefixes, as defined in RFC 5340 §A.4.1. A IPv6 prefix consists of:

@author Sylvain Daubert

Public Instance Methods

from_human(str) click to toggle source

Set prefix from a human-readable string. This method cannot set {#options} field. @param [String] str @return [void]

# File lib/packetgen/header/ospfv3/ipv6_prefix.rb, line 73
def from_human(str)
  pfx, len = str.split('/')
  len = (len || 128).to_i
  addr = IPv6::Addr.new.from_human(pfx)
  ary_size = (len + 31) / 32
  ary = addr.to_a[0...ary_size * 2]
  self.prefix.clear
  ary.each_with_index do |v, i|
    if i.even?
      self.prefix << v
    else
      self.prefix.last.value = (self.prefix.last.to_i << 16) | v.to_i
    end
  end
  self.length = len
end
to_human() click to toggle source

Get human-readable prefix @return [String]

# File lib/packetgen/header/ospfv3/ipv6_prefix.rb, line 60
def to_human
  ary = prefix.map(&:to_i).map do |v|
    "#{((v >> 16) & 0xffff).to_s(16)}:#{(v & 0xffff).to_s(16)}"
  end
  pfx = ary.join(':')
  pfx += '::' if prefix.size < (128 / 32)
  "#{IPAddr.new(pfx)}/#{length}"
end