Update router querying method (#834)
* Add address to router query options. Drop Query interface for QueryOptions * Cleanup isMatch function * Update network proto
This commit is contained in:
		| @@ -135,22 +135,44 @@ func (t *table) List() ([]Route, error) { | ||||
| } | ||||
|  | ||||
| // isMatch checks if the route matches given query options | ||||
| func isMatch(route Route, gateway, network, router string) bool { | ||||
| 	if gateway == "*" || gateway == route.Gateway { | ||||
| 		if network == "*" || network == route.Network { | ||||
| 			if router == "*" || router == route.Router { | ||||
| 				return true | ||||
| 			} | ||||
| func isMatch(route Route, address, gateway, network, router string) bool { | ||||
| 	// matches the values provided | ||||
| 	match := func(a, b string) bool { | ||||
| 		if a == "*" || a == b { | ||||
| 			return true | ||||
| 		} | ||||
| 		return false | ||||
| 	} | ||||
|  | ||||
| 	// a simple struct to hold our values | ||||
| 	type compare struct { | ||||
| 		a string | ||||
| 		b string | ||||
| 	} | ||||
|  | ||||
| 	// compare the following values | ||||
| 	values := []compare{ | ||||
| 		{gateway, route.Gateway}, | ||||
| 		{network, route.Network}, | ||||
| 		{router, route.Router}, | ||||
| 		{address, route.Address}, | ||||
| 	} | ||||
|  | ||||
| 	for _, v := range values { | ||||
| 		// attempt to match each value | ||||
| 		if !match(v.a, v.b) { | ||||
| 			return false | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
|  | ||||
| 	return true | ||||
| } | ||||
|  | ||||
| // findRoutes finds all the routes for given network and router and returns them | ||||
| func findRoutes(routes map[uint64]Route, gateway, network, router string) []Route { | ||||
| func findRoutes(routes map[uint64]Route, address, gateway, network, router string) []Route { | ||||
| 	var results []Route | ||||
| 	for _, route := range routes { | ||||
| 		if isMatch(route, gateway, network, router) { | ||||
| 		if isMatch(route, address, gateway, network, router) { | ||||
| 			results = append(results, route) | ||||
| 		} | ||||
| 	} | ||||
| @@ -158,21 +180,24 @@ func findRoutes(routes map[uint64]Route, gateway, network, router string) []Rout | ||||
| } | ||||
|  | ||||
| // Lookup queries routing table and returns all routes that match the lookup query | ||||
| func (t *table) Query(q Query) ([]Route, error) { | ||||
| func (t *table) Query(q ...QueryOption) ([]Route, error) { | ||||
| 	t.RLock() | ||||
| 	defer t.RUnlock() | ||||
|  | ||||
| 	if q.Options().Service != "*" { | ||||
| 		if _, ok := t.routes[q.Options().Service]; !ok { | ||||
| 	// create new query options | ||||
| 	opts := NewQuery(q...) | ||||
|  | ||||
| 	if opts.Service != "*" { | ||||
| 		if _, ok := t.routes[opts.Service]; !ok { | ||||
| 			return nil, ErrRouteNotFound | ||||
| 		} | ||||
| 		return findRoutes(t.routes[q.Options().Service], q.Options().Gateway, q.Options().Network, q.Options().Router), nil | ||||
| 		return findRoutes(t.routes[opts.Service], opts.Address, opts.Gateway, opts.Network, opts.Router), nil | ||||
| 	} | ||||
|  | ||||
| 	var results []Route | ||||
| 	// search through all destinations | ||||
| 	for _, routes := range t.routes { | ||||
| 		results = append(results, findRoutes(routes, q.Options().Gateway, q.Options().Network, q.Options().Router)...) | ||||
| 		results = append(results, findRoutes(routes, opts.Address, opts.Gateway, opts.Network, opts.Router)...) | ||||
| 	} | ||||
|  | ||||
| 	return results, nil | ||||
|   | ||||
		Reference in New Issue
	
	Block a user