mrt

package
v0.0.0-...-35f84e0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package mrt reads, decodes, and encodes MRT routing-information records (RFC 6396).

Index

Constants

View Source
const (
	AttrOrigin          uint8 = 1
	AttrASPath          uint8 = 2
	AttrNextHop         uint8 = 3
	AttrMED             uint8 = 4
	AttrLocalPref       uint8 = 5
	AttrAtomicAggregate uint8 = 6
	AttrAggregator      uint8 = 7
	AttrCommunity       uint8 = 8
	AttrOriginatorID    uint8 = 9
	AttrClusterList     uint8 = 10
	AttrMPReachNLRI     uint8 = 14
	AttrMPUnreachNLRI   uint8 = 15
	AttrExtCommunity    uint8 = 16
	AttrAS4Path         uint8 = 17
	AttrAS4Aggregator   uint8 = 18
	AttrLargeCommunity  uint8 = 32
	AttrOTC             uint8 = 35 // RFC 9234 Only to Customer
)

BGP path attribute type codes (IANA "BGP Path Attributes" registry).

View Source
const (
	TypeOSPFv2      uint16 = 11
	TypeTableDump   uint16 = 12
	TypeTableDumpV2 uint16 = 13
	TypeBGP4MP      uint16 = 16
	TypeBGP4MPET    uint16 = 17
	TypeISIS        uint16 = 32
	TypeISISET      uint16 = 33
	TypeOSPFv3      uint16 = 48
	TypeOSPFv3ET    uint16 = 49
)

MRT Type codes (RFC 6396 Section 5.3).

View Source
const (
	TableDumpAFIIPv4 uint16 = 1
	TableDumpAFIIPv6 uint16 = 2
)

TABLE_DUMP subtypes (RFC 6396 Section 5.5).

View Source
const (
	TDV2PeerIndexTable     uint16 = 1
	TDV2RIBIPv4Unicast     uint16 = 2
	TDV2RIBIPv4Multicast   uint16 = 3
	TDV2RIBIPv6Unicast     uint16 = 4
	TDV2RIBIPv6Multicast   uint16 = 5
	TDV2RIBGeneric         uint16 = 6
	TDV2GeoPeerTable       uint16 = 7  // RFC 6397
	TDV2RIBIPv4UnicastAP   uint16 = 8  // RFC 8050
	TDV2RIBIPv4MulticastAP uint16 = 9  // RFC 8050
	TDV2RIBIPv6UnicastAP   uint16 = 10 // RFC 8050
	TDV2RIBIPv6MulticastAP uint16 = 11 // RFC 8050
	TDV2RIBGenericAP       uint16 = 12 // RFC 8050
)

TABLE_DUMP_V2 subtypes (RFC 6396 Section 5.6, RFC 6397, RFC 8050).

View Source
const (
	BGP4MPStateChange       uint16 = 0
	BGP4MPMessage           uint16 = 1
	BGP4MPEntry             uint16 = 2 // deprecated
	BGP4MPSnapshot          uint16 = 3 // deprecated
	BGP4MPMessageAS4        uint16 = 4
	BGP4MPStateChangeAS4    uint16 = 5
	BGP4MPMessageLocal      uint16 = 6
	BGP4MPMessageAS4Local   uint16 = 7
	BGP4MPMessageAP         uint16 = 8  // RFC 8050
	BGP4MPMessageAS4AP      uint16 = 9  // RFC 8050
	BGP4MPMessageLocalAP    uint16 = 10 // RFC 8050
	BGP4MPMessageAS4LocalAP uint16 = 11 // RFC 8050
)

BGP4MP / BGP4MP_ET subtypes (RFC 6396 Section 5.7, RFC 8050).

View Source
const (
	FSMIdle        uint16 = 1
	FSMConnect     uint16 = 2
	FSMActive      uint16 = 3
	FSMOpenSent    uint16 = 4
	FSMOpenConfirm uint16 = 5
	FSMEstablished uint16 = 6
)

BGP FSM states (RFC 4271 Section 8.2.2).

