2019-06-10 01:09:38 +03:00
|
|
|
package router
|
|
|
|
|
2019-06-18 20:33:05 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
)
|
2019-06-13 00:30:42 +03:00
|
|
|
|
2019-06-13 17:12:07 +03: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 01:09:38 +03:00
|
|
|
// AddPolicy defines routing table addition policy
|
|
|
|
type AddPolicy int
|
|
|
|
|
|
|
|
const (
|
2019-06-14 00:28:47 +03:00
|
|
|
// OverrideIfExists overrides route if it already exists
|
2019-06-10 01:09:38 +03:00
|
|
|
OverrideIfExists AddPolicy = iota
|
2019-06-14 00:28:47 +03:00
|
|
|
// IgnoreIfExists does not modify existing route
|
2019-06-13 00:30:42 +03:00
|
|
|
IgnoreIfExists
|
2019-06-10 01:09:38 +03:00
|
|
|
)
|
|
|
|
|
2019-06-18 20:33:05 +03: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-14 00:28:47 +03:00
|
|
|
// RouteOption is used to set routing table entry options
|
2019-06-13 14:09:49 +03:00
|
|
|
type RouteOption func(*RouteOptions)
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// RouteOptions are route options
|
2019-06-10 15:34:23 +03:00
|
|
|
type RouteOptions struct {
|
2019-06-10 01:09:38 +03:00
|
|
|
// DestAddr is destination address
|
|
|
|
DestAddr string
|
2019-06-12 01:59:25 +03:00
|
|
|
// Gateway is the next route hop
|
|
|
|
Gateway Router
|
2019-06-10 21:50:54 +03:00
|
|
|
// Network defines micro network
|
|
|
|
Network string
|
2019-06-10 01:09:38 +03:00
|
|
|
// Metric is route cost metric
|
|
|
|
Metric int
|
2019-06-10 15:34:23 +03:00
|
|
|
// Policy defines route addition policy
|
2019-06-10 01:09:38 +03:00
|
|
|
Policy AddPolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
// DestAddr sets destination address
|
2019-06-10 15:34:23 +03:00
|
|
|
func DestAddr(a string) RouteOption {
|
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 01:09:38 +03:00
|
|
|
o.DestAddr = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 01:59:25 +03:00
|
|
|
// Gateway sets the route gateway
|
|
|
|
func Gateway(r Router) RouteOption {
|
2019-06-10 15:34:23 +03:00
|
|
|
return func(o *RouteOptions) {
|
2019-06-12 01:59:25 +03:00
|
|
|
o.Gateway = r
|
2019-06-10 01:09:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 21:50:54 +03:00
|
|
|
// Network sets micro network
|
|
|
|
func Network(n string) RouteOption {
|
2019-06-10 15:34:23 +03:00
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 21:50:54 +03:00
|
|
|
o.Network = n
|
2019-06-10 01:09:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// Metric sets route metric
|
|
|
|
func Metric(m int) RouteOption {
|
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 01:09:38 +03:00
|
|
|
o.Metric = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// RoutePolicy sets add route policy
|
|
|
|
func RoutePolicy(p AddPolicy) RouteOption {
|
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 01:09:38 +03:00
|
|
|
o.Policy = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// Route is routing table route
|
|
|
|
type Route interface {
|
|
|
|
// Options returns route options
|
|
|
|
Options() RouteOptions
|
2019-06-10 01:09:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
type route struct {
|
|
|
|
opts RouteOptions
|
2019-06-10 01:09:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// NewRoute returns new routing table route
|
|
|
|
func NewRoute(opts ...RouteOption) Route {
|
|
|
|
eopts := RouteOptions{}
|
2019-06-10 01:09:38 +03:00
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&eopts)
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
return &route{
|
2019-06-10 01:09:38 +03:00
|
|
|
opts: eopts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 15:34:23 +03:00
|
|
|
// Options returns route options
|
|
|
|
func (r *route) Options() RouteOptions {
|
|
|
|
return r.opts
|
2019-06-10 01:09:38 +03:00
|
|
|
}
|
2019-06-18 20:33:05 +03: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()
|
|
|
|
}
|