class Sip::Capture::Hep3

Constants

GENERIC_VENDOR_ID
HEP3_FIELDS
SIGNATURE

Public Class Methods

capture_all(host, port, &block) click to toggle source
# File lib/sip/capture/hep3.rb, line 11
def self.capture_all(host, port, &block)
  h = Hep3.new
  h.bind(host, port)
  h.capture_all(&block)
end

Public Instance Methods

bind(host, port) click to toggle source
# File lib/sip/capture/hep3.rb, line 17
def bind(host, port)
  @socket = UDPSocket.new
  @socket.bind(host, port)
end
capture_all(&block) click to toggle source
# File lib/sip/capture/hep3.rb, line 28
def capture_all(&block)
  @in_loop = true
  while @in_loop do
    begin
      data = @socket.recvfrom_nonblock(65535)  #=> ["aaa", ["AF_INET", 33302, "localhost.localdomain", "127.0.0.1"]]
      process_data data[0], &block
    rescue IO::WaitReadable
      IO.select([@socket], [], [], 10)
      retry
    end
  end
end
process_data(data) { |item| ... } click to toggle source
# File lib/sip/capture/hep3.rb, line 22
def process_data(data)
  if (item = parse(data))
    yield item if block_given?
  end
end
stop() click to toggle source
# File lib/sip/capture/hep3.rb, line 41
def stop
  @in_loop = false
end

Private Instance Methods

hep3?(data) click to toggle source
# File lib/sip/capture/hep3.rb, line 86
def hep3?(data)
  data[0..3] == SIGNATURE
end
parse(data) click to toggle source
# File lib/sip/capture/hep3.rb, line 65
def parse(data)
  # See https://github.com/sipcapture/HEP
  if hep3?(data)
    item = {}
    length = data[4..5].unpack("S>").first
    pos = 6
    while pos < length do
      vendor_id, type_id, chunk_length = data[pos..pos+5].unpack("S>S>S>")
      payload = data[pos+6...pos+chunk_length]
      if vendor_id == GENERIC_VENDOR_ID
        if (info = HEP3_FIELDS[type_id])
          name, parse_method = info
          item[name] = send(:"parse_#{parse_method}", payload)
        end
      end
      pos += chunk_length
    end
    item
  end
end
parse_ipv4(payload) click to toggle source
# File lib/sip/capture/hep3.rb, line 102
def parse_ipv4(payload)
  payload.unpack("CCCC").join(".")
end
parse_ipv6(payload) click to toggle source
# File lib/sip/capture/hep3.rb, line 106
  def parse_ipv6(payload)
#    payload.unpack("CCCCCC").map{}.join(".")
    nil
  end
parse_str(payload) click to toggle source
# File lib/sip/capture/hep3.rb, line 111
def parse_str(payload)
  payload.force_encoding('UTF-8')
end
parse_uint16(payload) click to toggle source
# File lib/sip/capture/hep3.rb, line 94
def parse_uint16(payload)
  payload.unpack("S>").first
end
parse_uint32(payload) click to toggle source
# File lib/sip/capture/hep3.rb, line 98
def parse_uint32(payload)
  payload.unpack("L>").first
end
parse_uint8(payload) click to toggle source
# File lib/sip/capture/hep3.rb, line 90
def parse_uint8(payload)
  payload.unpack("C").first
end