blob: 1dd9e61d0fc18b4811ea43f428d4bf074a5e589b [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,
78 grpc_endpoint *secure_endpoint) {
79 connector *c = arg;
80 grpc_iomgr_closure *notify;
81 if (status != GRPC_SECURITY_OK) {
82 gpr_log(GPR_ERROR, "Secure transport setup failed with error %d.", status);
83 memset(c->result, 0, sizeof(*c->result));
Craig Tilleracf0f072015-06-29 08:24:16 -070084 } else {
Craig Tiller079a11b2015-06-30 10:07:15 -070085 c->result->transport =
86 grpc_create_chttp2_transport(c->args.channel_args, secure_endpoint,
87 NULL, 0, c->args.metadata_context, 1);
Craig Tillerd9a50882015-06-29 15:57:36 -070088 c->result->filters = gpr_malloc(sizeof(grpc_channel_filter *) * 2);
89 c->result->filters[0] = &grpc_client_auth_filter;
90 c->result->filters[1] = &grpc_http_client_filter;
91 c->result->num_filters = 2;
Craig Tilleracf0f072015-06-29 08:24:16 -070092 }
Craig Tillerd9a50882015-06-29 15:57:36 -070093 notify = c->notify;
94 c->notify = NULL;
95 grpc_iomgr_add_callback(notify);
Craig Tilleracf0f072015-06-29 08:24:16 -070096}
97
98static void connected(void *arg, grpc_endpoint *tcp) {
99 connector *c = arg;
100 grpc_iomgr_closure *notify;
101 if (tcp != NULL) {
102 grpc_setup_secure_transport(&c->security_connector->base, tcp,
103 on_secure_transport_setup_done, c);
104 } else {
105 memset(c->result, 0, sizeof(*c->result));
106 notify = c->notify;
107 c->notify = NULL;
108 grpc_iomgr_add_callback(notify);
109 }
110}
111
Craig Tiller079a11b2015-06-30 10:07:15 -0700112static void connector_connect(grpc_connector *con,
113 const grpc_connect_in_args *args,
114 grpc_connect_out_args *result,
115 grpc_iomgr_closure *notify) {
Craig Tilleracf0f072015-06-29 08:24:16 -0700116 connector *c = (connector *)con;
117 GPR_ASSERT(c->notify == NULL);
118 GPR_ASSERT(notify->cb);
119 c->notify = notify;
120 c->args = *args;
121 c->result = result;
Craig Tiller079a11b2015-06-30 10:07:15 -0700122 grpc_tcp_client_connect(connected, c, args->interested_parties, args->addr,
123 args->addr_len, args->deadline);
Craig Tilleracf0f072015-06-29 08:24:16 -0700124}
125
Craig Tiller079a11b2015-06-30 10:07:15 -0700126static const grpc_connector_vtable connector_vtable = {
127 connector_ref, connector_unref, connector_connect};
Craig Tilleracf0f072015-06-29 08:24:16 -0700128
129typedef struct {
130 grpc_subchannel_factory base;
131 gpr_refcount refs;
132 grpc_mdctx *mdctx;
Craig Tillerd9a50882015-06-29 15:57:36 -0700133 grpc_channel_args *merge_args;
Craig Tilleracf0f072015-06-29 08:24:16 -0700134 grpc_channel_security_connector *security_connector;
135} subchannel_factory;
136
137static void subchannel_factory_ref(grpc_subchannel_factory *scf) {
138 subchannel_factory *f = (subchannel_factory *)scf;
139 gpr_ref(&f->refs);
140}
141
142static void subchannel_factory_unref(grpc_subchannel_factory *scf) {
143 subchannel_factory *f = (subchannel_factory *)scf;
144 if (gpr_unref(&f->refs)) {
Craig Tiller6806e1e2015-06-30 10:28:29 -0700145 grpc_channel_args_destroy(f->merge_args);
Craig Tilleracf0f072015-06-29 08:24:16 -0700146 grpc_mdctx_unref(f->mdctx);
147 gpr_free(f);
148 }
149}
150
Craig Tiller079a11b2015-06-30 10:07:15 -0700151static grpc_subchannel *subchannel_factory_create_subchannel(
152 grpc_subchannel_factory *scf, grpc_subchannel_args *args) {
Craig Tilleracf0f072015-06-29 08:24:16 -0700153 subchannel_factory *f = (subchannel_factory *)scf;
154 connector *c = gpr_malloc(sizeof(*c));
Craig Tillerd9a50882015-06-29 15:57:36 -0700155 grpc_channel_args *final_args =
156 grpc_channel_args_merge(args->args, f->merge_args);
Craig Tilleracf0f072015-06-29 08:24:16 -0700157 grpc_subchannel *s;
158 memset(c, 0, sizeof(*c));
159 c->base.vtable = &connector_vtable;
160 c->security_connector = f->security_connector;
161 gpr_ref_init(&c->refs, 1);
162 args->mdctx = f->mdctx;
Craig Tillerd9a50882015-06-29 15:57:36 -0700163 args->args = final_args;
Craig Tilleracf0f072015-06-29 08:24:16 -0700164 s = grpc_subchannel_create(&c->base, args);
165 grpc_connector_unref(&c->base);
Craig Tillerd9a50882015-06-29 15:57:36 -0700166 grpc_channel_args_destroy(final_args);
Craig Tilleracf0f072015-06-29 08:24:16 -0700167 return s;
168}
169
Craig Tiller079a11b2015-06-30 10:07:15 -0700170static const grpc_subchannel_factory_vtable subchannel_factory_vtable = {
171 subchannel_factory_ref, subchannel_factory_unref,
172 subchannel_factory_create_subchannel};
Craig Tilleracf0f072015-06-29 08:24:16 -0700173
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800174/* Create a secure client channel:
175 Asynchronously: - resolve target
176 - connect to it (trying alternatives as presented)
177 - perform handshakes */
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700178grpc_channel *grpc_secure_channel_create(grpc_credentials *creds,
179 const char *target,
180 const grpc_channel_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800181 grpc_channel *channel;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700182 grpc_arg connector_arg;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800183 grpc_channel_args *args_copy;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700184 grpc_channel_args *new_args_from_connector;
Craig Tiller1a727fd2015-04-24 13:21:22 -0700185 grpc_channel_security_connector *connector;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700186 grpc_mdctx *mdctx;
Craig Tilleracf0f072015-06-29 08:24:16 -0700187 grpc_resolver *resolver;
188 subchannel_factory *f;
Julien Boeuf54b21922015-02-04 16:39:35 -0800189#define MAX_FILTERS 3
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800190 const grpc_channel_filter *filters[MAX_FILTERS];
191 int n = 0;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700192
193 if (grpc_find_security_connector_in_args(args) != NULL) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800194 gpr_log(GPR_ERROR, "Cannot set security context in channel args.");
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700195 return grpc_lame_client_channel_create();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800196 }
197
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700198 if (grpc_credentials_create_security_connector(
199 creds, target, args, NULL, &connector, &new_args_from_connector) !=
200 GRPC_SECURITY_OK) {
201 return grpc_lame_client_channel_create();
202 }
Julien Boeuf75c9b6f2015-05-29 13:12:12 -0700203 mdctx = grpc_mdctx_create();
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700204
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700205 connector_arg = grpc_security_connector_to_arg(&connector->base);
206 args_copy = grpc_channel_args_copy_and_add(
207 new_args_from_connector != NULL ? new_args_from_connector : args,
Craig Tillerd9a50882015-06-29 15:57:36 -0700208 &connector_arg, 1);
Alistair Veitch9686dab2015-05-26 14:26:47 -0700209 /* TODO(census)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800210 if (grpc_channel_args_is_census_enabled(args)) {
211 filters[n++] = &grpc_client_census_filter;
Alistair Veitch9686dab2015-05-26 14:26:47 -0700212 } */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800213 filters[n++] = &grpc_client_channel_filter;
214 GPR_ASSERT(n <= MAX_FILTERS);
Craig Tilleracf0f072015-06-29 08:24:16 -0700215
216 f = gpr_malloc(sizeof(*f));
217 f->base.vtable = &subchannel_factory_vtable;
218 gpr_ref_init(&f->refs, 1);
Craig Tiller614e27a2015-06-30 09:32:17 -0700219 grpc_mdctx_ref(mdctx);
Craig Tilleracf0f072015-06-29 08:24:16 -0700220 f->mdctx = mdctx;
221 f->security_connector = connector;
Craig Tillerd9a50882015-06-29 15:57:36 -0700222 f->merge_args = grpc_channel_args_copy(args_copy);
Craig Tilleracf0f072015-06-29 08:24:16 -0700223 resolver = grpc_resolver_create(target, &f->base);
224 if (!resolver) {
225 return NULL;
226 }
227
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800228 channel = grpc_channel_create_from_filters(filters, n, args_copy, mdctx, 1);
Craig Tiller079a11b2015-06-30 10:07:15 -0700229 grpc_client_channel_set_resolver(grpc_channel_get_channel_stack(channel),
230 resolver);
Craig Tiller614e27a2015-06-30 09:32:17 -0700231 GRPC_RESOLVER_UNREF(resolver, "create");
Craig Tilleracf0f072015-06-29 08:24:16 -0700232
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800233 grpc_channel_args_destroy(args_copy);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700234 if (new_args_from_connector != NULL) {
235 grpc_channel_args_destroy(new_args_from_connector);
236 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800237
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800238 return channel;
Craig Tiller190d3602015-02-18 09:23:38 -0800239}