summaryrefslogtreecommitdiffhomepage
path: root/cmd
diff options
context:
space:
mode:
authorThomas Rosenstein <thomas.rosenstein@creamfinance.com>2020-10-18 21:34:36 +0200
committerFUJITA Tomonori <fujita.tomonori@gmail.com>2020-10-20 09:01:40 +0900
commit0aff30a74216f499b8abfabc50016b041b319749 (patch)
treeb885cc8f8f9be0b9e11736ed9a74272944d59b90 /cmd
parent4e4113124f8d4a43b638d63d10af8698b4370d93 (diff)
fix #2289: improve regex to give expected results
Diffstat (limited to 'cmd')
-rw-r--r--cmd/gobgp/policy.go58
1 files changed, 4 insertions, 54 deletions
diff --git a/cmd/gobgp/policy.go b/cmd/gobgp/policy.go
index 710fdb38..f90b68b0 100644
--- a/cmd/gobgp/policy.go
+++ b/cmd/gobgp/policy.go
@@ -31,65 +31,15 @@ import (
api "github.com/osrg/gobgp/api"
"github.com/osrg/gobgp/internal/pkg/config"
+ table "github.com/osrg/gobgp/internal/pkg/table"
"github.com/osrg/gobgp/pkg/packet/bgp"
)
var (
_regexpCommunity = regexp.MustCompile(`\^\^(\S+)\$\$`)
- repexpCommunity = regexp.MustCompile(`(\d+.)*\d+:\d+`)
- regexpLargeCommunity = regexp.MustCompile(`\d+:\d+:\d+`)
regexpCommunityString = regexp.MustCompile(`[\^\$]`)
)
-func parseCommunityRegexp(arg string) (*regexp.Regexp, error) {
- i, err := strconv.ParseUint(arg, 10, 32)
- if err == nil {
- return regexp.Compile(fmt.Sprintf("^%d:%d$", i>>16, i&0x0000ffff))
- }
- if repexpCommunity.MatchString(arg) {
- return regexp.Compile(fmt.Sprintf("^%s$", arg))
- }
- for i, v := range bgp.WellKnownCommunityNameMap {
- if strings.Replace(strings.ToLower(arg), "_", "-", -1) == v {
- return regexp.Compile(fmt.Sprintf("^%d:%d$", i>>16, i&0x0000ffff))
- }
- }
- exp, err := regexp.Compile(arg)
- if err != nil {
- return nil, fmt.Errorf("invalid community format: %s", arg)
- }
- return exp, nil
-}
-
-func parseExtCommunityRegexp(arg string) (bgp.ExtendedCommunityAttrSubType, *regexp.Regexp, error) {
- var subtype bgp.ExtendedCommunityAttrSubType
- elems := strings.SplitN(arg, ":", 2)
- if len(elems) < 2 {
- return subtype, nil, fmt.Errorf("invalid ext-community format([rt|soo]:<value>)")
- }
- switch strings.ToLower(elems[0]) {
- case "rt":
- subtype = bgp.EC_SUBTYPE_ROUTE_TARGET
- case "soo":
- subtype = bgp.EC_SUBTYPE_ROUTE_ORIGIN
- default:
- return subtype, nil, fmt.Errorf("unknown ext-community subtype. rt, soo is supported")
- }
- exp, err := parseCommunityRegexp(elems[1])
- return subtype, exp, err
-}
-
-func parseLargeCommunityRegexp(arg string) (*regexp.Regexp, error) {
- if regexpLargeCommunity.MatchString(arg) {
- return regexp.Compile(fmt.Sprintf("^%s$", arg))
- }
- exp, err := regexp.Compile(arg)
- if err != nil {
- return nil, fmt.Errorf("invalid large-community format: %s", arg)
- }
- return exp, nil
-}
-
func routeTypePrettyString(s api.Conditions_RouteType) string {
switch s {
case api.Conditions_ROUTE_TYPE_EXTERNAL:
@@ -370,7 +320,7 @@ func parseCommunitySet(args []string) (*api.DefinedSet, error) {
name := args[0]
args = args[1:]
for _, arg := range args {
- if _, err := parseCommunityRegexp(arg); err != nil {
+ if _, err := table.ParseCommunityRegexp(arg); err != nil {
return nil, err
}
}
@@ -388,7 +338,7 @@ func parseExtCommunitySet(args []string) (*api.DefinedSet, error) {
name := args[0]
args = args[1:]
for _, arg := range args {
- if _, _, err := parseExtCommunityRegexp(arg); err != nil {
+ if _, _, err := table.ParseExtCommunityRegexp(arg); err != nil {
return nil, err
}
}
@@ -406,7 +356,7 @@ func parseLargeCommunitySet(args []string) (*api.DefinedSet, error) {
name := args[0]
args = args[1:]
for _, arg := range args {
- if _, err := parseLargeCommunityRegexp(arg); err != nil {
+ if _, err := table.ParseLargeCommunityRegexp(arg); err != nil {
return nil, err
}
}