Class: Tablomat::IPTablesBase::Table

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

Overview

Puts the Table in IPTables

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(system, name, owned = true) ⇒ Table

Returns a new instance of Table.



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

def initialize(system, name, owned = true)
  @system = system
  @name = name
  @chains = {}
  # whether this table is owned/created by us or external
  @owned = owned
  @active = false
  activate if @system.active
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



10
11
12
# File 'lib/tablomat/iptables/table.rb', line 10

def active
  @active
end

#chainsObject (readonly)

Returns the value of attribute chains.



10
11
12
# File 'lib/tablomat/iptables/table.rb', line 10

def chains
  @chains
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/tablomat/iptables/table.rb', line 10

def name
  @name
end

#ownedObject

Returns the value of attribute owned.



9
10
11
# File 'lib/tablomat/iptables/table.rb', line 9

def owned
  @owned
end

#systemObject (readonly)

Returns the value of attribute system.



10
11
12
# File 'lib/tablomat/iptables/table.rb', line 10

def system
  @system
end

Instance Method Details

#activate(override = false) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/tablomat/iptables/table.rb', line 57

def activate(override = false)
  return unless @owned || override

  @active = true
  return if override

  @chains.each do |_name, chain|
    chain.activate
  end
end

#append(chain_name, data) ⇒ Object



47
48
49
# File 'lib/tablomat/iptables/table.rb', line 47

def append(chain_name, data)
  chain(chain_name).append(data)
end

#chain(name, owned = true, &block) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/tablomat/iptables/table.rb', line 26

def chain(name, owned = true, &block)
  key = get_key name
  (@chains[key] || Chain.new(self, name, owned)).tap do |chain|
    @chains[key] = chain
    block&.call(chain)
  end
end

#chain_exists(name) ⇒ Object



34
35
36
37
38
39
# File 'lib/tablomat/iptables/table.rb', line 34

def chain_exists(name)
  key = get_key name
  return true if @chains.key? key

  false
end

#deactivate(override = false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/tablomat/iptables/table.rb', line 68

def deactivate(override = false)
  return unless @owned || override

  @active = false
  return if override

  @chains.each do |_name, chain|
    chain.deactivate
  end
end

#delete(chain_name, data) ⇒ Object



51
52
53
54
55
# File 'lib/tablomat/iptables/table.rb', line 51

def delete(chain_name, data)
  chain chain_name do |chain|
    chain.delete(data)
  end
end

#get_key(name) ⇒ Object



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

def get_key(name)
  name.to_s.downcase[0, 28]
end

#insert(chain_name, data, pos) ⇒ Object



41
42
43
44
45
# File 'lib/tablomat/iptables/table.rb', line 41

def insert(chain_name, data, pos)
  chain chain_name do |chain|
    chain.insert(data, pos)
  end
end


79
80
81
82
# File 'lib/tablomat/iptables/table.rb', line 79

def print
  require 'pp'
  pp self
end