Class: PacketFu::TcpFlags

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

Overview

Implements flags for TCPHeader.

Header Definition

Fixnum (1 bit)  :urg
Fixnum (1 bit)  :ack
Fixnum (1 bit)  :psh
Fixnum (1 bit)  :rst
Fixnum (1 bit)  :syn
Fixnum (1 bit)  :fin

Flags can typically be set by setting them either to 1 or 0, or to true or false.

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 = {}) ⇒ TcpFlags

Returns a new instance of TcpFlags.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/packetfu/protos/tcp.rb', line 140

def initialize(args={})
  # This technique attemts to ensure that flags are always 0 (off)
  # or 1 (on). Statements like nil and false shouldn't be lurking in here.
  if args.nil? || args.size.zero?
    super( 0, 0, 0, 0, 0, 0)
  else
    super(
      (args[:urg] ? 1 : 0), 
      (args[:ack] ? 1 : 0), 
      (args[:psh] ? 1 : 0), 
      (args[:rst] ? 1 : 0), 
      (args[:syn] ? 1 : 0), 
      (args[:fin] ? 1 : 0)
    )
  end
end

Instance Attribute Details

#ackObject

Returns the value of attribute ack

Returns:

  • (Object)

    the current value of ack



136
137
138
# File 'lib/packetfu/protos/tcp.rb', line 136

def ack
  @ack
end

#finObject

Returns the value of attribute fin

Returns:

  • (Object)

    the current value of fin



136
137
138
# File 'lib/packetfu/protos/tcp.rb', line 136

def fin
  @fin
end

#pshObject

Returns the value of attribute psh

Returns:

  • (Object)

    the current value of psh



136
137
138
# File 'lib/packetfu/protos/tcp.rb', line 136

def psh
  @psh
end

#rstObject

Returns the value of attribute rst

Returns:

  • (Object)

    the current value of rst



136
137
138
# File 'lib/packetfu/protos/tcp.rb', line 136

def rst
  @rst
end

#synObject

Returns the value of attribute syn

Returns:

  • (Object)

    the current value of syn



136
137
138
# File 'lib/packetfu/protos/tcp.rb', line 136

def syn
  @syn
end

#urgObject

Returns the value of attribute urg

Returns:

  • (Object)

    the current value of urg



136
137
138
# File 'lib/packetfu/protos/tcp.rb', line 136

def urg
  @urg
end

Instance Method Details

#read(str) ⇒ Object

Reads a string to populate the object.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/packetfu/protos/tcp.rb', line 187

def read(str)
  force_binary(str)
  return self if str.nil?
  if 1.respond_to? :ord
    byte = str[0].ord
  else
    byte = str[0]
  end
  self[:urg] = byte & 0b00100000 == 0b00100000 ? 1 : 0
  self[:ack] = byte & 0b00010000 == 0b00010000 ? 1 : 0
  self[:psh] = byte & 0b00001000 == 0b00001000 ? 1 : 0
  self[:rst] = byte & 0b00000100 == 0b00000100 ? 1 : 0
  self[:syn] = byte & 0b00000010 == 0b00000010 ? 1 : 0
  self[:fin] = byte & 0b00000001 == 0b00000001 ? 1 : 0
  self
end

#to_iObject

Returns the TcpFlags as an integer. Also not a great candidate for to_s due to the short bitspace.



159
160
161
162
# File 'lib/packetfu/protos/tcp.rb', line 159

def to_i
  (urg.to_i << 5) + (ack.to_i << 4) + (psh.to_i << 3) + 
  (rst.to_i << 2) + (syn.to_i << 1) + fin.to_i
end

#zero_or_one(i = 0) ⇒ Object

Helper to determine if this flag is a 1 or a 0.



165
166
167
168
169
170
171
# File 'lib/packetfu/protos/tcp.rb', line 165

def zero_or_one(i=0)
  if i == 0 || i == false || i == nil
    0
  else
    1
  end
end