msg

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2024 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

	Value Value // NB: 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) 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) Modified added in v0.1.7

func (msg *Msg) Modified()

Modified ditches msg.Data and its internal JSON representation, making the upper layer the only source of information about msg.

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

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. Does not reference data in msg.Data.

func (*Msg) Reset

func (msg *Msg) Reset() *Msg

Reset clears the message

func (*Msg) String

func (msg *Msg) String() string

String dumps msg to JSON string, without the trailing newline

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) Use added in v0.1.7

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

Use selects the upper layer of given type for active use. Calls msg.Modified(), but does not reset the selected upper layer. Use Reset() on msg or the selected layer if needed.

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

	Attrs attrs.Attrs // parsed attributes
}

Update represents a BGP UPDATE message

func (*Update) AS added in v0.4.0

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

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

func (*Update) AsPath added in v0.4.0

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

AsPath returns the ATTR_ASPATH from u, or nil if not defined. TODO: support ATTR_AS4PATH

func (*Update) FromJSON

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

FromJSON reads u JSON representation from src

func (*Update) GetReach added in v0.4.0

func (u *Update) GetReach(dst []nlri.NLRI) []nlri.NLRI

GetReach appends all reachable IP prefixes in u to dst (can be nil)

func (*Update) GetUnreach added in v0.4.0

func (u *Update) GetUnreach(dst []nlri.NLRI) []nlri.NLRI

GetUnreach appends all unreachable IP prefixes in u to dst (can be nil)

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 AF).

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 AF).

func (*Update) Init

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

Init initializes u to use parent m

func (*Update) MP added in v0.4.0

func (u *Update) MP(ac attrs.Code) *attrs.MP

MP returns raw MP-BGP attribute ac, or nil

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) 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)

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