21
21
#include < algorithm>
22
22
#include < vector>
23
23
24
- #include " absl/strings/string_view.h"
25
-
26
24
#include " src/core/lib/iomgr/sockaddr.h"
27
25
28
26
// IWYU pragma: no_include <arpa/nameser.h>
@@ -600,10 +598,12 @@ static void grpc_ares_request_unref_locked(grpc_ares_request* r)
600
598
601
599
void grpc_ares_complete_request_locked (grpc_ares_request* r)
602
600
ABSL_EXCLUSIVE_LOCKS_REQUIRED(r->mu) {
603
- // Invoke on_done callback and destroy the request
601
+ /* Invoke on_done callback and destroy the
602
+ request */
604
603
r->ev_driver = nullptr ;
605
- if (r->addresses_out != nullptr && *r->addresses_out != nullptr ) {
606
- grpc_cares_wrapper_address_sorting_sort (r, r->addresses_out ->get ());
604
+ ServerAddressList* addresses = r->addresses_out ->get ();
605
+ if (addresses != nullptr ) {
606
+ grpc_cares_wrapper_address_sorting_sort (r, addresses);
607
607
GRPC_ERROR_UNREF (r->error );
608
608
r->error = GRPC_ERROR_NONE;
609
609
// TODO(apolcyn): allow c-ares to return a service config
@@ -816,7 +816,6 @@ static void on_txt_done_locked(void* arg, int status, int /*timeouts*/,
816
816
}
817
817
// Clean up.
818
818
ares_free_data (reply);
819
- grpc_ares_request_unref_locked (r);
820
819
return ;
821
820
fail:
822
821
std::string error_msg =
@@ -828,22 +827,46 @@ static void on_txt_done_locked(void* arg, int status, int /*timeouts*/,
828
827
r->error = grpc_error_add_child (error, r->error );
829
828
}
830
829
831
- grpc_error_handle set_request_dns_server (grpc_ares_request* r,
832
- absl::string_view dns_server)
833
- ABSL_EXCLUSIVE_LOCKS_REQUIRED(r->mu) {
834
- if (!dns_server.empty ()) {
835
- GRPC_CARES_TRACE_LOG (" request:%p Using DNS server %s" , r,
836
- dns_server.data ());
830
+ void grpc_dns_lookup_ares_continue_after_check_localhost_and_ip_literals_locked (
831
+ grpc_ares_request* r, const char * dns_server, const char * name,
832
+ const char * default_port, grpc_pollset_set* interested_parties,
833
+ int query_timeout_ms) ABSL_EXCLUSIVE_LOCKS_REQUIRED(r->mu) {
834
+ grpc_error_handle error = GRPC_ERROR_NONE;
835
+ grpc_ares_hostbyname_request* hr = nullptr ;
836
+ /* parse name, splitting it into host and port parts */
837
+ std::string host;
838
+ std::string port;
839
+ grpc_core::SplitHostPort (name, &host, &port);
840
+ if (host.empty ()) {
841
+ error = grpc_error_set_str (
842
+ GRPC_ERROR_CREATE_FROM_STATIC_STRING (" unparseable host:port" ),
843
+ GRPC_ERROR_STR_TARGET_ADDRESS, name);
844
+ goto error_cleanup;
845
+ } else if (port.empty ()) {
846
+ if (default_port == nullptr || strlen (default_port) == 0 ) {
847
+ error = grpc_error_set_str (
848
+ GRPC_ERROR_CREATE_FROM_STATIC_STRING (" no port in name" ),
849
+ GRPC_ERROR_STR_TARGET_ADDRESS, name);
850
+ goto error_cleanup;
851
+ }
852
+ port = default_port;
853
+ }
854
+ error = grpc_ares_ev_driver_create_locked (&r->ev_driver , interested_parties,
855
+ query_timeout_ms, r);
856
+ if (!GRPC_ERROR_IS_NONE (error)) goto error_cleanup;
857
+ // If dns_server is specified, use it.
858
+ if (dns_server != nullptr && dns_server[0 ] != ' \0 ' ) {
859
+ GRPC_CARES_TRACE_LOG (" request:%p Using DNS server %s" , r, dns_server);
837
860
grpc_resolved_address addr;
838
- if (grpc_parse_ipv4_hostport (dns_server, &addr, /* log_errors= */ false )) {
861
+ if (grpc_parse_ipv4_hostport (dns_server, &addr, false /* log_errors */ )) {
839
862
r->dns_server_addr .family = AF_INET;
840
863
struct sockaddr_in * in = reinterpret_cast <struct sockaddr_in *>(addr.addr );
841
864
memcpy (&r->dns_server_addr .addr .addr4 , &in->sin_addr ,
842
865
sizeof (struct in_addr ));
843
866
r->dns_server_addr .tcp_port = grpc_sockaddr_get_port (&addr);
844
867
r->dns_server_addr .udp_port = grpc_sockaddr_get_port (&addr);
845
868
} else if (grpc_parse_ipv6_hostport (dns_server, &addr,
846
- /* log_errors= */ false )) {
869
+ false /* log_errors */ )) {
847
870
r->dns_server_addr .family = AF_INET6;
848
871
struct sockaddr_in6 * in6 =
849
872
reinterpret_cast <struct sockaddr_in6 *>(addr.addr );
@@ -852,49 +875,51 @@ grpc_error_handle set_request_dns_server(grpc_ares_request* r,
852
875
r->dns_server_addr .tcp_port = grpc_sockaddr_get_port (&addr);
853
876
r->dns_server_addr .udp_port = grpc_sockaddr_get_port (&addr);
854
877
} else {
855
- return GRPC_ERROR_CREATE_FROM_CPP_STRING (
856
- absl::StrCat (" cannot parse authority " , dns_server));
878
+ error = grpc_error_set_str (
879
+ GRPC_ERROR_CREATE_FROM_STATIC_STRING (" cannot parse authority" ),
880
+ GRPC_ERROR_STR_TARGET_ADDRESS, name);
881
+ goto error_cleanup;
857
882
}
858
883
int status =
859
884
ares_set_servers_ports (r->ev_driver ->channel , &r->dns_server_addr );
860
885
if (status != ARES_SUCCESS) {
861
- return GRPC_ERROR_CREATE_FROM_CPP_STRING (absl::StrCat (
886
+ error = GRPC_ERROR_CREATE_FROM_CPP_STRING (absl::StrCat (
862
887
" C-ares status is not ARES_SUCCESS: " , ares_strerror (status)));
888
+ goto error_cleanup;
863
889
}
864
890
}
865
- return GRPC_ERROR_NONE;
866
- }
867
-
868
- // Common logic for all lookup methods.
869
- // If an error occurs, callers must run the client callback.
870
- grpc_error_handle grpc_dns_lookup_ares_continued (
871
- grpc_ares_request* r, const char * dns_server, const char * name,
872
- const char * default_port, grpc_pollset_set* interested_parties,
873
- int query_timeout_ms, std::string* host, std::string* port, bool check_port)
874
- ABSL_EXCLUSIVE_LOCKS_REQUIRED(r->mu) {
875
- grpc_error_handle error = GRPC_ERROR_NONE;
876
- /* parse name, splitting it into host and port parts */
877
- grpc_core::SplitHostPort (name, host, port);
878
- if (host->empty ()) {
879
- error = grpc_error_set_str (
880
- GRPC_ERROR_CREATE_FROM_STATIC_STRING (" unparseable host:port" ),
881
- GRPC_ERROR_STR_TARGET_ADDRESS, name);
882
- return error;
883
- } else if (check_port && port->empty ()) {
884
- if (default_port == nullptr || strlen (default_port) == 0 ) {
885
- error = grpc_error_set_str (
886
- GRPC_ERROR_CREATE_FROM_STATIC_STRING (" no port in name" ),
887
- GRPC_ERROR_STR_TARGET_ADDRESS, name);
888
- return error;
889
- }
890
- *port = default_port;
891
+ r->pending_queries = 1 ;
892
+ if (grpc_ares_query_ipv6 ()) {
893
+ hr = create_hostbyname_request_locked (r, host.c_str (),
894
+ grpc_strhtons (port.c_str ()),
895
+ /* is_balancer=*/ false , " AAAA" );
896
+ ares_gethostbyname (r->ev_driver ->channel , hr->host , AF_INET6,
897
+ on_hostbyname_done_locked, hr);
891
898
}
892
- error = grpc_ares_ev_driver_create_locked (&r->ev_driver , interested_parties,
893
- query_timeout_ms, r);
894
- if (!GRPC_ERROR_IS_NONE (error)) return error;
895
- // If dns_server is specified, use it.
896
- error = set_request_dns_server (r, dns_server);
897
- return error;
899
+ hr = create_hostbyname_request_locked (r, host.c_str (),
900
+ grpc_strhtons (port.c_str ()),
901
+ /* is_balancer=*/ false , " A" );
902
+ ares_gethostbyname (r->ev_driver ->channel , hr->host , AF_INET,
903
+ on_hostbyname_done_locked, hr);
904
+ if (r->balancer_addresses_out != nullptr ) {
905
+ /* Query the SRV record */
906
+ std::string service_name = absl::StrCat (" _grpclb._tcp." , host);
907
+ GrpcAresQuery* srv_query = new GrpcAresQuery (r, service_name);
908
+ ares_query (r->ev_driver ->channel , service_name.c_str (), ns_c_in, ns_t_srv,
909
+ on_srv_query_done_locked, srv_query);
910
+ }
911
+ if (r->service_config_json_out != nullptr ) {
912
+ std::string config_name = absl::StrCat (" _grpc_config." , host);
913
+ GrpcAresQuery* txt_query = new GrpcAresQuery (r, config_name);
914
+ ares_search (r->ev_driver ->channel , config_name.c_str (), ns_c_in, ns_t_txt,
915
+ on_txt_done_locked, txt_query);
916
+ }
917
+ grpc_ares_ev_driver_start_locked (r->ev_driver );
918
+ grpc_ares_request_unref_locked (r);
919
+ return ;
920
+
921
+ error_cleanup:
922
+ grpc_core::ExecCtx::Run (DEBUG_LOCATION, r->on_done , error);
898
923
}
899
924
900
925
static bool inner_resolve_as_ip_literal_locked (
@@ -1026,18 +1051,21 @@ static bool grpc_ares_maybe_resolve_localhost_manually_locked(
1026
1051
}
1027
1052
#endif /* GRPC_ARES_RESOLVE_LOCALHOST_MANUALLY */
1028
1053
1029
- static grpc_ares_request* grpc_dns_lookup_hostname_ares_impl (
1054
+ static grpc_ares_request* grpc_dns_lookup_ares_impl (
1030
1055
const char * dns_server, const char * name, const char * default_port,
1031
1056
grpc_pollset_set* interested_parties, grpc_closure* on_done,
1032
1057
std::unique_ptr<grpc_core::ServerAddressList>* addrs,
1033
- int query_timeout_ms) {
1058
+ std::unique_ptr<grpc_core::ServerAddressList>* balancer_addrs,
1059
+ char ** service_config_json, int query_timeout_ms) {
1034
1060
grpc_ares_request* r = new grpc_ares_request ();
1035
1061
grpc_core::MutexLock lock (&r->mu );
1036
1062
r->ev_driver = nullptr ;
1037
1063
r->on_done = on_done;
1038
1064
r->addresses_out = addrs;
1065
+ r->balancer_addresses_out = balancer_addrs;
1066
+ r->service_config_json_out = service_config_json;
1039
1067
GRPC_CARES_TRACE_LOG (
1040
- " request:%p c-ares grpc_dns_lookup_hostname_ares_impl name=%s, "
1068
+ " request:%p c-ares grpc_dns_lookup_ares_impl name=%s, "
1041
1069
" default_port=%s" ,
1042
1070
r, name, default_port);
1043
1071
// Early out if the target is an ipv4 or ipv6 literal.
@@ -1051,129 +1079,26 @@ static grpc_ares_request* grpc_dns_lookup_hostname_ares_impl(
1051
1079
grpc_ares_complete_request_locked (r);
1052
1080
return r;
1053
1081
}
1054
- // Look up name using c-ares lib.
1055
- std::string host;
1056
- std::string port;
1057
- grpc_error_handle error = grpc_dns_lookup_ares_continued (
1058
- r, dns_server, name, default_port, interested_parties, query_timeout_ms,
1059
- &host, &port, true );
1060
- if (!GRPC_ERROR_IS_NONE (error)) {
1061
- grpc_core::ExecCtx::Run (DEBUG_LOCATION, r->on_done , error);
1062
- return r;
1063
- }
1064
- r->pending_queries = 1 ;
1065
- grpc_ares_hostbyname_request* hr = nullptr ;
1066
- if (grpc_ares_query_ipv6 ()) {
1067
- hr = create_hostbyname_request_locked (r, host.c_str (),
1068
- grpc_strhtons (port.c_str ()),
1069
- /* is_balancer=*/ false , " AAAA" );
1070
- ares_gethostbyname (r->ev_driver ->channel , hr->host , AF_INET6,
1071
- on_hostbyname_done_locked, hr);
1072
- }
1073
- hr = create_hostbyname_request_locked (r, host.c_str (),
1074
- grpc_strhtons (port.c_str ()),
1075
- /* is_balancer=*/ false , " A" );
1076
- ares_gethostbyname (r->ev_driver ->channel , hr->host , AF_INET,
1077
- on_hostbyname_done_locked, hr);
1078
- grpc_ares_ev_driver_start_locked (r->ev_driver );
1079
- grpc_ares_request_unref_locked (r);
1080
- return r;
1081
- }
1082
-
1083
- grpc_ares_request* grpc_dns_lookup_srv_ares_impl (
1084
- const char * dns_server, const char * name,
1085
- grpc_pollset_set* interested_parties, grpc_closure* on_done,
1086
- std::unique_ptr<grpc_core::ServerAddressList>* balancer_addresses,
1087
- int query_timeout_ms) {
1088
- grpc_ares_request* r = new grpc_ares_request ();
1089
- grpc_core::MutexLock lock (&r->mu );
1090
- r->ev_driver = nullptr ;
1091
- r->on_done = on_done;
1092
- r->balancer_addresses_out = balancer_addresses;
1093
- GRPC_CARES_TRACE_LOG (
1094
- " request:%p c-ares grpc_dns_lookup_srv_ares_impl name=%s" , r, name);
1095
- grpc_error_handle error = GRPC_ERROR_NONE;
1096
- // Don't query for SRV records if the target is "localhost"
1097
- if (target_matches_localhost (name)) {
1098
- grpc_core::ExecCtx::Run (DEBUG_LOCATION, r->on_done , error);
1099
- return r;
1100
- }
1101
- // Look up name using c-ares lib.
1102
- std::string host;
1103
- std::string port;
1104
- error = grpc_dns_lookup_ares_continued (r, dns_server, name, nullptr ,
1105
- interested_parties, query_timeout_ms,
1106
- &host, &port, false );
1107
- if (!GRPC_ERROR_IS_NONE (error)) {
1108
- grpc_core::ExecCtx::Run (DEBUG_LOCATION, r->on_done , error);
1109
- return r;
1110
- }
1111
- r->pending_queries = 1 ;
1112
- /* Query the SRV record */
1113
- std::string service_name = absl::StrCat (" _grpclb._tcp." , host);
1114
- GrpcAresQuery* srv_query = new GrpcAresQuery (r, service_name);
1115
- ares_query (r->ev_driver ->channel , service_name.c_str (), ns_c_in, ns_t_srv,
1116
- on_srv_query_done_locked, srv_query);
1117
- grpc_ares_ev_driver_start_locked (r->ev_driver );
1118
- grpc_ares_request_unref_locked (r);
1119
- return r;
1120
- }
1121
-
1122
- grpc_ares_request* grpc_dns_lookup_txt_ares_impl (
1123
- const char * dns_server, const char * name,
1124
- grpc_pollset_set* interested_parties, grpc_closure* on_done,
1125
- char ** service_config_json, int query_timeout_ms) {
1126
- grpc_ares_request* r = new grpc_ares_request ();
1127
- grpc_core::MutexLock lock (&r->mu );
1128
- r->ev_driver = nullptr ;
1129
- r->on_done = on_done;
1130
- r->service_config_json_out = service_config_json;
1131
- GRPC_CARES_TRACE_LOG (
1132
- " request:%p c-ares grpc_dns_lookup_txt_ares_impl name=%s" , r, name);
1133
- grpc_error_handle error = GRPC_ERROR_NONE;
1134
- // Don't query for TXT records if the target is "localhost"
1082
+ // Don't query for SRV and TXT records if the target is "localhost", so
1083
+ // as to cut down on lookups over the network, especially in tests:
1084
+ // https://github.com/grpc/proposal/pull/79
1135
1085
if (target_matches_localhost (name)) {
1136
- grpc_core::ExecCtx::Run (DEBUG_LOCATION, r->on_done , error) ;
1137
- return r ;
1086
+ r->balancer_addresses_out = nullptr ;
1087
+ r-> service_config_json_out = nullptr ;
1138
1088
}
1139
1089
// Look up name using c-ares lib.
1140
- std::string host;
1141
- std::string port;
1142
- error = grpc_dns_lookup_ares_continued (r, dns_server, name, nullptr ,
1143
- interested_parties, query_timeout_ms,
1144
- &host, &port, false );
1145
- if (!GRPC_ERROR_IS_NONE (error)) {
1146
- grpc_core::ExecCtx::Run (DEBUG_LOCATION, r->on_done , error);
1147
- return r;
1148
- }
1149
- r->pending_queries = 1 ;
1150
- /* Query the TXT record */
1151
- std::string config_name = absl::StrCat (" _grpc_config." , host);
1152
- GrpcAresQuery* txt_query = new GrpcAresQuery (r, config_name);
1153
- ares_search (r->ev_driver ->channel , config_name.c_str (), ns_c_in, ns_t_txt,
1154
- on_txt_done_locked, txt_query);
1155
- grpc_ares_ev_driver_start_locked (r->ev_driver );
1156
- grpc_ares_request_unref_locked (r);
1090
+ grpc_dns_lookup_ares_continue_after_check_localhost_and_ip_literals_locked (
1091
+ r, dns_server, name, default_port, interested_parties, query_timeout_ms);
1157
1092
return r;
1158
1093
}
1159
1094
1160
- grpc_ares_request* (*grpc_dns_lookup_hostname_ares )(
1095
+ grpc_ares_request* (*grpc_dns_lookup_ares )(
1161
1096
const char * dns_server, const char * name, const char * default_port,
1162
1097
grpc_pollset_set* interested_parties, grpc_closure* on_done,
1163
1098
std::unique_ptr<grpc_core::ServerAddressList>* addrs,
1164
- int query_timeout_ms) = grpc_dns_lookup_hostname_ares_impl;
1165
-
1166
- grpc_ares_request* (*grpc_dns_lookup_srv_ares)(
1167
- const char * dns_server, const char * name,
1168
- grpc_pollset_set* interested_parties, grpc_closure* on_done,
1169
- std::unique_ptr<grpc_core::ServerAddressList>* balancer_addresses,
1170
- int query_timeout_ms) = grpc_dns_lookup_srv_ares_impl;
1171
-
1172
- grpc_ares_request* (*grpc_dns_lookup_txt_ares)(
1173
- const char * dns_server, const char * name,
1174
- grpc_pollset_set* interested_parties, grpc_closure* on_done,
1099
+ std::unique_ptr<grpc_core::ServerAddressList>* balancer_addrs,
1175
1100
char ** service_config_json,
1176
- int query_timeout_ms) = grpc_dns_lookup_txt_ares_impl ;
1101
+ int query_timeout_ms) = grpc_dns_lookup_ares_impl ;
1177
1102
1178
1103
static void grpc_cancel_ares_request_impl (grpc_ares_request* r) {
1179
1104
GPR_ASSERT (r != nullptr );
0 commit comments