View Source
const (
	AFIIPv4 uint16 = 1
	AFIIPv6 uint16 = 2
)

Address families for MRT headers.

View Source
const (
	PeerAS4  byte = 0x02 // bit 6: 1 = 32-bit AS
	PeerIPv6 byte = 0x01 // bit 7: 1 = IPv6
)

Peer Type bit flags (RFC 6396 Section 4.3.1 Figure 7).

View Source
const (
	CommonHeaderLen = 12       // Timestamp(4) + Type(2) + Subtype(2) + Length(4)
	ExtTimestampLen = 4        // Microsecond Timestamp field
	MaxRecordLen    = 16 << 20 // 16 MiB safety cap
)

Header sizes.

Variables

View Source
var (
	// ErrShortData reports input that ends before a field the format requires.
	ErrShortData = errors.New("mrt: short data")
	// ErrBadAFI reports an Address Family Identifier this decoder does not handle.
	ErrBadAFI = errors.New("mrt: unsupported address family")
)

Exported so a caller can tell the failure kinds apart with errors.Is instead of matching on message text. internal/analyze needs exactly that: a truncated NLRI (ErrShortData) is a damaged record it should count and keep going on, while an unrecognized address family (ErrBadAFI) means the record is not one it understands at all.

View Source
var ErrRecordTooLarge = errors.New("mrt: record length exceeds maximum")

ErrRecordTooLarge is returned when a record's Length exceeds MaxRecordLen.

Functions

func ASPathIsFourByte

func ASPathIsFourByte(mrtType, subtype uint16) bool

ASPathIsFourByte reports whether AS_PATH inside the given MRT record type and subtype uses 4-byte AS numbers.

RFC 6396 fixes the width per record type; it is never inferable from the attribute bytes, because a 2-byte path and a 4-byte path can share a byte count. Callers MUST derive the width from the record, which is what this function is for.

TABLE_DUMP (12)          2-byte  (Section 4.2)
TABLE_DUMP_V2 (13)       4-byte  (Section 4.3.4)
BGP4MP_MESSAGE           2-byte  (Section 4.4.2)
BGP4MP_MESSAGE_AS4       4-byte  (Section 4.4.3)

Types that carry no BGP AS_PATH report false (the pre-V2 legacy width).

func DecodeMicrosecond

func DecodeMicrosecond(data []byte) (uint32, error)

DecodeMicrosecond reads the 4-byte microsecond timestamp that follows the common header in _ET type records (RFC 6396 Section 3).

func ExtractCommunities

func ExtractCommunities(attrs []PathAttribute) []uint32

ExtractCommunities returns standard communities as (high:low) uint32 pairs.

func ExtractLargeCommunities

func ExtractLargeCommunities(attrs []PathAttribute) [][3]uint32

ExtractLargeCommunities returns large communities (RFC 8092) as (global, local1, local2) triples.

func ExtractLocalPref

func ExtractLocalPref(attrs []PathAttribute) (uint32, bool)

ExtractLocalPref returns the LOCAL_PREF value.

func ExtractMED

func ExtractMED(attrs []PathAttribute) (uint32, bool)

ExtractMED returns the MULTI_EXIT_DISC value.

func ExtractNextHop

func ExtractNextHop(attrs []PathAttribute) netip.Addr

ExtractNextHop returns the next-hop address from path attributes. Checks NEXT_HOP (type 3) first, then MP_REACH_NLRI (type 14).

func ExtractNextHopRIB

func ExtractNextHopRIB(attrs []PathAttribute) netip.Addr

ExtractNextHopRIB returns the next hop for a TABLE_DUMP_V2 RIB entry.

It prefers the plain NEXT_HOP attribute (type 3), which is how IPv4 RIB entries carry the next hop, and otherwise decodes the abbreviated MP_REACH_NLRI defined by RFC 6396 Section 4.3.4.

Use ExtractNextHop instead for attributes taken from a BGP UPDATE message.

func ExtractOrigin

func ExtractOrigin(attrs []PathAttribute) (uint8, bool)

