Skip to content

Commit

Permalink
packet/bgp: Avoid raw binary in EVPN ESI string
Browse files Browse the repository at this point in the history
Currently, the value field of EVPN ESI might be corrupted when ESI type
is ARBITRARY (not "single-homed") or unknown type because the value
field will be formatted as the raw byte slice in this case.

This patch fixes to convert the byte slice into the colon separated hex
values which is corresponding to the format of the CLI input.

Signed-off-by: IWASE Yusuke <iwase.yusuke0@gmail.com>
  • Loading branch information
iwaseyusuke authored and fujita committed Nov 21, 2017
1 parent 2d80501 commit 794a164
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packet/bgp/bgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1952,14 +1952,19 @@ func isZeroBuf(buf []byte) bool {
}

func (esi *EthernetSegmentIdentifier) String() string {
toHexArray := func(data []byte) string {
// Converts byte slice into the colon separated hex values and the
// number of elements are 9 at most (excluding Type field).
values := make([]string, 0, 9)
for _, v := range data {
values = append(values, fmt.Sprintf("%02x", v))
}
return strings.Join(values, ":")
}

s := bytes.NewBuffer(make([]byte, 0, 64))
s.WriteString(fmt.Sprintf("%s | ", esi.Type.String()))
switch esi.Type {
case ESI_ARBITRARY:
if isZeroBuf(esi.Value) {
return "single-homed"
}
s.WriteString(fmt.Sprintf("%s", esi.Value))
case ESI_LACP:
s.WriteString(fmt.Sprintf("system mac %s, ", net.HardwareAddr(esi.Value[:6]).String()))
s.WriteString(fmt.Sprintf("port key %d", binary.BigEndian.Uint16(esi.Value[6:8])))
Expand All @@ -1975,8 +1980,13 @@ func (esi *EthernetSegmentIdentifier) String() string {
case ESI_AS:
s.WriteString(fmt.Sprintf("as %d, ", binary.BigEndian.Uint32(esi.Value[:4])))
s.WriteString(fmt.Sprintf("local discriminator %d", binary.BigEndian.Uint32(esi.Value[4:8])))
case ESI_ARBITRARY:
if isZeroBuf(esi.Value) {
return "single-homed"
}
fallthrough
default:
s.WriteString(fmt.Sprintf("value %s", esi.Value))
s.WriteString(toHexArray(esi.Value))
}
return s.String()
}
Expand Down

0 comments on commit 794a164

Please sign in to comment.