Class: PacketGen::PcapNG::File
- Inherits:
-
Object
- Object
- PacketGen::PcapNG::File
- Defined in:
- lib/packetgen/pcapng/file.rb
Overview
PcapNG::File is a complete Pcap-NG file handler.
Constant Summary collapse
- KNOWN_LINK_TYPES =
Known link types
{ LINKTYPE_ETHERNET => 'Eth', LINKTYPE_IEEE802_11 => 'Dot11', LINKTYPE_IEEE802_11_RADIOTAP => 'RadioTap', LINKTYPE_PPI => 'PPI', LINKTYPE_IPV4 => 'IP', LINKTYPE_IPV6 => 'IPv6' }.freeze
- BLOCK_TYPES =
PcapNG.constants(false).select { |c| c.to_s.include?('_TYPE') }.to_h do |c| type_value = PcapNG.const_get(c).to_i klass = PcapNG.const_get(c.to_s.delete_suffix('_TYPE')) [type_value, klass] end.freeze
Instance Attribute Summary collapse
-
#sections ⇒ Array
Get file sections.
Instance Method Summary collapse
-
#append(filename = 'out.pcapng') ⇒ Array
Shorthand method for appending to a file.
-
#array_to_file(options = {}) ⇒ Object
deprecated
Deprecated.
Prefer use of #read_array or #read_hash.
-
#clear ⇒ void
Clear the contents of the Pcapng::File.
- #file_to_array(options = {}) ⇒ Array<Packet>, Array<Hash> deprecated Deprecated.
-
#initialize ⇒ File
constructor
A new instance of File.
- #inspect ⇒ String
-
#read(str) ⇒ self
Read a string to populate the object.
-
#read!(str) ⇒ self
Clear the contents of the Pcapng::File prior to reading in a new string.
-
#read_array(packets, timestamp: nil, ts_inc: nil) ⇒ self
Update current object from an array of packets.
-
#read_hash(hsh) ⇒ self
Update current object from a hash of packets and timestamps.
-
#read_packet_bytes(fname, &blk) ⇒ Object
Give an array of raw packets (raw data from packets).
-
#read_packets(fname, &blk) ⇒ Object
Return an array of parsed packets.
-
#readfile(fname) {|block| ... } ⇒ Integer
Read a given file and analyze it.
-
#to_a ⇒ Array<Packet>
Translates a File into an array of packets.
-
#to_file(filename, options = {}) ⇒ Array
(also: #to_f)
Writes the File to a file.
-
#to_h ⇒ Hash{Time => Packet}
Translates a File into a hash with timestamps as keys.
-
#to_s ⇒ String
Return the object as a String.
-
#write(filename = 'out.pcapng') ⇒ Array
Shorthand method for writing to a file.
Constructor Details
#initialize ⇒ File
Returns a new instance of File.
35 36 37 |
# File 'lib/packetgen/pcapng/file.rb', line 35 def initialize @sections = [] end |
Instance Attribute Details
#sections ⇒ Array
Get file sections
33 34 35 |
# File 'lib/packetgen/pcapng/file.rb', line 33 def sections @sections end |
Instance Method Details
#append(filename = 'out.pcapng') ⇒ Array
Shorthand method for appending to a file.
222 223 224 |
# File 'lib/packetgen/pcapng/file.rb', line 222 def append(filename='out.pcapng') self.to_file(filename.to_s, append: true) end |
#array_to_file(ary) ⇒ self #array_to_file(options = {}) ⇒ Array
Prefer use of #read_array or #read_hash.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/packetgen/pcapng/file.rb', line 244 def array_to_file(={}) filename, ary, ts, ts_inc, append = () section = create_new_shb_section ary.each do |pkt| classify_block(section, epb_from_pkt(pkt, section, ts)) ts += ts_inc end if filename self.to_f(filename, append: append) else self end end |
#clear ⇒ void
This method returns an undefined value.
Clear the contents of the Pcapng::File.
139 140 141 |
# File 'lib/packetgen/pcapng/file.rb', line 139 def clear @sections.clear end |
#file_to_array(options = {}) ⇒ Array<Packet>, Array<Hash>
Translates a PacketGen::PcapNG::File into an array of packets.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/packetgen/pcapng/file.rb', line 153 def file_to_array(={}) Deprecation.deprecated(self.class, __method__) file = [:file] || [:filename] reread file ary = [] blk = if [:keep_timestamps] || [:keep_ts] proc { |pkt| { pkt. => pkt.data.to_s } } else proc { |pkt| pkt.data.to_s } end each_packet_with_interface do |pkt, _itf| ary << blk.call(pkt) end ary end |
#inspect ⇒ String
296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/packetgen/pcapng/file.rb', line 296 def inspect str = +'' sections.each do |section| str << section.inspect section.interfaces.each do |itf| str << itf.inspect itf.packets.each { |block| str << block.inspect } end end str end |
#read(str) ⇒ self
Read a string to populate the object. Note that this appends new blocks to the Pcapng::File object.
43 44 45 46 47 48 |
# File 'lib/packetgen/pcapng/file.rb', line 43 def read(str) PacketGen.force_binary(str) io = StringIO.new(str) parse_section(io) self end |
#read!(str) ⇒ self
Clear the contents of the Pcapng::File prior to reading in a new string. This string should contain a Section Header Block and an Interface Description Block to create a conform pcapng file.
55 56 57 58 |
# File 'lib/packetgen/pcapng/file.rb', line 55 def read!(str) clear read(str) end |
#read_array(packets, timestamp: nil, ts_inc: nil) ⇒ self
270 271 272 273 274 275 276 277 278 279 |
# File 'lib/packetgen/pcapng/file.rb', line 270 def read_array(packets, timestamp: nil, ts_inc: nil) ts = section = create_new_shb_section packets.each do |pkt| block = create_block_from_pkt(pkt, section, ts, ts_inc) classify_block(section, block) ts = update_ts(ts, ts_inc) end self end |
#read_hash(hsh) ⇒ self
Update current object from a hash of packets and timestamps
285 286 287 288 289 290 291 292 |
# File 'lib/packetgen/pcapng/file.rb', line 285 def read_hash(hsh) section = create_new_shb_section hsh.each do |ts, pkt| block = create_block_from_pkt(pkt, section, ts, 0) classify_block(section, block) end self end |
#read_packet_bytes(fname) ⇒ Array #read_packet_bytes(fname) {|raw, interface's| ... } ⇒ Integer
Give an array of raw packets (raw data from packets). If a block is given, yield raw packet data from the given file.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/packetgen/pcapng/file.rb', line 92 def read_packet_bytes(fname, &blk) packets = [] unless blk count = readfile(fname) do |packet| if blk yield packet.data.to_s, packet.interface.link_type else packets << packet.data.to_s end end blk ? count : packets end |
#read_packets(fname) ⇒ Array<Packet> #read_packets(fname) {|packet| ... } ⇒ Integer
Return an array of parsed packets. If a block is given, yield parsed packets from the given file.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/packetgen/pcapng/file.rb', line 116 def read_packets(fname, &blk) packets = [] unless blk count = read_packet_bytes(fname) do |packet, link_type| parsed_pkt = parse_packet(packet, link_type) if blk yield parsed_pkt else packets << parsed_pkt end end blk ? count : packets end |
#readfile(fname) {|block| ... } ⇒ Integer
Read a given file and analyze it. If given a block, it will yield PcapNG::EPB or PcapNG::SPB objects. This is the only way to get packet timestamps.
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/packetgen/pcapng/file.rb', line 67 def readfile(fname, &blk) raise ArgumentError, "cannot read file #{fname}" unless ::File.readable?(fname) ::File.open(fname, 'rb') { |f| parse_section(f) until f.eof? } return unless blk count = 0 each_packet_with_interface do |pkt, _itf| count += 1 yield pkt end count end |
#to_a ⇒ Array<Packet>
Translates a PacketGen::PcapNG::File into an array of packets.
175 176 177 178 179 180 181 182 |
# File 'lib/packetgen/pcapng/file.rb', line 175 def to_a ary = [] each_packet_with_interface do |pkt, itf| ary << parse_packet(pkt.data.to_s, itf.link_type) end ary end |
#to_file(filename, options = {}) ⇒ Array Also known as: to_f
for 4.0, replace options
by append
kwarg
Writes the PacketGen::PcapNG::File to a file.
205 206 207 208 209 |
# File 'lib/packetgen/pcapng/file.rb', line 205 def to_file(filename, ={}) mode = [:append] && ::File.exist?(filename) ? 'ab' : 'wb' ::File.open(filename, mode) { |f| f.write(self.to_s) } [filename, self.to_s.size] end |
#to_h ⇒ Hash{Time => Packet}
Translates a PacketGen::PcapNG::File into a hash with timestamps as keys.
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/packetgen/pcapng/file.rb', line 188 def to_h hsh = {} each_packet_with_interface do |pkt, itf| next if pkt.is_a?(SPB) hsh[pkt.] = parse_packet(pkt.data.to_s, itf.link_type) end hsh end |
#to_s ⇒ String
Return the object as a String
133 134 135 |
# File 'lib/packetgen/pcapng/file.rb', line 133 def to_s @sections.map(&:to_s).join end |
#write(filename = 'out.pcapng') ⇒ Array
Shorthand method for writing to a file.
215 216 217 |
# File 'lib/packetgen/pcapng/file.rb', line 215 def write(filename='out.pcapng') self.to_file(filename.to_s, append: false) end |