Method: PacketGen::Packet#parse

Defined in:
lib/packetgen/packet.rb

#parse(binary_str, first_header: nil) ⇒ self

Parse a binary string and populate Packet from it.

Parameters:

  • binary_str (String)
  • first_header (String, nil) (defaults to: nil)

    First protocol header name. nil means discover it!

Returns:

  • (self)

Raises:



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/packetgen/packet.rb', line 340

def parse(binary_str, first_header: nil)
  headers.clear

  if first_header.nil?
    # No decoding forced for first header. Have to guess it!
    first_header = guess_first_header(binary_str)
    raise ParseError, "cannot identify first header in string: #{binary_str.inspect}" if first_header.nil?
  end

  add(first_header)
  headers[-1, 1] = last_header.read(binary_str)

  # Decode upper headers recursively
  decode_bottom_up
  self
end