Skip to content
Snippets Groups Projects
peer.go 1.49 KiB
Newer Older
Oliver Herms's avatar
Oliver Herms committed
package server

import (
	"net"

	"github.com/bio-routing/bio-rd/protocols/bgp/packet"
	"github.com/bio-routing/bio-rd/routingtable"
Oliver Herms's avatar
Oliver Herms committed
	"github.com/bio-routing/bio-rd/routingtable/locRIB"

Oliver Herms's avatar
Oliver Herms committed
	"github.com/bio-routing/bio-rd/config"
Oliver Herms's avatar
Oliver Herms committed
)

type Peer struct {
	addr          net.IP
	asn           uint32
	fsm           *FSM
	rib           *locRIB.LocRIB
	routerID      uint32
	addPathSend   routingtable.ClientOptions
Oliver Herms's avatar
Oliver Herms committed
	addPathRecv   bool
	optOpenParams []packet.OptParam
Oliver Herms's avatar
Oliver Herms committed
}

Oliver Herms's avatar
Oliver Herms committed
func NewPeer(c config.Peer, rib *locRIB.LocRIB) (*Peer, error) {
Oliver Herms's avatar
Oliver Herms committed
	p := &Peer{
		addr:          c.PeerAddress,
		asn:           c.PeerAS,
		rib:           rib,
		addPathSend:   c.AddPathSend,
		addPathRecv:   c.AddPathRecv,
		optOpenParams: make([]packet.OptParam, 0),
Oliver Herms's avatar
Oliver Herms committed
	}
	p.fsm = NewFSM(p, c, rib)

	caps := make([]packet.Capability, 0)

	addPath := uint8(0)
Oliver Herms's avatar
Oliver Herms committed
	if c.AddPathRecv {
		addPath += packet.AddPathReceive
	}
	if !c.AddPathSend.BestOnly {
		addPath += packet.AddPathSend
	}

	if addPath > 0 {
		caps = append(caps, packet.Capability{
			Code: packet.AddPathCapabilityCode,
			Value: packet.AddPathCapability{
				AFI:         packet.IPv4AFI,
				SAFI:        packet.UnicastSAFI,
				SendReceive: addPath,
			},
		})
	}

	for _, cap := range caps {
		p.optOpenParams = append(p.optOpenParams, packet.OptParam{
			Type:  packet.CapabilitiesParamType,
			Value: cap,
		})
	}

Oliver Herms's avatar
Oliver Herms committed
	return p, nil
}

func (p *Peer) GetAddr() net.IP {
	return p.addr
}

func (p *Peer) GetASN() uint32 {
	return p.asn
}

func (p *Peer) Start() {
	p.fsm.start()
	p.fsm.activate()
}