Class: TCPHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/sflow/lib/sflow/models/tcpheader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(packet, offset = 0, length = nil, lower = nil) ⇒ TCPHeader

Returns a new instance of TCPHeader.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 7

def initialize(packet,offset=0,length=nil,lower=nil)
  @packet = packet.force_encoding("ASCII-8BIT")
  @offset = offset
  @length = length || packet.bytesize-offset
  header = packet.unpack("x#{offset}n2N2n4")
  @sndr_port = header[0]
  @dest_port = header[1]
  @seq_num = header[2]
  @ack_num = header[3]
  @header_length = (header[4]>>12)*4
  @urg = (header[4] & 0b100000) != 0
  @ack = (header[4] & 0b010000) != 0
  @psh = (header[4] & 0b001000) != 0
  @rst = (header[4] & 0b000100) != 0
  @syn = (header[4] & 0b000010) != 0
  @fin = (header[4] & 0b000001) != 0
  @win_size = header[5]
  @checksum = header[6]
  @emgcy_ptr = header[7]

  @packet_length = @length
  @data_length = @packet_length-@header_length

  @lower = lower

  # check checksum
  calc_cs = false
  if calc_cs
  tmp =  @packet[@offset..@offset+@length]
  if (tmp.length % 2) != 0
    tmp += "\0"
  end
  data = @lower.get_virtual_header + tmp
  sum = 0
  list = data.unpack("n*")
  list.each do |d|
    sum += d
  end
  sum  = (sum & 0xffff) + (sum >> 16)
  sum  = (sum & 0xffff) + (sum >> 16)
  raise if sum != 65535
  end




end

Instance Attribute Details

#ackObject (readonly)

Returns the value of attribute ack.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def ack
  @ack
end

#ack_numObject (readonly)

Returns the value of attribute ack_num.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def ack_num
  @ack_num
end

#checksumObject (readonly)

Returns the value of attribute checksum.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def checksum
  @checksum
end

#data_lengthObject (readonly)

Returns the value of attribute data_length.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def data_length
  @data_length
end

#dest_portObject (readonly)

Returns the value of attribute dest_port.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def dest_port
  @dest_port
end

#emgcy_ptrObject (readonly)

Returns the value of attribute emgcy_ptr.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def emgcy_ptr
  @emgcy_ptr
end

#finObject (readonly)

Returns the value of attribute fin.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def fin
  @fin
end

#header_lengthObject (readonly)

Returns the value of attribute header_length.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def header_length
  @header_length
end

#lowerObject (readonly)

Returns the value of attribute lower.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def lower
  @lower
end

#packet_lengthObject (readonly)

Returns the value of attribute packet_length.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def packet_length
  @packet_length
end

#pshObject (readonly)

Returns the value of attribute psh.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def psh
  @psh
end

#rstObject (readonly)

Returns the value of attribute rst.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def rst
  @rst
end

#seq_numObject (readonly)

Returns the value of attribute seq_num.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def seq_num
  @seq_num
end

#sndr_portObject (readonly)

Returns the value of attribute sndr_port.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def sndr_port
  @sndr_port
end

#synObject (readonly)

Returns the value of attribute syn.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def syn
  @syn
end

#urgObject (readonly)

Returns the value of attribute urg.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def urg
  @urg
end

#win_sizeObject (readonly)

Returns the value of attribute win_size.



3
4
5
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 3

def win_size
  @win_size
end

Instance Method Details

#dataObject



55
56
57
58
59
60
61
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 55

def data
  if(@data_length>0)
    @packet[@offset+@header_length..@offset+@length]
  else
    ""
  end
end

#to_sObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sflow/lib/sflow/models/tcpheader.rb', line 63

def to_s
  "TCP Header\n" <<
  "  Sender Port     : #{@sndr_port}\n" <<
  "  Destination Port: #{@dest_port}\n" <<
  "  Sequence Number : #{@seq_num}\n" <<
  "  ACK Number      : #{@ack_num}\n" <<
  "  Header Length   : #{@header_length}\n" <<
  "  URG             : #{@urg}\n" <<
  "  ACK             : #{@ack}\n" <<
  "  PSH             : #{@psh}\n" <<
  "  RST             : #{@rst}\n" <<
  "  SYN             : #{@syn}\n" <<
  "  FIN             : #{@fin}\n" <<
  "  Window Size     : #{@win_size}\n" <<
  "  Checksum        : #{@checksum}\n" <<
  "  Emergency Ptr   : #{@emgcy_ptr}\n" <<
  "  (Packet Length) : #{@packet_length}\n" <<
  "  (Data Length)   : #{@data_length}"
end