blob: 0d5a811ddfad92896ee172c76fbb15e58cf454ca [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -04001/*
2 *
3 * Copyright 2020 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19// Package unix implements a resolver for unix targets.
20package unix
21
22import (
23 "fmt"
24
25 "google.golang.org/grpc/internal/transport/networktype"
26 "google.golang.org/grpc/resolver"
27)
28
29const unixScheme = "unix"
30const unixAbstractScheme = "unix-abstract"
31
32type builder struct {
33 scheme string
34}
35
36func (b *builder) Build(target resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) (resolver.Resolver, error) {
37 if target.Authority != "" {
38 return nil, fmt.Errorf("invalid (non-empty) authority: %v", target.Authority)
39 }
40 addr := resolver.Address{Addr: target.Endpoint}
41 if b.scheme == unixAbstractScheme {
42 // prepend "\x00" to address for unix-abstract
43 addr.Addr = "\x00" + addr.Addr
44 }
45 cc.UpdateState(resolver.State{Addresses: []resolver.Address{networktype.Set(addr, "unix")}})
46 return &nopResolver{}, nil
47}
48
49func (b *builder) Scheme() string {
50 return b.scheme
51}
52
53type nopResolver struct {
54}
55
56func (*nopResolver) ResolveNow(resolver.ResolveNowOptions) {}
57
58func (*nopResolver) Close() {}
59
60func init() {
61 resolver.Register(&builder{scheme: unixScheme})
62 resolver.Register(&builder{scheme: unixAbstractScheme})
63}