msg

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2025 License: MIT Imports: 16 Imported by: 6

Documentation

Overview

Package msg represents BGP messages.

This package can read/write BGP messages in wire and JSON formats.

Index

Constants

View Source
const (
	// BGP header length, per rfc4271/4.1
	HEADLEN = 19 // = marker(16) + length(2) + type(1)

	// BGP maximum message length, per rfc4271
	MAXLEN = 4096

	// BGP maximum extended message length, per rfc8654
	MAXLEN_EXT = 65535

	// JSON date and time format
	JSON_TIME = `2006-01-02T15:04:05.000`
)
View Source
const (
	OPEN_MINLEN   = 29 - HEADLEN // rfc4271/4.2
	OPEN_VERSION  = 4
	OPEN_HOLDTIME = 90

	PARAM_CAPS   = 2
	PARAM_EXTLEN = 255

	AS_TRANS = 23456
)
View Source
const (
	UPDATE_MINLEN = 23 - HEADLEN // rfc4271/4.3
)

Variables

View Source
var (
	// generic errors
	ErrTODO        = errors.New("not implemented")
	ErrUnsupported = errors.New("unsupported")
	ErrType        = errors.New("invalid type")
	ErrValue       = errors.New("invalid value")
	ErrLength      = errors.New("invalid length")
	ErrShort       = errors.New("too short")
	ErrLong        = errors.New("too long")

	ErrNoData    = errors.New("no message data")
	ErrNoUpper   = errors.New("no upper layer")
	ErrMarker    = errors.New("marker not found")
	ErrVersion   = errors.New("invalid version")
	ErrParams    = errors.New("invalid parameters")
	ErrCaps      = errors.New("invalid capabilities")
	ErrAttrDupe  = errors.New("duplicate attribute")
	ErrAttrCode  = errors.New("invalid attribute code")
	ErrAttrFlags = errors.New("invalid attribute flags")
	ErrAttrs     = errors.New("invalid attributes")
	ErrSegType   = errors.New("invalid segment type")
	ErrSegLen    = errors.New("invalid segment length")
)
View Source
var (

	// https://datatracker.ietf.org/doc/html/rfc4271#autoid-9
	BgpMarker = []byte{
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
		0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	}
)

Functions

func TypeStrings

func TypeStrings() []string

TypeStrings returns a slice of all String values of the enum

Types

type Msg

type Msg struct {
	Dir  dir.Dir   // message destination
	Seq  int64     // sequence number
	Time time.Time // message timestamp

	Type Type   // message type
	Data []byte // message data (referenced or owned), can be nil

	Upper  Type   // which of the upper layers is valid?
	Open   Open   // BGP OPEN message
	Update Update // BGP UPDATE message

	Version int   // contents version number, incremented on each change
	Value   Value // attached value, NOTE: this is *NOT* affected by Reset()
	// contains filtered or unexported fields
}

Msg represents a BGP message. Use NewMsg to get a new valid object.

func NewMsg

func NewMsg() *Msg

NewMsg returns new empty message

func (*Msg) CopyData

func (msg *Msg) CopyData() *Msg

CopyData makes msg the owner of msg.Data, copying referenced external data iff needed.

func (*Msg) Edit added in v0.5.1

func (msg *Msg) Edit(cond ...bool)

Edit ditches msg.Data and its internal JSON representation, making the Upper layer the only source of information about msg. It also increments msg.Version, to signal that the message has changed.

The optional variadic parameter cond allows conditional editing: if cond is provided and none of its values are true, Edit will return early and make no changes. This is useful for callers who want to skip editing unless certain conditions are met.

Edit must be called when the Upper layer is modified, to signal that both msg.Data and the JSON representation must be regenerated when needed.

func (*Msg) FromBytes added in v0.1.7

func (msg *Msg) FromBytes(buf []byte) (off int, err error)

FromBytes reads one BGP message from buf, referencing buf data inside msg.Data. If needed, call CopyData(), DropData() or Reset() later to remove the reference. Returns the number of bytes read from buf, which can be less than len(buf).

func (*Msg) FromJSON

func (msg *Msg) FromJSON(src []byte) (reterr error)

FromJSON reads msg JSON representation from src into Upper

func (*Msg) GetJSON added in v0.1.7

func (msg *Msg) GetJSON() []byte

