diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-11-15 20:14:21 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2018-11-16 06:03:56 +0900 |
commit | 62076da8d430889d29276314465bdad9fa1b7e2c (patch) | |
tree | 2cd9e674088c9ab1e1f37ff8b16179f950cb9f8f /pkg | |
parent | 2dddaeffb8bfe9087a77636961b1e701f4236a7c (diff) |
packet: fix NewPathAttributeNextHop() to handle ipv6 addresss
PathAttributeNextHop struct can handle ipv6 address but
NewPathAttributeNextHop() can't.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/packet/bgp/bgp.go | 13 | ||||
-rw-r--r-- | pkg/packet/bgp/bgp_test.go | 11 |
2 files changed, 21 insertions, 3 deletions
diff --git a/pkg/packet/bgp/bgp.go b/pkg/packet/bgp/bgp.go index 7586f47e..941fe185 100644 --- a/pkg/packet/bgp/bgp.go +++ b/pkg/packet/bgp/bgp.go @@ -5654,15 +5654,22 @@ func (p *PathAttributeNextHop) MarshalJSON() ([]byte, error) { }) } -func NewPathAttributeNextHop(value string) *PathAttributeNextHop { +func NewPathAttributeNextHop(addr string) *PathAttributeNextHop { t := BGP_ATTR_TYPE_NEXT_HOP + ip := net.ParseIP(addr) + l := net.IPv4len + if ip.To4() == nil { + l = net.IPv6len + } else { + ip = ip.To4() + } return &PathAttributeNextHop{ PathAttribute: PathAttribute{ Flags: PathAttrFlags[t], Type: t, - Length: 4, + Length: uint16(l), }, - Value: net.ParseIP(value).To4(), + Value: ip, } } diff --git a/pkg/packet/bgp/bgp_test.go b/pkg/packet/bgp/bgp_test.go index a4801f27..6a2d5f12 100644 --- a/pkg/packet/bgp/bgp_test.go +++ b/pkg/packet/bgp/bgp_test.go @@ -1192,3 +1192,14 @@ func TestNormalizeFlowSpecOpValues(t *testing.T) { }) } } + +func Test_PathAttributeNextHop(t *testing.T) { + f := func(addr string) { + b, _ := NewPathAttributeNextHop(addr).Serialize() + p := PathAttributeNextHop{} + p.DecodeFromBytes(b) + assert.Equal(t, addr, p.Value.String()) + } + f("192.0.2.1") + f("2001:db8::68") +} |