#!/usr/bin/ruby
#
# Details: Fgen subsystem, Naive rule generator. Outputs a very basic
#	   definition of a pattern, including IP addresses, ports,
#	   IP protocol and datagram size if possible.
# Authors: Dmitry Maksyoma <dmaks@esphion.com>
# Started: 18/01/2006
#
# (c) 2005 Esphion Limited
# Copyright in this document, whether in written, electronic or other
# format including any source code or other computer code set forth
# therein or attached thereto and copyright in all parts thereof is owned
# by Esphion Limited, who reserves all rights therein.
# In particular, no part of this document/computer code may be reproduced,
# copied, stored in a retrieval system, used or transmitted by any means
# whatsoever without the prior written consent of Esphion Limited.
#
# All enquiries in relation thereto should be directed to:
#
# Esphion Ltd
# P.O. Box 300496,
# Albany,
# Auckland,
# New Zealand.
#
# Phone: +64 9 415 0227
# Fax:	 +64 9 415 0228
# Email: info@esphion.com

$:.insert(-2, File.join(File.dirname(File.expand_path($0)), "../lib"))
require 'pattern/entities'
require 'fgen/common'
include Entities

def ftm_ip(ip)
  ip.map { |octet| octet or 'x' }.join('.')
end

def format_pattern(pattern, name, src_ips, dst_ips)
  return unless ipl = pattern.ip

  ippl = pattern.ipp
  src_port, dst_port = ippl.srcport, ippl.dstport \
    if ippl && ippl.has_fld?(:srcport)
  proto = IPHeader.proto_desc[ipl.proto]

  ftm_ip(ipl[:srcip]) << (src_port ? ':' + src_port.to_s : '') << ' > ' <<
    ftm_ip(ipl[:dstip]) << (dst_port ? ':' + dst_port.to_s : '') <<
    (proto ? ' ' + proto : '') << 
    (ipl.total_len ? ' ' + ipl.total_len.to_s : '')
end

do_format IP_PROTO_LAYER
