Skip to content

Commit 8fbb5ab

Browse files
bbassingthwaitefujita
authored andcommitted
Add support for the gRPC client to connect on a unix domain socket
1 parent f99c067 commit 8fbb5ab

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

cmd/gobgp/common.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net"
2424
"os"
2525
"strconv"
26+
"strings"
2627
"time"
2728

2829
"google.golang.org/grpc"
@@ -207,11 +208,18 @@ func newClient(ctx context.Context) (api.GobgpApiClient, context.CancelFunc, err
207208
grpcOpts = append(grpcOpts, grpc.WithInsecure())
208209
}
209210

210-
target := net.JoinHostPort(globalOpts.Host, strconv.Itoa(globalOpts.Port))
211+
target := globalOpts.Target
211212
if target == "" {
212-
target = ":50051"
213+
target = net.JoinHostPort(globalOpts.Host, strconv.Itoa(globalOpts.Port))
214+
} else if strings.HasPrefix(target, "unix://") {
215+
target = target[len("unix://"):]
216+
dialer := func(addr string, t time.Duration) (net.Conn, error) {
217+
return net.Dial("unix", addr)
218+
}
219+
grpcOpts = append(grpcOpts, grpc.WithDialer(dialer))
213220
}
214221
cc, cancel := context.WithTimeout(ctx, time.Second)
222+
215223
conn, err := grpc.DialContext(cc, target, grpcOpts...)
216224
if err != nil {
217225
return nil, cancel, err

cmd/gobgp/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
var globalOpts struct {
2929
Host string
3030
Port int
31+
Target string
3132
Debug bool
3233
Quiet bool
3334
Json bool
@@ -83,6 +84,7 @@ func newRootCmd() *cobra.Command {
8384

8485
rootCmd.PersistentFlags().StringVarP(&globalOpts.Host, "host", "u", "127.0.0.1", "host")
8586
rootCmd.PersistentFlags().IntVarP(&globalOpts.Port, "port", "p", 50051, "port")
87+
rootCmd.PersistentFlags().StringVarP(&globalOpts.Target, "target", "", "", "alternative to host/port when using UDS. Ex: unix:///var/run/go-bgp.sock if running gobgpd with a UDS socket.")
8688
rootCmd.PersistentFlags().BoolVarP(&globalOpts.Json, "json", "j", false, "use json format to output format")
8789
rootCmd.PersistentFlags().BoolVarP(&globalOpts.Debug, "debug", "d", false, "use debug")
8890
rootCmd.PersistentFlags().BoolVarP(&globalOpts.Quiet, "quiet", "q", false, "use quiet")

0 commit comments

Comments
 (0)