Class: PcapTools::Loader::MyParser

Inherits:
Ox::Sax
  • Object
show all
Defined in:
lib/pcap_tools/loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(block) ⇒ MyParser

Returns a new instance of MyParser.



11
12
13
14
15
16
17
18
# File 'lib/pcap_tools/loader.rb', line 11

def initialize block
  @current_packet_index = 0
  @current_packet = nil
  @current_processing = nil
  @current_proto_name = nil
  @current_field_name = nil
  @block = block
end

Instance Method Details

#attr(name, value) ⇒ Object



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
54
55
56
57
58
# File 'lib/pcap_tools/loader.rb', line 20

def attr name, value
  if @current_processing == :proto && name == :name
    @current_proto_name = value
    @current_packet[:protos] << value
  elsif @current_processing == :field && name == :name
    @current_field_name = value
    # p @current_field_name
  elsif name == :show
    if @current_proto_name == "geninfo" && @current_field_name == "timestamp"
      @current_packet[:time] = Time.parse value
    elsif @current_proto_name == "ip" && @current_field_name == "ip.src"
      @current_packet[:from] = value
    elsif @current_proto_name == "ip" && @current_field_name == "ip.dst"
      @current_packet[:to] = value
    elsif @current_proto_name == "tcp" && @current_field_name == "tcp.len"
      @current_packet[:size] = value.to_i
    elsif @current_proto_name == "tcp" && @current_field_name == "tcp.stream"
      @current_packet[:stream] = value.to_i
    elsif @current_proto_name == "tcp" && @current_field_name == "tcp.srcport"
      @current_packet[:from_port] = value.to_i
    elsif @current_proto_name == "tcp" && @current_field_name == "tcp.dstport"
      @current_packet[:to_port] = value.to_i
    elsif @current_proto_name == "tcp" && @current_field_name == "tcp.flags.fin"
      @current_packet[:tcp_flags][:fin] = value == "1"
    elsif @current_proto_name == "tcp" && @current_field_name == "tcp.flags.reset"
      @current_packet[:tcp_flags][:rst] = value == "1"
    elsif @current_proto_name == "tcp" && @current_field_name == "tcp.flags.ack"
      @current_packet[:tcp_flags][:ack] = value == "1"
    elsif @current_proto_name == "tcp" && @current_field_name == "tcp.flags.syn"
      @current_packet[:tcp_flags][:syn] = value == "1"
    end
  elsif name == :value
    if @current_proto_name == "fake-field-wrapper" && @current_field_name == "data"
      @current_packet[:data] = [value].pack("H*")
    elsif @current_proto_name == "tcp" && @current_field_name == "tcp.segment_data"
      @current_packet[:data] = [value].pack("H*")
    end
  end
end

#end_element(name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pcap_tools/loader.rb', line 79

def end_element name
  if name == :packet
    # p @current_packet
    if @current_packet[:protos].include? "malformed"
      $stderr.puts "Malformed packet #{@current_packet_index}"
      return
    end
    raise "No data found in packet #{@current_packet_index}, protocols found #{@current_packet[:protos]}" if @current_packet[:data].nil? && @current_packet[:size] > 0
    @current_packet.delete :protos
    @block.call @current_packet_index, @current_packet
    @current_packet_index += 1
  end
end

#start_element(name, attrs = []) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pcap_tools/loader.rb', line 60

def start_element name, attrs = []
  case name
    when :packet
      @current_packet = {
        :tcp_flags => {},
        :packet_index => @current_packet_index + 1,
        :protos => [],
      }
    when :proto
      @current_processing = :proto
    when :field
      @current_processing = :field
    when :pdml
    else
      raise "Unknown element [#{name}]"
  end
end