Class: OsCtld::Routing::Table

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

Overview

Represents routing table for a network interface

Defined Under Namespace

Classes: Version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tables: nil) ⇒ Table

Returns a new instance of Table.



108
109
110
# File 'lib/osctld/routing/table.rb', line 108

def initialize(tables: nil)
  @tables = tables || {4 => Version.new, 6 => Version.new}
end

Instance Attribute Details

#tablesObject (readonly, protected)

Returns the value of attribute tables.



208
209
210
# File 'lib/osctld/routing/table.rb', line 208

def tables
  @tables
end

Class Method Details

.load(cfg) ⇒ Object

Load the table from config

Parameters:

  • cfg (Hash)


101
102
103
104
105
106
# File 'lib/osctld/routing/table.rb', line 101

def self.load(cfg)
  new(tables: {
    4 => (cfg['v4'] && Version.load(cfg['v4'])) || Version.new,
    6 => (cfg['v6'] && Version.load(cfg['v6'])) || Version.new,
  })
end

Instance Method Details

#<<(addr) ⇒ Object

Parameters:

  • addr (IPAddress::IPv4, IPAddress::IPv6)


113
114
115
116
# File 'lib/osctld/routing/table.rb', line 113

def <<(addr)
  add(addr)
  self
end

#add(addr, via: nil) ⇒ Routing::Route

Parameters:

  • addr (IPAddress::IPv4, IPAddress::IPv6)
  • via (IPAddress::IPv4, IPAddress::IPv6) (defaults to: nil)

Returns:



121
122
123
# File 'lib/osctld/routing/table.rb', line 121

def add(addr, via: nil)
  t(addr).add(addr, via: via)
end

#any?(v) ⇒ Boolean

Check if there are any routes for given IP version

Parameters:

  • v (Integer)

    IP version

Returns:

  • (Boolean)


158
159
160
# File 'lib/osctld/routing/table.rb', line 158

def any?(v)
  tables[v].any?
end

#contains?(addr) ⇒ Boolean

Check if there is an exact entry for `addr`

Parameters:

  • addr (IPAddress::IPv4, IPAddress::IPv6)

Returns:

  • (Boolean)


152
153
154
# File 'lib/osctld/routing/table.rb', line 152

def contains?(addr)
  t(addr).contains?(addr)
end

#dumpHash

Dump the table into config

Returns:

  • (Hash)


203
204
205
# File 'lib/osctld/routing/table.rb', line 203

def dump
  Hash[ tables.map { |version, table| ["v#{version}", table.dump] } ]
end

#each(ip_v) {|version, addr| ... } ⇒ Object

Iterate over all routes

Yield Parameters:



170
171
172
173
174
175
176
177
178
# File 'lib/osctld/routing/table.rb', line 170

def each(ip_v, &block)
  ret = []

  tables.each do |version, table|
    ret.concat(table.get.map { |route| [version, route] })
  end

  Hash[ret].each(&block)
end

#each_version(ip_v) {|addr| ... } ⇒ Object

Iterate over all routes for IP version

Parameters:

  • ip_v (Integer)

Yield Parameters:



183
184
185
# File 'lib/osctld/routing/table.rb', line 183

def each_version(ip_v, &block)
  tables[ip_v].get.each(&block)
end

#empty?(v) ⇒ Boolean

Parameters:

  • v (Integer)

    IP version

Returns:

  • (Boolean)


163
164
165
# File 'lib/osctld/routing/table.rb', line 163

def empty?(v)
  tables[v].empty?
end

#exportHash

Export the table to clients

Returns:

  • (Hash)


197
198
199
# File 'lib/osctld/routing/table.rb', line 197

def export
  Hash[ tables.map { |version, table| [version, table.export] } ]
end

#remove(addr) ⇒ Routing::Route?

Parameters:

  • addr (IPAddress::IPv4, IPAddress::IPv6)

Returns:



127
128
129
# File 'lib/osctld/routing/table.rb', line 127

def remove(addr)
  t(addr).remove(addr)
end

#remove_all(ip_v = nil) ⇒ Array<Routing::Route>

Parameters:

  • ip_v (Integer, nil) (defaults to: nil)

Returns:



133
134
135
136
137
138
139
140
141
142
# File 'lib/osctld/routing/table.rb', line 133

def remove_all(ip_v = nil)
  ret = []

  (ip_v ? [ip_v] : [4, 6]).each do |v|
    ret.concat(tables[v].get)
    tables[v].clear
  end

  ret
end

#remove_version_if(ip_v) {|addr| ... } ⇒ Object

Remove routes for which the block returns truthy value

Parameters:

  • ip_v (Integer)

Yield Parameters:

Yield Returns:

  • (Boolean)


191
192
193
# File 'lib/osctld/routing/table.rb', line 191

def remove_version_if(ip_v, &block)
  tables[ip_v].remove_if(&block)
end

#route?(addr) ⇒ Boolean

Check if the table routes `addr`

Parameters:

  • addr (IPAddress::IPv4, IPAddress::IPv6)

Returns:

  • (Boolean)


146
147
148
# File 'lib/osctld/routing/table.rb', line 146

def route?(addr)
  t(addr).route?(addr)
end

#t(addr) ⇒ Object (protected)



210
211
212
# File 'lib/osctld/routing/table.rb', line 210

def t(addr)
  addr.ipv4? ? tables[4] : tables[6]
end