diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-08-03 14:04:34 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-08-03 21:59:39 +0900 |
commit | 84dd9d6983564b37b7e146264c44da6874a08cf4 (patch) | |
tree | 1dcda7f9b8164aec87c0fc6c889f8499c6fd8492 /packet | |
parent | d895c87c741084b4469bb260e3ef94f7793a8f92 (diff) |
api: use serialized buffer rather than protobuf struct to add paths
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'packet')
-rw-r--r-- | packet/bgp.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/packet/bgp.go b/packet/bgp.go index b5bf7924..7fd4f9e6 100644 --- a/packet/bgp.go +++ b/packet/bgp.go @@ -24,6 +24,8 @@ import ( "math" "net" "reflect" + "regexp" + "strconv" "strings" ) @@ -856,6 +858,40 @@ func getRouteDistinguisher(data []byte) RouteDistinguisherInterface { return rd } +func parseRd(input string) ([]string, error) { + exp := regexp.MustCompile("^((\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)|((\\d+)\\.)?(\\d+)):(\\d+)$") + group := exp.FindSubmatch([]byte(input)) + if len(group) != 10 { + return nil, fmt.Errorf("failed to parse") + } + elems := make([]string, 0, len(group)) + for _, elem := range group { + elems = append(elems, string(elem)) + } + return elems, nil +} + +func ParseRouteDistinguisher(rd string) (RouteDistinguisherInterface, error) { + elems, err := parseRd(rd) + if err != nil { + return nil, err + } + assigned, _ := strconv.Atoi(elems[9]) + ip := net.ParseIP(elems[1]) + switch { + case ip.To4() != nil: + return NewRouteDistinguisherIPAddressAS(elems[1], uint16(assigned)), nil + case elems[6] == "" && elems[7] == "": + asn, _ := strconv.Atoi(elems[8]) + return NewRouteDistinguisherTwoOctetAS(uint16(asn), uint32(assigned)), nil + default: + fst, _ := strconv.Atoi(elems[7]) + snd, _ := strconv.Atoi(elems[8]) + asn := fst<<16 | snd + return NewRouteDistinguisherFourOctetAS(uint32(asn), uint16(assigned)), nil + } +} + // // RFC3107 Carrying Label Information in BGP-4 // |