Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
| 34 | #include <grpc/grpc.h> |
| 35 | |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
| 40 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include "src/core/channel/channel_args.h" |
| 42 | #include "src/core/channel/client_channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 43 | #include "src/core/channel/http_client_filter.h" |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 44 | #include "src/core/client_config/resolver_registry.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 45 | #include "src/core/iomgr/tcp_client.h" |
Julien Boeuf | c6f8d0a | 2015-05-11 22:40:02 -0700 | [diff] [blame] | 46 | #include "src/core/security/auth_filters.h" |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 47 | #include "src/core/security/credentials.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | #include "src/core/security/secure_transport_setup.h" |
| 49 | #include "src/core/surface/channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | #include "src/core/transport/chttp2_transport.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | #include "src/core/tsi/transport_security_interface.h" |
| 52 | |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 53 | typedef 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 | |
| 64 | static void connector_ref(grpc_connector *con) { |
| 65 | connector *c = (connector *)con; |
| 66 | gpr_ref(&c->refs); |
| 67 | } |
| 68 | |
| 69 | static void connector_unref(grpc_connector *con) { |
| 70 | connector *c = (connector *)con; |
| 71 | if (gpr_unref(&c->refs)) { |
| 72 | gpr_free(c); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static void on_secure_transport_setup_done(void *arg, |
| 77 | grpc_security_status status, |
yang-g | 097468d | 2015-07-15 22:51:39 -0700 | [diff] [blame^] | 78 | grpc_endpoint *wrapped_endpoint, |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 79 | 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 Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 85 | } else { |
Craig Tiller | 825b21c | 2015-07-06 08:35:02 -0700 | [diff] [blame] | 86 | 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 Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 89 | 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 Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 93 | } |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 94 | notify = c->notify; |
| 95 | c->notify = NULL; |
| 96 | grpc_iomgr_add_callback(notify); |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static 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 Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 113 | static 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 Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 117 | 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 Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 123 | grpc_tcp_client_connect(connected, c, args->interested_parties, args->addr, |
| 124 | args->addr_len, args->deadline); |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 127 | static const grpc_connector_vtable connector_vtable = { |
| 128 | connector_ref, connector_unref, connector_connect}; |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 129 | |
| 130 | typedef struct { |
| 131 | grpc_subchannel_factory base; |
| 132 | gpr_refcount refs; |
| 133 | grpc_mdctx *mdctx; |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 134 | grpc_channel_args *merge_args; |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 135 | grpc_channel_security_connector *security_connector; |
| 136 | } subchannel_factory; |
| 137 | |
| 138 | static void subchannel_factory_ref(grpc_subchannel_factory *scf) { |
| 139 | subchannel_factory *f = (subchannel_factory *)scf; |
| 140 | gpr_ref(&f->refs); |
| 141 | } |
| 142 | |
| 143 | static void subchannel_factory_unref(grpc_subchannel_factory *scf) { |
| 144 | subchannel_factory *f = (subchannel_factory *)scf; |
| 145 | if (gpr_unref(&f->refs)) { |
Craig Tiller | b367153 | 2015-07-01 10:37:40 -0700 | [diff] [blame] | 146 | GRPC_SECURITY_CONNECTOR_UNREF(&f->security_connector->base, |
| 147 | "subchannel_factory"); |
Craig Tiller | 6806e1e | 2015-06-30 10:28:29 -0700 | [diff] [blame] | 148 | grpc_channel_args_destroy(f->merge_args); |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 149 | grpc_mdctx_unref(f->mdctx); |
| 150 | gpr_free(f); |
| 151 | } |
| 152 | } |
| 153 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 154 | static grpc_subchannel *subchannel_factory_create_subchannel( |
| 155 | grpc_subchannel_factory *scf, grpc_subchannel_args *args) { |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 156 | subchannel_factory *f = (subchannel_factory *)scf; |
| 157 | connector *c = gpr_malloc(sizeof(*c)); |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 158 | grpc_channel_args *final_args = |
| 159 | grpc_channel_args_merge(args->args, f->merge_args); |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 160 | 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 Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 166 | args->args = final_args; |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 167 | s = grpc_subchannel_create(&c->base, args); |
| 168 | grpc_connector_unref(&c->base); |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 169 | grpc_channel_args_destroy(final_args); |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 170 | return s; |
| 171 | } |
| 172 | |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 173 | static const grpc_subchannel_factory_vtable subchannel_factory_vtable = { |
| 174 | subchannel_factory_ref, subchannel_factory_unref, |
| 175 | subchannel_factory_create_subchannel}; |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 176 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 177 | /* Create a secure client channel: |
| 178 | Asynchronously: - resolve target |
| 179 | - connect to it (trying alternatives as presented) |
| 180 | - perform handshakes */ |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 181 | grpc_channel *grpc_secure_channel_create(grpc_credentials *creds, |
| 182 | const char *target, |
| 183 | const grpc_channel_args *args) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 184 | grpc_channel *channel; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 185 | grpc_arg connector_arg; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 186 | grpc_channel_args *args_copy; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 187 | grpc_channel_args *new_args_from_connector; |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 188 | grpc_channel_security_connector *connector; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 189 | grpc_mdctx *mdctx; |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 190 | grpc_resolver *resolver; |
| 191 | subchannel_factory *f; |
Julien Boeuf | 54b2192 | 2015-02-04 16:39:35 -0800 | [diff] [blame] | 192 | #define MAX_FILTERS 3 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 193 | const grpc_channel_filter *filters[MAX_FILTERS]; |
| 194 | int n = 0; |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 195 | |
| 196 | if (grpc_find_security_connector_in_args(args) != NULL) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 197 | gpr_log(GPR_ERROR, "Cannot set security context in channel args."); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 198 | return grpc_lame_client_channel_create(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 201 | 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 Boeuf | 75c9b6f | 2015-05-29 13:12:12 -0700 | [diff] [blame] | 206 | mdctx = grpc_mdctx_create(); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 207 | |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 208 | 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 Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 211 | &connector_arg, 1); |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 212 | /* TODO(census) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 213 | if (grpc_channel_args_is_census_enabled(args)) { |
| 214 | filters[n++] = &grpc_client_census_filter; |
Alistair Veitch | 9686dab | 2015-05-26 14:26:47 -0700 | [diff] [blame] | 215 | } */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 216 | filters[n++] = &grpc_client_channel_filter; |
| 217 | GPR_ASSERT(n <= MAX_FILTERS); |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 218 | |
| 219 | f = gpr_malloc(sizeof(*f)); |
| 220 | f->base.vtable = &subchannel_factory_vtable; |
| 221 | gpr_ref_init(&f->refs, 1); |
Craig Tiller | 614e27a | 2015-06-30 09:32:17 -0700 | [diff] [blame] | 222 | grpc_mdctx_ref(mdctx); |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 223 | f->mdctx = mdctx; |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 224 | GRPC_SECURITY_CONNECTOR_REF(&connector->base, "subchannel_factory"); |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 225 | f->security_connector = connector; |
Craig Tiller | d9a5088 | 2015-06-29 15:57:36 -0700 | [diff] [blame] | 226 | f->merge_args = grpc_channel_args_copy(args_copy); |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 227 | resolver = grpc_resolver_create(target, &f->base); |
| 228 | if (!resolver) { |
| 229 | return NULL; |
| 230 | } |
| 231 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 232 | channel = grpc_channel_create_from_filters(filters, n, args_copy, mdctx, 1); |
Craig Tiller | 079a11b | 2015-06-30 10:07:15 -0700 | [diff] [blame] | 233 | grpc_client_channel_set_resolver(grpc_channel_get_channel_stack(channel), |
| 234 | resolver); |
Craig Tiller | 614e27a | 2015-06-30 09:32:17 -0700 | [diff] [blame] | 235 | GRPC_RESOLVER_UNREF(resolver, "create"); |
Craig Tiller | 5d44c06 | 2015-07-01 08:55:28 -0700 | [diff] [blame] | 236 | grpc_subchannel_factory_unref(&f->base); |
| 237 | GRPC_SECURITY_CONNECTOR_UNREF(&connector->base, "channel_create"); |
Craig Tiller | acf0f07 | 2015-06-29 08:24:16 -0700 | [diff] [blame] | 238 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 239 | grpc_channel_args_destroy(args_copy); |
Julien Boeuf | 7d1d9ca | 2015-04-17 14:38:48 -0700 | [diff] [blame] | 240 | if (new_args_from_connector != NULL) { |
| 241 | grpc_channel_args_destroy(new_args_from_connector); |
| 242 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 243 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 244 | return channel; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 245 | } |