Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / lib / iomgr / resolve_address_posix.cc
1 /*
2  *
3  * Copyright 2015 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 "src/core/lib/iomgr/port.h"
22 #ifdef GRPC_POSIX_SOCKET_RESOLVE_ADDRESS
23
24 #include "src/core/lib/iomgr/sockaddr.h"
25
26 #include "src/core/lib/iomgr/resolve_address.h"
27
28 #include <string.h>
29 #include <sys/types.h>
30
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/string_util.h>
34 #include <grpc/support/time.h>
35
36 #include "src/core/lib/gpr/string.h"
37 #include "src/core/lib/gpr/useful.h"
38 #include "src/core/lib/gprpp/host_port.h"
39 #include "src/core/lib/gprpp/thd.h"
40 #include "src/core/lib/iomgr/block_annotate.h"
41 #include "src/core/lib/iomgr/executor.h"
42 #include "src/core/lib/iomgr/iomgr_internal.h"
43 #include "src/core/lib/iomgr/unix_sockets_posix.h"
44
45 static grpc_error* posix_blocking_resolve_address(
46     const char* name, const char* default_port,
47     grpc_resolved_addresses** addresses) {
48   grpc_core::ExecCtx exec_ctx;
49   struct addrinfo hints;
50   struct addrinfo *result = nullptr, *resp;
51   int s;
52   size_t i;
53   grpc_error* err;
54
55   if (name[0] == 'u' && name[1] == 'n' && name[2] == 'i' && name[3] == 'x' &&
56       name[4] == ':' && name[5] != 0) {
57     return grpc_resolve_unix_domain_address(name + 5, addresses);
58   }
59
60   grpc_core::UniquePtr<char> host;
61   grpc_core::UniquePtr<char> port;
62   /* parse name, splitting it into host and port parts */
63   grpc_core::SplitHostPort(name, &host, &port);
64   if (host == nullptr) {
65     err = grpc_error_set_str(
66         GRPC_ERROR_CREATE_FROM_STATIC_STRING("unparseable host:port"),
67         GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name));
68     goto done;
69   }
70   if (port == nullptr) {
71     if (default_port == nullptr) {
72       err = grpc_error_set_str(
73           GRPC_ERROR_CREATE_FROM_STATIC_STRING("no port in name"),
74           GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name));
75       goto done;
76     }
77     port.reset(gpr_strdup(default_port));
78   }
79
80   /* Call getaddrinfo */
81   memset(&hints, 0, sizeof(hints));
82   hints.ai_family = AF_UNSPEC;     /* ipv4 or ipv6 */
83   hints.ai_socktype = SOCK_STREAM; /* stream socket */
84   hints.ai_flags = AI_PASSIVE;     /* for wildcard IP address */
85
86   GRPC_SCHEDULING_START_BLOCKING_REGION;
87   s = getaddrinfo(host.get(), port.get(), &hints, &result);
88   GRPC_SCHEDULING_END_BLOCKING_REGION;
89
90   if (s != 0) {
91     /* Retry if well-known service name is recognized */
92     const char* svc[][2] = {{"http", "80"}, {"https", "443"}};
93     for (i = 0; i < GPR_ARRAY_SIZE(svc); i++) {
94       if (strcmp(port.get(), svc[i][0]) == 0) {
95         GRPC_SCHEDULING_START_BLOCKING_REGION;
96         s = getaddrinfo(host.get(), svc[i][1], &hints, &result);
97         GRPC_SCHEDULING_END_BLOCKING_REGION;
98         break;
99       }
100     }
101   }
102
103   if (s != 0) {
104     err = grpc_error_set_str(
105         grpc_error_set_str(
106             grpc_error_set_str(
107                 grpc_error_set_int(
108                     GRPC_ERROR_CREATE_FROM_STATIC_STRING(gai_strerror(s)),
109                     GRPC_ERROR_INT_ERRNO, s),
110                 GRPC_ERROR_STR_OS_ERROR,
111                 grpc_slice_from_static_string(gai_strerror(s))),
112             GRPC_ERROR_STR_SYSCALL,
113             grpc_slice_from_static_string("getaddrinfo")),
114         GRPC_ERROR_STR_TARGET_ADDRESS, grpc_slice_from_copied_string(name));
115     goto done;
116   }
117
118   /* Success path: set addrs non-NULL, fill it in */
119   *addresses = static_cast<grpc_resolved_addresses*>(
120       gpr_malloc(sizeof(grpc_resolved_addresses)));
121   (*addresses)->naddrs = 0;
122   for (resp = result; resp != nullptr; resp = resp->ai_next) {
123     (*addresses)->naddrs++;
124   }
125   (*addresses)->addrs = static_cast<grpc_resolved_address*>(
126       gpr_malloc(sizeof(grpc_resolved_address) * (*addresses)->naddrs));
127   i = 0;
128   for (resp = result; resp != nullptr; resp = resp->ai_next) {
129     memcpy(&(*addresses)->addrs[i].addr, resp->ai_addr, resp->ai_addrlen);
130     (*addresses)->addrs[i].len = resp->ai_addrlen;
131     i++;
132   }
133   err = GRPC_ERROR_NONE;
134
135 done:
136   if (result) {
137     freeaddrinfo(result);
138   }
139   return err;
140 }
141
142 typedef struct {
143   char* name;
144   char* default_port;
145   grpc_closure* on_done;
146   grpc_resolved_addresses** addrs_out;
147   grpc_closure request_closure;
148   void* arg;
149 } request;
150
151 /* Callback to be passed to grpc Executor to asynch-ify
152  * grpc_blocking_resolve_address */
153 static void do_request_thread(void* rp, grpc_error* error) {
154   request* r = static_cast<request*>(rp);
155   GRPC_CLOSURE_SCHED(r->on_done, grpc_blocking_resolve_address(
156                                      r->name, r->default_port, r->addrs_out));
157   gpr_free(r->name);
158   gpr_free(r->default_port);
159   gpr_free(r);
160 }
161
162 static void posix_resolve_address(const char* name, const char* default_port,
163                                   grpc_pollset_set* interested_parties,
164                                   grpc_closure* on_done,
165                                   grpc_resolved_addresses** addrs) {
166   request* r = static_cast<request*>(gpr_malloc(sizeof(request)));
167   GRPC_CLOSURE_INIT(
168       &r->request_closure, do_request_thread, r,
169       grpc_core::Executor::Scheduler(grpc_core::ExecutorType::RESOLVER,
170                                      grpc_core::ExecutorJobType::SHORT));
171   r->name = gpr_strdup(name);
172   r->default_port = gpr_strdup(default_port);
173   r->on_done = on_done;
174   r->addrs_out = addrs;
175   GRPC_CLOSURE_SCHED(&r->request_closure, GRPC_ERROR_NONE);
176 }
177
178 grpc_address_resolver_vtable grpc_posix_resolver_vtable = {
179     posix_resolve_address, posix_blocking_resolve_address};
180 #endif