class Havox::Policy

Constants

ETHERTYPE_IP
MERLIN_ETHERTYPE
MERLIN_IP_DST
MERLIN_IP_SRC

Attributes

rules[R]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/havox/classes/policy.rb, line 5
def initialize(opts = {})
  @opts = opts
  @rules = nil
  generate_rules
  check_ip_netmasks if Havox::Network.reachable.any?
end

Public Instance Methods

to_json() click to toggle source
# File lib/havox/classes/policy.rb, line 12
def to_json
  @rules.map(&:to_h).to_json
end

Private Instance Methods

check_ip_netmasks() click to toggle source
# File lib/havox/classes/policy.rb, line 31
def check_ip_netmasks
  @rules.each do |r|
    r.matches[src_ip] = netmasked_or_nil(r.matches[src_ip]) if r.matches.key?(src_ip)
    r.matches[dst_ip] = netmasked_or_nil(r.matches[dst_ip]) if r.matches.key?(dst_ip)
    delete_ip_match(r.matches, src_ip) if has_key_but_nil?(r.matches, src_ip)
    delete_ip_match(r.matches, dst_ip) if has_key_but_nil?(r.matches, dst_ip)
  end
end
delete_ip_match(matches, target_ip_key) click to toggle source
# File lib/havox/classes/policy.rb, line 52
def delete_ip_match(matches, target_ip_key)
  matches.delete(target_ip_key)
  matches[ethertype] = ETHERTYPE_IP unless matches.key?(ethertype)
end
dst_ip() click to toggle source
# File lib/havox/classes/policy.rb, line 44
def dst_ip
  Havox::Translator.instance.fields_to(@opts[:syntax])[MERLIN_IP_DST]
end
ethertype() click to toggle source
# File lib/havox/classes/policy.rb, line 48
def ethertype
  Havox::Translator.instance.fields_to(@opts[:syntax])[MERLIN_ETHERTYPE]
end
generate_rules() click to toggle source
# File lib/havox/classes/policy.rb, line 23
def generate_rules
  @rules = Havox::Merlin.compile!(
    @opts[:merlin_topology],
    @opts[:merlin_policy],
    @opts
  )
end
has_key_but_nil?(matches, target_key) click to toggle source
# File lib/havox/classes/policy.rb, line 64
def has_key_but_nil?(matches, target_key)
  matches.key?(target_key) && matches[target_key].nil?
end
netmasked_or_nil(ip) click to toggle source
# File lib/havox/classes/policy.rb, line 57
def netmasked_or_nil(ip)
  Havox::Network.reachable.each do |network|
    return network if IPAddr.new(network).include?(ip)
  end
  nil
end
src_ip() click to toggle source
# File lib/havox/classes/policy.rb, line 40
def src_ip
  Havox::Translator.instance.fields_to(@opts[:syntax])[MERLIN_IP_SRC]
end