2019-06-10 00:09:38 +02:00
|
|
|
package router
|
|
|
|
|
2019-06-18 19:33:05 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-05 20:15:32 +02:00
|
|
|
"hash/fnv"
|
2019-06-18 19:33:05 +02:00
|
|
|
"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-19 22:22:14 +02:00
|
|
|
// RoutePolicy defines routing table addition policy
|
|
|
|
type RoutePolicy int
|
2019-06-10 00:09:38 +02:00
|
|
|
|
|
|
|
const (
|
2019-06-27 23:52:51 +02:00
|
|
|
// AddIfNotExist adds the route if it does not exist
|
2019-07-01 23:57:27 +02:00
|
|
|
AddIfNotExists RoutePolicy = iota
|
|
|
|
// OverrideIfExists overrides route if it already exists
|
|
|
|
OverrideIfExists
|
2019-06-26 17:03:19 +02:00
|
|
|
// IgnoreIfExists instructs to 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
|
2019-06-19 22:22:14 +02:00
|
|
|
func (p RoutePolicy) String() string {
|
2019-06-18 19:33:05 +02:00
|
|
|
switch p {
|
2019-06-27 23:52:51 +02:00
|
|
|
case AddIfNotExists:
|
|
|
|
return "ADD_IF_NOT_EXISTS"
|
2019-07-02 00:19:35 +02:00
|
|
|
case OverrideIfExists:
|
|
|
|
return "OVERRIDE_IF_EXISTS"
|
2019-06-18 19:33:05 +02:00
|
|
|
case IgnoreIfExists:
|
2019-06-27 23:52:51 +02:00
|
|
|
return "IGNORE_IF_EXISTS"
|
2019-06-18 19:33:05 +02:00
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 22:22:14 +02:00
|
|
|
// Route is network route
|
|
|
|
type Route struct {
|
|
|
|
// Destination is destination address
|
|
|
|
Destination string
|
2019-06-26 17:03:19 +02:00
|
|
|
// Gateway is route gateway
|
|
|
|
Gateway string
|
|
|
|
// Router is the network router address
|
|
|
|
Router string
|
2019-06-19 22:22:14 +02:00
|
|
|
// Network is micro network address
|
2019-06-10 20:50:54 +02:00
|
|
|
Network string
|
2019-06-19 22:22:14 +02:00
|
|
|
// Metric is the route cost metric
|
2019-06-10 00:09:38 +02:00
|
|
|
Metric int
|
2019-06-19 22:22:14 +02:00
|
|
|
// Policy defines route policy
|
|
|
|
Policy RoutePolicy
|
2019-06-10 00:09:38 +02:00
|
|
|
}
|
2019-06-18 19:33:05 +02:00
|
|
|
|
2019-07-05 20:15:32 +02:00
|
|
|
// Hash returns route hash sum.
|
|
|
|
func (r *Route) Hash() uint64 {
|
|
|
|
h := fnv.New64()
|
|
|
|
h.Reset()
|
|
|
|
h.Write([]byte(r.Destination + r.Gateway + r.Network))
|
|
|
|
|
|
|
|
return h.Sum64()
|
|
|
|
}
|
|
|
|
|
2019-06-18 19:33:05 +02:00
|
|
|
// String allows to print the route
|
2019-07-05 20:15:32 +02:00
|
|
|
func (r Route) String() string {
|
2019-06-18 19:33:05 +02:00
|
|
|
// this will help us build routing table string
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
|
|
|
// create nice table printing structure
|
|
|
|
table := tablewriter.NewWriter(sb)
|
2019-06-26 17:03:19 +02:00
|
|
|
table.SetHeader([]string{"Destination", "Gateway", "Router", "Network", "Metric"})
|
2019-06-18 19:33:05 +02:00
|
|
|
|
|
|
|
strRoute := []string{
|
2019-06-19 22:22:14 +02:00
|
|
|
r.Destination,
|
2019-06-26 17:03:19 +02:00
|
|
|
r.Gateway,
|
|
|
|
r.Router,
|
2019-06-19 22:22:14 +02:00
|
|
|
r.Network,
|
|
|
|
fmt.Sprintf("%d", r.Metric),
|
2019-06-18 19:33:05 +02:00
|
|
|
}
|
|
|
|
table.Append(strRoute)
|
|
|
|
|
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
}
|