class Unified2::Protocol

Protocol

Public Class Methods

new(protocol, packet=nil) click to toggle source

Initialize protocol object

@param [String] protocol Event protocol

@param [Event#packet] packet PacketFu object

# File lib/unified2/protocol.rb, line 17
def initialize(protocol, packet=nil)
  @protocol = protocol
  @packet = packet
end

Public Instance Methods

header()
Alias for: to_h
icmp?() click to toggle source

ICMP?

@return [true, false] Check is protocol is icmp

# File lib/unified2/protocol.rb, line 27
def icmp?
  @protocol == :ICMP
end
tcp?() click to toggle source

TCP?

@return [true, false] Check is protocol is tcp

# File lib/unified2/protocol.rb, line 36
def tcp?
  @protocol == :TCP
end
to_h() click to toggle source

Convert To Hash

@return [Hash] Protocol header hash object

@example

event.protocol.to_h #=> {:length=>379, :seq=>3934511163, :ack=>1584708129 ... }
# File lib/unified2/protocol.rb, line 69
def to_h
  hash = {
    :type => @protocol.to_s
  }

  if send(:"#{@protocol.downcase}?")
    hash.merge!(self.send(:"#{@protocol.downcase}"))
  end

  hash
end
Also aliased as: header
to_s() click to toggle source

Convert To String

@return [String] Protocol

@example

event.protocol #=> 'TCP'
# File lib/unified2/protocol.rb, line 57
def to_s
  @protocol.to_s
end
udp?() click to toggle source

UDP?

@return [true, false] Check is protocol is udp

# File lib/unified2/protocol.rb, line 45
def udp?
  @protocol == :UDP
end

Private Instance Methods

hdr() click to toggle source
# File lib/unified2/protocol.rb, line 84
def hdr
  return nil unless @packet.send(:"is_#{@protocol.downcase}?")
  @packet.send(:"#{@protocol.downcase}_header")
end
icmp(include_body=false) click to toggle source
# File lib/unified2/protocol.rb, line 89
def icmp(include_body=false)
  icmp = {
    :length => hdr.len,
    :type => hdr.icmp_type,
    :csum => hdr.icmp_sum,
    :code => hdr.icmp_code
  }
  
  icmp[:body] = hdr.body if include_body
  
  icmp
end
tcp(include_body=false) click to toggle source
# File lib/unified2/protocol.rb, line 113
def tcp(include_body=false)
  tcp = {
    :length => hdr.len,
    :seq => hdr.tcp_seq,
    :ack => hdr.tcp_ack,
    :win => hdr.tcp_win,
    :csum => hdr.tcp_sum,
    :urg => hdr.tcp_urg,
    :hlen => hdr.tcp_hlen,
    :reserved => hdr.tcp_reserved,
    :ecn => hdr.tcp_ecn,
    :opts_len => hdr.tcp_opts_len,
    :rand_port => hdr.rand_port,
    :options => hdr.tcp_options
  }
  
  tcp[:body] = hdr.body if include_body
  
  tcp
end
udp(include_body=false) click to toggle source
# File lib/unified2/protocol.rb, line 102
def udp(include_body=false)
  udp = {
    :length => hdr.len,
    :csum => hdr.udp_sum,
  }
  
  udp[:body] = hdr.body if include_body
  
  udp
end