From 82ecdf8b407f72400930d67a53e70bf90b43963e Mon Sep 17 00:00:00 2001 From: Jan Lamecki Date: Thu, 29 Sep 2022 18:35:23 +0200 Subject: [PATCH] Removes trailing \0 from unix abstract address. Trailing nul byte is added by golang Dial wrapper. To avoid that one should use '@socket' as address instead of '\0socket' (see [1]). This change makes go-grpc behave like grpc/grpc (see [2]), making connecting to golang server with grpc_cli possible. [1]: https://go.googlesource.com/sys/+/master/unix/syscall_linux.go#420 [2]: https://github.com/grpc/grpc/blob/d43511f4af992862ae17c6fce5caa3ab3ce60995/src/core/lib/address_utils/parse_address.cc#L109 --- examples/features/unix_abstract/server/main.go | 2 +- internal/resolver/unix/unix.go | 5 +++-- test/authority_test.go | 8 +++----- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/examples/features/unix_abstract/server/main.go b/examples/features/unix_abstract/server/main.go index 4ef4ff5b7637..7013466b4917 100644 --- a/examples/features/unix_abstract/server/main.go +++ b/examples/features/unix_abstract/server/main.go @@ -51,7 +51,7 @@ func (s *ecServer) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.Echo func main() { flag.Parse() netw := "unix" - socketAddr := fmt.Sprintf("\x00%v", *addr) + socketAddr := fmt.Sprintf("@%v", *addr) lis, err := net.Listen(netw, socketAddr) if err != nil { log.Fatalf("net.Listen(%q, %q) failed: %v", netw, socketAddr, err) diff --git a/internal/resolver/unix/unix.go b/internal/resolver/unix/unix.go index 20852e59df29..7f1a702cacbe 100644 --- a/internal/resolver/unix/unix.go +++ b/internal/resolver/unix/unix.go @@ -49,8 +49,9 @@ func (b *builder) Build(target resolver.Target, cc resolver.ClientConn, _ resolv } addr := resolver.Address{Addr: endpoint} if b.scheme == unixAbstractScheme { - // prepend "\x00" to address for unix-abstract - addr.Addr = "\x00" + addr.Addr + // We can not prepend \0 as c++ gRPC does, as in Golang '@' is used to signify we do + // not want trailing \0 in address. + addr.Addr = "@" + addr.Addr } cc.UpdateState(resolver.State{Addresses: []resolver.Address{networktype.Set(addr, "unix")}}) return &nopResolver{}, nil diff --git a/test/authority_test.go b/test/authority_test.go index 452b896eebf9..8148cd2347ea 100644 --- a/test/authority_test.go +++ b/test/authority_test.go @@ -125,10 +125,10 @@ var authorityTests = []authorityTest{ }, { name: "UnixAbstract", - address: "\x00abc efg", + address: "@abc efg", target: "unix-abstract:abc efg", authority: "localhost", - dialTargetWant: "\x00abc efg", + dialTargetWant: "unix:@abc efg", }, } @@ -155,9 +155,7 @@ func (s) TestUnixCustomDialer(t *testing.T) { if address != test.dialTargetWant { return nil, fmt.Errorf("expected target %v in custom dialer, instead got %v", test.dialTargetWant, address) } - if !strings.HasPrefix(test.target, "unix-abstract:") { - address = address[len("unix:"):] - } + address = address[len("unix:"):] return (&net.Dialer{}).DialContext(ctx, "unix", address) } runUnixTest(t, test.address, test.target, test.authority, dialer)