ExtractOrigin returns the ORIGIN value (0=IGP, 1=EGP, 2=INCOMPLETE).

func FormatASPath

func FormatASPath(segments []ASPathSegment) string

FormatASPath renders parsed AS-path segments as a space-separated string. AS_SEQUENCE segments produce space-separated ASNs; AS_SET segments produce {asn,asn}.

func HasAtomicAggregate

func HasAtomicAggregate(attrs []PathAttribute) bool

HasAtomicAggregate reports whether ATOMIC_AGGREGATE (RFC 4271 Section 5.1.6) is present. The attribute is a flag: its value is zero-length by definition.

func IsAS4Subtype

func IsAS4Subtype(subtype uint16) bool

IsAS4Subtype reports whether a BGP4MP subtype uses 4-byte AS numbers.

func IsAddPathBGP4MPSubtype

func IsAddPathBGP4MPSubtype(subtype uint16) bool

IsAddPathBGP4MPSubtype reports whether a BGP4MP subtype carries add-path NLRI.

func IsAddPathRIBSubtype

func IsAddPathRIBSubtype(subtype uint16) bool

IsAddPathRIBSubtype reports whether a TABLE_DUMP_V2 subtype carries Path Identifiers per RFC 8050.

func MatchCommunityRegex

func MatchCommunityRegex(attrs []PathAttribute, match func(s string) bool) bool

MatchCommunityRegex tests whether any community in the attributes matches the given regex. Checks standard (type 8), large (type 32), and extended (type 16). Returns true on first match.

func ParseMPReachRIBEntry

func ParseMPReachRIBEntry(value []byte) (netip.Addr, error)

ParseMPReachRIBEntry decodes the abbreviated MP_REACH_NLRI carried inside a TABLE_DUMP_V2 RIB entry and returns its next hop.

RFC 6396 Section 4.3.4: "only the Next Hop Address Length and Next Hop Address fields are included. The Address Family Identifier, Subsequent AFI, NLRI and Reserved fields are omitted." The AFI/SAFI and prefix already live in the enclosing RIB record header, so the value is exactly:

+---------------------+------------------------+
| Next Hop Length (1) | Next Hop Address (var) |
+---------------------+------------------------+

Decoding this with the full-form parser reads the length from the wrong offset and yields a garbage or empty next hop, which is why this is a separate entry point.

func ParsePrefixes

func ParsePrefixes(data []byte, addPath bool) ([]netip.Prefix, error)

ParsePrefixes parses packed IPv4 NLRI prefixes into netip.Prefix values. Set addPath=true when the NLRI includes a 4-byte Path Identifier per prefix.

The withdrawn-routes and NLRI fields of a BGP UPDATE are always IPv4 (RFC 4271 Section 4.3); IPv6 reachability travels in MP_REACH_NLRI. Use ParsePrefixesAFI for those.

func ParsePrefixesAFI

func ParsePrefixesAFI(data []byte, afi uint16, addPath bool) ([]netip.Prefix, error)

ParsePrefixesAFI parses packed NLRI prefixes for the given address family.

Malformed input is reported, never silently dropped. A damaged NLRI field returns the prefixes decoded so far together with an error naming the offset and the offending value, so a caller can both salvage the good entries and tell the operator the record is damaged. Returning the short list alone would make "fewer routes than the file contains" indistinguishable from "the file has fewer routes" (ai/rules/evidence.md).

A prefix length beyond the family width is never emitted: netip's zero Prefix reads downstream as a default route.

An unrecognized AFI yields no prefixes and an error, per RFC 6396 Section 4.3.3 ("SHOULD discard the remainder of the MRT record").

func RIBSubtypeAFI

func RIBSubtypeAFI(subtype uint16) uint16

RIBSubtypeAFI returns the AFI for an AFI/SAFI-specific RIB subtype. Returns 0 for non-AFI-specific subtypes (RIB_GENERIC, PEER_INDEX_TABLE, GEO_PEER_TABLE).

func ReadFile

func ReadFile(filename string, handler *Handler) error

ReadFile opens an MRT file, auto-detects compression by extension (.gz, .bz2), and iterates records through handler callbacks.

