Class: PacketFu::TcpFlags
- Includes:
- StructFu
- Defined in:
- lib/packetfu/protos/tcp/flags.rb
Overview
Implements flags for TCPHeader.
Header Definition
Integer(1 bit) :urg
Integer(1 bit) :ack
Integer(1 bit) :psh
Integer(1 bit) :rst
Integer(1 bit) :syn
Integer(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
-
#ack ⇒ Object
Returns the value of attribute ack.
-
#fin ⇒ Object
Returns the value of attribute fin.
-
#psh ⇒ Object
Returns the value of attribute psh.
-
#rst ⇒ Object
Returns the value of attribute rst.
-
#syn ⇒ Object
Returns the value of attribute syn.
-
#urg ⇒ Object
Returns the value of attribute urg.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ TcpFlags
constructor
A new instance of TcpFlags.
-
#read(str) ⇒ Object
Reads a string to populate the object.
-
#to_i ⇒ Object
Returns the TcpFlags as an integer.
-
#zero_or_one(i = 0) ⇒ Object
Helper to determine if this flag is a 1 or a 0.
Methods included from StructFu
#body=, #clone, #set_endianness, #sz, #typecast
Methods inherited from Struct
Constructor Details
#initialize(args = {}) ⇒ TcpFlags
Returns a new instance of TcpFlags.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/packetfu/protos/tcp/flags.rb', line 19 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
#ack ⇒ Object
Returns the value of attribute ack
15 16 17 |
# File 'lib/packetfu/protos/tcp/flags.rb', line 15 def ack @ack end |
#fin ⇒ Object
Returns the value of attribute fin
15 16 17 |
# File 'lib/packetfu/protos/tcp/flags.rb', line 15 def fin @fin end |
#psh ⇒ Object
Returns the value of attribute psh
15 16 17 |
# File 'lib/packetfu/protos/tcp/flags.rb', line 15 def psh @psh end |
#rst ⇒ Object
Returns the value of attribute rst
15 16 17 |
# File 'lib/packetfu/protos/tcp/flags.rb', line 15 def rst @rst end |
#syn ⇒ Object
Returns the value of attribute syn
15 16 17 |
# File 'lib/packetfu/protos/tcp/flags.rb', line 15 def syn @syn end |
#urg ⇒ Object
Returns the value of attribute urg
15 16 17 |
# File 'lib/packetfu/protos/tcp/flags.rb', line 15 def urg @urg end |
Instance Method Details
#read(str) ⇒ Object
Reads a string to populate the object.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/packetfu/protos/tcp/flags.rb', line 66 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_i ⇒ Object
Returns the TcpFlags as an integer. Also not a great candidate for to_s due to the short bitspace.
38 39 40 41 |
# File 'lib/packetfu/protos/tcp/flags.rb', line 38 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.
44 45 46 47 48 49 50 |
# File 'lib/packetfu/protos/tcp/flags.rb', line 44 def zero_or_one(i=0) if i == 0 || i == false || i == nil 0 else 1 end end |