Module: PacketGen::StructFu

Overview

StructFu, a nifty way to leverage Ruby’s built in Struct class to create meaningful binary data.

Copied from PacketFu:

Copyright © 2008-2014, Tod Beardsley All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.
* Neither the name of Tod Beardsley nor the
  names of its contributors may be used to endorse or promote products
  derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY TOD BEARDSLEY ”AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL TOD BEARDSLEY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Defined Under Namespace

Classes: Int, Int16, Int16be, Int16le, Int32, Int32be, Int32le, Int64, Int64be, Int64le, Int8, IntString, String

Instance Method Summary collapse

Instance Method Details

#body=(i) ⇒ Object

Used like typecast(), but specifically for casting Strings to StructFu::Strings.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/packetgen/structfu.rb', line 53

def body=(i)
  if i.kind_of? ::String
    typecast(i)
  elsif i.kind_of? StructFu
    self[:body] = i
  elsif i.nil?
    self[:body] = StructFu::String.new.read("")
  else
    raise ArgumentError, "Can't cram a #{i.class} into a StructFu :body"
  end
end

#cloneObject

Handle deep copies correctly.



66
67
68
# File 'lib/packetgen/structfu.rb', line 66

def clone
  Marshal.load(Marshal.dump(self))
end

#set_endianness(e) ⇒ :little, :big

Set the endianness for the various Int classes handled by self

Parameters:

  • e (:little, :big)

Returns:

  • (:little, :big)

    returns e



73
74
75
76
77
78
79
80
81
# File 'lib/packetgen/structfu.rb', line 73

def set_endianness(e)
  unless [:little, :big].include? e
    raise ArgumentError, "unknown endianness for #{self.class}"
  end
  @int64 = e == :little ? Int64le : Int64be
  @int32 = e == :little ? Int32le : Int32be
  @int16 = e == :little ? Int16le : Int16be
  e
end

#szObject Also known as: len

Normally, self.size and self.length will refer to the Struct size as an array. It’s a hassle to redefine, so this introduces some shorthand to get at the size of the resultant string.



38
39
40
# File 'lib/packetgen/structfu.rb', line 38

def sz
  self.to_s.size
end

#to_sString

Get binary string

Returns:



85
86
87
# File 'lib/packetgen/structfu.rb', line 85

def to_s
  to_a.map { |field| field.to_s }.join
end

#typecast(i) ⇒ Object

Typecast is used mostly by packet header classes, such as IPHeader, TCPHeader, and the like. It takes an argument, and casts it to the expected type for that element.



47
48
49
50
# File 'lib/packetgen/structfu.rb', line 47

def typecast(i)
  c = caller[0].match(/.*`([^']+)='/)[1]
  self[c.intern].read i
end