summaryrefslogtreecommitdiffhomepage
path: root/packet/bgp/bgp.go
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2018-03-19 22:56:12 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2018-03-19 22:56:12 +0900
commitcda7c44afe723be74e39266823b54ba28d0ec019 (patch)
tree08cf46dd438c449b562000aed2c45780a019c535 /packet/bgp/bgp.go
parent33b1ed91697abc273317c9ad7d1579dabebb9e5f (diff)
packet/bgp: make serialization of ipv4/v6 nlri goroutine-safe
The serialization of ipv4/v6 nlri should be goroutine-safe but somehow not. Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'packet/bgp/bgp.go')
-rw-r--r--packet/bgp/bgp.go28
1 files changed, 14 insertions, 14 deletions
diff --git a/packet/bgp/bgp.go b/packet/bgp/bgp.go
index 7c6fb582..514c4eb9 100644
--- a/packet/bgp/bgp.go
+++ b/packet/bgp/bgp.go
@@ -1077,6 +1077,14 @@ func (r *IPAddrPrefixDefault) decodePrefix(data []byte, bitlen uint8, addrlen ui
}
b := make([]byte, addrlen)
copy(b, data[:bytelen])
+ // clear trailing bits in the last byte. rfc doesn't require
+ // this but some bgp implementations need this...
+ rem := bitlen % 8
+ if rem != 0 {
+ mask := 0xff00 >> rem
+ lastByte := b[bytelen-1] & byte(mask)
+ b[bytelen-1] = lastByte
+ }
r.Prefix = b
return nil
}
@@ -1085,16 +1093,6 @@ func (r *IPAddrPrefixDefault) serializePrefix(bitLen uint8) ([]byte, error) {
byteLen := (int(bitLen) + 7) / 8
buf := make([]byte, byteLen)
copy(buf, r.Prefix)
- // clear trailing bits in the last byte. rfc doesn't require
- // this though.
- rem := bitLen % 8
- if rem != 0 {
- mask := 0xff00 >> rem
- lastByte := buf[byteLen-1] & byte(mask)
- buf[byteLen-1] = lastByte
- }
- r.Prefix = make([]byte, len(r.Prefix))
- copy(r.Prefix, buf)
return buf, nil
}
@@ -1173,13 +1171,14 @@ func (r *IPAddrPrefix) SAFI() uint8 {
}
func NewIPAddrPrefix(length uint8, prefix string) *IPAddrPrefix {
- return &IPAddrPrefix{
+ p := &IPAddrPrefix{
IPAddrPrefixDefault{
Length: length,
- Prefix: net.ParseIP(prefix).To4(),
},
4,
}
+ p.IPAddrPrefixDefault.decodePrefix(net.ParseIP(prefix).To4(), length, 4)
+ return p
}
func isIPv4MappedIPv6(ip net.IP) bool {
@@ -1203,15 +1202,16 @@ func (r *IPv6AddrPrefix) String() string {
}
func NewIPv6AddrPrefix(length uint8, prefix string) *IPv6AddrPrefix {
- return &IPv6AddrPrefix{
+ p := &IPv6AddrPrefix{
IPAddrPrefix{
IPAddrPrefixDefault{
Length: length,
- Prefix: net.ParseIP(prefix),
},
16,
},
}
+ p.IPAddrPrefixDefault.decodePrefix(net.ParseIP(prefix), length, 16)
+ return p
}
const (