2019-06-10 00:09:38 +02:00
|
|
|
package router
|
|
|
|
|
2019-06-18 19:33:05 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
)
|
2019-06-12 23:30:42 +02:00
|
|
|
|
2019-06-13 16:12:07 +02:00
|
|
|
var (
|
|
|
|
// DefaultLocalMetric is default route cost for local network
|
|
|
|
DefaultLocalMetric = 1
|
|
|
|
// DefaultNetworkMetric is default route cost for micro network
|
|
|
|
DefaultNetworkMetric = 10
|
|
|
|
)
|
|
|
|
|
2019-06-10 00:09:38 +02:00
|
|
|
// AddPolicy defines routing table addition policy
|
|
|
|
type AddPolicy int
|
|
|
|
|
|
|
|
const (
|
2019-06-13 23:28:47 +02:00
|
|
|
// OverrideIfExists overrides route if it already exists
|
2019-06-10 00:09:38 +02:00
|
|
|
OverrideIfExists AddPolicy = iota
|
2019-06-13 23:28:47 +02:00
|
|
|
// IgnoreIfExists does not modify existing route
|
2019-06-12 23:30:42 +02:00
|
|
|
IgnoreIfExists
|
2019-06-10 00:09:38 +02:00
|
|
|
)
|
|
|
|
|
2019-06-18 19:33:05 +02:00
|
|
|
// String returns human reprensentation of policy
|
|
|
|
func (p AddPolicy) String() string {
|
|
|
|
switch p {
|
|
|
|
case OverrideIfExists:
|
|
|
|
return "OVERRIDE"
|
|
|
|
case IgnoreIfExists:
|
|
|
|
return "IGNORE"
|
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// RouteOption is used to set routing table entry options
|
2019-06-13 13:09:49 +02:00
|
|
|
type RouteOption func(*RouteOptions)
|
|
|
|
|
2019-06-13 23:28:47 +02:00
|
|
|
// RouteOptions are route options
|
2019-06-10 14:34:23 +02:00
|
|
|
type RouteOptions struct {
|
2019-06-10 00:09:38 +02:00
|
|
|
// DestAddr is destination address
|
|
|
|
DestAddr string
|
2019-06-12 00:59:25 +02:00
|
|
|
// Gateway is the next route hop
|
|
|
|
Gateway Router
|
2019-06-10 20:50:54 +02:00
|
|
|
// Network defines micro network
|
|
|
|
Network string
|
2019-06-10 00:09:38 +02:00
|
|
|
// Metric is route cost metric
|
|
|
|
Metric int
|
2019-06-10 14:34:23 +02:00
|
|
|
// Policy defines route addition policy
|
2019-06-10 00:09:38 +02:00
|
|
|
Policy AddPolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
// DestAddr sets destination address
|
2019-06-10 14:34:23 +02:00
|
|
|
func DestAddr(a string) RouteOption {
|
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 00:09:38 +02:00
|
|
|
o.DestAddr = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 00:59:25 +02:00
|
|
|
// Gateway sets the route gateway
|
|
|
|
func Gateway(r Router) RouteOption {
|
2019-06-10 14:34:23 +02:00
|
|
|
return func(o *RouteOptions) {
|
2019-06-12 00:59:25 +02:00
|
|
|
o.Gateway = r
|
2019-06-10 00:09:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 20:50:54 +02:00
|
|
|
// Network sets micro network
|
|
|
|
func Network(n string) RouteOption {
|
2019-06-10 14:34:23 +02:00
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 20:50:54 +02:00
|
|
|
o.Network = n
|
2019-06-10 00:09:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
// Metric sets route metric
|
|
|
|
func Metric(m int) RouteOption {
|
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 00:09:38 +02:00
|
|
|
o.Metric = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
// RoutePolicy sets add route policy
|
|
|
|
func RoutePolicy(p AddPolicy) RouteOption {
|
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 00:09:38 +02:00
|
|
|
o.Policy = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
// Route is routing table route
|
|
|
|
type Route interface {
|
|
|
|
// Options returns route options
|
|
|
|
Options() RouteOptions
|
2019-06-10 00:09:38 +02:00
|
|
|
}
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
type route struct {
|
|
|
|
opts RouteOptions
|
2019-06-10 00:09:38 +02:00
|
|
|
}
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
// NewRoute returns new routing table route
|
|
|
|
func NewRoute(opts ...RouteOption) Route {
|
|
|
|
eopts := RouteOptions{}
|
2019-06-10 00:09:38 +02:00
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&eopts)
|
|
|
|
}
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
return &route{
|
2019-06-10 00:09:38 +02:00
|
|
|
opts: eopts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
// Options returns route options
|
|
|
|
func (r *route) Options() RouteOptions {
|
|
|
|
return r.opts
|
2019-06-10 00:09:38 +02:00
|
|
|
}
|
2019-06-18 19:33:05 +02:00
|
|
|
|
|
|
|
// String allows to print the route
|
|
|
|
func (r *route) String() string {
|
|
|
|
// this will help us build routing table string
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
|
|
|
// create nice table printing structure
|
|
|
|
table := tablewriter.NewWriter(sb)
|
|
|
|
table.SetHeader([]string{"Destination", "Gateway", "Network", "Metric"})
|
|
|
|
|
|
|
|
strRoute := []string{
|
|
|
|
r.opts.DestAddr,
|
|
|
|
r.opts.Gateway.Address(),
|
|
|
|
r.opts.Network,
|
|
|
|
fmt.Sprintf("%d", r.opts.Metric),
|
|
|
|
}
|
|
|
|
table.Append(strRoute)
|
|
|
|
|
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
}
|