blob: 8e56868d4207319c50265eac21a34a836b7253f5 [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"
47#include "src/core/channel/http_filter.h"
ctiller18b49ab2014-12-09 14:39:16 -080048#include "src/core/iomgr/resolve_address.h"
49#include "src/core/iomgr/tcp_client.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050#include "src/core/security/auth.h"
51#include "src/core/security/security_context.h"
52#include "src/core/security/secure_transport_setup.h"
Craig Tiller485d7762015-01-23 12:54:05 -080053#include "src/core/support/string.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054#include "src/core/surface/channel.h"
55#include "src/core/surface/client.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056#include "src/core/transport/chttp2_transport.h"
57#include <grpc/grpc_security.h>
58#include <grpc/support/alloc.h>
59#include <grpc/support/log.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080060#include <grpc/support/sync.h>
61#include <grpc/support/useful.h>
62#include "src/core/tsi/transport_security_interface.h"
63
64typedef struct setup setup;
65
66/* A single setup request (started via initiate) */
67typedef struct {
68 grpc_client_setup_request *cs_request;
69 setup *setup;
70 /* Resolved addresses, or null if resolution not yet completed. */
71 grpc_resolved_addresses *resolved;
72 /* which address in resolved should we pick for the next connection attempt */
73 size_t resolved_index;
74} request;
75
76struct setup {
77 grpc_channel_security_context *security_context;
78 const char *target;
79 grpc_transport_setup_callback setup_callback;
80 void *setup_user_data;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080081};
82
83static int maybe_try_next_resolved(request *r);
84
85static void done(request *r, int was_successful) {
86 grpc_client_setup_request_finish(r->cs_request, was_successful);
87 if (r->resolved) {
88 grpc_resolved_addresses_destroy(r->resolved);
89 }
90 gpr_free(r);
91}
92
93static void on_secure_transport_setup_done(void *rp,
94 grpc_security_status status,
95 grpc_endpoint *secure_endpoint) {
96 request *r = rp;
97 if (status != GRPC_SECURITY_OK) {
98 gpr_log(GPR_ERROR, "Secure transport setup failed with error %d.", status);
99 done(r, 0);
Craig Tiller43a2b172015-02-18 17:17:51 -0800100 } else if (grpc_client_setup_cb_begin(r->cs_request)) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800101 grpc_create_chttp2_transport(
102 r->setup->setup_callback, r->setup->setup_user_data,
103 grpc_client_setup_get_channel_args(r->cs_request), secure_endpoint,
104 NULL, 0, grpc_client_setup_get_mdctx(r->cs_request), 1);
Craig Tiller43a2b172015-02-18 17:17:51 -0800105 grpc_client_setup_cb_end(r->cs_request);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106 done(r, 1);
Craig Tiller43a2b172015-02-18 17:17:51 -0800107 } else {
108 done(r, 0);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109 }
110}
111
112/* connection callback: tcp is either valid, or null on error */
113static void on_connect(void *rp, grpc_endpoint *tcp) {
114 request *r = rp;
115
116 if (!grpc_client_setup_request_should_continue(r->cs_request)) {
117 if (tcp) {
118 grpc_endpoint_shutdown(tcp);
119 grpc_endpoint_destroy(tcp);
120 }
121 done(r, 0);
122 return;
123 }
124
125 if (!tcp) {
126 if (!maybe_try_next_resolved(r)) {
127 done(r, 0);
128 return;
129 } else {
130 return;
131 }
132 } else {
133 grpc_setup_secure_transport(&r->setup->security_context->base, tcp,
134 on_secure_transport_setup_done, r);
135 }
136}
137
138/* attempt to connect to the next available resolved address */
139static int maybe_try_next_resolved(request *r) {
140 grpc_resolved_address *addr;
141 if (!r->resolved) return 0;
142 if (r->resolved_index == r->resolved->naddrs) return 0;
143 addr = &r->resolved->addrs[r->resolved_index++];
ctiller18b49ab2014-12-09 14:39:16 -0800144 grpc_tcp_client_connect(on_connect, r, (struct sockaddr *)&addr->addr,
145 addr->len,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800146 grpc_client_setup_request_deadline(r->cs_request));
147 return 1;
148}
149
150/* callback for when our target address has been resolved */
151static void on_resolved(void *rp, grpc_resolved_addresses *resolved) {
152 request *r = rp;
153
154 /* if we're not still the active request, abort */
155 if (!grpc_client_setup_request_should_continue(r->cs_request)) {
156 if (resolved) {
157 grpc_resolved_addresses_destroy(resolved);
158 }
159 done(r, 0);
160 return;
161 }
162
163 if (!resolved) {
164 done(r, 0);
165 return;
166 } else {
167 r->resolved = resolved;
168 r->resolved_index = 0;
169 if (!maybe_try_next_resolved(r)) {
170 done(r, 0);
171 }
172 }
173}
174
175static void initiate_setup(void *sp, grpc_client_setup_request *cs_request) {
176 request *r = gpr_malloc(sizeof(request));
177 r->setup = sp;
178 r->cs_request = cs_request;
179 r->resolved = NULL;
180 r->resolved_index = 0;
181 /* TODO(klempner): Make grpc_resolve_address respect deadline */
182 grpc_resolve_address(r->setup->target, "https", on_resolved, r);
183}
184
185static void done_setup(void *sp) {
186 setup *s = sp;
187 gpr_free((void *)s->target);
188 grpc_security_context_unref(&s->security_context->base);
189 gpr_free(s);
190}
191
192static grpc_transport_setup_result complete_setup(void *channel_stack,
193 grpc_transport *transport,
194 grpc_mdctx *mdctx) {
Julien Boeuf54b21922015-02-04 16:39:35 -0800195 static grpc_channel_filter const *extra_filters[] = {
196 &grpc_client_auth_filter, &grpc_http_client_filter, &grpc_http_filter};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800197 return grpc_client_channel_transport_setup_complete(
198 channel_stack, transport, extra_filters, GPR_ARRAY_SIZE(extra_filters),
199 mdctx);
200}
201
202/* Create a secure client channel:
203 Asynchronously: - resolve target
204 - connect to it (trying alternatives as presented)
205 - perform handshakes */
206grpc_channel *grpc_secure_channel_create_internal(
207 const char *target, const grpc_channel_args *args,
208 grpc_channel_security_context *context) {
209 setup *s;
210 grpc_channel *channel;
211 grpc_arg context_arg;
212 grpc_channel_args *args_copy;
213 grpc_mdctx *mdctx = grpc_mdctx_create();
Julien Boeuf54b21922015-02-04 16:39:35 -0800214#define MAX_FILTERS 3
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800215 const grpc_channel_filter *filters[MAX_FILTERS];
216 int n = 0;
217 if (grpc_find_security_context_in_args(args) != NULL) {
218 gpr_log(GPR_ERROR, "Cannot set security context in channel args.");
219 }
220
221 s = gpr_malloc(sizeof(setup));
222 context_arg = grpc_security_context_to_arg(&context->base);
223 args_copy = grpc_channel_args_copy_and_add(args, &context_arg);
224 filters[n++] = &grpc_client_surface_filter;
225 if (grpc_channel_args_is_census_enabled(args)) {
226 filters[n++] = &grpc_client_census_filter;
227 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800228 filters[n++] = &grpc_client_channel_filter;
229 GPR_ASSERT(n <= MAX_FILTERS);
230 channel = grpc_channel_create_from_filters(filters, n, args_copy, mdctx, 1);
231 grpc_channel_args_destroy(args_copy);
232
233 s->target = gpr_strdup(target);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800234 s->setup_callback = complete_setup;
235 s->setup_user_data = grpc_channel_get_channel_stack(channel);
236 s->security_context =
237 (grpc_channel_security_context *)grpc_security_context_ref(
238 &context->base);
239 grpc_client_setup_create_and_attach(grpc_channel_get_channel_stack(channel),
240 args, mdctx, initiate_setup, done_setup,
ctiller18b49ab2014-12-09 14:39:16 -0800241 s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800242 return channel;
Craig Tiller190d3602015-02-18 09:23:38 -0800243}