Skip to content
Snippets Groups Projects
Commit 6c593157 authored by Daniel Czerwonk's avatar Daniel Czerwonk
Browse files

changed peer and local address to our IP type

parent 515a8f82
No related branches found
No related tags found
No related merge requests found
package config
import (
"net"
"time"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/filter"
)
// Peer defines the configuration for a BGP session
type Peer struct {
AdminEnabled bool
ReconnectInterval time.Duration
KeepAlive time.Duration
HoldTime time.Duration
LocalAddress net.IP
PeerAddress net.IP
LocalAddress bnet.IP
PeerAddress bnet.IP
LocalAS uint32
PeerAS uint32
Passive bool
......
......@@ -42,8 +42,8 @@ func main() {
AdminEnabled: true,
LocalAS: 65200,
PeerAS: 65300,
PeerAddress: net.IP([]byte{172, 17, 0, 3}),
LocalAddress: net.IP([]byte{169, 254, 200, 0}),
PeerAddress: bnet.IPv4FromOctets(172, 17, 0, 3),
LocalAddress: bnet.IPv4FromOctets(169, 254, 200, 0),
ReconnectInterval: time.Second * 15,
HoldTime: time.Second * 90,
KeepAlive: time.Second * 30,
......@@ -61,8 +61,8 @@ func main() {
AdminEnabled: true,
LocalAS: 65200,
PeerAS: 65100,
PeerAddress: net.IP([]byte{172, 17, 0, 2}),
LocalAddress: net.IP([]byte{169, 254, 100, 1}),
PeerAddress: bnet.IPv4FromOctets(172, 17, 0, 2),
LocalAddress: bnet.IPv4FromOctets(169, 254, 100, 1),
ReconnectInterval: time.Second * 15,
HoldTime: time.Second * 90,
KeepAlive: time.Second * 30,
......
......@@ -2,6 +2,7 @@ package net
import (
"fmt"
"net"
)
// IP represents an IPv4 or IPv6 address
......@@ -45,9 +46,14 @@ func (ip *IP) ToUint32() uint32 {
return uint32(^uint64(0) >> 32 & ip.lower)
}
// Equal returns true if ip is equal to other
func (ip IP) Equal(other IP) bool {
return ip == other
}
// Compare compares two IP addresses (returns 0 if equal, -1 if `ip` is smaller than `other`, 1 if `ip` is greater than `other`)
func (ip IP) Compare(other IP) int {
if ip == other {
if ip.Equal(other) {
return 0
}
......@@ -133,3 +139,8 @@ func (ip IP) bytesIPv6() []byte {
byte(ip.lower & 0x00000000000000FF),
}
}
// ToNetIP converts the IP address in a `net.IP`
func (ip IP) ToNetIP() net.IP {
return net.IP(ip.Bytes())
}
......@@ -2,6 +2,7 @@ package net
import (
"math"
"net"
"testing"
"github.com/stretchr/testify/assert"
......@@ -254,3 +255,36 @@ func TestIPv6FromBlocks(t *testing.T) {
})
}
}
func TestToNetIP(t *testing.T) {
tests := []struct {
name string
ip IP
expected net.IP
}{
{
name: "IPv4",
ip: IPv4FromOctets(192, 168, 1, 1),
expected: net.IP{192, 168, 1, 1},
},
{
name: "IPv6",
ip: IPv6FromBlocks(
0x2001,
0x678,
0x1e0,
0x1234,
0x5678,
0xdead,
0xbeef,
0xcafe),
expected: net.IP{32, 1, 6, 120, 1, 224, 18, 52, 86, 120, 222, 173, 190, 239, 202, 254},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, test.ip.ToNetIP())
})
}
}
......@@ -170,7 +170,7 @@ func (fsm *FSM) tcpConnector() error {
for {
select {
case <-fsm.initiateCon:
c, err := net.DialTCP("tcp", &net.TCPAddr{IP: fsm.local}, &net.TCPAddr{IP: fsm.peer.addr, Port: BGPPORT})
c, err := net.DialTCP("tcp", &net.TCPAddr{IP: fsm.local}, &net.TCPAddr{IP: fsm.peer.addr.ToNetIP(), Port: BGPPORT})
if err != nil {
select {
case fsm.conErrCh <- err:
......
......@@ -73,7 +73,7 @@ func (s *establishedState) init() error {
n := &routingtable.Neighbor{
Type: route.BGPPathType,
Address: bnet.IPv4(bnet.IPv4ToUint32(s.fsm.peer.addr)),
Address: s.fsm.peer.addr,
IBGP: s.fsm.peer.localASN == s.fsm.peer.peerASN,
LocalASN: s.fsm.peer.localASN,
RouteServerClient: s.fsm.peer.routeServerClient,
......@@ -219,7 +219,7 @@ func (s *establishedState) updates(u *packet.BGPUpdate) {
path := &route.Path{
Type: route.BGPPathType,
BGPPath: &route.BGPPath{
Source: bnet.IPv4(bnet.IPv4ToUint32(s.fsm.peer.addr)),
Source: s.fsm.peer.addr,
EBGP: s.fsm.peer.localASN != s.fsm.peer.peerASN,
},
}
......
package server
import (
"net"
"sync"
"testing"
"time"
......@@ -10,12 +9,14 @@ import (
"github.com/bio-routing/bio-rd/routingtable/filter"
"github.com/bio-routing/bio-rd/routingtable/locRIB"
"github.com/stretchr/testify/assert"
bnet "github.com/bio-routing/bio-rd/net"
)
// TestFSM100Updates emulates receiving 100 BGP updates and withdraws. Checks route counts.
func TestFSM100Updates(t *testing.T) {
fsmA := newFSM2(&peer{
addr: net.ParseIP("169.254.100.100"),
addr: bnet.IPv4FromOctets(169, 254, 100, 100),
rib: locRIB.New(),
importFilter: filter.NewAcceptAllFilter(),
exportFilter: filter.NewAcceptAllFilter(),
......
package server
import (
"net"
"sync"
"time"
"github.com/bio-routing/bio-rd/config"
bnet "github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/filter"
......@@ -13,14 +13,14 @@ import (
)
type PeerInfo struct {
PeerAddr net.IP
PeerAddr bnet.IP
PeerASN uint32
LocalASN uint32
}
type peer struct {
server *bgpServer
addr net.IP
addr bnet.IP
peerASN uint32
localASN uint32
......@@ -184,7 +184,7 @@ func filterOrDefault(f *filter.Filter) *filter.Filter {
}
// GetAddr returns the IP address of the peer
func (p *peer) GetAddr() net.IP {
func (p *peer) GetAddr() bnet.IP {
return p.addr
}
......
package server
import (
"net"
"testing"
"time"
......@@ -9,6 +8,8 @@ import (
"github.com/bio-routing/bio-rd/routingtable"
"github.com/bio-routing/bio-rd/routingtable/filter"
"github.com/bio-routing/bio-rd/routingtable/locRIB"
bnet "github.com/bio-routing/bio-rd/net"
)
func TestBgpServerPeerSnapshot(t *testing.T) {
......@@ -30,8 +31,8 @@ func TestBgpServerPeerSnapshot(t *testing.T) {
pc := config.Peer{
AdminEnabled: true,
PeerAS: 65300,
PeerAddress: net.IP([]byte{169, 254, 200, 1}),
LocalAddress: net.IP([]byte{169, 254, 200, 0}),
PeerAddress: bnet.IPv4FromOctets(169, 254, 200, 1),
LocalAddress: bnet.IPv4FromOctets(169, 254, 200, 0),
ReconnectInterval: time.Second * 15,
HoldTime: time.Second * 90,
KeepAlive: time.Second * 30,
......@@ -56,7 +57,7 @@ func TestBgpServerPeerSnapshot(t *testing.T) {
break
}
if want, got := net.ParseIP("169.254.200.1"), peer.PeerAddr; !want.Equal(got) {
if want, got := bnet.IPv4FromOctets(169, 254, 200, 1), peer.PeerAddr; !want.Equal(got) {
t.Errorf("PeerAddr: got %v, want %v", got, want)
}
if want, got := uint32(65300), peer.PeerASN; want != got {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment