blob: 562e27ff6d43e76dc3ad81b5624c343a493a4505 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * 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);
100 } else {
101 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);
105 done(r, 1);
106 }
107}
108
109/* connection callback: tcp is either valid, or null on error */
110static void on_connect(void *rp, grpc_endpoint *tcp) {
111 request *r = rp;
112
113 if (!grpc_client_setup_request_should_continue(r->cs_request)) {
114 if (tcp) {
115 grpc_endpoint_shutdown(tcp);
116 grpc_endpoint_destroy(tcp);
117 }
118 done(r, 0);
119 return;
120 }
121
122 if (!tcp) {
123 if (!maybe_try_next_resolved(r)) {
124 done(r, 0);
125 return;
126 } else {
127 return;
128 }
129 } else {
130 grpc_setup_secure_transport(&r->setup->security_context->base, tcp,
131 on_secure_transport_setup_done, r);
132 }
133}
134
135/* attempt to connect to the next available resolved address */
136static int maybe_try_next_resolved(request *r) {
137 grpc_resolved_address *addr;
138 if (!r->resolved) return 0;
139 if (r->resolved_index == r->resolved->naddrs) return 0;
140 addr = &r->resolved->addrs[r->resolved_index++];
ctiller18b49ab2014-12-09 14:39:16 -0800141 grpc_tcp_client_connect(on_connect, r, (struct sockaddr *)&addr->addr,
142 addr->len,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800143 grpc_client_setup_request_deadline(r->cs_request));
144 return 1;
145}
146
147/* callback for when our target address has been resolved */
148static void on_resolved(void *rp, grpc_resolved_addresses *resolved) {
149 request *r = rp;
150
151 /* if we're not still the active request, abort */
152 if (!grpc_client_setup_request_should_continue(r->cs_request)) {
153 if (resolved) {
154 grpc_resolved_addresses_destroy(resolved);
155 }
156 done(r, 0);
157 return;
158 }
159
160 if (!resolved) {
161 done(r, 0);
162 return;
163 } else {
164 r->resolved = resolved;
165 r->resolved_index = 0;
166 if (!maybe_try_next_resolved(r)) {
167 done(r, 0);
168 }
169 }
170}
171
172static void initiate_setup(void *sp, grpc_client_setup_request *cs_request) {
173 request *r = gpr_malloc(sizeof(request));
174 r->setup = sp;
175 r->cs_request = cs_request;
176 r->resolved = NULL;
177 r->resolved_index = 0;
178 /* TODO(klempner): Make grpc_resolve_address respect deadline */
179 grpc_resolve_address(r->setup->target, "https", on_resolved, r);
180}
181
182static void done_setup(void *sp) {
183 setup *s = sp;
184 gpr_free((void *)s->target);
185 grpc_security_context_unref(&s->security_context->base);
186 gpr_free(s);
187}
188
189static grpc_transport_setup_result complete_setup(void *channel_stack,
190 grpc_transport *transport,
191 grpc_mdctx *mdctx) {
Julien Boeuf54b21922015-02-04 16:39:35 -0800192 static grpc_channel_filter const *extra_filters[] = {
193 &grpc_client_auth_filter, &grpc_http_client_filter, &grpc_http_filter};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800194 return grpc_client_channel_transport_setup_complete(
195 channel_stack, transport, extra_filters, GPR_ARRAY_SIZE(extra_filters),
196 mdctx);
197}
198
199/* Create a secure client channel:
200 Asynchronously: - resolve target
201 - connect to it (trying alternatives as presented)
202 - perform handshakes */
203grpc_channel *grpc_secure_channel_create_internal(
204 const char *target, const grpc_channel_args *args,
205 grpc_channel_security_context *context) {
206 setup *s;
207 grpc_channel *channel;
208 grpc_arg context_arg;
209 grpc_channel_args *args_copy;
210 grpc_mdctx *mdctx = grpc_mdctx_create();
Julien Boeuf54b21922015-02-04 16:39:35 -0800211#define MAX_FILTERS 3
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800212 const grpc_channel_filter *filters[MAX_FILTERS];
213 int n = 0;
214 if (grpc_find_security_context_in_args(args) != NULL) {
215 gpr_log(GPR_ERROR, "Cannot set security context in channel args.");
216 }
217
218 s = gpr_malloc(sizeof(setup));
219 context_arg = grpc_security_context_to_arg(&context->base);
220 args_copy = grpc_channel_args_copy_and_add(args, &context_arg);
221 filters[n++] = &grpc_client_surface_filter;
222 if (grpc_channel_args_is_census_enabled(args)) {
223 filters[n++] = &grpc_client_census_filter;
224 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800225 filters[n++] = &grpc_client_channel_filter;
226 GPR_ASSERT(n <= MAX_FILTERS);
227 channel = grpc_channel_create_from_filters(filters, n, args_copy, mdctx, 1);
228 grpc_channel_args_destroy(args_copy);
229
230 s->target = gpr_strdup(target);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800231 s->setup_callback = complete_setup;
232 s->setup_user_data = grpc_channel_get_channel_stack(channel);
233 s->security_context =
234 (grpc_channel_security_context *)grpc_security_context_ref(
235 &context->base);
236 grpc_client_setup_create_and_attach(grpc_channel_get_channel_stack(channel),
237 args, mdctx, initiate_setup, done_setup,
ctiller18b49ab2014-12-09 14:39:16 -0800238 s);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800239 return channel;
240}