func ReadFrom

func ReadFrom(r io.Reader, handler *Handler) error

ReadFrom iterates MRT records from any reader, dispatching to handler callbacks.

func SniffDecompress

func SniffDecompress(rc io.ReadCloser) (io.ReadCloser, error)

SniffDecompress wraps rc with a gzip or bzip2 decompressor when the stream's leading magic bytes indicate compression (gzip 1f 8b, bzip2 "BZh"), otherwise returns rc reading raw. Only the few peeked bytes are buffered, so the stream stays unbuffered for multi-GB inputs. Used for stdin ("-"), which has no filename extension to sniff. The returned ReadCloser owns rc and closes it.

func UpdateAttributeBytes

func UpdateAttributeBytes(body []byte) ([]byte, error)

UpdateAttributeBytes returns the raw Path Attributes section of an UPDATE body, without decoding the individual attributes.

Callers that need the attribute bytes verbatim (to re-pack them, hash them, or hand them to a matcher) use this; callers that want decoded attributes use ParseUpdateBody. Both share one implementation of the field offsets, so the two can never disagree about where the section starts.

func UpdateSections

func UpdateSections(body []byte) (withdrawn, attrs, nlri []byte, err error)

UpdateSections splits an UPDATE body into its withdrawn-routes, path-attribute and NLRI sections (RFC 4271 Section 4.3).

This is the exported face of the single source of truth for the UPDATE field layout. A caller that needs more than the attribute bytes -- counting the prefixes in all four NLRI locations, for instance -- MUST come through here rather than re-deriving the offsets, because a second copy of the layout is a second thing to get wrong and it will not be wrong in the same way.

func WriteBGP4MPMessage

func WriteBGP4MPMessage(buf []byte, off int, h *BGP4MPHeader, as4 bool, bgpMsg []byte) int

func WriteBGP4MPStateChange

func WriteBGP4MPStateChange(buf []byte, off int, h *BGP4MPHeader, as4 bool, oldState, newState uint16) int

func WriteCommonHeader

func WriteCommonHeader(buf []byte, off int, timestamp uint32, typ, subtype uint16, msgLen uint32) int

func WriteExtendedHeader

func WriteExtendedHeader(buf []byte, off int, timestamp, microsecond uint32, typ, subtype uint16, msgLen uint32) int

func WritePeerEntry

func WritePeerEntry(buf []byte, off int, p *PeerEntry) int

func WritePeerIndexTable

func WritePeerIndexTable(buf []byte, off int, collectorBGPID [4]byte, viewName string, peers []PeerEntry) int

func WriteRIBEntries

func WriteRIBEntries(buf []byte, off int, entries []RIBEntry, addPath bool) int

func WriteRIBEntry

func WriteRIBEntry(buf []byte, off int, e *RIBEntry) int

func WriteRIBEntryAddPath

func WriteRIBEntryAddPath(buf []byte, off int, e *RIBEntry) int

func WriteRIBGenericHeader

func WriteRIBGenericHeader(buf []byte, off int, seq uint32, afi uint16, safi uint8, nlri []byte) int

func WriteRIBHeader

func WriteRIBHeader(buf []byte, off int, seq uint32, prefixLen uint8, prefix []byte) int

func WriteTableDump

func WriteTableDump(buf []byte, off int, r *TableDumpRecord) int

Types

type ASPathSegment

type ASPathSegment struct {
	Type uint8
	ASNs []uint32
}

ASPathSegment is a segment in an AS path (for offline parsing).

func ParseASPath

func ParseASPath(data []byte, fourByte bool) ([]ASPathSegment, error)

ParseASPath parses AS_PATH value bytes into a list of (segType, []asn) pairs. fourByte=true for 4-byte ASN encoding (TABLE_DUMP_V2, BGP4MP_MESSAGE_AS4).

type Aggregator

type Aggregator struct {
	ASN     uint32
	Address netip.Addr
}

Aggregator is a decoded AGGREGATOR attribute (RFC 4271 Section 5.1.7).

func ExtractAggregator

