diff options
author | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2018-02-22 14:26:36 +0900 |
---|---|---|
committer | IWASE Yusuke <iwase.yusuke0@gmail.com> | 2018-02-22 15:19:13 +0900 |
commit | 77a3484a1223033065df7986987a7ad0b0043ab7 (patch) | |
tree | 5a63239ec9fa37d1d9ed139354c5a99852e25cf5 | |
parent | 89f40dfa4a015d000489a1ffa70824722e7eafed (diff) |
packet/bgp: Return specified type value for UnknownExtended
Currently, "UnknownExtended.GetTypes()" returns always the constant
values even if "Type" value is given.
This patch fixes to return the given type value and use the first
"Value" byte as the sub type.
Also, introduces "NewUnknownExtended()" function for the convenience.
Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com>
-rw-r--r-- | packet/bgp/bgp.go | 25 | ||||
-rw-r--r-- | packet/bgp/helper.go | 2 |
2 files changed, 21 insertions, 6 deletions
diff --git a/packet/bgp/bgp.go b/packet/bgp/bgp.go index c013f23a..7c6fb582 100644 --- a/packet/bgp/bgp.go +++ b/packet/bgp/bgp.go @@ -7217,18 +7217,19 @@ type UnknownExtended struct { } func (e *UnknownExtended) Serialize() ([]byte, error) { - buf := make([]byte, 8) + if len(e.Value) != 7 { + return nil, fmt.Errorf("invalid value length for unknown extended community: %d", len(e.Value)) + } + buf := make([]byte, 8, 8) buf[0] = uint8(e.Type) copy(buf[1:], e.Value) - e.Value = buf[1:] return buf, nil } func (e *UnknownExtended) String() string { buf := make([]byte, 8) copy(buf[1:], e.Value) - v := binary.BigEndian.Uint64(buf) - return fmt.Sprintf("%d", v) + return fmt.Sprintf("%d", binary.BigEndian.Uint64(buf)) } func (e *UnknownExtended) MarshalJSON() ([]byte, error) { @@ -7245,7 +7246,21 @@ func (e *UnknownExtended) MarshalJSON() ([]byte, error) { } func (e *UnknownExtended) GetTypes() (ExtendedCommunityAttrType, ExtendedCommunityAttrSubType) { - return ExtendedCommunityAttrType(0xFF), ExtendedCommunityAttrSubType(0xFF) + var subType ExtendedCommunityAttrSubType + if len(e.Value) > 0 { + // Use the first byte of value as the sub type + subType = ExtendedCommunityAttrSubType(e.Value[0]) + } + return e.Type, subType +} + +func NewUnknownExtended(typ ExtendedCommunityAttrType, value []byte) *UnknownExtended { + v := make([]byte, 7, 7) + copy(v, value) + return &UnknownExtended{ + Type: typ, + Value: v, + } } type PathAttributeExtendedCommunities struct { diff --git a/packet/bgp/helper.go b/packet/bgp/helper.go index 851332fd..3b152628 100644 --- a/packet/bgp/helper.go +++ b/packet/bgp/helper.go @@ -67,7 +67,7 @@ func NewTestBGPUpdateMessage() *BGPMessage { NewIPv4AddressSpecificExtended(EC_SUBTYPE_ROUTE_TARGET, "192.2.1.2", 3000, isTransitive), NewOpaqueExtended(false, []byte{1, 2, 3, 4, 5, 6, 7}), NewValidationExtended(VALIDATION_STATE_INVALID), - &UnknownExtended{Type: 99, Value: []byte{0, 1, 2, 3, 4, 5, 6, 7}}, + NewUnknownExtended(99, []byte{0, 1, 2, 3, 4, 5, 6, 7}), NewESILabelExtended(1000, true), NewESImportRouteTarget("11:22:33:44:55:66"), NewMacMobilityExtended(123, false), |