2019-06-10 00:09:38 +02:00
|
|
|
package router
|
|
|
|
|
|
|
|
// AddPolicy defines routing table addition policy
|
|
|
|
type AddPolicy int
|
|
|
|
|
|
|
|
const (
|
2019-06-10 14:34:23 +02:00
|
|
|
// Override overrides existing routing table route
|
2019-06-10 00:09:38 +02:00
|
|
|
OverrideIfExists AddPolicy = iota
|
2019-06-10 14:34:23 +02:00
|
|
|
// ErrIfExists returns error if the route already exists
|
2019-06-10 00:09:38 +02:00
|
|
|
ErrIfExists
|
|
|
|
)
|
|
|
|
|
2019-06-10 14:34:23 +02:00
|
|
|
// RouteOptions defines micro network routing table route options
|
|
|
|
type RouteOptions struct {
|
2019-06-10 00:09:38 +02:00
|
|
|
// DestAddr is destination address
|
|
|
|
DestAddr string
|
|
|
|
// Hop is the next route hop
|
|
|
|
Hop Router
|
|
|
|
// SrcAddr defines local routing address
|
|
|
|
// On local networkss, this will be the address of local router
|
|
|
|
SrcAddr string
|
|
|
|
// 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-10 14:34:23 +02:00
|
|
|
// Hop allows to set the route route options
|
|
|
|
func Hop(r Router) RouteOption {
|
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 00:09:38 +02:00
|
|
|
o.Hop = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SrcAddr sets source address
|
2019-06-10 14:34:23 +02:00
|
|
|
func SrcAddr(a string) RouteOption {
|
|
|
|
return func(o *RouteOptions) {
|
2019-06-10 00:09:38 +02:00
|
|
|
o.SrcAddr = a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|