1465b0c644e0dcb25c32ab675471f63e0431a48f
[motion2.git] /
1 /*
2  *
3  * Copyright 2015-2016 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 #include <grpc/support/port_platform.h>
20
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/string_util.h>
28
29 #include "src/core/ext/filters/client_channel/parse_address.h"
30 #include "src/core/ext/filters/client_channel/resolver_registry.h"
31 #include "src/core/ext/filters/client_channel/server_address.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/gpr/host_port.h"
34 #include "src/core/lib/gpr/string.h"
35 #include "src/core/lib/iomgr/combiner.h"
36 #include "src/core/lib/iomgr/resolve_address.h"
37 #include "src/core/lib/iomgr/unix_sockets_posix.h"
38 #include "src/core/lib/slice/slice_internal.h"
39 #include "src/core/lib/slice/slice_string_helpers.h"
40
41 namespace grpc_core {
42
43 namespace {
44
45 class SockaddrResolver : public Resolver {
46  public:
47   SockaddrResolver(ServerAddressList addresses, ResolverArgs args);
48   ~SockaddrResolver() override;
49
50   void StartLocked() override;
51
52   void ShutdownLocked() override {}
53
54  private:
55   ServerAddressList addresses_;
56   const grpc_channel_args* channel_args_ = nullptr;
57 };
58
59 SockaddrResolver::SockaddrResolver(ServerAddressList addresses,
60                                    ResolverArgs args)
61     : Resolver(args.combiner, std::move(args.result_handler)),
62       addresses_(std::move(addresses)),
63       channel_args_(grpc_channel_args_copy(args.args)) {}
64
65 SockaddrResolver::~SockaddrResolver() {
66   grpc_channel_args_destroy(channel_args_);
67 }
68
69 void SockaddrResolver::StartLocked() {
70   Result result;
71   result.addresses = std::move(addresses_);
72   // TODO(roth): Use std::move() once channel args is converted to C++.
73   result.args = channel_args_;
74   channel_args_ = nullptr;
75   result_handler()->ReturnResult(std::move(result));
76 }
77
78 //
79 // Factory
80 //
81
82 void DoNothing(void* ignored) {}
83
84 OrphanablePtr<Resolver> CreateSockaddrResolver(
85     ResolverArgs args,
86     bool parse(const grpc_uri* uri, grpc_resolved_address* dst)) {
87   if (0 != strcmp(args.uri->authority, "")) {
88     gpr_log(GPR_ERROR, "authority-based URIs not supported by the %s scheme",
89             args.uri->scheme);
90     return nullptr;
91   }
92   // Construct addresses.
93   grpc_slice path_slice =
94       grpc_slice_new(args.uri->path, strlen(args.uri->path), DoNothing);
95   grpc_slice_buffer path_parts;
96   grpc_slice_buffer_init(&path_parts);
97   grpc_slice_split(path_slice, ",", &path_parts);
98   ServerAddressList addresses;
99   bool errors_found = false;
100   for (size_t i = 0; i < path_parts.count; i++) {
101     grpc_uri ith_uri = *args.uri;
102     UniquePtr<char> part_str(grpc_slice_to_c_string(path_parts.slices[i]));
103     ith_uri.path = part_str.get();
104     grpc_resolved_address addr;
105     if (!parse(&ith_uri, &addr)) {
106       errors_found = true;
107       break;
108     }
109     addresses.emplace_back(addr, nullptr /* args */);
110   }
111   grpc_slice_buffer_destroy_internal(&path_parts);
112   grpc_slice_unref_internal(path_slice);
113   if (errors_found) {
114     return OrphanablePtr<Resolver>(nullptr);
115   }
116   // Instantiate resolver.
117   return OrphanablePtr<Resolver>(
118       New<SockaddrResolver>(std::move(addresses), std::move(args)));
119 }
120
121 class IPv4ResolverFactory : public ResolverFactory {
122  public:
123   OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
124     return CreateSockaddrResolver(std::move(args), grpc_parse_ipv4);
125   }
126
127   const char* scheme() const override { return "ipv4"; }
128 };
129
130 class IPv6ResolverFactory : public ResolverFactory {
131  public:
132   OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
133     return CreateSockaddrResolver(std::move(args), grpc_parse_ipv6);
134   }
135
136   const char* scheme() const override { return "ipv6"; }
137 };
138
139 #ifdef GRPC_HAVE_UNIX_SOCKET
140 class UnixResolverFactory : public ResolverFactory {
141  public:
142   OrphanablePtr<Resolver> CreateResolver(ResolverArgs args) const override {
143     return CreateSockaddrResolver(std::move(args), grpc_parse_unix);
144   }
145
146   UniquePtr<char> GetDefaultAuthority(grpc_uri* uri) const override {
147     return UniquePtr<char>(gpr_strdup("localhost"));
148   }
149
150   const char* scheme() const override { return "unix"; }
151 };
152 #endif  // GRPC_HAVE_UNIX_SOCKET
153
154 }  // namespace
155
156 }  // namespace grpc_core
157
158 void grpc_resolver_sockaddr_init() {
159   grpc_core::ResolverRegistry::Builder::RegisterResolverFactory(
160       grpc_core::UniquePtr<grpc_core::ResolverFactory>(
161           grpc_core::New<grpc_core::IPv4ResolverFactory>()));
162   grpc_core::ResolverRegistry::Builder::RegisterResolverFactory(
163       grpc_core::UniquePtr<grpc_core::ResolverFactory>(
164           grpc_core::New<grpc_core::IPv6ResolverFactory>()));
165 #ifdef GRPC_HAVE_UNIX_SOCKET
166   grpc_core::ResolverRegistry::Builder::RegisterResolverFactory(
167       grpc_core::UniquePtr<grpc_core::ResolverFactory>(
168           grpc_core::New<grpc_core::UnixResolverFactory>()));
169 #endif
170 }
171
172 void grpc_resolver_sockaddr_shutdown() {}