diff --git a/protocols/bgp/types/as_path.go b/protocols/bgp/types/as_path.go index 63c37d084f1aee27827ce0041fffbe4f715ef801..3f7ebe4a537ee6f83d75d4c6c8c214c615a02a3e 100644 --- a/protocols/bgp/types/as_path.go +++ b/protocols/bgp/types/as_path.go @@ -44,12 +44,54 @@ func (a *ASPath) Compare(b *ASPath) bool { return true } +// GetFirstSequenceSegment gets the first sequence of an AS path +func (a *ASPath) GetFirstSequenceSegment() *ASPathSegment { + for _, seg := range *a { + if seg.Type == ASSequence { + return &seg + } + } + + return nil +} + +// GetLastSequenceSegment gets the last sequence of an AS path +func (a *ASPath) GetLastSequenceSegment() *ASPathSegment { + for i := len(*a) - 1; i >= 0; i-- { + if (*a)[i].Type == ASSequence { + return &(*a)[i] + } + } + + return nil +} + // ASPathSegment represents an AS Path Segment (RFC4271) type ASPathSegment struct { Type uint8 ASNs []uint32 } +// GetFirstASN returns the first ASN of an AS path segment +func (s ASPathSegment) GetFirstASN() *uint32 { + if len(s.ASNs) == 0 { + return nil + } + + ret := s.ASNs[0] + return &ret +} + +// GetLastASN returns the last ASN of an AS path segment +func (s ASPathSegment) GetLastASN() *uint32 { + if len(s.ASNs) == 0 { + return nil + } + + ret := s.ASNs[len(s.ASNs)-1] + return &ret +} + // Compare checks if ASPathSegments are the same func (s ASPathSegment) Compare(t ASPathSegment) bool { if s.Type != t.Type {