Skip to content
Snippets Groups Projects
update_helper.go 1.83 KiB
Newer Older
package server

import (
	"fmt"

	"github.com/bio-routing/bio-rd/protocols/bgp/packet"
	"github.com/bio-routing/bio-rd/route"
func pathAttribues(p *route.Path) (*packet.PathAttribute, error) {
Daniel Czerwonk's avatar
Daniel Czerwonk committed
	asPath := &packet.PathAttribute{
		TypeCode: packet.ASPathAttr,
		Value:    p.BGPPath.ASPath,
	}

	origin := &packet.PathAttribute{
		TypeCode: packet.OriginAttr,
		Value:    p.BGPPath.Origin,
	}
Daniel Czerwonk's avatar
Daniel Czerwonk committed
	asPath.Next = origin

	nextHop := &packet.PathAttribute{
		TypeCode: packet.NextHopAttr,
		Value:    p.BGPPath.NextHop,
	}
Daniel Czerwonk's avatar
Daniel Czerwonk committed
	origin.Next = nextHop
Daniel Czerwonk's avatar
Daniel Czerwonk committed
	localPref := &packet.PathAttribute{
		TypeCode: packet.LocalPrefAttr,
		Value:    p.BGPPath.LocalPref,
	}
	nextHop.Next = localPref

	if p.BGPPath != nil {
Daniel Czerwonk's avatar
Daniel Czerwonk committed
		err := addOptionalPathAttribues(p, localPref)

		if err != nil {
			return nil, err
		}
	}

Daniel Czerwonk's avatar
Daniel Czerwonk committed
	return asPath, nil
}

func addOptionalPathAttribues(p *route.Path, parent *packet.PathAttribute) error {
	current := parent

Daniel Czerwonk's avatar
Daniel Czerwonk committed
	if len(p.BGPPath.Communities) > 0 {
		communities := &packet.PathAttribute{
			TypeCode: packet.CommunitiesAttr,
			Value:    p.BGPPath.Communities,
Daniel Czerwonk's avatar
Daniel Czerwonk committed
		}
		current.Next = communities
		current = communities
	}

	if len(p.BGPPath.LargeCommunities) > 0 {
		largeCommunities := &packet.PathAttribute{
			TypeCode: packet.LargeCommunitiesAttr,
			Value:    p.BGPPath.LargeCommunities,
		}
		current.Next = largeCommunities
		current = largeCommunities
	}

	return nil
}
	SerializeUpdate(opt *packet.Options) ([]byte, error)
func serializeAndSendUpdate(out io.Writer, update serializeAbleUpdate, opt *packet.Options) error {
	updateBytes, err := update.SerializeUpdate(opt)
	if err != nil {
		log.Errorf("Unable to serialize BGP Update: %v", err)
		return nil
	}

	_, err = out.Write(updateBytes)
	if err != nil {
		return fmt.Errorf("Failed sending Update: %v", err)
	}
	return nil
}