#!/usr/bin/env ruby

require 'RubyLibtrace'

# 1925, Sun 6 Apr 08 (PDT)
# trace_test.rb: Demonstrates a few trace-management methods; 
#                also shows how to use ruby-libtrace Global Constants
# Copyright (C) 2008, Nevil Brownlee, U Auckland | CAIDA | Wand


print "TRACE_ERR constants: TRACE_ERR_NOERROR = #{TRACE_ERR_NOERROR}\n"
print "TRACE_DLT constants: TRACE_DLT_PPP = #{TRACE_TYPE_PPP}\n"
print "TRACE_OPTION constants: TRACE_OPTION_FILTER = #{TRACE_OPTION_FILTER}\n\n"


def print_addresses(ver, src, dst)
  print "IPv#{ver}: #{src} ->  #{dst}\n"
end

f = Trace.new(ARGV[0])
f.start

n = nip = 0
f.each_packet do |pkt|
   n += 1

   ip6 = pkt.ip6
   ip = pkt.ip
   if ip6 || ip
      nip += 1
      print "packet #{n}: "
      print_addresses(6, ip6.src_prefix, ip6.dst_prefix) if ip6
      print_addresses(4, ip.src_prefix, ip.dst_prefix) if ip
   end

   break if nip == 10
end

print "f.packet_drops=#{f.packet_drops}\n\n"
f.close


begin
   f = Trace.new('foo.bar')  # No such file
   f.start  # libtrace doesn't see error until it tries to open the trace
rescue => e
  print "e=#{e}, e.class=#{e.class}\n\n"
end

f = Trace.new('foo.bar')  # Don't catch it this time
f.start

