Skip to content

Commit

Permalink
packet/bgp: Return specified type value for UnknownExtended
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
iwaseyusuke committed Feb 22, 2018
1 parent 89f40df commit 77a3484
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
25 changes: 20 additions & 5 deletions packet/bgp/bgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packet/bgp/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 77a3484

Please sign in to comment.