func ExtractAggregator(attrs []PathAttribute) (Aggregator, bool)

ExtractAggregator returns the AGGREGATOR attribute (RFC 4271 Section 5.1.7), falling back to AS4_AGGREGATOR (RFC 6793 Section 3) when only that is present.

The AS width is taken from the attribute length, which is unambiguous here: RFC 6793 fixes AGGREGATOR at 6 octets for a 2-byte AS and 8 for a 4-byte AS. (AS_PATH has no such length tell, which is why ParseASPath takes the width as a parameter instead.)

type BGP4MPHeader

type BGP4MPHeader struct {
	PeerAS  uint32
	LocalAS uint32
	IfIndex uint16
	AFI     uint16
	PeerIP  []byte // 4 or 16 bytes
	LocalIP []byte // 4 or 16 bytes
}

BGP4MPHeader represents the common fields of BGP4MP messages.

type Capability

type Capability struct {
	Code  uint8
	Value []byte
}

Capability is a single BGP capability from an OPEN message.

type ExtendedCommunity

type ExtendedCommunity struct {
	Type    uint8
	Subtype uint8
	Value   [6]byte
}

ExtendedCommunity is one 8-octet extended community (RFC 4360 Section 2).

func ExtractExtendedCommunities

func ExtractExtendedCommunities(attrs []PathAttribute) []ExtendedCommunity

ExtractExtendedCommunities returns the extended communities (RFC 4360 Section 2) as 8-octet records. A value whose length is not a multiple of 8 is malformed and yields none.

type GeoPeerEntry

type GeoPeerEntry struct {
	Type      byte
	BGPID     [4]byte
	Latitude  float32
	Longitude float32
}

GeoPeerEntry represents a peer in a GEO_PEER_TABLE (RFC 6397 Section 4.1).

type Handler

type Handler struct {
	OnHeader      func(h Header, microsecond uint32, data []byte) error
	OnPeerIndex   func(h Header, pit *PeerIndexTable) error
	OnRIB         func(h Header, r *RIBRecord) error
	OnRIBGeneric  func(h Header, r *RIBGenericRecord) error
	OnGeoPeer     func(h Header, g *geoPeerTable) error
	OnMessage     func(h Header, microsecond uint32, m *MessageRecord) error
	OnStateChange func(h Header, microsecond uint32, s *StateChangeRecord) error
	OnTableDump   func(h Header, t *TableDumpRecord) error
}

Handler receives decoded MRT records. Set callbacks for the record types you want; nil callbacks are skipped. Return a non-nil error to stop iteration.

type Header struct {
	Timestamp uint32
	Type      uint16
	Subtype   uint16
	Length    uint32
}

Header is the MRT common header (RFC 6396 Section 2).

func DecodeHeader

func DecodeHeader(data []byte) (Header, error)

DecodeHeader parses a 12-byte MRT common header.

type MPReach

type MPReach struct {
	AFI       uint16
	SAFI      uint8
	NextHop   netip.Addr
	LinkLocal netip.Addr // set when the next hop is the 32-byte RFC 2545 form
	Prefixes  []netip.Prefix
}

MPReach is a decoded MP_REACH_NLRI attribute (RFC 4760 Section 3), the full on-the-wire form carried in a BGP UPDATE message.

RIB entries inside a TABLE_DUMP_V2 record carry an abbreviated form instead; decode those with ParseMPReachRIBEntry, never with this function.

func ParseMPReach

func ParseMPReach(value []byte) (*MPReach, error)

ParseMPReach decodes the full MP_REACH_NLRI attribute (RFC 4760 Section 3): AFI(2) + SAFI(1) + Next Hop Length(1) + Next Hop(var) + Reserved(1) + NLRI(var).

Use ParseMPReachRIBEntry for TABLE_DUMP_V2 RIB entries, which omit every field except the next hop.

