Class: Unified2::Packet
- Inherits:
-
Object
- Object
- Unified2::Packet
- Defined in:
- lib/unified2/packet.rb
Overview
Packet
Instance Attribute Summary collapse
-
#event_id ⇒ Object
readonly
Build method defaults.
-
#event_timestamp ⇒ Object
readonly
Build method defaults.
-
#length ⇒ Object
readonly
Build method defaults.
-
#link_type ⇒ Object
readonly
Build method defaults.
-
#microsecond ⇒ Object
readonly
Build method defaults.
-
#packet ⇒ Object
readonly
Build method defaults.
-
#raw ⇒ String
readonly
Raw.
-
#timestamp ⇒ Object
readonly
Build method defaults.
Instance Method Summary collapse
-
#blank? ⇒ true, false
Blank?.
-
#checksum ⇒ String
Checksum.
-
#dump(options = {}) {|index, hex_segment, print_segment| ... } ⇒ nil
Dump.
-
#eth? ⇒ true, false
(also: #ethernet?)
Ehternet.
-
#hex(include_header = true) ⇒ String
Hex.
-
#hexdump(options = {}) ⇒ Object
Hexdump.
-
#initialize(packet) ⇒ Packet
constructor
Initialize packet Object.
-
#ip_header ⇒ Hash
IP Header.
-
#ipv4? ⇒ true, false
(also: #ip?)
IP Version 4.
-
#ipv6? ⇒ true, false
IP Version 6.
-
#payload ⇒ Payload
Payload.
-
#protocol ⇒ Protocol
Protocol.
-
#to_file(filename, mode) ⇒ Object
Output to file.
- #to_h ⇒ Object
-
#to_pcap ⇒ Object
Convert to libpcap format.
-
#to_s ⇒ String
String.
-
#valid? ⇒ true, false
Valid.
Constructor Details
#initialize(packet) ⇒ Packet
Initialize packet Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/unified2/packet.rb', line 26 def initialize(packet) @raw = packet @link_type = packet[:linktype] @microsecond = packet[:packet_microsecond] @event_timestamp = Time.at(packet[:timestamp]) @timestamp = Time.at(packet[:packet_timestamp]) @length = packet[:packet_length].to_i @event_id = packet[:event_id] @packet ||= PacketFu::Packet.parse(packet[:packet]) @protocol = @packet.protocol.last.to_sym end |
Instance Attribute Details
#event_id ⇒ Object (readonly)
Build method defaults
17 18 19 |
# File 'lib/unified2/packet.rb', line 17 def event_id @event_id end |
#event_timestamp ⇒ Object (readonly)
Build method defaults
17 18 19 |
# File 'lib/unified2/packet.rb', line 17 def @event_timestamp end |
#length ⇒ Object (readonly)
Build method defaults
17 18 19 |
# File 'lib/unified2/packet.rb', line 17 def length @length end |
#link_type ⇒ Object (readonly)
Build method defaults
17 18 19 |
# File 'lib/unified2/packet.rb', line 17 def link_type @link_type end |
#microsecond ⇒ Object (readonly)
Build method defaults
17 18 19 |
# File 'lib/unified2/packet.rb', line 17 def microsecond @microsecond end |
#packet ⇒ Object (readonly)
Build method defaults
17 18 19 |
# File 'lib/unified2/packet.rb', line 17 def packet @packet end |
#timestamp ⇒ Object (readonly)
Build method defaults
17 18 19 |
# File 'lib/unified2/packet.rb', line 17 def @timestamp end |
Instance Method Details
#blank? ⇒ true, false
Blank?
149 150 151 152 |
# File 'lib/unified2/packet.rb', line 149 def blank? return true unless @packet false end |
#checksum ⇒ String
Checksum
Create a unique payload checksum
271 272 273 |
# File 'lib/unified2/packet.rb', line 271 def checksum Digest::MD5.hexdigest(hex(false)) end |
#dump(options = {}) {|index, hex_segment, print_segment| ... } ⇒ nil
Please view the hexdump documentation for more information. Hexdump is a great lib by @postmodern. (github.com/postmodern/hexdump)
Dump
237 238 239 240 241 242 243 244 245 |
# File 'lib/unified2/packet.rb', line 237 def dump(={}) packet = if [:header] @raw[:packet] else @packet.payload end Hexdump.dump(packet, ) end |
#eth? ⇒ true, false Also known as: ethernet?
Ehternet
79 80 81 |
# File 'lib/unified2/packet.rb', line 79 def eth? @packet.is_eth? end |
#hex(include_header = true) ⇒ String
Hex
184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/unified2/packet.rb', line 184 def hex(include_header=true) packet = if include_header @packet.to_s else @packet.payload.to_s end hex = packet.unpack('H*') return hex.first if hex nil end |
#hexdump(options = {}) ⇒ Object
Hexdump
255 256 257 258 259 260 261 262 |
# File 'lib/unified2/packet.rb', line 255 def hexdump(={}) hexdump = [:output] ||= "" [:width] ||= 30 [:header] ||= true dump() hexdump end |
#ip_header ⇒ Hash
IP Header
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/unified2/packet.rb', line 45 def ip_header if @packet.is_ip? ip_header = { :ip_ver => @packet.ip_header.ip_v, :ip_hlen => @packet.ip_header.ip_hl, :ip_tos => @packet.ip_header.ip_tos, :ip_len => @packet.ip_header.ip_len, :ip_id => @packet.ip_header.ip_id, :ip_frag => @packet.ip_header.ip_frag, :ip_ttl => @packet.ip_header.ip_ttl, :ip_proto => @packet.ip_header.ip_proto, :ip_csum => @packet.ip_header.ip_sum } else ip_header = {} end ip_header end |
#ipv4? ⇒ true, false Also known as: ip?
IP Version 4
89 90 91 |
# File 'lib/unified2/packet.rb', line 89 def ipv4? @packet.is_ip? end |
#ipv6? ⇒ true, false
IP Version 6
99 100 101 |
# File 'lib/unified2/packet.rb', line 99 def ipv6? @packet.is_ipv6? end |
#payload ⇒ Payload
Payload
140 141 142 |
# File 'lib/unified2/packet.rb', line 140 def payload @packet.payload end |
#protocol ⇒ Protocol
Protocol
108 109 110 |
# File 'lib/unified2/packet.rb', line 108 def protocol @proto ||= Protocol.new(@protocol, @packet) end |
#to_file(filename, mode) ⇒ Object
Output to file
131 132 133 |
# File 'lib/unified2/packet.rb', line 131 def to_file(filename, mode) @packet.to_f(filename, mode) end |
#to_h ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/unified2/packet.rb', line 163 def to_h @to_hash = { :event_timestamp => .to_s, :timestamp => .to_s, :length => length, :microsecond => microsecond, :hex => hex, :hexdump => hexdump, :checksum => checksum, :payload => payload, :link_type => link_type, :protocol => protocol.to_h, :ip_header => ip_header } end |
#to_pcap ⇒ Object
Convert to libpcap format
124 125 126 |
# File 'lib/unified2/packet.rb', line 124 def to_pcap @packet.to_pcap end |
#to_s ⇒ String
String
117 118 119 |
# File 'lib/unified2/packet.rb', line 117 def to_s payload.to_s end |
#valid? ⇒ true, false
Valid
70 71 72 |
# File 'lib/unified2/packet.rb', line 70 def valid? !@packet.is_invalid? end |