Skip to content

Instantly share code, notes, and snippets.

@whywaita
Last active January 2, 2019 15:40
Show Gist options
  • Save whywaita/9a80e74f9ec9f98fa6462a4258448319 to your computer and use it in GitHub Desktop.
Save whywaita/9a80e74f9ec9f98fa6462a4258448319 to your computer and use it in GitHub Desktop.
GoBGP v1.29 client code
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/osrg/gobgp/client"
"github.com/osrg/gobgp/packet/bgp"
"github.com/osrg/gobgp/table"
)
// func newClient(ctx context.Context) (api.GobgpApiClient, context.CancelFunc, error) {
// grpcOpts := []grpc.DialOption{grpc.WithBlock()}
// grpcOpts = append(grpcOpts, grpc.WithInsecure())
//
// target := ":50051"
// cc, cancel := context.WithTimeout(ctx, time.Second)
// conn, err := grpc.DialContext(cc, target, grpcOpts...)
// if err != nil {
// return nil, cancel, err
// }
// return api.NewGobgpApiClient(conn), cancel, nil
// }
func main() {
ctx := context.Background()
// client, cancel, err := newClient(ctx)
client, err := client.NewWith(ctx, "")
if err != nil {
log.Println("newClient error")
log.Fatal(err)
}
// ipv4UC := &api.Family{
// Afi: api.Family_AFI_IP,
// Safi: api.Family_SAFI_UNICAST,
// }
family := bgp.RF_IPv4_UC
// info, err := client.GetRIBInfo(family)
lp := table.LookupPrefix{
Prefix: "4.3.2.1/32",
}
lps := []*table.LookupPrefix{&lp}
table, err := client.GetRIB(family, lps)
// resp, err := client.GetRib(ctx, &api.GetRibRequest{
// Table: table,
// })
// if err != nil {
// log.Println("GetTable error")
// log.Fatal(err)
// }
j, _ := json.Marshal(table)
fmt.Println(string(j))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment