class Metasploit::Aggregator::Tlv::GroupTlv

Group TLVs contain zero or more TLVs

Attributes

tlvs[RW]

Public Class Methods

new(type) click to toggle source

Initializes the group TLV container to the supplied type and creates an empty TLV array.

Calls superclass method Metasploit::Aggregator::Tlv::Tlv::new
# File lib/metasploit/aggregator/tlv/packet.rb, line 383
def initialize(type)
  super(type)

  self.tlvs = []
end

Public Instance Methods

add_tlv(type, value = nil, replace = false, compress=false) click to toggle source

Adds a TLV of a given type and value.

# File lib/metasploit/aggregator/tlv/packet.rb, line 451
def add_tlv(type, value = nil, replace = false, compress=false)

  # If we should replace any TLVs with the same type...remove them first
  if replace
    each(type) { |tlv|
      if (tlv.type == type)
        self.tlvs.delete(tlv)
      end
    }
  end

  if (type & TLV_META_TYPE_GROUP == TLV_META_TYPE_GROUP)
    tlv = GroupTlv.new(type)
  else
    tlv = Tlv.new(type, value, compress)
  end

  self.tlvs << tlv

  tlv
end
add_tlvs(tlvs) click to toggle source

Adds zero or more TLVs to the packet.

# File lib/metasploit/aggregator/tlv/packet.rb, line 476
def add_tlvs(tlvs)
  if tlvs
    tlvs.each { |tlv|
      add_tlv(tlv['type'], tlv['value'])
    }
  end
end
each(type = TLV_TYPE_ANY, &block) click to toggle source

Enumerates TLVs of the supplied type.

# File lib/metasploit/aggregator/tlv/packet.rb, line 398
def each(type = TLV_TYPE_ANY, &block)
  get_tlvs(type).each(&block)
end
each_tlv(type = TLV_TYPE_ANY, &block) click to toggle source

Synonym for each.

# File lib/metasploit/aggregator/tlv/packet.rb, line 405
def each_tlv(type = TLV_TYPE_ANY, &block)
  each(type, &block)
end
each_tlv_with_index(type = TLV_TYPE_ANY, &block) click to toggle source

Synonym for each_with_index.

# File lib/metasploit/aggregator/tlv/packet.rb, line 419
def each_tlv_with_index(type = TLV_TYPE_ANY, &block)
  each_with_index(type, block)
end
each_with_index(type = TLV_TYPE_ANY, &block) click to toggle source

Enumerates TLVs of a supplied type with indexes.

# File lib/metasploit/aggregator/tlv/packet.rb, line 412
def each_with_index(type = TLV_TYPE_ANY, &block)
  get_tlvs(type).each_with_index(&block)
end
from_r(raw) click to toggle source

Converts the TLV group container from raw to all of the individual TLVs.

# File lib/metasploit/aggregator/tlv/packet.rb, line 552
def from_r(raw)
  offset = HEADER_SIZE

  # Reset the TLVs array
  self.tlvs = []
  self.type = raw.unpack("NN")[1]

  # Enumerate all of the TLVs
  while offset < raw.length-1

    tlv = nil

    # Get the length and type
    length, type = raw[offset..offset+HEADER_SIZE].unpack("NN")

    if (type & TLV_META_TYPE_GROUP == TLV_META_TYPE_GROUP)
      tlv = GroupTlv.new(type)
    else
      tlv = Tlv.new(type)
    end

    tlv.from_r(raw[offset..offset+length])

    # Insert it into the list of TLVs
    tlvs << tlv

    # Move up
    offset += length
  end
end
get_tlv(type, index = 0) click to toggle source

Gets the first TLV of a given type.

# File lib/metasploit/aggregator/tlv/packet.rb, line 487
def get_tlv(type, index = 0)
  type_tlvs = get_tlvs(type)

  if type_tlvs.length > index
    type_tlvs[index]
  else
    nil
  end

end
get_tlv_value(type, index = 0) click to toggle source

Returns the value of a TLV if it exists, otherwise nil.

# File lib/metasploit/aggregator/tlv/packet.rb, line 501
def get_tlv_value(type, index = 0)
  tlv = get_tlv(type, index)

  (tlv != nil) ? tlv.value : nil
end
get_tlv_values(type) click to toggle source

Returns an array of values for all tlvs of type type.

# File lib/metasploit/aggregator/tlv/packet.rb, line 510
def get_tlv_values(type)
  get_tlvs(type).collect { |a| a.value }
end
get_tlvs(type) click to toggle source

Returns an array of TLVs for the given type.

# File lib/metasploit/aggregator/tlv/packet.rb, line 426
def get_tlvs(type)
  if type == TLV_TYPE_ANY
    self.tlvs
  else
    type_tlvs = []

    self.tlvs.each() { |tlv|
      if (tlv.type?(type))
        type_tlvs << tlv
      end
    }

    type_tlvs
  end
end
has_tlv?(type) click to toggle source

Checks to see if the container has a TLV of a given type.

# File lib/metasploit/aggregator/tlv/packet.rb, line 517
def has_tlv?(type)
  get_tlv(type) != nil
end
reset() click to toggle source

Zeros out the array of TLVs.

# File lib/metasploit/aggregator/tlv/packet.rb, line 524
def reset
  self.tlvs = []
end
to_r() click to toggle source

Converts all of the TLVs in the TLV array to raw and prefixes them with a container TLV of this instance's TLV type.

# File lib/metasploit/aggregator/tlv/packet.rb, line 538
def to_r
  raw = ''

  self.each() { |tlv|
    raw << tlv.to_r
  }

  [raw.length + HEADER_SIZE, self.type].pack("NN") + raw
end