Documentation ¶
Overview ¶
Package msg represents BGP messages.
This package can read/write BGP messages in wire and JSON formats.
Index ¶
- Constants
- Variables
- func DirStrings() []string
- func TypeStrings() []string
- type Dir
- type Msg
- func (msg *Msg) CopyData() *Msg
- func (msg *Msg) FromBytes(buf []byte) (off int, err error)
- func (msg *Msg) FromJSON(src []byte) (reterr error)
- func (msg *Msg) GetJSON() []byte
- func (msg *Msg) Length() int
- func (msg *Msg) Marshal(cps caps.Caps) error
- func (msg *Msg) Modified()
- func (msg *Msg) Parse(cps caps.Caps) error
- func (msg *Msg) Reset() *Msg
- func (msg *Msg) String() string
- func (msg *Msg) ToJSON(dst []byte) []byte
- func (msg *Msg) Use(typ Type) *Msg
- func (msg *Msg) WriteTo(w io.Writer) (n int64, err error)
- type Open
- func (o *Open) FromJSON(src []byte) error
- func (o *Open) GetASN() int
- func (o *Open) Init(m *Msg)
- func (o *Open) Marshal() error
- func (o *Open) MarshalCaps() error
- func (o *Open) Parse() error
- func (o *Open) ParseCaps() error
- func (o *Open) Reset()
- func (o *Open) SetASN(asn int)
- func (o *Open) String() string
- func (o *Open) ToJSON(dst []byte) []byte
- type Type
- type Update
- func (u *Update) AF() af.AF
- func (u *Update) FromJSON(src []byte) error
- func (u *Update) Init(m *Msg)
- func (u *Update) Marshal(cps caps.Caps) error
- func (u *Update) MarshalAttrs(cps caps.Caps) error
- func (u *Update) Parse() error
- func (u *Update) ParseAttrs(cps caps.Caps) error
- func (u *Update) Reset()
- func (u *Update) String() string
- func (u *Update) ToJSON(dst []byte) []byte
- type Value
Constants ¶
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` )
const ( OPEN_MINLEN = 29 - HEADLEN // rfc4271/4.2 OPEN_VERSION = 4 OPEN_HOLDTIME = 90 PARAM_CAPS = 2 PARAM_EXTLEN = 255 AS_TRANS = 23456 )
const (
UPDATE_MINLEN = 23 - HEADLEN // rfc4271/4.3
)
Variables ¶
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") )
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 DirStrings ¶
func DirStrings() []string
DirStrings 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 Dir ¶
type Dir byte
BGP message direction
func DirString ¶
DirString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.
type Msg ¶
type Msg struct { 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 (*Msg) CopyData ¶
CopyData makes msg the owner of msg.Data, copying referenced external data iff needed.
func (*Msg) FromBytes ¶ added in v0.1.7
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) GetJSON ¶ added in v0.1.7
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) Length ¶
Length 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
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 ¶
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) ToJSON ¶
ToJSON appends JSON representation of msg + "\n" to dst (may be nil to allocate)
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) MarshalCaps ¶
MarshalCaps marshals o.Caps into o.Params. Sets o.ParamsExt.
type Type ¶
type Type byte
BGP message type
func TypeString ¶
TypeString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.
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) AF ¶ added in v0.1.4
AF returns the message address family, giving priority to MP-BGP attributes
func (*Update) MarshalAttrs ¶
MarshalAttrs marshals u.Attrs into u.RawAttrs
func (*Update) ParseAttrs ¶
ParseAttrs parses all attributes from RawAttrs into Attrs.