280825045654af847f681a5010d060922ffc4373
[platform/upstream/grpc.git] / src / core / ext / filters / client_channel / resolver / dns / c_ares / grpc_ares_wrapper.h
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_DNS_C_ARES_GRPC_ARES_WRAPPER_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_DNS_C_ARES_GRPC_ARES_WRAPPER_H
21
22 #include <grpc/support/port_platform.h>
23
24 #include "src/core/ext/filters/client_channel/server_address.h"
25 #include "src/core/lib/iomgr/iomgr.h"
26 #include "src/core/lib/iomgr/polling_entity.h"
27 #include "src/core/lib/iomgr/resolve_address.h"
28
29 #define GRPC_DNS_ARES_DEFAULT_QUERY_TIMEOUT_MS 10000
30
31 extern grpc_core::TraceFlag grpc_trace_cares_address_sorting;
32
33 extern grpc_core::TraceFlag grpc_trace_cares_resolver;
34
35 #define GRPC_CARES_TRACE_LOG(format, ...)                         \
36   if (grpc_trace_cares_resolver.enabled()) {                      \
37     gpr_log(GPR_DEBUG, "(c-ares resolver) " format, __VA_ARGS__); \
38   }
39
40 typedef struct grpc_ares_request grpc_ares_request;
41
42 /* Asynchronously resolve \a name. Use \a default_port if a port isn't
43    designated in \a name, otherwise use the port in \a name. grpc_ares_init()
44    must be called at least once before this function. \a on_done may be
45    called directly in this function without being scheduled with \a exec_ctx,
46    so it must not try to acquire locks that are being held by the caller. */
47 extern void (*grpc_resolve_address_ares)(const char* name,
48                                          const char* default_port,
49                                          grpc_pollset_set* interested_parties,
50                                          grpc_closure* on_done,
51                                          grpc_resolved_addresses** addresses);
52
53 /* Asynchronously resolve \a name. It will try to resolve grpclb SRV records in
54   addition to the normal address records. For normal address records, it uses
55   \a default_port if a port isn't designated in \a name, otherwise it uses the
56   port in \a name. grpc_ares_init() must be called at least once before this
57   function. \a on_done may be called directly in this function without being
58   scheduled with \a exec_ctx, so it must not try to acquire locks that are
59   being held by the caller. The returned grpc_ares_request object is owned
60   by the caller and it is safe to free after on_done is called back. */
61 extern grpc_ares_request* (*grpc_dns_lookup_ares_locked)(
62     const char* dns_server, const char* name, const char* default_port,
63     grpc_pollset_set* interested_parties, grpc_closure* on_done,
64     grpc_core::UniquePtr<grpc_core::ServerAddressList>* addresses,
65     bool check_grpclb, char** service_config_json, int query_timeout_ms,
66     grpc_combiner* combiner);
67
68 /* Cancel the pending grpc_ares_request \a request */
69 extern void (*grpc_cancel_ares_request_locked)(grpc_ares_request* request);
70
71 /* Initialize gRPC ares wrapper. Must be called at least once before
72    grpc_resolve_address_ares(). */
73 grpc_error* grpc_ares_init(void);
74
75 /* Uninitialized gRPC ares wrapper. If there was more than one previous call to
76    grpc_ares_init(), this function uninitializes the gRPC ares wrapper only if
77    it has been called the same number of times as grpc_ares_init(). */
78 void grpc_ares_cleanup(void);
79
80 /** Schedules the desired callback for request completion
81  * and destroys the grpc_ares_request */
82 void grpc_ares_complete_request_locked(grpc_ares_request* request);
83
84 /* Indicates whether or not AAAA queries should be attempted. */
85 /* E.g., return false if ipv6 is known to not be available. */
86 bool grpc_ares_query_ipv6();
87
88 /* Maybe (depending on the current platform) checks if "name" matches
89  * "localhost" and if so fills in addrs with the correct sockaddr structures.
90  * Returns a bool indicating whether or not such an action was performed.
91  * See https://github.com/grpc/grpc/issues/15158. */
92 bool grpc_ares_maybe_resolve_localhost_manually_locked(
93     const char* name, const char* default_port,
94     grpc_core::UniquePtr<grpc_core::ServerAddressList>* addrs);
95
96 /* Sorts destinations in lb_addrs according to RFC 6724. */
97 void grpc_cares_wrapper_address_sorting_sort(
98     grpc_core::ServerAddressList* addresses);
99
100 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVER_DNS_C_ARES_GRPC_ARES_WRAPPER_H \
101         */