A damaged NLRI section returns BOTH the attribute decoded so far (AFI, SAFI, next hop, and the prefixes read before the damage) AND an error, mirroring ParsePrefixesAFI. A caller that wants to render what survived checks the value; a caller that wants correctness checks the error. Returning nil with the error would throw away good prefixes and leave a caller that counts them unable to tell "3 prefixes" from "3 prefixes and the rest is unreadable". A failure BEFORE the NLRI section (a truncated fixed header or an unusable next hop) yields nil, because nothing was decoded.

type MPUnreach

type MPUnreach struct {
	AFI      uint16
	SAFI     uint8
	Prefixes []netip.Prefix
}

MPUnreach is a decoded MP_UNREACH_NLRI attribute (RFC 4760 Section 4).

func ParseMPUnreach

func ParseMPUnreach(value []byte) (*MPUnreach, error)

ParseMPUnreach decodes the MP_UNREACH_NLRI attribute (RFC 4760 Section 4): AFI(2) + SAFI(1) + Withdrawn Routes(var).

Like ParseMPReach, a damaged withdrawn-routes section returns both the prefixes decoded so far and an error.

type MessageRecord

type MessageRecord struct {
	BGP4MPHeader
	BGPMessage []byte
}

MessageRecord represents a BGP4MP MESSAGE record (RFC 6396 Section 4.4.2).

func DecodeBGP4MPMessage

func DecodeBGP4MPMessage(subtype uint16, data []byte) (*MessageRecord, error)

DecodeBGP4MPMessage parses a BGP4MP MESSAGE variant. Supports subtypes: 1 (MESSAGE), 4 (MESSAGE_AS4), 6 (MESSAGE_LOCAL), 7 (MESSAGE_AS4_LOCAL), and add-path variants 8-11.

type ParsedMessage

type ParsedMessage struct {
	Type         uint8
	Open         *ParsedOpen
	Update       *ParsedUpdate
	Notification *ParsedNotification
}

ParsedMessage is a parsed BGP message from an MRT record.

func ParseBGPMessage

func ParseBGPMessage(data []byte) (*ParsedMessage, error)

ParseBGPMessage parses a complete BGP message (including 19-byte header).

For an UPDATE it may return a non-nil message TOGETHER with a non-nil error: the message holds everything that decoded and the error names what did not (see parseUpdate). Callers that only want fully-clean records check err first, as before; callers that render records check the value too.

type ParsedNotification

type ParsedNotification struct {
	Code    uint8
	Subcode uint8
	Data    []byte
}

ParsedNotification contains fields from a BGP NOTIFICATION message.

type ParsedOpen

type ParsedOpen struct {
	Version  uint8
	ASN      uint32
	HoldTime uint16
	RouterID [4]byte
	Caps     []Capability
}

ParsedOpen contains fields from a BGP OPEN message.

type ParsedUpdate

type ParsedUpdate struct {
	WithdrawnPrefixes []netip.Prefix
	Attributes        []PathAttribute
	AnnouncedPrefixes []netip.Prefix
}

ParsedUpdate contains fields from a BGP UPDATE message.

type PathAttribute

type PathAttribute struct {
	Flags uint8
	Code  uint8
	Value []byte
}

PathAttribute is a single parsed path attribute.

func FindAttribute

func FindAttribute(attrs []PathAttribute, code uint8) *PathAttribute

FindAttribute returns the first attribute with the given type code, or nil.

func ParseAttributes

func ParseAttributes(data []byte) []PathAttribute

ParseAttributes extracts path attributes from raw attribute bytes.

type PeerEntry

type PeerEntry struct {
	Type  byte
	BGPID [4]byte
	IP    []byte // 4 or 16 bytes
	ASN   uint32
}

PeerEntry represents a peer in a PEER_INDEX_TABLE.

func (*PeerEntry) IsAS4

func (p *PeerEntry) IsAS4() bool

IsAS4 reports whether the peer uses 4-byte AS numbers.

func (*PeerEntry) IsIPv6

func (p *PeerEntry) IsIPv6() bool

IsIPv6 reports whether the peer uses IPv6.

type PeerIndexTable

type PeerIndexTable struct {
	CollectorBGPID [4]byte
	ViewName       string
	Peers          []PeerEntry
}

