Class: IPTables::ChainComparison

Inherits:
Object
  • Object
show all
Defined in:
lib/iptables/tables.rb

Instance Method Summary collapse

Constructor Details

#initialize(chain1, chain2) ⇒ ChainComparison

Returns a new instance of ChainComparison.



517
518
519
520
521
522
523
524
525
# File 'lib/iptables/tables.rb', line 517

def initialize(chain1, chain2)
  raise "must provide two chains" unless (chain1.class == IPTables::Chain) and (chain2.class == IPTables::Chain)
  raise "first and second chain should have same name" unless chain1.name == chain2.name
  @chain1 = chain1
  @chain2 = chain2

  @including_comments = true
  @compared = false
end

Instance Method Details

#as_arrayObject



583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# File 'lib/iptables/tables.rb', line 583

def as_array
  self.compare
  array = []
  return array if self.equal?
  array << "Changed chain: #{@chain1.name}"
  array << "New policy: #{@chain2.policy}" if self.new_policy?
  if self.missing.any?
    self.missing.keys.sort.each{ |rule_num|
      array << "-#{rule_num}: #{self.missing[rule_num]}"
    }
  end
  if self.new.any?
    self.new.keys.sort.each{ |rule_num|
      array << "+#{rule_num}: #{self.new[rule_num]}"
    }
  end
  return array
end

#compareObject



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/iptables/tables.rb', line 537

def compare
  return if @compared

  @equal = true

  @missing_rules = {}
  @new_rules = {}
  Diff::LCS.diff(
    @chain1.as_array(@including_comments), 
    @chain2.as_array(@including_comments)
  ).each{ |diffgroup|
    diffgroup.each{ |diff|
      if diff.action == '-'
        @missing_rules[diff.position] = diff.element
      else
        @new_rules[diff.position] = diff.element
      end
      @equal = false
    }
  }

  @new_policy = false
  unless @chain1.policy == @chain2.policy
    @new_policy = true
    @equal = false
  end

  @compared = true
  return nil
end

#equal?Boolean

Returns:



568
569
570
571
# File 'lib/iptables/tables.rb', line 568

def equal?
  self.compare
  return @equal
end

#ignore_commentsObject



527
528
529
530
# File 'lib/iptables/tables.rb', line 527

def ignore_comments
  @including_comments = false
  @compared = false
end

#include_commentsObject



532
533
534
535
# File 'lib/iptables/tables.rb', line 532

def include_comments
  @including_comments = true
  @compared = false
end

#missingObject



573
574
575
576
# File 'lib/iptables/tables.rb', line 573

def missing
  self.compare
  return @missing_rules
end

#newObject



578
579
580
581
# File 'lib/iptables/tables.rb', line 578

def new
  self.compare
  return @new_rules
end

#new_policy?Boolean

Returns:



602
603
604
605
# File 'lib/iptables/tables.rb', line 602

def new_policy?
  self.compare
  return @new_policy
end