blob: a280311ba0cd94a4abecc45b40ab81f8632262f7 [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
34#include <grpc/grpc.h>
35
36#include <stdlib.h>
37#include <string.h>
38
Craig Tilleracf0f072015-06-29 08:24:16 -070039#include <grpc/support/alloc.h>
40
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include "src/core/channel/channel_args.h"
42#include "src/core/channel/client_channel.h"
David Garcia Quintas5927aec2015-06-18 17:24:44 -070043#include "src/core/channel/compress_filter.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044#include "src/core/channel/http_client_filter.h"
Craig Tilleracf0f072015-06-29 08:24:16 -070045#include "src/core/client_config/resolver_registry.h"
ctiller18b49ab2014-12-09 14:39:16 -080046#include "src/core/iomgr/tcp_client.h"
Julien Boeufc6f8d0a2015-05-11 22:40:02 -070047#include "src/core/security/auth_filters.h"
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070048#include "src/core/security/credentials.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049#include "src/core/security/secure_transport_setup.h"
50#include "src/core/surface/channel.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051#include "src/core/transport/chttp2_transport.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052#include "src/core/tsi/transport_security_interface.h"
53
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080054typedef struct {
Craig Tilleracf0f072015-06-29 08:24:16 -070055 grpc_connector base;
56 gpr_refcount refs;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070058 grpc_channel_security_connector *security_connector;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059
Craig Tilleracf0f072015-06-29 08:24:16 -070060 grpc_iomgr_closure *notify;
61 grpc_connect_in_args args;
62 grpc_connect_out_args *result;
63} connector;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080064
Craig Tilleracf0f072015-06-29 08:24:16 -070065static void connector_ref(grpc_connector *con) {
66 connector *c = (connector *)con;
67 gpr_ref(&c->refs);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068}
69
Craig Tilleracf0f072015-06-29 08:24:16 -070070static void connector_unref(grpc_connector *con) {
71 connector *c = (connector *)con;
72 if (gpr_unref(&c->refs)) {
73 gpr_free(c);
74 }
75}
76
77static void on_secure_transport_setup_done(void *arg,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078 grpc_security_status status,
yang-g097468d2015-07-15 22:51:39 -070079 grpc_endpoint *wrapped_endpoint,
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080080 grpc_endpoint *secure_endpoint) {
Craig Tilleracf0f072015-06-29 08:24:16 -070081 connector *c = arg;
82 grpc_iomgr_closure *notify;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083 if (status != GRPC_SECURITY_OK) {
84 gpr_log(GPR_ERROR, "Secure transport setup failed with error %d.", status);
Craig Tilleracf0f072015-06-29 08:24:16 -070085 memset(c->result, 0, sizeof(*c->result));
Craig Tiller43a2b172015-02-18 17:17:51 -080086 } else {
Craig Tiller825b21c2015-07-06 08:35:02 -070087 c->result->transport = grpc_create_chttp2_transport(
88 c->args.channel_args, secure_endpoint, c->args.metadata_context, 1);
89 grpc_chttp2_transport_start_reading(c->result->transport, NULL, 0);
Craig Tillerd9a50882015-06-29 15:57:36 -070090 c->result->filters = gpr_malloc(sizeof(grpc_channel_filter *) * 2);
91 c->result->filters[0] = &grpc_client_auth_filter;
92 c->result->filters[1] = &grpc_http_client_filter;
93 c->result->num_filters = 2;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080094 }
Craig Tillerd9a50882015-06-29 15:57:36 -070095 notify = c->notify;
96 c->notify = NULL;
97 grpc_iomgr_add_callback(notify);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080098}
99
Craig Tilleracf0f072015-06-29 08:24:16 -0700100static void connected(void *arg, grpc_endpoint *tcp) {
101 connector *c = arg;
102 grpc_iomgr_closure *notify;
103 if (tcp != NULL) {
104 grpc_setup_secure_transport(&c->security_connector->base, tcp,
105 on_secure_transport_setup_done, c);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106 } else {
Craig Tilleracf0f072015-06-29 08:24:16 -0700107 memset(c->result, 0, sizeof(*c->result));
108 notify = c->notify;
109 c->notify = NULL;
110 grpc_iomgr_add_callback(notify);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800111 }
112}
113
Craig Tiller079a11b2015-06-30 10:07:15 -0700114static void connector_connect(grpc_connector *con,
115 const grpc_connect_in_args *args,
116 grpc_connect_out_args *result,
117 grpc_iomgr_closure *notify) {
Craig Tilleracf0f072015-06-29 08:24:16 -0700118 connector *c = (connector *)con;
119 GPR_ASSERT(c->notify == NULL);
120 GPR_ASSERT(notify->cb);
121 c->notify = notify;
122 c->args = *args;
123 c->result = result;
Craig Tiller079a11b2015-06-30 10:07:15 -0700124 grpc_tcp_client_connect(connected, c, args->interested_parties, args->addr,
125 args->addr_len, args->deadline);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800126}
127
Craig Tiller079a11b2015-06-30 10:07:15 -0700128static const grpc_connector_vtable connector_vtable = {
129 connector_ref, connector_unref, connector_connect};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800130
Craig Tilleracf0f072015-06-29 08:24:16 -0700131typedef struct {
132 grpc_subchannel_factory base;
133 gpr_refcount refs;
134 grpc_mdctx *mdctx;
Craig Tillerd9a50882015-06-29 15:57:36 -0700135 grpc_channel_args *merge_args;
Craig Tilleracf0f072015-06-29 08:24:16 -0700136 grpc_channel_security_connector *security_connector;
137} subchannel_factory;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800138
Craig Tilleracf0f072015-06-29 08:24:16 -0700139static void subchannel_factory_ref(grpc_subchannel_factory *scf) {
140 subchannel_factory *f = (subchannel_factory *)scf;
141 gpr_ref(&f->refs);
142}
143
144static void subchannel_factory_unref(grpc_subchannel_factory *scf) {
145 subchannel_factory *f = (subchannel_factory *)scf;
146 if (gpr_unref(&f->refs)) {
Craig Tillerb3671532015-07-01 10:37:40 -0700147 GRPC_SECURITY_CONNECTOR_UNREF(&f->security_connector->base,
148 "subchannel_factory");
Craig Tiller6806e1e2015-06-30 10:28:29 -0700149 grpc_channel_args_destroy(f->merge_args);
Craig Tilleracf0f072015-06-29 08:24:16 -0700150 grpc_mdctx_unref(f->mdctx);
151 gpr_free(f);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800152 }
153}
154
Craig Tiller079a11b2015-06-30 10:07:15 -0700155static grpc_subchannel *subchannel_factory_create_subchannel(
156 grpc_subchannel_factory *scf, grpc_subchannel_args *args) {
Craig Tilleracf0f072015-06-29 08:24:16 -0700157 subchannel_factory *f = (subchannel_factory *)scf;
158 connector *c = gpr_malloc(sizeof(*c));
Craig Tillerd9a50882015-06-29 15:57:36 -0700159 grpc_channel_args *final_args =
160 grpc_channel_args_merge(args->args, f->merge_args);
Craig Tilleracf0f072015-06-29 08:24:16 -0700161 grpc_subchannel *s;
162 memset(c, 0, sizeof(*c));
163 c->base.vtable = &connector_vtable;
164 c->security_connector = f->security_connector;
165 gpr_ref_init(&c->refs, 1);
166 args->mdctx = f->mdctx;
Craig Tillerd9a50882015-06-29 15:57:36 -0700167 args->args = final_args;
Craig Tilleracf0f072015-06-29 08:24:16 -0700168 s = grpc_subchannel_create(&c->base, args);
169 grpc_connector_unref(&c->base);
Craig Tillerd9a50882015-06-29 15:57:36 -0700170 grpc_channel_args_destroy(final_args);
Craig Tilleracf0f072015-06-29 08:24:16 -0700171 return s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800172}
173
Craig Tiller079a11b2015-06-30 10:07:15 -0700174static const grpc_subchannel_factory_vtable subchannel_factory_vtable = {
175 subchannel_factory_ref, subchannel_factory_unref,
176 subchannel_factory_create_subchannel};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800177
178/* Create a secure client channel:
179 Asynchronously: - resolve target
180 - connect to it (trying alternatives as presented)
181 - perform handshakes */
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700182grpc_channel *grpc_secure_channel_create(grpc_credentials *creds,
183 const char *target,
184 const grpc_channel_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800185 grpc_channel *channel;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700186 grpc_arg connector_arg;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187 grpc_channel_args *args_copy;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700188 grpc_channel_args *new_args_from_connector;
Craig Tiller1a727fd2015-04-24 13:21:22 -0700189 grpc_channel_security_connector *connector;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700190 grpc_mdctx *mdctx;
Craig Tilleracf0f072015-06-29 08:24:16 -0700191 grpc_resolver *resolver;
192 subchannel_factory *f;
Julien Boeuf54b21922015-02-04 16:39:35 -0800193#define MAX_FILTERS 3
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800194 const grpc_channel_filter *filters[MAX_FILTERS];
195 int n = 0;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700196
197 if (grpc_find_security_connector_in_args(args) != NULL) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800198 gpr_log(GPR_ERROR, "Cannot set security context in channel args.");
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700199 return grpc_lame_client_channel_create(target);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800200 }
201
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700202 if (grpc_credentials_create_security_connector(
203 creds, target, args, NULL, &connector, &new_args_from_connector) !=
204 GRPC_SECURITY_OK) {
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700205 return grpc_lame_client_channel_create(target);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700206 }
Julien Boeuf75c9b6f2015-05-29 13:12:12 -0700207 mdctx = grpc_mdctx_create();
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700208
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700209 connector_arg = grpc_security_connector_to_arg(&connector->base);
210 args_copy = grpc_channel_args_copy_and_add(
211 new_args_from_connector != NULL ? new_args_from_connector : args,
Craig Tillerd9a50882015-06-29 15:57:36 -0700212 &connector_arg, 1);
Alistair Veitch9686dab2015-05-26 14:26:47 -0700213 /* TODO(census)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800214 if (grpc_channel_args_is_census_enabled(args)) {
215 filters[n++] = &grpc_client_census_filter;
Alistair Veitch9686dab2015-05-26 14:26:47 -0700216 } */
David Garcia Quintasd7d9ce22015-06-30 23:29:03 -0700217 filters[n++] = &grpc_compress_filter;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800218 filters[n++] = &grpc_client_channel_filter;
219 GPR_ASSERT(n <= MAX_FILTERS);
Craig Tilleracf0f072015-06-29 08:24:16 -0700220
221 f = gpr_malloc(sizeof(*f));
222 f->base.vtable = &subchannel_factory_vtable;
223 gpr_ref_init(&f->refs, 1);
Craig Tiller614e27a2015-06-30 09:32:17 -0700224 grpc_mdctx_ref(mdctx);
Craig Tilleracf0f072015-06-29 08:24:16 -0700225 f->mdctx = mdctx;
Craig Tiller5d44c062015-07-01 08:55:28 -0700226 GRPC_SECURITY_CONNECTOR_REF(&connector->base, "subchannel_factory");
Craig Tilleracf0f072015-06-29 08:24:16 -0700227 f->security_connector = connector;
Craig Tillerd9a50882015-06-29 15:57:36 -0700228 f->merge_args = grpc_channel_args_copy(args_copy);
Craig Tilleracf0f072015-06-29 08:24:16 -0700229 resolver = grpc_resolver_create(target, &f->base);
230 if (!resolver) {
231 return NULL;
232 }
233
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700234 channel =
235 grpc_channel_create_from_filters(target, filters, n, args_copy, mdctx, 1);
Craig Tiller079a11b2015-06-30 10:07:15 -0700236 grpc_client_channel_set_resolver(grpc_channel_get_channel_stack(channel),
237 resolver);
Craig Tiller614e27a2015-06-30 09:32:17 -0700238 GRPC_RESOLVER_UNREF(resolver, "create");
Craig Tiller5d44c062015-07-01 08:55:28 -0700239 grpc_subchannel_factory_unref(&f->base);
240 GRPC_SECURITY_CONNECTOR_UNREF(&connector->base, "channel_create");
Craig Tilleracf0f072015-06-29 08:24:16 -0700241
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800242 grpc_channel_args_destroy(args_copy);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700243 if (new_args_from_connector != NULL) {
244 grpc_channel_args_destroy(new_args_from_connector);
245 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800246
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800247 return channel;
Craig Tiller190d3602015-02-18 09:23:38 -0800248}