2019-06-10 00:09:38 +02:00
|
|
|
package router
|
|
|
|
|
2019-06-12 23:30:42 +02:00
|
|
|
import "context"
|
|
|
|
|
2019-06-10 00:09:38 +02:00
|
|
|
// 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-12 23:30:42 +02:00
|
|
|
// IgnoreIfExists does not add new route
|
|
|
|
IgnoreIfExists
|
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-13 13:09:49 +02:00
|
|
|
// RouteOption is used to define routing table entry options
|
|
|
|
type RouteOption func(*RouteOptions)
|
|
|
|
|
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
|
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
|
2019-06-12 23:30:42 +02:00
|
|
|
// Context stores other arbitrary options
|
|
|
|
Context context.Context
|
2019-06-10 00:09:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|