GetJSON returns JSON representation of msg + "\n" directly from an internal buffer. The result is always non-nil and non-empty. Copy the result if you need to keep it.

func (*Msg) Len added in v0.3.0

func (msg *Msg) Len() int

Len returns total BGP message length, including the header. Call msg.Marshal() first if needed. Returns 0 on error.

func (*Msg) Marshal added in v0.1.7

func (msg *Msg) Marshal(cps caps.Caps) error

Marshal marshals the upper layer to msg.Data iff possible and needed. caps can influence the upper layer encoders.

func (*Msg) Parse

func (msg *Msg) Parse(cps caps.Caps) error

Parse parses msg.Data into the upper layer iff needed. Capabilities in caps can infuence the upper layer decoders. Can reference data in msg.Data, so call CopyData() first if needed.

func (*Msg) Reset

func (msg *Msg) Reset() *Msg

Reset clears the message. It does not reset the upper layer.

func (*Msg) String

func (msg *Msg) String() string

String dumps msg to JSON string, without the trailing newline

func (*Msg) Switch added in v0.5.2

func (msg *Msg) Switch(typ Type) *Msg

Switch selects the upper layer of given type for active use, resets the upper layer, and calls msg.Edit() to signal the change.

func (*Msg) ToJSON

func (msg *Msg) ToJSON(dst []byte) []byte

ToJSON appends JSON representation of msg + "\n" to dst (may be nil to allocate)

func (*Msg) WriteTo

func (msg *Msg) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes raw BGP msg.Data to w, implementing io.WriterTo. Call msg.Marshal() first if needed.

type Open

type Open struct {
	Msg *Msg // parent BGP message

	Version    byte       // must be 4
	ASN        uint16     // 2-byte local ASN
	HoldTime   uint16     // proposed hold time
	Identifier netip.Addr // router identifier
	Params     []byte     // raw Optional Parameters
	ParamsExt  bool       // true iff Params use extended length

	Caps caps.Caps // BGP capabilities, usually parsed from Params
}

Open represents a BGP OPEN message

func (*Open) FromJSON

func (o *Open) FromJSON(src []byte) error

FromJSON reads o JSON representation from src

func (*Open) GetASN

func (o *Open) GetASN() int

GetASN returns the local ASN, preferably from AS4

func (*Open) Init

func (o *Open) Init(m *Msg)

Init initializes o to use parent m

func (*Open) Marshal

func (o *Open) Marshal() error

Marshal marshals o to o.Msg.Data.

func (*Open) MarshalCaps

func (o *Open) MarshalCaps() error

MarshalCaps marshals o.Caps into o.Params. Sets o.ParamsExt.

func (*Open) Parse

func (o *Open) Parse() error

Parse parses o.Msg.Data as BGP OPEN

func (*Open) ParseCaps

func (o *Open) ParseCaps() error

ParseCaps parses all capability codes from Params to Caps

func (*Open) Reset

func (o *Open) Reset()

Reset prepares o for re-use

func (*Open) SetASN

func (o *Open) SetASN(asn int)

SetASN sets local ASN number in o and its AS4

func (*Open) String

func (o *Open) String() string

String dumps o to JSON

func (*Open) ToJSON

func (o *Open) ToJSON(dst []byte) []byte

ToJSON appends JSON representation of o to dst

type Type

type Type byte

BGP message type

const (
	INVALID   Type = 0 // NOT DEFINED / INVALID
	OPEN      Type = 1 // OPEN
	UPDATE    Type = 2 // UPDATE
	NOTIFY    Type = 3 // NOTFICATION
	KEEPALIVE Type = 4 // KEEPALIVE
	REFRESH   Type = 5 // ROUTE-REFRESH RFC2918
)

func TypeString

func TypeString(s string) (Type, error)

TypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func TypeValues

func TypeValues() []Type

TypeValues returns all values of the enum

func (Type) IsAType

func (i Type) IsAType() bool

IsAType returns "true" if the value is listed in the enum definition. "false" otherwise

func (Type) String

func (i Type) String() string

type Update

type Update struct {
	Msg *Msg // parent BGP message

	Reach    []nlri.NLRI // reachable IPv4 unicast
	Unreach  []nlri.NLRI // unreachable IPv4 unicast
	RawAttrs []byte      // raw attributes, referencing Msg.Data

	Attrs attrs.Attrs // parsed attributes
	// contains filtered or unexported fields
}

