Skip to content

Instantly share code, notes, and snippets.

@vista-
Last active June 12, 2024 14:27
Show Gist options
  • Save vista-/0a6907bc44aaaebc141b7649e62641b1 to your computer and use it in GitHub Desktop.
Save vista-/0a6907bc44aaaebc141b7649e62641b1 to your computer and use it in GitHub Desktop.
gobgp-test
// Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"compress/gzip"
"encoding/hex"
"fmt"
"io"
"os"
"strings"
"github.com/osrg/gobgp/v3/pkg/packet/mrt"
)
var (
Debug = false
)
func readMrt(fileName string) error {
var file io.ReadCloser
fileReader, err := os.Open(fileName)
if err != nil {
return fmt.Errorf("failed to open file: %s", err)
}
defer fileReader.Close()
if strings.HasSuffix(fileName, ".gz") {
gzReader, err := gzip.NewReader(fileReader)
fmt.Printf("Reading gzip!\n")
if err != nil {
return fmt.Errorf("failed to open gzipped file: %s", err)
}
defer gzReader.Close()
file = gzReader
} else {
file = fileReader
}
var position = 0
var peers []*mrt.Peer
for _ = range 20 {
buf := make([]byte, mrt.MRT_COMMON_HEADER_LEN)
_, err := file.Read(buf)
headerHex := hex.EncodeToString(buf)
fmt.Printf("Header: %s\n", headerHex)
position += mrt.MRT_COMMON_HEADER_LEN
fmt.Printf("Read so far %d bytes\n", position)
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("failed to read: %s", err)
}
h := &mrt.MRTHeader{}
err = h.DecodeFromBytes(buf)
if err != nil {
return fmt.Errorf("failed to parse")
}
buf = make([]byte, h.Len)
_, err = file.Read(buf)
position += int(h.Len)
fmt.Printf("%+v\n", h)
fmt.Printf("Length of buffer: %d\n", len(buf))
fmt.Printf("Read so far %d bytes\n", position)
bodyHex := hex.EncodeToString(buf)
fmt.Printf("Body: %s\n", bodyHex)
if err != nil {
return fmt.Errorf("failed to read")
}
msg, err := mrt.ParseMRTBody(h, buf)
if err != nil {
return fmt.Errorf("failed to parse: %s", err)
}
if Debug {
fmt.Println(msg)
}
if msg.Header.Type == mrt.TABLE_DUMPv2 {
subType := mrt.MRTSubTypeTableDumpv2(msg.Header.SubType)
switch subType {
case mrt.PEER_INDEX_TABLE:
peers = msg.Body.(*mrt.PeerIndexTable).Peers
continue
case mrt.RIB_IPV4_UNICAST, mrt.RIB_IPV4_UNICAST_ADDPATH:
case mrt.RIB_IPV6_UNICAST, mrt.RIB_IPV6_UNICAST_ADDPATH:
case mrt.GEO_PEER_TABLE:
fmt.Printf("WARNING: Skipping GEO_PEER_TABLE: %s", msg.Body.(*mrt.GeoPeerTable))
default:
return fmt.Errorf("unsupported subType: %v", subType)
}
if peers == nil {
return fmt.Errorf("not found PEER_INDEX_TABLE")
}
rib := msg.Body.(*mrt.Rib)
nlri := rib.Prefix
fmt.Printf("%v\n", nlri)
}
}
return nil
}
func main() {
if len(os.Args) < 2 {
fmt.Printf("Missing filename! ./gobgp-test [filename]\n")
return
}
fileName := os.Args[1]
err := readMrt(fileName)
if err != nil {
fmt.Printf("%v\n", err)
}
}
@vista-
Copy link
Author

vista- commented Jun 12, 2024

Get test files by running:

wget -q https://data.ris.ripe.net/rrc03/latest-bview.gz
gunzip -c latest-bview.gz > latest-bview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment