Skip to content

Commit

Permalink
Removes trailing \0 from unix abstract address.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jachor committed Sep 29, 2022
1 parent 36e4810 commit 82ecdf8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/features/unix_abstract/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions internal/resolver/unix/unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions test/authority_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

Expand All @@ -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)
Expand Down

0 comments on commit 82ecdf8

Please sign in to comment.