Class: IPScriptables::Table

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/ipscriptables/table.rb,
lib/ipscriptables/pretty_print.rb

Constant Summary collapse

BUILTIN_CHAINS =
{
  filter:   [:INPUT, :FORWARD, :OUTPUT],
  nat:      [:PREROUTING, :INPUT, :OUTPUT, :POSTROUTING],
  mangle:   [:PREROUTING, :INPUT, :OUTPUT, :FORWARD, :POSTROUTING],
  raw:      [:PREROUTING, :OUTPUT],
  security: [:INPUT, :OUTPUT, :FORWARD]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ruleset, &block) ⇒ Table

Returns a new instance of Table.



12
13
14
15
16
17
18
19
20
# File 'lib/ipscriptables/table.rb', line 12

def initialize(name, ruleset, &block)
  @name = name.to_sym
  @chains = Hashie::Mash.new
  @ruleset = ruleset

  create_builtin_chains unless ruleset.opts[:skip_builtin_chains]

  Docile.dsl_eval(self, &block) if block_given?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/ipscriptables/table.rb', line 11

def name
  @name
end

#rulesetObject (readonly)

Returns the value of attribute ruleset.



11
12
13
# File 'lib/ipscriptables/table.rb', line 11

def ruleset
  @ruleset
end

Instance Method Details

#chain(name, *args, &block) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/ipscriptables/table.rb', line 62

def chain(name, *args, &block)
  if @chains.key?(name)
    @chains[name].alter(*args, &block)
  else
    @chains[name] = Chain.new(name, self, *args, &block)
  end
end

#create_builtin_chainsObject



34
35
36
37
38
39
40
41
42
# File 'lib/ipscriptables/table.rb', line 34

def create_builtin_chains
  if BUILTIN_CHAINS.key? @name
    BUILTIN_CHAINS[@name].each do |builtin|
      chain builtin, :ACCEPT
    end
  else
    warn "Unrecognized table #{@name}, not creating builtin chains"
  end
end

#inherit(*names, &block) ⇒ Object

rubocop:disable MethodLength



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ipscriptables/table.rb', line 44

def inherit(*names, &block) # rubocop:disable MethodLength
  fail 'Need original to inherit' unless ruleset.original
  original_table = ruleset.original[name]
  names = original_table.keys if names.empty?
  names.each do |name|
    original_chain = original_table[name]
    original_rules = original_chain.rules
    original_rules = original_rules.select(&block) if block_given?
    chain name, original_chain.policy, original_chain.counters do
      rules.concat(original_rules)
    end
  end
end

#inspectObject



20
21
22
# File 'lib/ipscriptables/pretty_print.rb', line 20

def inspect
  "#<#{self.class} #{name} [#{map(&:inspect).join(', ')}]>"
end

#originalObject



22
23
24
# File 'lib/ipscriptables/table.rb', line 22

def original
  ruleset.original[name] if ruleset.original
end

#pretty_print(q) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/ipscriptables/pretty_print.rb', line 24

def pretty_print(q)
  q.group(2, "*#{name} {", '}') do
    unless @chains.empty?
      q.breakable
      q.seplist(self, -> { q.breakable }) { |v| q.pp v }
    end
  end
end

#renderObject



70
71
72
73
74
75
# File 'lib/ipscriptables/table.rb', line 70

def render
  ["*#{name}",
   map(&:render_header).join("\n"),
   map(&:render_rules).compact.join("\n"),
   'COMMIT'].reject { |piece| piece == '' }.join("\n")
end

#to_aryObject



58
59
60
# File 'lib/ipscriptables/table.rb', line 58

def to_ary
  @chains.values
end