Class: Ronin::Support::Binary::Packet

Inherits:
Struct show all
Defined in:
lib/ronin/support/binary/packet.rb

Overview

Represents a network packet in network byte-order.

Examples

Defining Packet Fields

class Packet < Ronin::Support::Binary::Packet

  member :flags,  :uint8
  member :src,    :uint32
  member :dst,    :uint32
  member :length, :uint32

end

Initializing Packets

From a Hash:

packet = Packet.new(
  flags:  0x0c,
  src:    IPAddr.new('1.2.3.4').to_i,
  dst:    IPAddr.new('5.6.7.8').to_i,
  length: 1024
)

From a buffer:

packet = Packet.unpack("\f\x01\x02\x03\x04\x05\x06\a\b\x00\x00\x04\x00")

Reading Fields

packet = Packet.new("\f\x01\x02\x03\x04\x05\x06\a\b\x00\x00\x04\x00")
packet[:flags]
# => 12
packet.flags
# => 12

Packing Packets

class Packet < Ronin::Support::Binary::Packet

  member :flags,  :uint8
  member :src,    :uint32
  member :dst,    :uint32
  member :length, :uint32

end

packet = Packet.new(
  flags:  0x0c,
  src:    IPAddr.new('1.2.3.4').to_i,
  dst:    IPAddr.new('5.6.7.8').to_i,
  length: 1024
)
packet.pack
# => "\f\x01\x02\x03\x04\x05\x06\a\b\x00\x00\x04\x00"

Unpacking Packets

class Packet < Ronin::Support::Binary::Packet

  member :flags,  :uint8
  member :src,    :uint32
  member :dst,    :uint32
  member :length, :uint32

end

packet = Packet.unpack("\f\x01\x02\x03\x04\x05\x06\a\b\x00\x00\x04\x00")
packet.flags
# => 12
packet.src
# => 16909060
packet.dst
# => 84281096
packet.length
# => 1024

Instance Attribute Summary

Attributes inherited from Struct

#type

Attributes included from CTypes::Mixin

#arch, #endian, #os, #type_resolver, #type_system

Attributes inherited from Memory

#string

Method Summary

Methods inherited from Struct

#[], #[]=, align, alignment, #each, has_member?, #initialize, member, members, pack, padding, platform, read_from, size, #to_a, #to_h, translate, type, type_resolver, type_system, unpack

Methods included from CTypes::Mixin

#initialize_type_system

Methods inherited from Memory

#+, #[], #[]=, #byteslice, #clear, #copy_from, #copy_to, #initialize, #pack, #read_from, #size

Constructor Details

This class inherits a constructor from Ronin::Support::Binary::Struct