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.



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

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.



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

def tables
  @tables
end

Class Method Details

.load(cfg) ⇒ Object

Load the table from config

Parameters:

  • cfg (Hash)


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

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)


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

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:



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

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

#any?(v) ⇒ Boolean

Check if there are any routes for given IP version

Parameters:

  • v (Integer)

    IP version

Returns:

  • (Boolean)


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

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)


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

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

#dumpHash

Dump the table into config

Returns:

  • (Hash)


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

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

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

Iterate over all routes

Yield Parameters:



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

def each(_ip_v, &)
  ret = []

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

  ret.to_h.each(&)
end

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

Iterate over all routes for IP version

Parameters:

  • ip_v (Integer)

Yield Parameters:



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

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

#empty?(v) ⇒ Boolean

Parameters:

  • v (Integer)

    IP version

Returns:

  • (Boolean)


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

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

#exportHash

Export the table to clients

Returns:

  • (Hash)


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

def export
  tables.transform_values(&:export)
end

#remove(addr) ⇒ Routing::Route?

Parameters:

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

Returns:



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

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:



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

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)


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

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

#route?(addr) ⇒ Boolean

Check if the table routes ‘addr`

Parameters:

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

Returns:

  • (Boolean)


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

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

#t(addr) ⇒ Object (protected)



212
213
214
# File 'lib/osctld/routing/table.rb', line 212

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