PeerIndexTable represents a TABLE_DUMP_V2 PEER_INDEX_TABLE (RFC 6396 Section 4.3.1).

func DecodePeerIndexTable

func DecodePeerIndexTable(data []byte) (*PeerIndexTable, error)

DecodePeerIndexTable parses a TABLE_DUMP_V2 PEER_INDEX_TABLE message body.

type RIBEntry

type RIBEntry struct {
	PeerIndex  uint16
	OrigTime   uint32
	PathID     uint32 // only for add-path subtypes (RFC 8050)
	Attributes []byte
}

RIBEntry represents a single RIB entry within a TABLE_DUMP_V2 RIB record.

type RIBGenericRecord

type RIBGenericRecord struct {
	SequenceNumber uint32
	AFI            uint16
	SAFI           uint8
	NLRI           []byte
	Entries        []RIBEntry
}

RIBGenericRecord represents a TABLE_DUMP_V2 RIB_GENERIC record (RFC 6396 Section 4.3.3).

func DecodeRIBGenericRecord

func DecodeRIBGenericRecord(subtype uint16, data []byte) (*RIBGenericRecord, error)

DecodeRIBGenericRecord parses a TABLE_DUMP_V2 RIB_GENERIC record (subtypes 6 and 12).

type RIBRecord

type RIBRecord struct {
	SequenceNumber uint32
	PrefixLength   uint8
	Prefix         []byte
	Entries        []RIBEntry
}

RIBRecord represents a TABLE_DUMP_V2 AFI/SAFI-specific RIB record (RFC 6396 Section 4.3.2).

func DecodeRIBRecord

func DecodeRIBRecord(subtype uint16, data []byte) (*RIBRecord, error)

DecodeRIBRecord parses a TABLE_DUMP_V2 AFI/SAFI-specific RIB record (subtypes 2-5 and add-path 8-11).

type StateChangeRecord

type StateChangeRecord struct {
	BGP4MPHeader
	OldState uint16
	NewState uint16
}

StateChangeRecord represents a BGP4MP STATE_CHANGE record (RFC 6396 Section 4.4.1).

func DecodeBGP4MPStateChange

func DecodeBGP4MPStateChange(subtype uint16, data []byte) (*StateChangeRecord, error)

DecodeBGP4MPStateChange parses a BGP4MP STATE_CHANGE variant (subtypes 0 and 5).

type TableDumpRecord

type TableDumpRecord struct {
	ViewNumber uint16
	SeqNumber  uint16
	Prefix     []byte // 4 or 16 bytes
	PrefixLen  uint8
	Status     uint8
	OrigTime   uint32
	PeerIP     []byte // 4 or 16 bytes
	PeerAS     uint16
	Attributes []byte
}

TableDumpRecord represents a TABLE_DUMP (v1) record.

func DecodeTableDump

func DecodeTableDump(subtype uint16, data []byte) (*TableDumpRecord, error)

DecodeTableDump parses a TABLE_DUMP (type 12) message body. Subtype determines IP size: 1 = IPv4 (4 bytes), 2 = IPv6 (16 bytes).

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer writes MRT records to files with strftime-based filename rotation.

func NewWriter

func NewWriter(pattern string, opts ...WriterOption) *Writer

NewWriter creates a Writer that expands strftime codes in pattern for each file it opens. Records are buffered before flushing to disk.

func (*Writer) Close

func (w *Writer) Close() error

Close flushes and closes the current file.

func (*Writer) Flush

func (w *Writer) Flush() error

Flush flushes buffered data to the underlying file.

func (*Writer) Rotate

func (w *Writer) Rotate() error

Rotate forces a file rotation: flush, close, reopen with a new name.

func (*Writer) Write

func (w *Writer) Write(record []byte) error

Write writes a complete MRT record, rotating the file first if needed.

type WriterOption

type WriterOption func(*Writer)

WriterOption configures a Writer.

func WithInterval

func WithInterval(d time.Duration) WriterOption

WithInterval sets the file rotation interval. Zero disables rotation.

Jump to

Keyboard shortcuts

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