Class: Netfilter::Table
- Inherits:
-
Object
- Object
- Netfilter::Table
- Defined in:
- lib/netfilter/table.rb
Instance Attribute Summary collapse
-
#chains ⇒ Object
Returns the value of attribute chains.
-
#name ⇒ Object
Returns the value of attribute name.
-
#tool ⇒ Object
Returns the value of attribute tool.
Class Method Summary collapse
Instance Method Summary collapse
- #chain(name, &block) ⇒ Object
- #commands ⇒ Object
- #export ⇒ Object
-
#initialize(tool, name) {|_self| ... } ⇒ Table
constructor
A new instance of Table.
- #native? ⇒ Boolean
Constructor Details
#initialize(tool, name) {|_self| ... } ⇒ Table
Returns a new instance of Table.
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
#chains ⇒ Object
Returns the value of attribute chains.
4 5 6 |
# File 'lib/netfilter/table.rb', line 4 def chains @chains end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/netfilter/table.rb', line 4 def name @name end |
#tool ⇒ Object
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 |
#commands ⇒ Object
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 |
#export ⇒ Object
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
34 35 36 |
# File 'lib/netfilter/table.rb', line 34 def native? NATIVE_TABLES.include?(name) end |