Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, 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 <limits.h> |
Craig Tiller | 0b54163 | 2016-04-05 17:21:05 -0700 | [diff] [blame] | 35 | #include <stdbool.h> |
| 36 | #include <string.h> |
| 37 | |
Craig Tiller | 16c1345 | 2016-04-05 20:17:38 -0700 | [diff] [blame] | 38 | #include <grpc/support/alloc.h> |
| 39 | |
Craig Tiller | 0b54163 | 2016-04-05 17:21:05 -0700 | [diff] [blame] | 40 | #include "src/core/ext/client_config/client_channel.h" |
| 41 | #include "src/core/ext/client_config/lb_policy_registry.h" |
| 42 | #include "src/core/ext/client_config/resolver_registry.h" |
| 43 | #include "src/core/ext/client_config/subchannel_index.h" |
| 44 | #include "src/core/lib/surface/channel_init.h" |
| 45 | |
| 46 | #ifndef GRPC_DEFAULT_NAME_PREFIX |
| 47 | #define GRPC_DEFAULT_NAME_PREFIX "dns:///" |
| 48 | #endif |
| 49 | |
| 50 | static bool append_filter(grpc_channel_stack_builder *builder, void *arg) { |
| 51 | return grpc_channel_stack_builder_append_filter( |
| 52 | builder, (const grpc_channel_filter *)arg, NULL, NULL); |
| 53 | } |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 54 | |
| 55 | static bool set_default_host_if_unset(grpc_channel_stack_builder *builder, |
Craig Tiller | 0b54163 | 2016-04-05 17:21:05 -0700 | [diff] [blame] | 56 | void *unused) { |
| 57 | const grpc_channel_args *args = |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 58 | grpc_channel_stack_builder_get_channel_arguments(builder); |
| 59 | for (size_t i = 0; i < args->num_args; i++) { |
| 60 | if (0 == strcmp(args->args[i].key, GRPC_ARG_DEFAULT_AUTHORITY)) { |
| 61 | return true; |
| 62 | } |
| 63 | } |
| 64 | grpc_arg arg; |
| 65 | arg.type = GRPC_ARG_STRING; |
| 66 | arg.key = GRPC_ARG_DEFAULT_AUTHORITY; |
Craig Tiller | 0b54163 | 2016-04-05 17:21:05 -0700 | [diff] [blame] | 67 | arg.value.string = grpc_get_default_authority( |
| 68 | grpc_channel_stack_builder_get_target(builder)); |
Craig Tiller | 16c1345 | 2016-04-05 20:17:38 -0700 | [diff] [blame] | 69 | grpc_channel_args *new_args = grpc_channel_args_copy_and_add(args, &arg, 1); |
| 70 | grpc_channel_stack_builder_set_channel_arguments(builder, new_args); |
| 71 | gpr_free(arg.value.string); |
| 72 | grpc_channel_args_destroy(new_args); |
Craig Tiller | 0b54163 | 2016-04-05 17:21:05 -0700 | [diff] [blame] | 73 | return true; |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void grpc_client_config_init(void) { |
| 77 | grpc_lb_policy_registry_init(); |
| 78 | grpc_resolver_registry_init(GRPC_DEFAULT_NAME_PREFIX); |
| 79 | grpc_subchannel_index_init(); |
| 80 | grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MIN, |
| 81 | set_default_host_if_unset, NULL); |
| 82 | grpc_channel_init_register_stage(GRPC_CLIENT_CHANNEL, INT_MAX, append_filter, |
| 83 | (void *)&grpc_client_channel_filter); |
| 84 | } |
| 85 | |
| 86 | void grpc_client_config_shutdown(void) { |
| 87 | grpc_subchannel_index_shutdown(); |
| 88 | grpc_channel_init_shutdown(); |
| 89 | grpc_resolver_registry_shutdown(); |
| 90 | grpc_lb_policy_registry_shutdown(); |
| 91 | } |