blob: f3c7d8397b74886d55cb4ab73b2b24577b51f24d [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"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080043#include "src/core/channel/http_client_filter.h"
Craig Tilleracf0f072015-06-29 08:24:16 -070044#include "src/core/client_config/resolver_registry.h"
ctiller18b49ab2014-12-09 14:39:16 -080045#include "src/core/iomgr/tcp_client.h"
Julien Boeufc6f8d0a2015-05-11 22:40:02 -070046#include "src/core/security/auth_filters.h"
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -070047#include "src/core/security/credentials.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048#include "src/core/security/secure_transport_setup.h"
49#include "src/core/surface/channel.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050#include "src/core/transport/chttp2_transport.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051#include "src/core/tsi/transport_security_interface.h"
52
Craig Tilleracf0f072015-06-29 08:24:16 -070053typedef struct {
54 grpc_connector base;
55 gpr_refcount refs;
56
57 grpc_channel_security_connector *security_connector;
58
59 grpc_iomgr_closure *notify;
60 grpc_connect_in_args args;
61 grpc_connect_out_args *result;
62} connector;
63
64static void connector_ref(grpc_connector *con) {
65 connector *c = (connector *)con;
66 gpr_ref(&c->refs);
67}
68
69static void connector_unref(grpc_connector *con) {
70 connector *c = (connector *)con;
71 if (gpr_unref(&c->refs)) {
72 gpr_free(c);
73 }
74}
75
76static void on_secure_transport_setup_done(void *arg,
77 grpc_security_status status,
yang-g097468d2015-07-15 22:51:39 -070078 grpc_endpoint *wrapped_endpoint,
Craig Tilleracf0f072015-06-29 08:24:16 -070079 grpc_endpoint *secure_endpoint) {
80 connector *c = arg;
81 grpc_iomgr_closure *notify;
82 if (status != GRPC_SECURITY_OK) {
83 gpr_log(GPR_ERROR, "Secure transport setup failed with error %d.", status);
84 memset(c->result, 0, sizeof(*c->result));
Craig Tilleracf0f072015-06-29 08:24:16 -070085 } else {
Craig Tiller825b21c2015-07-06 08:35:02 -070086 c->result->transport = grpc_create_chttp2_transport(
87 c->args.channel_args, secure_endpoint, c->args.metadata_context, 1);
88 grpc_chttp2_transport_start_reading(c->result->transport, NULL, 0);
Craig Tillerd9a50882015-06-29 15:57:36 -070089 c->result->filters = gpr_malloc(sizeof(grpc_channel_filter *) * 2);
90 c->result->filters[0] = &grpc_client_auth_filter;
91 c->result->filters[1] = &grpc_http_client_filter;
92 c->result->num_filters = 2;
Craig Tilleracf0f072015-06-29 08:24:16 -070093 }
Craig Tillerd9a50882015-06-29 15:57:36 -070094 notify = c->notify;
95 c->notify = NULL;
96 grpc_iomgr_add_callback(notify);
Craig Tilleracf0f072015-06-29 08:24:16 -070097}
98
99static void connected(void *arg, grpc_endpoint *tcp) {
100 connector *c = arg;
101 grpc_iomgr_closure *notify;
102 if (tcp != NULL) {
103 grpc_setup_secure_transport(&c->security_connector->base, tcp,
104 on_secure_transport_setup_done, c);
105 } else {
106 memset(c->result, 0, sizeof(*c->result));
107 notify = c->notify;
108 c->notify = NULL;
109 grpc_iomgr_add_callback(notify);
110 }
111}
112
Craig Tiller079a11b2015-06-30 10:07:15 -0700113static void connector_connect(grpc_connector *con,
114 const grpc_connect_in_args *args,
115 grpc_connect_out_args *result,
116 grpc_iomgr_closure *notify) {
Craig Tilleracf0f072015-06-29 08:24:16 -0700117 connector *c = (connector *)con;
118 GPR_ASSERT(c->notify == NULL);
119 GPR_ASSERT(notify->cb);
120 c->notify = notify;
121 c->args = *args;
122 c->result = result;
Craig Tiller079a11b2015-06-30 10:07:15 -0700123 grpc_tcp_client_connect(connected, c, args->interested_parties, args->addr,
124 args->addr_len, args->deadline);
Craig Tilleracf0f072015-06-29 08:24:16 -0700125}
126
Craig Tiller079a11b2015-06-30 10:07:15 -0700127static const grpc_connector_vtable connector_vtable = {
128 connector_ref, connector_unref, connector_connect};
Craig Tilleracf0f072015-06-29 08:24:16 -0700129
130typedef struct {
131 grpc_subchannel_factory base;
132 gpr_refcount refs;
133 grpc_mdctx *mdctx;
Craig Tillerd9a50882015-06-29 15:57:36 -0700134 grpc_channel_args *merge_args;
Craig Tilleracf0f072015-06-29 08:24:16 -0700135 grpc_channel_security_connector *security_connector;
136} subchannel_factory;
137
138static void subchannel_factory_ref(grpc_subchannel_factory *scf) {
139 subchannel_factory *f = (subchannel_factory *)scf;
140 gpr_ref(&f->refs);
141}
142
143static void subchannel_factory_unref(grpc_subchannel_factory *scf) {
144 subchannel_factory *f = (subchannel_factory *)scf;
145 if (gpr_unref(&f->refs)) {
Craig Tillerb3671532015-07-01 10:37:40 -0700146 GRPC_SECURITY_CONNECTOR_UNREF(&f->security_connector->base,
147 "subchannel_factory");
Craig Tiller6806e1e2015-06-30 10:28:29 -0700148 grpc_channel_args_destroy(f->merge_args);
Craig Tilleracf0f072015-06-29 08:24:16 -0700149 grpc_mdctx_unref(f->mdctx);
150 gpr_free(f);
151 }
152}
153
Craig Tiller079a11b2015-06-30 10:07:15 -0700154static grpc_subchannel *subchannel_factory_create_subchannel(
155 grpc_subchannel_factory *scf, grpc_subchannel_args *args) {
Craig Tilleracf0f072015-06-29 08:24:16 -0700156 subchannel_factory *f = (subchannel_factory *)scf;
157 connector *c = gpr_malloc(sizeof(*c));
Craig Tillerd9a50882015-06-29 15:57:36 -0700158 grpc_channel_args *final_args =
159 grpc_channel_args_merge(args->args, f->merge_args);
Craig Tilleracf0f072015-06-29 08:24:16 -0700160 grpc_subchannel *s;
161 memset(c, 0, sizeof(*c));
162 c->base.vtable = &connector_vtable;
163 c->security_connector = f->security_connector;
164 gpr_ref_init(&c->refs, 1);
165 args->mdctx = f->mdctx;
Craig Tillerd9a50882015-06-29 15:57:36 -0700166 args->args = final_args;
Craig Tilleracf0f072015-06-29 08:24:16 -0700167 s = grpc_subchannel_create(&c->base, args);
168 grpc_connector_unref(&c->base);
Craig Tillerd9a50882015-06-29 15:57:36 -0700169 grpc_channel_args_destroy(final_args);
Craig Tilleracf0f072015-06-29 08:24:16 -0700170 return s;
171}
172
Craig Tiller079a11b2015-06-30 10:07:15 -0700173static const grpc_subchannel_factory_vtable subchannel_factory_vtable = {
174 subchannel_factory_ref, subchannel_factory_unref,
175 subchannel_factory_create_subchannel};
Craig Tilleracf0f072015-06-29 08:24:16 -0700176
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800177/* Create a secure client channel:
178 Asynchronously: - resolve target
179 - connect to it (trying alternatives as presented)
180 - perform handshakes */
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700181grpc_channel *grpc_secure_channel_create(grpc_credentials *creds,
182 const char *target,
183 const grpc_channel_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800184 grpc_channel *channel;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700185 grpc_arg connector_arg;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800186 grpc_channel_args *args_copy;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700187 grpc_channel_args *new_args_from_connector;
Craig Tiller1a727fd2015-04-24 13:21:22 -0700188 grpc_channel_security_connector *connector;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700189 grpc_mdctx *mdctx;
Craig Tilleracf0f072015-06-29 08:24:16 -0700190 grpc_resolver *resolver;
191 subchannel_factory *f;
Julien Boeuf54b21922015-02-04 16:39:35 -0800192#define MAX_FILTERS 3
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800193 const grpc_channel_filter *filters[MAX_FILTERS];
194 int n = 0;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700195
196 if (grpc_find_security_connector_in_args(args) != NULL) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800197 gpr_log(GPR_ERROR, "Cannot set security context in channel args.");
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700198 return grpc_lame_client_channel_create();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800199 }
200
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700201 if (grpc_credentials_create_security_connector(
202 creds, target, args, NULL, &connector, &new_args_from_connector) !=
203 GRPC_SECURITY_OK) {
204 return grpc_lame_client_channel_create();
205 }
Julien Boeuf75c9b6f2015-05-29 13:12:12 -0700206 mdctx = grpc_mdctx_create();
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700207
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700208 connector_arg = grpc_security_connector_to_arg(&connector->base);
209 args_copy = grpc_channel_args_copy_and_add(
210 new_args_from_connector != NULL ? new_args_from_connector : args,
Craig Tillerd9a50882015-06-29 15:57:36 -0700211 &connector_arg, 1);
Alistair Veitch9686dab2015-05-26 14:26:47 -0700212 /* TODO(census)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800213 if (grpc_channel_args_is_census_enabled(args)) {
214 filters[n++] = &grpc_client_census_filter;
Alistair Veitch9686dab2015-05-26 14:26:47 -0700215 } */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800216 filters[n++] = &grpc_client_channel_filter;
217 GPR_ASSERT(n <= MAX_FILTERS);
Craig Tilleracf0f072015-06-29 08:24:16 -0700218
219 f = gpr_malloc(sizeof(*f));
220 f->base.vtable = &subchannel_factory_vtable;
221 gpr_ref_init(&f->refs, 1);
Craig Tiller614e27a2015-06-30 09:32:17 -0700222 grpc_mdctx_ref(mdctx);
Craig Tilleracf0f072015-06-29 08:24:16 -0700223 f->mdctx = mdctx;
Craig Tiller5d44c062015-07-01 08:55:28 -0700224 GRPC_SECURITY_CONNECTOR_REF(&connector->base, "subchannel_factory");
Craig Tilleracf0f072015-06-29 08:24:16 -0700225 f->security_connector = connector;
Craig Tillerd9a50882015-06-29 15:57:36 -0700226 f->merge_args = grpc_channel_args_copy(args_copy);
Craig Tilleracf0f072015-06-29 08:24:16 -0700227 resolver = grpc_resolver_create(target, &f->base);
228 if (!resolver) {
229 return NULL;
230 }
231
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800232 channel = grpc_channel_create_from_filters(filters, n, args_copy, mdctx, 1);
Craig Tiller079a11b2015-06-30 10:07:15 -0700233 grpc_client_channel_set_resolver(grpc_channel_get_channel_stack(channel),
234 resolver);
Craig Tiller614e27a2015-06-30 09:32:17 -0700235 GRPC_RESOLVER_UNREF(resolver, "create");
Craig Tiller5d44c062015-07-01 08:55:28 -0700236 grpc_subchannel_factory_unref(&f->base);
237 GRPC_SECURITY_CONNECTOR_UNREF(&connector->base, "channel_create");
Craig Tilleracf0f072015-06-29 08:24:16 -0700238
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800239 grpc_channel_args_destroy(args_copy);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700240 if (new_args_from_connector != NULL) {
241 grpc_channel_args_destroy(new_args_from_connector);
242 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800243
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800244 return channel;
Craig Tiller190d3602015-02-18 09:23:38 -0800245}