Skip to content

Commit

Permalink
Add ability to inject gzipped MRT files
Browse files Browse the repository at this point in the history
  • Loading branch information
vista- authored and fujita committed Jun 13, 2024
1 parent d2ac253 commit 21c157a
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions cmd/gobgp/mrt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package main

import (
"compress/gzip"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"

"github.com/spf13/cobra"
Expand All @@ -31,11 +33,23 @@ import (
)

func injectMrt() error {

file, err := os.Open(mrtOpts.Filename)
var reader io.ReadCloser
fileReader, err := os.Open(mrtOpts.Filename)
if err != nil {
return fmt.Errorf("failed to open file: %s", err)
}
defer fileReader.Close()

if strings.HasSuffix(mrtOpts.Filename, ".gz") {
gzReader, err := gzip.NewReader(fileReader)
if err != nil {
return fmt.Errorf("failed to open gzip file: %s", err)
}
defer gzReader.Close()
reader = gzReader
} else {
reader = fileReader
}

if mrtOpts.NextHop != nil && !mrtOpts.SkipV4 && !mrtOpts.SkipV6 {
fmt.Println("You should probably specify either --no-ipv4 or --no-ipv6 when overwriting nexthop, unless your dump contains only one type of routes")
Expand All @@ -52,7 +66,7 @@ func injectMrt() error {
var peers []*mrt.Peer
for {
buf := make([]byte, mrt.MRT_COMMON_HEADER_LEN)
_, err := file.Read(buf)
_, err := io.ReadFull(reader, buf)
if err == io.EOF {
break
} else if err != nil {
Expand All @@ -66,7 +80,7 @@ func injectMrt() error {
}

buf = make([]byte, h.Len)
_, err = file.Read(buf)
_, err = io.ReadFull(reader, buf)
if err != nil {
exitWithError(fmt.Errorf("failed to read"))
}
Expand Down

0 comments on commit 21c157a

Please sign in to comment.