diff options
Diffstat (limited to 'packet/bgp.go')
-rw-r--r-- | packet/bgp.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/packet/bgp.go b/packet/bgp.go index 5e28fdc8..c71d1489 100644 --- a/packet/bgp.go +++ b/packet/bgp.go @@ -1164,6 +1164,27 @@ func NewMPLSLabelStack(labels ...uint32) *MPLSLabelStack { return &MPLSLabelStack{labels} } +func ParseMPLSLabelStack(buf string) (*MPLSLabelStack, error) { + elems := strings.Split(buf, "/") + labels := make([]uint32, 0, len(elems)) + if len(elems) == 0 { + goto ERR + } + for _, elem := range elems { + i, err := strconv.Atoi(elem) + if err != nil { + goto ERR + } + if i < 0 || i > ((1<<20)-1) { + goto ERR + } + labels = append(labels, uint32(i)) + } + return NewMPLSLabelStack(labels...), nil +ERR: + return nil, fmt.Errorf("invalid mpls label stack format") +} + // // RFC3107 Carrying Label Information in BGP-4 // @@ -1338,6 +1359,10 @@ func (l *LabeledIPAddrPrefix) Serialize() ([]byte, error) { return buf, nil } +func (l *LabeledIPAddrPrefix) String() string { + return fmt.Sprintf("%s/%d", l.Prefix.String(), int(l.Length)-l.Labels.Len()*8) +} + func NewLabeledIPAddrPrefix(length uint8, prefix string, label MPLSLabelStack) *LabeledIPAddrPrefix { return &LabeledIPAddrPrefix{ IPAddrPrefixDefault{length + uint8(label.Len()*8), net.ParseIP(prefix).To4()}, |