Class: OsCtld::Routing::Route

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

Overview

Represents one route from a routing table

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(addr, via: nil) ⇒ Route

Returns a new instance of Route.



31
32
33
34
35
36
37
# File 'lib/osctld/routing/route.rb', line 31

def initialize(addr, via: nil)
  @addr = addr
  @via = via
  @ip_version = addr.ipv4? ? 4 : 6
  @ip_spec = [addr.to_string]
  @ip_spec.push('via', via.to_s, 'onlink') if via
end

Instance Attribute Details

#addrIPAddress::IPv4, IPAddress::IPv6 (readonly)

Returns:

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


19
20
21
# File 'lib/osctld/routing/route.rb', line 19

def addr
  @addr
end

#ip_specArray (readonly)

Arguments for ‘ip` that identify the route

Returns:

  • (Array)


29
30
31
# File 'lib/osctld/routing/route.rb', line 29

def ip_spec
  @ip_spec
end

#ip_versionInteger (readonly)

Returns:

  • (Integer)


25
26
27
# File 'lib/osctld/routing/route.rb', line 25

def ip_version
  @ip_version
end

#viaIPAddress::IPv4, ... (readonly)

Returns:

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


22
23
24
# File 'lib/osctld/routing/route.rb', line 22

def via
  @via
end

Class Method Details

.load(cfg) ⇒ Object

Load route from config



7
8
9
10
11
12
13
14
15
16
# File 'lib/osctld/routing/route.rb', line 7

def self.load(cfg)
  if cfg.is_a?(Hash)
    new(
      IPAddress.parse(cfg['address']),
      via: cfg['via'] && IPAddress.parse(cfg['via'])
    )
  else
    new(IPAddress.parse(cfg))
  end
end

Instance Method Details

#dumpObject

Dump to config



45
46
47
48
49
50
51
# File 'lib/osctld/routing/route.rb', line 45

def dump
  if via
    { 'address' => addr.to_string, 'via' => via.to_s }
  else
    addr.to_string
  end
end

#exportObject

Export to clients



54
55
56
# File 'lib/osctld/routing/route.rb', line 54

def export
  { address: addr, via: }
end

#route?(addr) ⇒ Boolean

Parameters:

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

Returns:

  • (Boolean)


40
41
42
# File 'lib/osctld/routing/route.rb', line 40

def route?(addr)
  @addr == addr || (@addr.network? && @addr.include?(addr))
end