summaryrefslogtreecommitdiffhomepage
path: root/cmd
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@gmail.com>2019-02-17 21:39:32 +0900
committerFUJITA Tomonori <fujita.tomonori@gmail.com>2019-02-18 10:22:23 +0900
commitbc73604d4c14a84a32ca11536ec07fc0a8645d6c (patch)
treea76f3bbaadb4b38c0969d1717f36f1dcac4e9cf9 /cmd
parentaaa71b1bf45d077569fb8ff3412d9fa43bcf91d0 (diff)
cmd/gobgp: fix non existing neighbor crash
Fixes some crash bugs when non existing neighbor is specified. Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/gobgp/neighbor.go85
1 files changed, 21 insertions, 64 deletions
diff --git a/cmd/gobgp/neighbor.go b/cmd/gobgp/neighbor.go
index 9226e4e2..fbb7b52d 100644
--- a/cmd/gobgp/neighbor.go
+++ b/cmd/gobgp/neighbor.go
@@ -58,20 +58,10 @@ func updateColumnWidth(nlri, nexthop, aspath, label string) {
}
}
-func getNeighbors(vrf string) ([]*api.Peer, error) {
- adv := true
- if vrf != "" {
- adv = false
- } else if t := neighborsOpts.Transport; t != "" {
- switch t {
- case "ipv4", "ipv6":
- adv = false
- default:
- return nil, fmt.Errorf("invalid transport: %s", t)
- }
- }
+func getNeighbors(address string, enableAdv bool) ([]*api.Peer, error) {
stream, err := client.ListPeer(ctx, &api.ListPeerRequest{
- EnableAdvertised: adv,
+ Address: address,
+ EnableAdvertised: enableAdv,
})
l := make([]*api.Peer, 0, 1024)
@@ -84,6 +74,9 @@ func getNeighbors(vrf string) ([]*api.Peer, error) {
}
l = append(l, r.Peer)
}
+ if address != "" && len(l) == 0 {
+ return l, fmt.Errorf("not found neighbor %s", address)
+ }
return l, err
}
@@ -117,7 +110,7 @@ func counter(p *api.Peer) (uint64, uint64, uint64, error) {
}
func showNeighbors(vrf string) error {
- m, err := getNeighbors(vrf)
+ m, err := getNeighbors("", false)
if err != nil {
return err
}
@@ -223,18 +216,11 @@ func showNeighbors(vrf string) error {
}
func showNeighbor(args []string) error {
- stream, err := client.ListPeer(ctx, &api.ListPeerRequest{
- Address: args[0],
- EnableAdvertised: true,
- })
+ l, err := getNeighbors(args[0], true)
if err != nil {
return err
}
- r, err := stream.Recv()
- if err != nil && err != io.EOF {
- return err
- }
- p := r.Peer
+ p := l[0]
if globalOpts.Json {
j, _ := json.Marshal(p)
@@ -883,20 +869,11 @@ func showNeighborRib(r string, name string, args []string) error {
switch r {
case cmdLocal, cmdAdjIn, cmdAccepted, cmdRejected, cmdAdjOut:
if len(rib) == 0 {
- stream, err := client.ListPeer(ctx, &api.ListPeerRequest{
- Address: name,
- })
+ l, err := getNeighbors(name, false)
if err != nil {
return err
}
- r, err := stream.Recv()
- if err != nil && err != io.EOF {
- return err
- }
- if r == nil {
- return fmt.Errorf("neighbor %v is not found", name)
- }
- if r.Peer.State.SessionState != api.PeerState_ESTABLISHED {
+ if l[0].State.SessionState != api.PeerState_ESTABLISHED {
return fmt.Errorf("neighbor %v's BGP session is not established", name)
}
}
@@ -1229,17 +1206,11 @@ func modNeighbor(cmdType string, args []string) error {
}
peer.State.NeighborAddress = addr
case cmdUpdate:
- stream, err := client.ListPeer(ctx, &api.ListPeerRequest{
- Address: addr,
- })
- if err != nil {
- return nil, err
- }
- r, err := stream.Recv()
+ l, err := getNeighbors(addr, false)
if err != nil {
return nil, err
}
- peer = r.Peer
+ peer = l[0]
default:
return nil, fmt.Errorf("invalid command: %s", cmdType)
}
@@ -1354,20 +1325,6 @@ func newNeighborCmd() *cobra.Command {
c = append(c, cmds{[]string{cmdReset, cmdSoftReset, cmdSoftResetIn, cmdSoftResetOut}, resetNeighbor})
c = append(c, cmds{[]string{cmdShutdown, cmdEnable, cmdDisable}, stateChangeNeighbor})
- getPeer := func(addr string) (*api.Peer, error) {
- var r *api.ListPeerResponse
- stream, err := client.ListPeer(ctx, &api.ListPeerRequest{
- Address: addr,
- })
- if err == nil {
- r, err = stream.Recv()
- }
- if err != nil && err != io.EOF {
- return nil, err
- }
- return r.Peer, nil
- }
-
for _, v := range c {
f := v.f
for _, name := range v.names {
@@ -1382,11 +1339,11 @@ func newNeighborCmd() *cobra.Command {
}
}
if addr == "" {
- p, err := getPeer(args[len(args)-1])
+ l, err := getNeighbors(args[len(args)-1], false)
if err != nil {
exitWithError(err)
}
- addr = p.State.NeighborAddress
+ addr = l[0].State.NeighborAddress
}
err := f(cmd.Use, addr, args[:len(args)-1])
if err != nil {
@@ -1413,11 +1370,11 @@ func newNeighborCmd() *cobra.Command {
policyCmd := &cobra.Command{
Use: cmdPolicy,
Run: func(cmd *cobra.Command, args []string) {
- peer, err := getPeer(args[0])
+ l, err := getNeighbors(args[0], false)
if err != nil {
exitWithError(err)
}
- remoteIP := peer.State.NeighborAddress
+ remoteIP := l[0].State.NeighborAddress
for _, v := range []string{cmdImport, cmdExport} {
if err := showNeighborPolicy(remoteIP, v, 4); err != nil {
exitWithError(err)
@@ -1430,11 +1387,11 @@ func newNeighborCmd() *cobra.Command {
cmd := &cobra.Command{
Use: v,
Run: func(cmd *cobra.Command, args []string) {
- peer, err := getPeer(args[0])
+ l, err := getNeighbors(args[0], false)
if err != nil {
exitWithError(err)
}
- remoteIP := peer.State.NeighborAddress
+ remoteIP := l[0].State.NeighborAddress
err = showNeighborPolicy(remoteIP, cmd.Use, 0)
if err != nil {
exitWithError(err)
@@ -1446,11 +1403,11 @@ func newNeighborCmd() *cobra.Command {
subcmd := &cobra.Command{
Use: w,
Run: func(subcmd *cobra.Command, args []string) {
- peer, err := getPeer(args[len(args)-1])
+ l, err := getNeighbors(args[len(args)-1], false)
if err != nil {
exitWithError(err)
}
- remoteIP := peer.State.NeighborAddress
+ remoteIP := l[0].State.NeighborAddress
args = args[:len(args)-1]
if err = modNeighborPolicy(remoteIP, cmd.Use, subcmd.Use, args); err != nil {
exitWithError(err)