blob: 6cd8a2ffd248963ded130c323960f7aa4b28f6dc [file] [log] [blame]
Craig Tillerf82ddc42016-04-05 17:15:07 -07001/*
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 Tiller0b541632016-04-05 17:21:05 -070035#include <stdbool.h>
36#include <string.h>
37
Craig Tiller16c13452016-04-05 20:17:38 -070038#include <grpc/support/alloc.h>
39
Craig Tiller0b541632016-04-05 17:21:05 -070040#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
50static 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 Tillerf82ddc42016-04-05 17:15:07 -070054
55static bool set_default_host_if_unset(grpc_channel_stack_builder *builder,
Craig Tiller0b541632016-04-05 17:21:05 -070056 void *unused) {
57 const grpc_channel_args *args =
Craig Tillerf82ddc42016-04-05 17:15:07 -070058 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 Tiller0b541632016-04-05 17:21:05 -070067 arg.value.string = grpc_get_default_authority(
68 grpc_channel_stack_builder_get_target(builder));
Craig Tiller16c13452016-04-05 20:17:38 -070069 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 Tiller0b541632016-04-05 17:21:05 -070073 return true;
Craig Tillerf82ddc42016-04-05 17:15:07 -070074}
75
76void 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
86void 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}