blob: d35507926096d1ce2a7f7426ebc49161ad4b7608 [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 {
85 c->result->transport = grpc_create_chttp2_transport(
86 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
112static void connector_connect(
113 grpc_connector *con, const grpc_connect_in_args *args,
114 grpc_connect_out_args *result, grpc_iomgr_closure *notify) {
115 connector *c = (connector *)con;
116 GPR_ASSERT(c->notify == NULL);
117 GPR_ASSERT(notify->cb);
118 c->notify = notify;
119 c->args = *args;
120 c->result = result;
121 grpc_tcp_client_connect(connected, c, args->interested_parties, args->addr, args->addr_len, args->deadline);
122}
123
124static const grpc_connector_vtable connector_vtable = {connector_ref, connector_unref, connector_connect};
125
126typedef struct {
127 grpc_subchannel_factory base;
128 gpr_refcount refs;
129 grpc_mdctx *mdctx;
Craig Tillerd9a50882015-06-29 15:57:36 -0700130 grpc_channel_args *merge_args;
Craig Tilleracf0f072015-06-29 08:24:16 -0700131 grpc_channel_security_connector *security_connector;
132} subchannel_factory;
133
134static void subchannel_factory_ref(grpc_subchannel_factory *scf) {
135 subchannel_factory *f = (subchannel_factory *)scf;
136 gpr_ref(&f->refs);
137}
138
139static void subchannel_factory_unref(grpc_subchannel_factory *scf) {
140 subchannel_factory *f = (subchannel_factory *)scf;
141 if (gpr_unref(&f->refs)) {
142 grpc_mdctx_unref(f->mdctx);
143 gpr_free(f);
144 }
145}
146
147static grpc_subchannel *subchannel_factory_create_subchannel(grpc_subchannel_factory *scf, grpc_subchannel_args *args) {
148 subchannel_factory *f = (subchannel_factory *)scf;
149 connector *c = gpr_malloc(sizeof(*c));
Craig Tillerd9a50882015-06-29 15:57:36 -0700150 grpc_channel_args *final_args =
151 grpc_channel_args_merge(args->args, f->merge_args);
Craig Tilleracf0f072015-06-29 08:24:16 -0700152 grpc_subchannel *s;
153 memset(c, 0, sizeof(*c));
154 c->base.vtable = &connector_vtable;
155 c->security_connector = f->security_connector;
156 gpr_ref_init(&c->refs, 1);
157 args->mdctx = f->mdctx;
Craig Tillerd9a50882015-06-29 15:57:36 -0700158 args->args = final_args;
Craig Tilleracf0f072015-06-29 08:24:16 -0700159 s = grpc_subchannel_create(&c->base, args);
160 grpc_connector_unref(&c->base);
Craig Tillerd9a50882015-06-29 15:57:36 -0700161 grpc_channel_args_destroy(final_args);
Craig Tilleracf0f072015-06-29 08:24:16 -0700162 return s;
163}
164
165static const grpc_subchannel_factory_vtable subchannel_factory_vtable = {subchannel_factory_ref, subchannel_factory_unref, subchannel_factory_create_subchannel};
166
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800167/* Create a secure client channel:
168 Asynchronously: - resolve target
169 - connect to it (trying alternatives as presented)
170 - perform handshakes */
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700171grpc_channel *grpc_secure_channel_create(grpc_credentials *creds,
172 const char *target,
173 const grpc_channel_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800174 grpc_channel *channel;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700175 grpc_arg connector_arg;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800176 grpc_channel_args *args_copy;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700177 grpc_channel_args *new_args_from_connector;
Craig Tiller1a727fd2015-04-24 13:21:22 -0700178 grpc_channel_security_connector *connector;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700179 grpc_mdctx *mdctx;
Craig Tilleracf0f072015-06-29 08:24:16 -0700180 grpc_resolver *resolver;
181 subchannel_factory *f;
Julien Boeuf54b21922015-02-04 16:39:35 -0800182#define MAX_FILTERS 3
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800183 const grpc_channel_filter *filters[MAX_FILTERS];
184 int n = 0;
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700185
186 if (grpc_find_security_connector_in_args(args) != NULL) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187 gpr_log(GPR_ERROR, "Cannot set security context in channel args.");
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700188 return grpc_lame_client_channel_create();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800189 }
190
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700191 if (grpc_credentials_create_security_connector(
192 creds, target, args, NULL, &connector, &new_args_from_connector) !=
193 GRPC_SECURITY_OK) {
194 return grpc_lame_client_channel_create();
195 }
Julien Boeuf75c9b6f2015-05-29 13:12:12 -0700196 mdctx = grpc_mdctx_create();
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700197
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700198 connector_arg = grpc_security_connector_to_arg(&connector->base);
199 args_copy = grpc_channel_args_copy_and_add(
200 new_args_from_connector != NULL ? new_args_from_connector : args,
Craig Tillerd9a50882015-06-29 15:57:36 -0700201 &connector_arg, 1);
Alistair Veitch9686dab2015-05-26 14:26:47 -0700202 /* TODO(census)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800203 if (grpc_channel_args_is_census_enabled(args)) {
204 filters[n++] = &grpc_client_census_filter;
Alistair Veitch9686dab2015-05-26 14:26:47 -0700205 } */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800206 filters[n++] = &grpc_client_channel_filter;
207 GPR_ASSERT(n <= MAX_FILTERS);
Craig Tilleracf0f072015-06-29 08:24:16 -0700208
209 f = gpr_malloc(sizeof(*f));
210 f->base.vtable = &subchannel_factory_vtable;
211 gpr_ref_init(&f->refs, 1);
Craig Tiller614e27a2015-06-30 09:32:17 -0700212 grpc_mdctx_ref(mdctx);
Craig Tilleracf0f072015-06-29 08:24:16 -0700213 f->mdctx = mdctx;
214 f->security_connector = connector;
Craig Tillerd9a50882015-06-29 15:57:36 -0700215 f->merge_args = grpc_channel_args_copy(args_copy);
Craig Tilleracf0f072015-06-29 08:24:16 -0700216 resolver = grpc_resolver_create(target, &f->base);
217 if (!resolver) {
218 return NULL;
219 }
220
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800221 channel = grpc_channel_create_from_filters(filters, n, args_copy, mdctx, 1);
Craig Tilleracf0f072015-06-29 08:24:16 -0700222 grpc_client_channel_set_resolver(grpc_channel_get_channel_stack(channel), resolver);
Craig Tiller614e27a2015-06-30 09:32:17 -0700223 GRPC_RESOLVER_UNREF(resolver, "create");
Craig Tilleracf0f072015-06-29 08:24:16 -0700224
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800225 grpc_channel_args_destroy(args_copy);
Julien Boeuf7d1d9ca2015-04-17 14:38:48 -0700226 if (new_args_from_connector != NULL) {
227 grpc_channel_args_destroy(new_args_from_connector);
228 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800229
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800230 return channel;
Craig Tiller190d3602015-02-18 09:23:38 -0800231}