Class: PacketFu::TcpReserved

Inherits:
Struct
  • Object
show all
Includes:
StructFu
Defined in:
lib/packetfu/protos/tcp.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



95
96
97
98
99
100
# File 'lib/packetfu/protos/tcp.rb', line 95

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



91
92
93
# File 'lib/packetfu/protos/tcp.rb', line 91

def r1
  @r1
end

#r2Object

Returns the value of attribute r2



91
92
93
# File 'lib/packetfu/protos/tcp.rb', line 91

def r2
  @r2
end

#r3Object

Returns the value of attribute r3



91
92
93
# File 'lib/packetfu/protos/tcp.rb', line 91

def r3
  @r3
end

Instance Method Details

#read(str) ⇒ Object

Reads a string to populate the object.



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/packetfu/protos/tcp.rb', line 108

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.



103
104
105
# File 'lib/packetfu/protos/tcp.rb', line 103

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