blob: 3e331293b5ddcdbc3ae2bce0744bbbd3e2d63509 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Nicolas "Pixel" Nobled5a99852015-01-24 01:27:48 -080034#include "src/core/iomgr/sockaddr.h"
35
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036#include <grpc/grpc.h>
37
38#include <stdlib.h>
39#include <string.h>
40
41#include "src/core/channel/census_filter.h"
42#include "src/core/channel/channel_args.h"
43#include "src/core/channel/client_channel.h"
44#include "src/core/channel/client_setup.h"
45#include "src/core/channel/connected_channel.h"
46#include "src/core/channel/http_client_filter.h"
ctiller18b49ab2014-12-09 14:39:16 -080047#include "src/core/iomgr/resolve_address.h"
48#include "src/core/iomgr/tcp_client.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049#include "src/core/security/auth.h"
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070050#include "src/core/security/credentials.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051#include "src/core/security/secure_transport_setup.h"
Craig Tiller485d7762015-01-23 12:54:05 -080052#include "src/core/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053#include "src/core/surface/channel.h"
54#include "src/core/surface/client.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055#include "src/core/transport/chttp2_transport.h"
56#include <grpc/grpc_security.h>
57#include <grpc/support/alloc.h>
58#include <grpc/support/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059#include <grpc/support/sync.h>
60#include <grpc/support/useful.h>
61#include "src/core/tsi/transport_security_interface.h"
62
63typedef struct setup setup;
64
65/* A single setup request (started via initiate) */
66typedef struct {
67 grpc_client_setup_request *cs_request;
68 setup *setup;
69 /* Resolved addresses, or null if resolution not yet completed. */
70 grpc_resolved_addresses *resolved;
71 /* which address in resolved should we pick for the next connection attempt */
72 size_t resolved_index;
73} request;
74
75struct setup {
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070076 grpc_channel_security_connector *security_connector;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080077 const char *target;
78 grpc_transport_setup_callback setup_callback;
79 void *setup_user_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080};
81
82static int maybe_try_next_resolved(request *r);
83
84static void done(request *r, int was_successful) {
85 grpc_client_setup_request_finish(r->cs_request, was_successful);
86 if (r->resolved) {
87 grpc_resolved_addresses_destroy(r->resolved);
88 }
89 gpr_free(r);
90}
91
92static void on_secure_transport_setup_done(void *rp,
93 grpc_security_status status,
94 grpc_endpoint *secure_endpoint) {
95 request *r = rp;
96 if (status != GRPC_SECURITY_OK) {
97 gpr_log(GPR_ERROR, "Secure transport setup failed with error %d.", status);
98 done(r, 0);
Craig Tiller43a2b172015-02-18 17:17:51 -080099 } else if (grpc_client_setup_cb_begin(r->cs_request)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800100 grpc_create_chttp2_transport(
101 r->setup->setup_callback, r->setup->setup_user_data,
102 grpc_client_setup_get_channel_args(r->cs_request), secure_endpoint,
103 NULL, 0, grpc_client_setup_get_mdctx(r->cs_request), 1);
Craig Tiller43a2b172015-02-18 17:17:51 -0800104 grpc_client_setup_cb_end(r->cs_request);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800105 done(r, 1);
Craig Tiller43a2b172015-02-18 17:17:51 -0800106 } else {
107 done(r, 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800108 }
109}
110
111/* connection callback: tcp is either valid, or null on error */
112static void on_connect(void *rp, grpc_endpoint *tcp) {
113 request *r = rp;
114
115 if (!grpc_client_setup_request_should_continue(r->cs_request)) {
116 if (tcp) {
117 grpc_endpoint_shutdown(tcp);
118 grpc_endpoint_destroy(tcp);
119 }
120 done(r, 0);
121 return;
122 }
123
124 if (!tcp) {
125 if (!maybe_try_next_resolved(r)) {
126 done(r, 0);
127 return;
128 } else {
129 return;
130 }
131 } else {
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700132 grpc_setup_secure_transport(&r->setup->security_connector->base, tcp,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800133 on_secure_transport_setup_done, r);
134 }
135}
136
137/* attempt to connect to the next available resolved address */
138static int maybe_try_next_resolved(request *r) {
139 grpc_resolved_address *addr;
140 if (!r->resolved) return 0;
141 if (r->resolved_index == r->resolved->naddrs) return 0;
142 addr = &r->resolved->addrs[r->resolved_index++];
ctiller18b49ab2014-12-09 14:39:16 -0800143 grpc_tcp_client_connect(on_connect, r, (struct sockaddr *)&addr->addr,
144 addr->len,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800145 grpc_client_setup_request_deadline(r->cs_request));
146 return 1;
147}
148
149/* callback for when our target address has been resolved */
150static void on_resolved(void *rp, grpc_resolved_addresses *resolved) {
151 request *r = rp;
152
153 /* if we're not still the active request, abort */
154 if (!grpc_client_setup_request_should_continue(r->cs_request)) {
155 if (resolved) {
156 grpc_resolved_addresses_destroy(resolved);
157 }
158 done(r, 0);
159 return;
160 }
161
162 if (!resolved) {
163 done(r, 0);
164 return;
165 } else {
166 r->resolved = resolved;
167 r->resolved_index = 0;
168 if (!maybe_try_next_resolved(r)) {
169 done(r, 0);
170 }
171 }
172}
173
174static void initiate_setup(void *sp, grpc_client_setup_request *cs_request) {
175 request *r = gpr_malloc(sizeof(request));
176 r->setup = sp;
177 r->cs_request = cs_request;
178 r->resolved = NULL;
179 r->resolved_index = 0;
180 /* TODO(klempner): Make grpc_resolve_address respect deadline */
181 grpc_resolve_address(r->setup->target, "https", on_resolved, r);
182}
183
184static void done_setup(void *sp) {
185 setup *s = sp;
186 gpr_free((void *)s->target);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700187 grpc_security_connector_unref(&s->security_connector->base);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800188 gpr_free(s);
189}
190
191static grpc_transport_setup_result complete_setup(void *channel_stack,
192 grpc_transport *transport,
193 grpc_mdctx *mdctx) {
Julien Boeuf54b21922015-02-04 16:39:35 -0800194 static grpc_channel_filter const *extra_filters[] = {
Craig Tiller6e84aba2015-04-23 15:08:17 -0700195 &grpc_client_auth_filter, &grpc_http_client_filter};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800196 return grpc_client_channel_transport_setup_complete(
197 channel_stack, transport, extra_filters, GPR_ARRAY_SIZE(extra_filters),
198 mdctx);
199}
200
201/* Create a secure client channel:
202 Asynchronously: - resolve target
203 - connect to it (trying alternatives as presented)
204 - perform handshakes */
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700205grpc_channel *grpc_secure_channel_create(grpc_credentials *creds,
206 const char *target,
207 const grpc_channel_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800208 setup *s;
209 grpc_channel *channel;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700210 grpc_arg connector_arg;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800211 grpc_channel_args *args_copy;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700212 grpc_channel_args *new_args_from_connector;
Craig Tiller1a727fd2015-04-24 13:21:22 -0700213 grpc_channel_security_connector *connector;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700214 grpc_mdctx *mdctx;
Julien Boeuf54b21922015-02-04 16:39:35 -0800215#define MAX_FILTERS 3
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800216 const grpc_channel_filter *filters[MAX_FILTERS];
217 int n = 0;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700218
219 if (grpc_find_security_connector_in_args(args) != NULL) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800220 gpr_log(GPR_ERROR, "Cannot set security context in channel args.");
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700221 return grpc_lame_client_channel_create();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800222 }
223
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700224 if (grpc_credentials_create_security_connector(
225 creds, target, args, NULL, &connector, &new_args_from_connector) !=
226 GRPC_SECURITY_OK) {
227 return grpc_lame_client_channel_create();
228 }
229 mdctx = grpc_credentials_get_or_create_metadata_context(creds);
230
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800231 s = gpr_malloc(sizeof(setup));
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700232 connector_arg = grpc_security_connector_to_arg(&connector->base);
233 args_copy = grpc_channel_args_copy_and_add(
234 new_args_from_connector != NULL ? new_args_from_connector : args,
235 &connector_arg);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800236 filters[n++] = &grpc_client_surface_filter;
237 if (grpc_channel_args_is_census_enabled(args)) {
238 filters[n++] = &grpc_client_census_filter;
239 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800240 filters[n++] = &grpc_client_channel_filter;
241 GPR_ASSERT(n <= MAX_FILTERS);
242 channel = grpc_channel_create_from_filters(filters, n, args_copy, mdctx, 1);
243 grpc_channel_args_destroy(args_copy);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700244 if (new_args_from_connector != NULL) {
245 grpc_channel_args_destroy(new_args_from_connector);
246 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800247
248 s->target = gpr_strdup(target);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800249 s->setup_callback = complete_setup;
250 s->setup_user_data = grpc_channel_get_channel_stack(channel);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700251 s->security_connector = connector;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800252 grpc_client_setup_create_and_attach(grpc_channel_get_channel_stack(channel),
253 args, mdctx, initiate_setup, done_setup,
ctiller18b49ab2014-12-09 14:39:16 -0800254 s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800255 return channel;
Craig Tiller190d3602015-02-18 09:23:38 -0800256}