Class: PcapTools::TcpProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/pcap_tools/packet_processors/tcp.rb

Instance Method Summary collapse

Constructor Details

#initializeTcpProcessor

Returns a new instance of TcpProcessor.



7
8
9
10
# File 'lib/pcap_tools/packet_processors/tcp.rb', line 7

def initialize
  @streams = {}
  @stream_processors = []
end

Instance Method Details

#add_stream_processor(processor) ⇒ Object



12
13
14
# File 'lib/pcap_tools/packet_processors/tcp.rb', line 12

def add_stream_processor processor
  @stream_processors << processor
end

#finalizeObject



48
49
50
51
52
# File 'lib/pcap_tools/packet_processors/tcp.rb', line 48

def finalize
  @stream_processors.each do |p|
    p.finalize
  end
end

#inject(index, packet) ⇒ Object



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
# File 'lib/pcap_tools/packet_processors/tcp.rb', line 16

def inject index, packet
  stream_index = packet[:stream]
  if stream_index
    if packet[:tcp_flags][:syn] && packet[:tcp_flags][:ack] === false
      @streams[stream_index] = {
        :first => packet,
        :data => [],
        :tcp_lost_segment => false,
      }
    elsif packet[:tcp_flags][:fin] || packet[:tcp_flags][:rst]
      if @streams[stream_index]
        current = {:index => stream_index, :data => @streams[stream_index][:data]}
        @stream_processors.each do |p|
          current = p.process_stream current
          break unless current
        end
        @streams.delete stream_index
      end
    else
      if @streams[stream_index]
        packet[:type] = (packet[:from] == @streams[stream_index][:first][:from] && packet[:from_port] == @streams[stream_index][:first][:from_port]) ? :out : :in
        packet.delete :tcp_flags
        @streams[stream_index][:data] << packet if packet[:size] > 0
        if packet[:tcp_lost_segment]
          @streams.delete stream_index
          $stderr.puts "Ignoring tcp stream #{stream_index}, tcp segments are missing"
        end
      end
    end
  end
end