summaryrefslogtreecommitdiffhomepage
path: root/packet
diff options
context:
space:
mode:
authorHiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp>2015-01-27 20:44:00 +0900
committerHiroshi Yokoi <yokoi.hiroshi@po.ntts.co.jp>2015-01-28 14:53:24 +0900
commit838cf25f000481f4adf9e508e1e533ec7e8977d4 (patch)
tree84e31a2cb1a5991fe252edc6c197c0586fd7e11c /packet
parente53a0b535298758300f1db2bffa6611fea54a9e1 (diff)
packet: add String() to RouteTargetMembershipNLRI and ExtendedCommunityInterface
Diffstat (limited to 'packet')
-rw-r--r--packet/bgp.go36
-rw-r--r--packet/bgp_test.go49
2 files changed, 85 insertions, 0 deletions
diff --git a/packet/bgp.go b/packet/bgp.go
index e94b2a86..dc32d45a 100644
--- a/packet/bgp.go
+++ b/packet/bgp.go
@@ -437,6 +437,7 @@ type AddrPrefixInterface interface {
AFI() uint16
SAFI() uint8
Len() int
+ String() string
}
type IPAddrPrefixDefault struct {
@@ -942,6 +943,10 @@ func (n *RouteTargetMembershipNLRI) SAFI() uint8 {
func (n *RouteTargetMembershipNLRI) Len() int { return 12 }
+func (n *RouteTargetMembershipNLRI) String() string {
+ return fmt.Sprintf("%d:%s/%d", n.AS, n.RouteTarget.String(), n.Len()*8)
+}
+
func rfshift(afi uint16, safi uint8) RouteFamily {
return RouteFamily(int(afi)<<16 | int(safi))
}
@@ -2072,6 +2077,7 @@ func NewPathAttributeMpUnreachNLRI(nlri []AddrPrefixInterface) *PathAttributeMpU
type ExtendedCommunityInterface interface {
Serialize() ([]byte, error)
+ String() string
}
type TwoOctetAsSpecificExtended struct {
@@ -2089,6 +2095,10 @@ func (e *TwoOctetAsSpecificExtended) Serialize() ([]byte, error) {
return buf, nil
}
+func (e *TwoOctetAsSpecificExtended) String() string {
+ return fmt.Sprintf("%d:%d", e.AS, e.LocalAdmin)
+}
+
type IPv4AddressSpecificExtended struct {
SubType uint8
IPv4 net.IP
@@ -2104,6 +2114,10 @@ func (e *IPv4AddressSpecificExtended) Serialize() ([]byte, error) {
return buf, nil
}
+func (e *IPv4AddressSpecificExtended) String() string {
+ return fmt.Sprintf("%s:%d", e.IPv4.String(), e.LocalAdmin)
+}
+
type FourOctetAsSpecificExtended struct {
SubType uint8
AS uint32
@@ -2119,6 +2133,14 @@ func (e *FourOctetAsSpecificExtended) Serialize() ([]byte, error) {
return buf, nil
}
+func (e *FourOctetAsSpecificExtended) String() string {
+ buf := make([]byte, 4)
+ binary.BigEndian.PutUint32(buf, e.AS)
+ asUpper := binary.BigEndian.Uint16(buf[0:2])
+ asLower := binary.BigEndian.Uint16(buf[2:])
+ return fmt.Sprintf("%d.%d:%d", asUpper, asLower, e.LocalAdmin)
+}
+
type OpaqueExtended struct {
Value []byte
}
@@ -2130,6 +2152,13 @@ func (e *OpaqueExtended) Serialize() ([]byte, error) {
return buf, nil
}
+func (e *OpaqueExtended) String() string {
+ buf := make([]byte, 8)
+ copy(buf[1:], e.Value)
+ v := binary.BigEndian.Uint64(buf)
+ return fmt.Sprintf("%d", v)
+}
+
type UnknownExtended struct {
Type BGPAttrType
Value []byte
@@ -2142,6 +2171,13 @@ func (e *UnknownExtended) Serialize() ([]byte, error) {
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)
+}
+
type PathAttributeExtendedCommunities struct {
PathAttribute
Value []ExtendedCommunityInterface
diff --git a/packet/bgp_test.go b/packet/bgp_test.go
index f5023dae..d2d99b9e 100644
--- a/packet/bgp_test.go
+++ b/packet/bgp_test.go
@@ -2,6 +2,7 @@ package bgp
import (
"bytes"
+ "encoding/binary"
"github.com/stretchr/testify/assert"
"net"
"testing"
@@ -146,3 +147,51 @@ func Test_IPAddrPrefixString(t *testing.T) {
ipv6 = NewIPv6AddrPrefix(18, "3343:faba:3903::0")
assert.Equal(t, "3343:faba:3903::/18", ipv6.String())
}
+
+func Test_RouteTargetMembershipNLRIString(t *testing.T) {
+ r := &RouteTargetMembershipNLRI{}
+
+ assert := assert.New(t)
+
+ // TwoOctetAsSpecificExtended
+ buf := make([]byte, 12)
+ binary.BigEndian.PutUint32(buf[:4], 65546)
+ buf[4] = 0x00 // typehigh
+ binary.BigEndian.PutUint16(buf[6:8], 65000)
+ binary.BigEndian.PutUint32(buf[8:], 65546)
+ r.DecodeFromBytes(buf)
+ assert.Equal("65546:65000:65546/96", r.String())
+
+ // IPv4AddressSpecificExtended
+ binary.BigEndian.PutUint32(buf[:4], 65546)
+ buf[4] = 0x01 // typehigh
+ ip := net.ParseIP("10.0.0.1").To4()
+ copy(buf[6:10], []byte(ip))
+ binary.BigEndian.PutUint16(buf[10:], 65000)
+ r.DecodeFromBytes(buf)
+ assert.Equal("65546:10.0.0.1:65000/96", r.String())
+
+ // FourOctetAsSpecificExtended
+ binary.BigEndian.PutUint32(buf[:4], 65546)
+ buf[4] = 0x02 // typehigh
+ buf[5] = 0x01 // subtype
+ binary.BigEndian.PutUint32(buf[6:], 65546)
+ binary.BigEndian.PutUint16(buf[10:], 65000)
+ r.DecodeFromBytes(buf)
+ assert.Equal("65546:1.10:65000/96", r.String())
+
+ // OpaqueExtended
+ binary.BigEndian.PutUint32(buf[:4], 65546)
+ buf[4] = 0x03 // typehigh
+ binary.BigEndian.PutUint32(buf[8:], 1000000)
+ r.DecodeFromBytes(buf)
+ assert.Equal("65546:281479272677952/96", r.String())
+
+ // Unknown
+ binary.BigEndian.PutUint32(buf[:4], 65546)
+ buf[4] = 0x04 // typehigh
+ binary.BigEndian.PutUint32(buf[8:], 1000000)
+ r.DecodeFromBytes(buf)
+ assert.Equal("65546:281479272677952/96", r.String())
+
+}