Address review comments

reviewable/pr11237/r2
Yuchen Zeng 8 years ago
parent 01cc75087e
commit 94c5776143
  1. 162
      src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.c
  2. 2
      src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h

@ -185,44 +185,47 @@ static void on_hostbyname_done_cb(void *arg, int status, int timeouts,
sizeof(grpc_lb_address) * (*lb_addresses)->num_addresses); sizeof(grpc_lb_address) * (*lb_addresses)->num_addresses);
for (i = prev_naddr; i < (*lb_addresses)->num_addresses; i++) { for (i = prev_naddr; i < (*lb_addresses)->num_addresses; i++) {
memset(&(*lb_addresses)->addresses[i], 0, sizeof(grpc_lb_address)); memset(&(*lb_addresses)->addresses[i], 0, sizeof(grpc_lb_address));
if (hostent->h_addrtype == AF_INET6) { switch (hostent->h_addrtype) {
size_t addr_len = sizeof(struct sockaddr_in6); case AF_INET6: {
struct sockaddr_in6 addr; size_t addr_len = sizeof(struct sockaddr_in6);
memcpy(&addr.sin6_addr, hostent->h_addr_list[i - prev_naddr], struct sockaddr_in6 addr;
sizeof(struct in6_addr)); memcpy(&addr.sin6_addr, hostent->h_addr_list[i - prev_naddr],
addr.sin6_family = (sa_family_t)hostent->h_addrtype; sizeof(struct in6_addr));
addr.sin6_port = hr->port; addr.sin6_family = (sa_family_t)hostent->h_addrtype;
grpc_lb_addresses_set_address( addr.sin6_port = hr->port;
*lb_addresses, i, &addr, addr_len, grpc_lb_addresses_set_address(
hr->is_balancer /* is_balancer */, *lb_addresses, i, &addr, addr_len,
hr->is_balancer ? strdup(hr->host) : NULL /* balancer_name */, hr->is_balancer /* is_balancer */,
NULL /* user_data */); hr->is_balancer ? strdup(hr->host) : NULL /* balancer_name */,
NULL /* user_data */);
char output[INET6_ADDRSTRLEN]; char output[INET6_ADDRSTRLEN];
ares_inet_ntop(AF_INET6, &addr.sin6_addr, output, INET6_ADDRSTRLEN); ares_inet_ntop(AF_INET6, &addr.sin6_addr, output, INET6_ADDRSTRLEN);
gpr_log(GPR_DEBUG, gpr_log(GPR_DEBUG,
"c-ares resolver gets a AF_INET6 result: \n" "c-ares resolver gets a AF_INET6 result: \n"
" addr: %s\n port: %d\n sin6_scope_id: %d\n", " addr: %s\n port: %d\n sin6_scope_id: %d\n",
output, ntohs(hr->port), addr.sin6_scope_id); output, ntohs(hr->port), addr.sin6_scope_id);
} else { /* hostent->h_addrtype == AF_INET6 */ break;
size_t addr_len = sizeof(struct sockaddr_in); }
struct sockaddr_in addr; case AF_INET: {
memcpy(&addr.sin_addr, hostent->h_addr_list[i - prev_naddr], size_t addr_len = sizeof(struct sockaddr_in);
sizeof(struct in_addr)); struct sockaddr_in addr;
addr.sin_family = (sa_family_t)hostent->h_addrtype; memcpy(&addr.sin_addr, hostent->h_addr_list[i - prev_naddr],
addr.sin_port = hr->port; sizeof(struct in_addr));
grpc_lb_addresses_set_address( addr.sin_family = (sa_family_t)hostent->h_addrtype;
*lb_addresses, i, &addr, addr_len, addr.sin_port = hr->port;
hr->is_balancer /* is_balancer */, grpc_lb_addresses_set_address(
hr->is_balancer ? strdup(hr->host) : NULL /* balancer_name */, *lb_addresses, i, &addr, addr_len,
NULL /* user_data */); hr->is_balancer /* is_balancer */,
hr->is_balancer ? strdup(hr->host) : NULL /* balancer_name */,
char output[INET_ADDRSTRLEN]; NULL /* user_data */);
ares_inet_ntop(AF_INET, &addr.sin_addr, output, INET_ADDRSTRLEN); char output[INET_ADDRSTRLEN];
gpr_log(GPR_DEBUG, ares_inet_ntop(AF_INET, &addr.sin_addr, output, INET_ADDRSTRLEN);
"c-ares resolver gets a AF_INET result: \n" gpr_log(GPR_DEBUG,
" addr: %s\n port: %d\n", "c-ares resolver gets a AF_INET result: \n"
output, ntohs(hr->port)); " addr: %s\n port: %d\n",
output, ntohs(hr->port));
break;
}
} }
} }
} else { /* r->lb_addrs_out */ } else { /* r->lb_addrs_out */
@ -242,36 +245,40 @@ static void on_hostbyname_done_cb(void *arg, int status, int timeouts,
sizeof(grpc_resolved_address) * (*addresses)->naddrs); sizeof(grpc_resolved_address) * (*addresses)->naddrs);
for (i = prev_naddr; i < (*addresses)->naddrs; i++) { for (i = prev_naddr; i < (*addresses)->naddrs; i++) {
memset(&(*addresses)->addrs[i], 0, sizeof(grpc_resolved_address)); memset(&(*addresses)->addrs[i], 0, sizeof(grpc_resolved_address));
if (hostent->h_addrtype == AF_INET6) { switch (hostent->h_addrtype) {
(*addresses)->addrs[i].len = sizeof(struct sockaddr_in6); case AF_INET6: {
struct sockaddr_in6 *addr = (*addresses)->addrs[i].len = sizeof(struct sockaddr_in6);
(struct sockaddr_in6 *)&(*addresses)->addrs[i].addr; struct sockaddr_in6 *addr =
addr->sin6_family = (sa_family_t)hostent->h_addrtype; (struct sockaddr_in6 *)&(*addresses)->addrs[i].addr;
addr->sin6_port = hr->port; addr->sin6_family = (sa_family_t)hostent->h_addrtype;
addr->sin6_port = hr->port;
char output[INET6_ADDRSTRLEN]; char output[INET6_ADDRSTRLEN];
memcpy(&addr->sin6_addr, hostent->h_addr_list[i - prev_naddr], memcpy(&addr->sin6_addr, hostent->h_addr_list[i - prev_naddr],
sizeof(struct in6_addr)); sizeof(struct in6_addr));
ares_inet_ntop(AF_INET6, &addr->sin6_addr, output, INET6_ADDRSTRLEN); ares_inet_ntop(AF_INET6, &addr->sin6_addr, output,
gpr_log(GPR_DEBUG, INET6_ADDRSTRLEN);
"c-ares resolver gets a AF_INET6 result: \n" gpr_log(GPR_DEBUG,
" addr: %s\n port: %d\n sin6_scope_id: %d\n", "c-ares resolver gets a AF_INET6 result: \n"
output, ntohs(hr->port), addr->sin6_scope_id); " addr: %s\n port: %d\n sin6_scope_id: %d\n",
} else { /* hostent->h_addrtype == AF_INET6 */ output, ntohs(hr->port), addr->sin6_scope_id);
(*addresses)->addrs[i].len = sizeof(struct sockaddr_in); break;
struct sockaddr_in *addr = }
(struct sockaddr_in *)&(*addresses)->addrs[i].addr; case AF_INET: {
memcpy(&addr->sin_addr, hostent->h_addr_list[i - prev_naddr], (*addresses)->addrs[i].len = sizeof(struct sockaddr_in);
sizeof(struct in_addr)); struct sockaddr_in *addr =
addr->sin_family = (sa_family_t)hostent->h_addrtype; (struct sockaddr_in *)&(*addresses)->addrs[i].addr;
addr->sin_port = hr->port; memcpy(&addr->sin_addr, hostent->h_addr_list[i - prev_naddr],
sizeof(struct in_addr));
char output[INET_ADDRSTRLEN]; addr->sin_family = (sa_family_t)hostent->h_addrtype;
ares_inet_ntop(AF_INET, &addr->sin_addr, output, INET_ADDRSTRLEN); addr->sin_port = hr->port;
gpr_log(GPR_DEBUG, char output[INET_ADDRSTRLEN];
"c-ares resolver gets a AF_INET result: \n" ares_inet_ntop(AF_INET, &addr->sin_addr, output, INET_ADDRSTRLEN);
" addr: %s\n port: %d\n", gpr_log(GPR_DEBUG,
output, ntohs(hr->port)); "c-ares resolver gets a AF_INET result: \n"
" addr: %s\n port: %d\n",
output, ntohs(hr->port));
break;
}
} }
} }
} }
@ -317,7 +324,6 @@ static void on_srv_query_done_cb(void *arg, int status, int timeouts,
grpc_ares_ev_driver_start(&exec_ctx, r->ev_driver); grpc_ares_ev_driver_start(&exec_ctx, r->ev_driver);
} }
} }
if (reply != NULL) { if (reply != NULL) {
ares_free_data(reply); ares_free_data(reply);
} }
@ -337,11 +343,11 @@ static void on_srv_query_done_cb(void *arg, int status, int timeouts,
grpc_exec_ctx_finish(&exec_ctx); grpc_exec_ctx_finish(&exec_ctx);
} }
void grpc_dns_lookup_ares( void grpc_dns_lookup_ares(grpc_exec_ctx *exec_ctx, const char *dns_server,
grpc_exec_ctx *exec_ctx, const char *dns_server, const char *name, const char *name, const char *default_port,
const char *default_port, grpc_pollset_set *interested_parties, grpc_pollset_set *interested_parties,
grpc_closure *on_done, grpc_closure *on_done, void **addrs,
void **addrs, bool is_lb_addrs_out) { bool check_grpclb) {
grpc_error *error = GRPC_ERROR_NONE; grpc_error *error = GRPC_ERROR_NONE;
/* TODO(zyc): Enable tracing after #9603 is checked in */ /* TODO(zyc): Enable tracing after #9603 is checked in */
/* if (grpc_dns_trace) { /* if (grpc_dns_trace) {
@ -376,8 +382,8 @@ void grpc_dns_lookup_ares(
gpr_mu_init(&r->mu); gpr_mu_init(&r->mu);
r->ev_driver = ev_driver; r->ev_driver = ev_driver;
r->on_done = on_done; r->on_done = on_done;
r->lb_addrs_out = is_lb_addrs_out; r->lb_addrs_out = check_grpclb;
if (is_lb_addrs_out) { if (check_grpclb) {
r->addrs_out.lb_addrs = (grpc_lb_addresses **)addrs; r->addrs_out.lb_addrs = (grpc_lb_addresses **)addrs;
} else { } else {
r->addrs_out.addrs = (grpc_resolved_addresses **)addrs; r->addrs_out.addrs = (grpc_resolved_addresses **)addrs;
@ -428,7 +434,7 @@ void grpc_dns_lookup_ares(
grpc_ares_hostbyname_request *hr = create_hostbyname_request( grpc_ares_hostbyname_request *hr = create_hostbyname_request(
r, host, strhtons(port), false /* is_balancer */); r, host, strhtons(port), false /* is_balancer */);
ares_gethostbyname(*channel, hr->host, AF_INET, on_hostbyname_done_cb, hr); ares_gethostbyname(*channel, hr->host, AF_INET, on_hostbyname_done_cb, hr);
if (is_lb_addrs_out) { if (check_grpclb) {
/* Query the SRV record */ /* Query the SRV record */
grpc_ares_request_ref(r); grpc_ares_request_ref(r);
char *service_name; char *service_name;
@ -455,7 +461,7 @@ void grpc_resolve_address_ares_impl(grpc_exec_ctx *exec_ctx, const char *name,
grpc_resolved_addresses **addrs) { grpc_resolved_addresses **addrs) {
grpc_dns_lookup_ares(exec_ctx, NULL /* dns_server */, name, default_port, grpc_dns_lookup_ares(exec_ctx, NULL /* dns_server */, name, default_port,
interested_parties, on_done, (void **)addrs, interested_parties, on_done, (void **)addrs,
false /* is_lb_addrs_out */); false /* check_grpclb */);
} }
void (*grpc_resolve_address_ares)( void (*grpc_resolve_address_ares)(

@ -63,7 +63,7 @@ void grpc_dns_lookup_ares(grpc_exec_ctx *exec_ctx, const char *dns_server,
const char *addr, const char *default_port, const char *addr, const char *default_port,
grpc_pollset_set *interested_parties, grpc_pollset_set *interested_parties,
grpc_closure *on_done, void **addresses, grpc_closure *on_done, void **addresses,
bool is_lb_addrs_out); bool check_grpclb);
/* Initialize gRPC ares wrapper. Must be called at least once before /* Initialize gRPC ares wrapper. Must be called at least once before
grpc_resolve_address_ares(). */ grpc_resolve_address_ares(). */

Loading…
Cancel
Save