msg

package
v0.0.0-...-4712b44 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2023 License: MIT Imports: 14 Imported by: 3

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

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

Functions

func DstStrings

func DstStrings() []string

DstStrings returns a slice of all String values of the enum

func TypeStrings

func TypeStrings() []string

TypeStrings returns a slice of all String values of the enum

Types

type Dst

type Dst byte

BGP message destination (message direction)

const (
	DST_LR Dst = 0 // no particular destination (ie. both L and R)
	DST_L  Dst = 1 // destined for L ("left" or "local")
	DST_R  Dst = 2 // destined for R ("right" or "remote")
)

func DstString

func DstString(s string) (Dst, error)

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

func DstValues

func DstValues() []Dst

DstValues returns all values of the enum

func (Dst) IsADst

func (i Dst) IsADst() bool

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

func (Dst) String

func (i Dst) String() string

type Msg

type Msg struct {
	Dst  Dst       // 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 layer is valid?
	Dirty  bool   // if true, probably needs a marshal (Data does not reflect Upper)
	Open   Open   // parsed BGP OPEN message
	Update Update // parsed 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 copies the referenced data iff needed and makes msg the owner

func (*Msg) Disown

func (msg *Msg) Disown() *Msg

Disown tags msg as not the owner of data.

func (*Msg) FromJSON

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

FromJSON reads msg JSON representation from src

func (*Msg) Length

func (msg *Msg) Length() int

Length returns total BGP message length, including header

func (*Msg) MarshalUpper

func (msg *Msg) MarshalUpper(caps caps.Caps) error

MarshalUpper marshals the upper layer to msg.Data iff possible and needed. caps can infuence the upper layer encoders.

func (*Msg) Own

func (msg *Msg) Own() *Msg

Own tags msg as the owner of referenced data. Does not copy the data.

func (*Msg) Parse

func (msg *Msg) Parse(raw []byte) (off int, err error)

Parse parses the BGP message in raw. Does not copy. Returns the number of parsed bytes from raw.

func (*Msg) ParseUpper

func (msg *Msg) ParseUpper(caps caps.Caps) error

ParseUpper parses the upper layer iff needed. caps can infuence the upper layer decoders.

func (*Msg) Reset

func (msg *Msg) Reset() *Msg

Reset clears the message

func (*Msg) SetData

func (msg *Msg) SetData(data []byte) *Msg

SetData updates the data to reference given value

func (*Msg) String

func (msg *Msg) String() string

String dumps msg to JSON

func (*Msg) ToJSON

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

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

func (*Msg) Up

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

Up prepares to make use of and modify the upper layer of given type. Does not reset the upper layer struct, though.

func (*Msg) WriteTo

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

WriteTo marshals the BGP message to w, implementing io.WriterTo

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 and returns it

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 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    []netip.Prefix // reachable IPv4 unicast
	Unreach  []netip.Prefix // unreachable IPv4 unicast
	RawAttrs []byte         // raw attributes

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

Update represents a BGP UPDATE message

func (*Update) Afi

func (u *Update) Afi() af.AFI

Afi returns the AFI from MP_REACH attribute (or MP_UNREACH)

func (*Update) FromJSON

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

FromJSON reads u JSON representation from src

func (*Update) Init

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

Init initializes u to use parent m

func (*Update) Marshal

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

Marshal marshals o to o.Msg and returns it

func (*Update) MarshalAttrs

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

MarshalAttrs marshals u.Attrs into u.RawAttrs

func (*Update) Parse

func (u *Update) Parse() error

Parse parses msg.Data as BGP UPDATE

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.MPValue

ReachMP returns attr.ATTR_MP_REACH value from u, or nil if not defined

func (*Update) Reset

func (u *Update) Reset()

Reset prepares u for re-use

func (*Update) Safi

func (u *Update) Safi() af.SAFI

Safi returns the SAFI from MP_REACH attribute (or MP_UNREACH)

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.MPValue

UnreachMP returns attr.ATTR_MP_UNREACH value from u, or nil if not defined

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