Documentation
¶
Overview ¶
Package msg represents BGP messages.
This package can read/write BGP messages in wire and JSON formats.
Index ¶
- Constants
- Variables
- func TypeStrings() []string
- type Msg
- func (msg *Msg) CopyData() *Msg
- func (msg *Msg) Edit(cond ...bool)
- 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) Len() int
- func (msg *Msg) Marshal(cps caps.Caps) error
- func (msg *Msg) Parse(cps caps.Caps) error
- func (msg *Msg) Reset() *Msg
- func (msg *Msg) String() string
- func (msg *Msg) Switch(typ Type) *Msg
- func (msg *Msg) ToJSON(dst []byte) []byte
- 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) AddReach(prefixes ...nlri.NLRI)
- func (u *Update) AddUnreach(prefixes ...nlri.NLRI)
- func (u *Update) AfiSafi() afi.AS
- func (u *Update) AllReach() []nlri.NLRI
- func (u *Update) AllUnreach() []nlri.NLRI
- func (u *Update) AsPath() (aspath *attrs.Aspath)
- func (u *Update) Community() *attrs.Community
- func (u *Update) ExtCommunity() *attrs.Extcom
- func (u *Update) FromJSON(src []byte) error
- func (u *Update) HasReach() bool
- func (u *Update) HasUnreach() bool
- func (u *Update) Init(m *Msg)
- func (u *Update) LargeCommunity() *attrs.LargeCom
- func (u *Update) Marshal(cps caps.Caps) error
- func (u *Update) MarshalAttrs(cps caps.Caps) error
- func (u *Update) NextHop() (nh netip.Addr)
- func (u *Update) Parse(cps caps.Caps) error
- func (u *Update) ParseAttrs(cps caps.Caps) error
- func (u *Update) ReachMP() *attrs.MP
- func (u *Update) Reset()
- func (u *Update) String() string
- func (u *Update) ToJSON(dst []byte) []byte
- func (u *Update) UnreachMP() *attrs.MP
- 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 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 (*Msg) CopyData ¶
CopyData makes msg the owner of msg.Data, copying referenced external data iff needed.
func (*Msg) Edit ¶ added in v0.5.1
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
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) Len ¶ added in v0.3.0
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
Marshal marshals the upper layer to msg.Data iff possible and needed. caps can influence the upper layer encoders.
func (*Msg) Parse ¶
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) Switch ¶ added in v0.5.2
Switch selects the upper layer of given type for active use, resets the upper layer, and calls msg.Edit() to signal the change.
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 []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
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
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
AfiSafi returns the message AFI+SAFI, giving priority to MP-BGP attributes
func (*Update) AllReach ¶ added in v0.5.1
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
AllUnreach returns all unreachable prefixes, including those in MP-BGP attributes. Uses cached value if available. Do not modify the returned slice.
func (*Update) Community ¶ added in v0.5.1
Community returns the community attribute, or nil if not defined.
func (*Update) ExtCommunity ¶ added in v0.5.1
ExtCommunity returns the extended community attribute, or nil if not defined.
func (*Update) HasReach ¶ added in v0.4.0
HasReach returns true iff u announces reachable NLRI (for any address family).
func (*Update) HasUnreach ¶ added in v0.4.0
HasUnreach returns true iff u withdraws unreachable NLRI (for any address family).
func (*Update) LargeCommunity ¶ added in v0.5.1
LargeCommunity returns the large community attribute, or nil if not defined.
func (*Update) MarshalAttrs ¶
MarshalAttrs marshals u.Attrs into u.RawAttrs
func (*Update) NextHop ¶ added in v0.4.0
NextHop returns NEXT_HOP address, if possible. Check nh.IsValid() before using the value.
func (*Update) Parse ¶
Parse parses msg.Data as BGP UPDATE, in the context of BGP capabilities cps, which can be empty.
func (*Update) ParseAttrs ¶
ParseAttrs parses all attributes from RawAttrs into Attrs.