Class: PacketFu::TcpReserved

Inherits:
Struct
  • Object
show all
Includes:
StructFu
Defined in:
lib/packetfu/protos/tcp/reserved.rb

Overview

Implements the Reserved bits for TCPHeader.

Header Definition

Fixnum (1 bit)  :r1
Fixnum (1 bit)  :r2
Fixnum (1 bit)  :r3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StructFu

#body=, #clone, #set_endianness, #sz, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(args = {}) ⇒ TcpReserved

Returns a new instance of TcpReserved


15
16
17
18
19
20
# File 'lib/packetfu/protos/tcp/reserved.rb', line 15

def initialize(args={})
  super(
    args[:r1] || 0,
    args[:r2] || 0,
    args[:r3] || 0) if args.kind_of? Hash
end

Instance Attribute Details

#r1Object

Returns the value of attribute r1

Returns:

  • (Object)

    the current value of r1


11
12
13
# File 'lib/packetfu/protos/tcp/reserved.rb', line 11

def r1
  @r1
end

#r2Object

Returns the value of attribute r2

Returns:

  • (Object)

    the current value of r2


11
12
13
# File 'lib/packetfu/protos/tcp/reserved.rb', line 11

def r2
  @r2
end

#r3Object

Returns the value of attribute r3

Returns:

  • (Object)

    the current value of r3


11
12
13
# File 'lib/packetfu/protos/tcp/reserved.rb', line 11

def r3
  @r3
end

Instance Method Details

#read(str) ⇒ Object

Reads a string to populate the object.


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/packetfu/protos/tcp/reserved.rb', line 28

def read(str)
  force_binary(str)
  return self if str.nil? || str.size.zero?
  if 1.respond_to? :ord
    byte = str[0].ord
  else
    byte = str[0]
  end
  self[:r1] = byte & 0b00000100 == 0b00000100 ? 1 : 0
  self[:r2] = byte & 0b00000010 == 0b00000010 ? 1 : 0
  self[:r3] = byte & 0b00000001 == 0b00000001 ? 1 : 0
  self
end

#to_iObject

Returns the Reserved field as an integer.


23
24
25
# File 'lib/packetfu/protos/tcp/reserved.rb', line 23

def to_i
  (r1.to_i << 2) + (r2.to_i << 1) + r3.to_i
end