diff options
Diffstat (limited to 'packet/bgp.go')
-rw-r--r-- | packet/bgp.go | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/packet/bgp.go b/packet/bgp.go index db3c2093..977ca167 100644 --- a/packet/bgp.go +++ b/packet/bgp.go @@ -3285,12 +3285,26 @@ func NewPathAttributeOrigin(value uint8) *PathAttributeOrigin { } } +type AsPathParamFormat struct { + start string + end string + separator string +} + +var asPathParamFormatMap = map[uint8]*AsPathParamFormat{ + BGP_ASPATH_ATTR_TYPE_SET: &AsPathParamFormat{"{", "}", ","}, + BGP_ASPATH_ATTR_TYPE_SEQ: &AsPathParamFormat{"", "", " "}, + BGP_ASPATH_ATTR_TYPE_CONFED_SET: &AsPathParamFormat{"(", ")", " "}, + BGP_ASPATH_ATTR_TYPE_CONFED_SEQ: &AsPathParamFormat{"[", "]", ","}, +} + type AsPathParamInterface interface { Serialize() ([]byte, error) DecodeFromBytes([]byte) error Len() int ASLen() int MarshalJSON() ([]byte, error) + String() string } type AsPathParam struct { @@ -3344,6 +3358,22 @@ func (a *AsPathParam) ASLen() int { return 0 } +func (a *AsPathParam) String() string { + format, ok := asPathParamFormatMap[a.Type] + if !ok { + return fmt.Sprintf("%v", a.AS) + } + aspath := make([]string, 0, len(a.AS)) + for _, asn := range a.AS { + aspath = append(aspath, fmt.Sprintf("%d", asn)) + } + s := bytes.NewBuffer(make([]byte, 0, 32)) + s.WriteString(format.start) + s.WriteString(strings.Join(aspath, format.separator)) + s.WriteString(format.end) + return s.String() +} + func (a *AsPathParam) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type uint8 `json:"segment_type"` @@ -3415,6 +3445,22 @@ func (a *As4PathParam) ASLen() int { return 0 } +func (a *As4PathParam) String() string { + format, ok := asPathParamFormatMap[a.Type] + if !ok { + return fmt.Sprintf("%v", a.AS) + } + aspath := make([]string, 0, len(a.AS)) + for _, asn := range a.AS { + aspath = append(aspath, fmt.Sprintf("%d", asn)) + } + s := bytes.NewBuffer(make([]byte, 0, 32)) + s.WriteString(format.start) + s.WriteString(strings.Join(aspath, format.separator)) + s.WriteString(format.end) + return s.String() +} + func (a *As4PathParam) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type uint8 `json:"segment_type"` @@ -3538,6 +3584,14 @@ func (p *PathAttributeAsPath) Serialize() ([]byte, error) { return p.PathAttribute.Serialize() } +func (p *PathAttributeAsPath) String() string { + params := make([]string, 0, len(p.Value)) + for _, param := range p.Value { + params = append(params, param.String()) + } + return strings.Join(params, " ") +} + func (p *PathAttributeAsPath) MarshalJSON() ([]byte, error) { return json.Marshal(struct { Type BGPAttrType `json:"type"` @@ -5301,6 +5355,14 @@ func (p *PathAttributeAs4Path) Serialize() ([]byte, error) { return p.PathAttribute.Serialize() } +func (p *PathAttributeAs4Path) String() string { + params := make([]string, 0, len(p.Value)) + for _, param := range p.Value { + params = append(params, param.String()) + } + return strings.Join(params, " ") +} + func NewPathAttributeAs4Path(value []*As4PathParam) *PathAttributeAs4Path { t := BGP_ATTR_TYPE_AS4_PATH return &PathAttributeAs4Path{ |