Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, Google Inc. |
| 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 | |
| 39 | #include "src/core/channel/census_filter.h" |
| 40 | #include "src/core/channel/channel_args.h" |
| 41 | #include "src/core/channel/client_channel.h" |
| 42 | #include "src/core/channel/client_setup.h" |
| 43 | #include "src/core/channel/connected_channel.h" |
| 44 | #include "src/core/channel/http_client_filter.h" |
| 45 | #include "src/core/channel/http_filter.h" |
| 46 | #include "src/core/endpoint/resolve_address.h" |
| 47 | #include "src/core/endpoint/tcp.h" |
| 48 | #include "src/core/endpoint/tcp_client.h" |
| 49 | #include "src/core/security/auth.h" |
| 50 | #include "src/core/security/security_context.h" |
| 51 | #include "src/core/security/secure_transport_setup.h" |
| 52 | #include "src/core/surface/channel.h" |
| 53 | #include "src/core/surface/client.h" |
| 54 | #include "src/core/surface/surface_em.h" |
| 55 | #include "src/core/transport/chttp2_transport.h" |
| 56 | #include <grpc/grpc_security.h> |
| 57 | #include <grpc/support/alloc.h> |
| 58 | #include <grpc/support/log.h> |
| 59 | #include <grpc/support/string.h> |
| 60 | #include <grpc/support/sync.h> |
| 61 | #include <grpc/support/useful.h> |
| 62 | #include "src/core/tsi/transport_security_interface.h" |
| 63 | |
| 64 | typedef struct setup setup; |
| 65 | |
| 66 | /* A single setup request (started via initiate) */ |
| 67 | typedef struct { |
| 68 | grpc_client_setup_request *cs_request; |
| 69 | setup *setup; |
| 70 | /* Resolved addresses, or null if resolution not yet completed. */ |
| 71 | grpc_resolved_addresses *resolved; |
| 72 | /* which address in resolved should we pick for the next connection attempt */ |
| 73 | size_t resolved_index; |
| 74 | } request; |
| 75 | |
| 76 | struct setup { |
| 77 | grpc_channel_security_context *security_context; |
| 78 | const char *target; |
| 79 | grpc_transport_setup_callback setup_callback; |
| 80 | void *setup_user_data; |
| 81 | grpc_em *em; |
| 82 | }; |
| 83 | |
| 84 | static int maybe_try_next_resolved(request *r); |
| 85 | |
| 86 | static void done(request *r, int was_successful) { |
| 87 | grpc_client_setup_request_finish(r->cs_request, was_successful); |
| 88 | if (r->resolved) { |
| 89 | grpc_resolved_addresses_destroy(r->resolved); |
| 90 | } |
| 91 | gpr_free(r); |
| 92 | } |
| 93 | |
| 94 | static void on_secure_transport_setup_done(void *rp, |
| 95 | grpc_security_status status, |
| 96 | grpc_endpoint *secure_endpoint) { |
| 97 | request *r = rp; |
| 98 | if (status != GRPC_SECURITY_OK) { |
| 99 | gpr_log(GPR_ERROR, "Secure transport setup failed with error %d.", status); |
| 100 | done(r, 0); |
| 101 | } else { |
| 102 | grpc_create_chttp2_transport( |
| 103 | r->setup->setup_callback, r->setup->setup_user_data, |
| 104 | grpc_client_setup_get_channel_args(r->cs_request), secure_endpoint, |
| 105 | NULL, 0, grpc_client_setup_get_mdctx(r->cs_request), 1); |
| 106 | done(r, 1); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /* connection callback: tcp is either valid, or null on error */ |
| 111 | static void on_connect(void *rp, grpc_endpoint *tcp) { |
| 112 | request *r = rp; |
| 113 | |
| 114 | if (!grpc_client_setup_request_should_continue(r->cs_request)) { |
| 115 | if (tcp) { |
| 116 | grpc_endpoint_shutdown(tcp); |
| 117 | grpc_endpoint_destroy(tcp); |
| 118 | } |
| 119 | done(r, 0); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if (!tcp) { |
| 124 | if (!maybe_try_next_resolved(r)) { |
| 125 | done(r, 0); |
| 126 | return; |
| 127 | } else { |
| 128 | return; |
| 129 | } |
| 130 | } else { |
| 131 | grpc_setup_secure_transport(&r->setup->security_context->base, tcp, |
| 132 | on_secure_transport_setup_done, r); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* attempt to connect to the next available resolved address */ |
| 137 | static int maybe_try_next_resolved(request *r) { |
| 138 | grpc_resolved_address *addr; |
| 139 | if (!r->resolved) return 0; |
| 140 | if (r->resolved_index == r->resolved->naddrs) return 0; |
| 141 | addr = &r->resolved->addrs[r->resolved_index++]; |
| 142 | grpc_tcp_client_connect(on_connect, r, r->setup->em, |
| 143 | (struct sockaddr *)&addr->addr, addr->len, |
| 144 | grpc_client_setup_request_deadline(r->cs_request)); |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | /* callback for when our target address has been resolved */ |
| 149 | static void on_resolved(void *rp, grpc_resolved_addresses *resolved) { |
| 150 | request *r = rp; |
| 151 | |
| 152 | /* if we're not still the active request, abort */ |
| 153 | if (!grpc_client_setup_request_should_continue(r->cs_request)) { |
| 154 | if (resolved) { |
| 155 | grpc_resolved_addresses_destroy(resolved); |
| 156 | } |
| 157 | done(r, 0); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | if (!resolved) { |
| 162 | done(r, 0); |
| 163 | return; |
| 164 | } else { |
| 165 | r->resolved = resolved; |
| 166 | r->resolved_index = 0; |
| 167 | if (!maybe_try_next_resolved(r)) { |
| 168 | done(r, 0); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static void initiate_setup(void *sp, grpc_client_setup_request *cs_request) { |
| 174 | request *r = gpr_malloc(sizeof(request)); |
| 175 | r->setup = sp; |
| 176 | r->cs_request = cs_request; |
| 177 | r->resolved = NULL; |
| 178 | r->resolved_index = 0; |
| 179 | /* TODO(klempner): Make grpc_resolve_address respect deadline */ |
| 180 | grpc_resolve_address(r->setup->target, "https", on_resolved, r); |
| 181 | } |
| 182 | |
| 183 | static void done_setup(void *sp) { |
| 184 | setup *s = sp; |
| 185 | gpr_free((void *)s->target); |
| 186 | grpc_security_context_unref(&s->security_context->base); |
| 187 | gpr_free(s); |
| 188 | } |
| 189 | |
| 190 | static grpc_transport_setup_result complete_setup(void *channel_stack, |
| 191 | grpc_transport *transport, |
| 192 | grpc_mdctx *mdctx) { |
| 193 | static grpc_channel_filter const *extra_filters[] = {&grpc_http_client_filter, |
| 194 | &grpc_http_filter}; |
| 195 | return grpc_client_channel_transport_setup_complete( |
| 196 | channel_stack, transport, extra_filters, GPR_ARRAY_SIZE(extra_filters), |
| 197 | mdctx); |
| 198 | } |
| 199 | |
| 200 | /* Create a secure client channel: |
| 201 | Asynchronously: - resolve target |
| 202 | - connect to it (trying alternatives as presented) |
| 203 | - perform handshakes */ |
| 204 | grpc_channel *grpc_secure_channel_create_internal( |
| 205 | const char *target, const grpc_channel_args *args, |
| 206 | grpc_channel_security_context *context) { |
| 207 | setup *s; |
| 208 | grpc_channel *channel; |
| 209 | grpc_arg context_arg; |
| 210 | grpc_channel_args *args_copy; |
| 211 | grpc_mdctx *mdctx = grpc_mdctx_create(); |
| 212 | #define MAX_FILTERS 4 |
| 213 | const grpc_channel_filter *filters[MAX_FILTERS]; |
| 214 | int n = 0; |
| 215 | if (grpc_find_security_context_in_args(args) != NULL) { |
| 216 | gpr_log(GPR_ERROR, "Cannot set security context in channel args."); |
| 217 | } |
| 218 | |
| 219 | s = gpr_malloc(sizeof(setup)); |
| 220 | context_arg = grpc_security_context_to_arg(&context->base); |
| 221 | args_copy = grpc_channel_args_copy_and_add(args, &context_arg); |
| 222 | filters[n++] = &grpc_client_surface_filter; |
| 223 | if (grpc_channel_args_is_census_enabled(args)) { |
| 224 | filters[n++] = &grpc_client_census_filter; |
| 225 | } |
| 226 | filters[n++] = &grpc_client_auth_filter; |
| 227 | filters[n++] = &grpc_client_channel_filter; |
| 228 | GPR_ASSERT(n <= MAX_FILTERS); |
| 229 | channel = grpc_channel_create_from_filters(filters, n, args_copy, mdctx, 1); |
| 230 | grpc_channel_args_destroy(args_copy); |
| 231 | |
| 232 | s->target = gpr_strdup(target); |
| 233 | s->em = grpc_surface_em(); |
| 234 | s->setup_callback = complete_setup; |
| 235 | s->setup_user_data = grpc_channel_get_channel_stack(channel); |
| 236 | s->security_context = |
| 237 | (grpc_channel_security_context *)grpc_security_context_ref( |
| 238 | &context->base); |
| 239 | grpc_client_setup_create_and_attach(grpc_channel_get_channel_stack(channel), |
| 240 | args, mdctx, initiate_setup, done_setup, |
| 241 | s, s->em); |
| 242 | return channel; |
| 243 | } |