Class: PcapSimple::PcapFile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pcap_simple.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ PcapFile

Returns a new instance of PcapFile.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/pcap_simple.rb', line 16

def initialize(*args)
  Hash[*args].each {|k,v| self.send("%s="%k,v)}
  raise ArgumentError, "need to specify file_name" if file_name.nil?
  @file=File.open(file_name,"r")
  @header=PcapHeader.new(file.read(PCAP_HEADER_LEN))
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



14
15
16
# File 'lib/pcap_simple.rb', line 14

def file
  @file
end

#file_nameObject

Returns the value of attribute file_name.



13
14
15
# File 'lib/pcap_simple.rb', line 13

def file_name
  @file_name
end

#headerObject (readonly)

Returns the value of attribute header.



14
15
16
# File 'lib/pcap_simple.rb', line 14

def header
  @header
end

Instance Method Details

#each(&block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pcap_simple.rb', line 22

def each(&block)
  file.seek(PCAP_HEADER_LEN)
  loop do
    header_data=file.read(PACKET_HEADER_LEN)
    break if (header_data.nil? || header_data.length < PACKET_HEADER_LEN)
    header=PcapRecord.new(header_data)
    raw=file.read(header.incl_len)
    break if (raw.nil? || raw.length < header.incl_len)
    packet=Packet.new(:raw_data=>raw,:header=>header)

    yield packet unless packet.datagram.nil?
  end
end