Update represents a BGP UPDATE message

func (*Update) AddReach added in v0.5.2

func (u *Update) AddReach(prefixes ...nlri.NLRI)

AddReach adds all reachable prefixes to u. NB: it will purge non-IPv6 MP_REACH attribute if needed

func (*Update) AddUnreach added in v0.5.2

func (u *Update) AddUnreach(prefixes ...nlri.NLRI)

AddUnreach adds all unreachable prefixes to u. NB: it will purge non-IPv6 MP_UNREACH attribute if needed

func (*Update) AfiSafi added in v0.5.1

func (u *Update) AfiSafi() afi.AS

AfiSafi returns the message AFI+SAFI, giving priority to MP-BGP attributes

func (*Update) AllReach added in v0.5.1

func (u *Update) AllReach() []nlri.NLRI

AllReach returns all reachable prefixes, including those in MP-BGP attributes. Uses cached value if available. Do not modify the returned slice.

func (*Update) AllUnreach added in v0.5.1

func (u *Update) AllUnreach() []nlri.NLRI

AllUnreach returns all unreachable prefixes, including those in MP-BGP attributes. Uses cached value if available. Do not modify the returned slice.

func (*Update) AsPath added in v0.4.0

func (u *Update) AsPath() (aspath *attrs.Aspath)

AsPath returns the AS path from u, or nil if not defined.

func (*Update) Community added in v0.5.1

func (u *Update) Community() *attrs.Community

Community returns the community attribute, or nil if not defined.

func (*Update) ExtCommunity added in v0.5.1

func (u *Update) ExtCommunity() *attrs.Extcom

ExtCommunity returns the extended community attribute, or nil if not defined.

func (*Update) FromJSON

func (u *Update) FromJSON(src []byte) error

FromJSON reads u JSON representation from src

func (*Update) HasReach added in v0.4.0

func (u *Update) HasReach() bool

HasReach returns true iff u announces reachable NLRI (for any address family).

func (*Update) HasUnreach added in v0.4.0

func (u *Update) HasUnreach() bool

HasUnreach returns true iff u withdraws unreachable NLRI (for any address family).

func (*Update) Init

func (u *Update) Init(m *Msg)

Init initializes u to use parent m

func (*Update) LargeCommunity added in v0.5.1

func (u *Update) LargeCommunity() *attrs.LargeCom

LargeCommunity returns the large community attribute, or nil if not defined.

func (*Update) Marshal

func (u *Update) Marshal(cps caps.Caps) error

Marshal marshals u to u.Msg.Data.

func (*Update) MarshalAttrs

func (u *Update) MarshalAttrs(cps caps.Caps) error

MarshalAttrs marshals u.Attrs into u.RawAttrs

func (*Update) NextHop added in v0.4.0

func (u *Update) NextHop() (nh netip.Addr)

NextHop returns NEXT_HOP address, if possible. Check nh.IsValid() before using the value.

func (*Update) Parse

func (u *Update) Parse(cps caps.Caps) error

Parse parses msg.Data as BGP UPDATE, in the context of BGP capabilities cps, which can be empty.

func (*Update) ParseAttrs

func (u *Update) ParseAttrs(cps caps.Caps) error

ParseAttrs parses all attributes from RawAttrs into Attrs.

func (*Update) ReachMP

func (u *Update) ReachMP() *attrs.MP

ReachMP returns raw MP-BGP attribute ATTR_MP_REACH, or nil

func (*Update) Reset

func (u *Update) Reset()

Reset prepares u for re-use

func (*Update) String

func (u *Update) String() string

String dumps u to JSON

func (*Update) ToJSON

func (u *Update) ToJSON(dst []byte) []byte

ToJSON appends JSON representation of u to dst (may be nil)

func (*Update) UnreachMP

func (u *Update) UnreachMP() *attrs.MP

UnreachMP returns raw MP-BGP attribute ATTR_MP_UNREACH, or nil

type Value

type Value interface {
	// ToJSON appends JSON representation of the value to dst
	ToJSON(dst []byte) []byte

	// FromJSON reads from JSON representation in src
	FromJSON(src []byte) error
}

Value represents an optional, arbitrary value attached to a message

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL