Class: Netfilter::Table

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tool, name) {|_self| ... } ⇒ Table

Returns a new instance of Table.

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
# File 'lib/netfilter/table.rb', line 18

def initialize(tool, name)
  self.tool = tool
  self.name = name.to_s
  self.chains = {}
  raise ArgumentError, "unsupported table '#{name}'" unless native?
  yield(self) if block_given?
end

Instance Attribute Details

#chainsObject

Returns the value of attribute chains.



4
5
6
# File 'lib/netfilter/table.rb', line 4

def chains
  @chains
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/netfilter/table.rb', line 4

def name
  @name
end

#toolObject

Returns the value of attribute tool.



3
4
5
# File 'lib/netfilter/table.rb', line 3

def tool
  @tool
end

Class Method Details

.import(tool, data) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/netfilter/table.rb', line 8

def self.import(tool, data)
  data = data.symbolize_keys
  new(tool, data[:name]).tap do |table|
    data[:chains].each do |data|
      chain = Chain.import(table, data)
      table.chains[chain.name.to_s.downcase] = chain
    end
  end
end

Instance Method Details

#chain(name, &block) ⇒ Object



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

def chain(name, &block)
  key = name.to_s.downcase
  (chains[key] || Chain.new(self, name)).tap do |chain|
    chains[key] = chain
    block.call(chain) if block
  end
end

#commandsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/netfilter/table.rb', line 38

def commands
  [].tap do |commands|
    chains.values.each do |chain|
      chain.commands.each do |command|
        commands << command.unshift("--table #{name}")
      end
    end

    cmds = [[], []]
    commands.each do |cmd|
      index = cmd[1].include?("--new-chain") ? 0 : 1
      cmds[index] << cmd
    end
    commands.replace(cmds[0] + cmds[1])
  end
end

#exportObject



55
56
57
58
59
60
# File 'lib/netfilter/table.rb', line 55

def export
  {
    :name => name,
    :chains => chains.values.map{ |chain| chain.export },
  }
end

#native?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/netfilter/table.rb', line 34

def native?
  NATIVE_TABLES.include?(name)
end