2019-06-07 01:29:24 +03:00
|
|
|
package router
|
|
|
|
|
2019-06-10 01:09:38 +03:00
|
|
|
// LookupPolicy defines query policy
|
|
|
|
type LookupPolicy int
|
2019-06-07 01:29:24 +03:00
|
|
|
|
|
|
|
const (
|
2019-06-10 01:09:38 +03:00
|
|
|
// DiscardNoRoute discards query when no route is found
|
|
|
|
DiscardNoRoute LookupPolicy = iota
|
|
|
|
// ClosestMatch returns closest match to supplied query
|
2019-06-07 01:29:24 +03:00
|
|
|
ClosestMatch
|
|
|
|
)
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// QueryOption sets routing table query options
|
2019-06-13 14:09:49 +03:00
|
|
|
type QueryOption func(*QueryOptions)
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// QueryOptions are routing table query options
|
2019-06-07 01:29:24 +03:00
|
|
|
type QueryOptions struct {
|
2019-06-14 00:28:47 +03:00
|
|
|
// DestAddr is destination address
|
2019-06-13 00:30:42 +03:00
|
|
|
DestAddr string
|
2019-06-14 00:28:47 +03:00
|
|
|
// NetworkAddress is network address
|
2019-06-13 00:30:42 +03:00
|
|
|
Network string
|
2019-06-17 01:09:59 +03:00
|
|
|
// Gateway is gateway address
|
|
|
|
Gateway Router
|
2019-06-14 00:28:47 +03:00
|
|
|
// Policy is query lookup policy
|
2019-06-10 01:09:38 +03:00
|
|
|
Policy LookupPolicy
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
// QueryDestAddr sets query destination address
|
|
|
|
func QueryDestAddr(a string) QueryOption {
|
2019-06-07 01:29:24 +03:00
|
|
|
return func(o *QueryOptions) {
|
2019-06-13 00:30:42 +03:00
|
|
|
o.DestAddr = a
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
// QueryNetwork sets query network address
|
|
|
|
func QueryNetwork(a string) QueryOption {
|
2019-06-07 01:29:24 +03:00
|
|
|
return func(o *QueryOptions) {
|
2019-06-13 00:30:42 +03:00
|
|
|
o.Network = a
|
2019-06-07 01:29:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 01:09:59 +03:00
|
|
|
// QueryGateway sets query gateway address
|
|
|
|
func QueryGateway(r Router) QueryOption {
|
|
|
|
return func(o *QueryOptions) {
|
|
|
|
o.Gateway = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// QueryPolicy sets query policy
|
2019-06-13 00:30:42 +03:00
|
|
|
func QueryPolicy(p LookupPolicy) QueryOption {
|
2019-06-10 01:19:56 +03:00
|
|
|
return func(o *QueryOptions) {
|
2019-06-13 00:30:42 +03:00
|
|
|
o.Policy = p
|
2019-06-10 01:19:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// Query is routing table query
|
2019-06-07 01:29:24 +03:00
|
|
|
type Query interface {
|
|
|
|
// Options returns query options
|
|
|
|
Options() QueryOptions
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:28:47 +03:00
|
|
|
// query is a basic implementation of Query
|
2019-06-07 01:29:24 +03:00
|
|
|
type query struct {
|
|
|
|
opts QueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewQuery creates new query and returns it
|
|
|
|
func NewQuery(opts ...QueryOption) Query {
|
2019-06-17 01:09:59 +03:00
|
|
|
// default gateway for wildcard router
|
|
|
|
r := newRouter(ID("*"))
|
|
|
|
|
2019-06-13 00:30:42 +03:00
|
|
|
// default options
|
|
|
|
qopts := QueryOptions{
|
|
|
|
DestAddr: "*",
|
|
|
|
Network: "*",
|
2019-06-17 01:09:59 +03:00
|
|
|
Gateway: r,
|
2019-06-13 00:30:42 +03:00
|
|
|
Policy: DiscardNoRoute,
|
|
|
|
}
|
2019-06-07 01:29:24 +03:00
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&qopts)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &query{
|
|
|
|
opts: qopts,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options returns query options
|
|
|
|
func (q *query) Options() QueryOptions {
|
|
|
|
return q.opts
|
|
|
|
}
|