mirror of https://github.com/grpc/grpc.git
parent
751e5375b0
commit
41871bf7de
20 changed files with 282 additions and 9 deletions
@ -0,0 +1,134 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2019 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/iomgr/port.h" |
||||
#if GRPC_ARES == 1 && defined(GRPC_UV) |
||||
|
||||
#include <ares.h> |
||||
#include <uv.h> |
||||
|
||||
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h" |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
void ares_uv_poll_cb(uv_poll_t* handle, int status, int events); |
||||
|
||||
void ares_uv_poll_close_cb(uv_handle_t *handle) { |
||||
delete handle; |
||||
} |
||||
|
||||
class GrpcPolledFdLibuv : public GrpcPolledFd { |
||||
public: |
||||
GrpcPolledFdLibuv(ares_socket_t as): as_(as) { |
||||
gpr_asprintf(&name_, "c-ares socket: %" PRIdPTR, (intptr_t)as); |
||||
handle_ = new uv_poll_t; |
||||
uv_poll_init_socket(uv_default_loop(), handle_, as); |
||||
handle_->data = this; |
||||
} |
||||
|
||||
~GrpcPolledFdLibuv() { |
||||
gpr_free(name_); |
||||
} |
||||
|
||||
void RegisterForOnReadableLocked(grpc_closure* read_closure) override { |
||||
GPR_ASSERT(read_closure_ == nullptr); |
||||
GPR_ASSERT((poll_events_ & UV_READABLE) == 0); |
||||
read_closure_ = read_closure; |
||||
poll_events_ |= UV_READABLE; |
||||
uv_poll_start(handle_, poll_events_, ares_uv_poll_cb); |
||||
} |
||||
|
||||
void RegisterForOnWriteableLocked(grpc_closure* write_closure) override { |
||||
GPR_ASSERT(write_closure_ == nullptr); |
||||
GPR_ASSERT((poll_events_ & UV_WRITABLE) == 0); |
||||
write_closure_ = write_closure; |
||||
poll_events_ |= UV_WRITABLE; |
||||
uv_poll_start(handle_, poll_events_, ares_uv_poll_cb); |
||||
} |
||||
|
||||
bool IsFdStillReadableLocked() override { |
||||
/* uv_poll_t is based on poll, which is level triggered. So, if cares
|
||||
* leaves some data unread, the event will trigger again. */ |
||||
return false; |
||||
} |
||||
|
||||
void ShutdownLocked(grpc_error* error) override { |
||||
uv_poll_stop(handle_); |
||||
uv_close(reinterpret_cast<uv_handle_t*>(handle_), ares_uv_poll_close_cb); |
||||
} |
||||
|
||||
ares_socket_t GetWrappedAresSocketLocked() override { return as_; } |
||||
|
||||
const char* GetName() override { return name_; } |
||||
|
||||
char* name_; |
||||
ares_socket_t as_; |
||||
uv_poll_t* handle_; |
||||
grpc_closure* read_closure_ = nullptr; |
||||
grpc_closure* write_closure_ = nullptr; |
||||
int poll_events_ = 0; |
||||
}; |
||||
|
||||
void ares_uv_poll_cb(uv_poll_t* handle, int status, int events) { |
||||
grpc_core::ExecCtx exec_ctx; |
||||
GrpcPolledFdLibuv* polled_fd = reinterpret_cast<GrpcPolledFdLibuv*>(handle->data); |
||||
grpc_error *error = GRPC_ERROR_NONE; |
||||
if (status < 0) { |
||||
error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("cares polling error"); |
||||
error = grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR, |
||||
grpc_slice_from_static_string(uv_strerror(status))); |
||||
} |
||||
if ((events & UV_READABLE) && polled_fd->read_closure_) { |
||||
GRPC_CLOSURE_SCHED(polled_fd->read_closure_, error); |
||||
polled_fd->read_closure_ = nullptr; |
||||
polled_fd->poll_events_ &= ~UV_READABLE; |
||||
} |
||||
if ((events & UV_WRITABLE) && polled_fd->write_closure_) { |
||||
GRPC_CLOSURE_SCHED(polled_fd->write_closure_, error); |
||||
polled_fd->write_closure_ = nullptr; |
||||
polled_fd->poll_events_ &= ~UV_WRITABLE; |
||||
} |
||||
uv_poll_start(handle, polled_fd->poll_events_, ares_uv_poll_cb); |
||||
} |
||||
|
||||
class GrpcPolledFdFactoryLibuv : public GrpcPolledFdFactory { |
||||
public: |
||||
GrpcPolledFd* NewGrpcPolledFdLocked(ares_socket_t as, |
||||
grpc_pollset_set* driver_pollset_set, |
||||
grpc_combiner* combiner) override { |
||||
return New<GrpcPolledFdLibuv>(as); |
||||
} |
||||
|
||||
void ConfigureAresChannelLocked(ares_channel channel) override {} |
||||
}; |
||||
|
||||
UniquePtr<GrpcPolledFdFactory> NewGrpcPolledFdFactory(grpc_combiner* combiner) { |
||||
return UniquePtr<GrpcPolledFdFactory>(New<GrpcPolledFdFactoryLibuv>()); |
||||
} |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif /* GRPC_ARES == 1 && defined(GRPC_UV) */ |
@ -0,0 +1,103 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2016 gRPC authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/iomgr/port.h" |
||||
#if GRPC_ARES == 1 && defined(GRPC_UV) |
||||
|
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/ext/filters/client_channel/parse_address.h" |
||||
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h" |
||||
#include "src/core/ext/filters/client_channel/server_address.h" |
||||
#include "src/core/lib/gpr/host_port.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
|
||||
bool grpc_ares_query_ipv6() {
|
||||
/* The libuv grpc code currently does not have the code to probe for this,
|
||||
* so we assume for nowthat IPv6 is always available in contexts where this |
||||
* code will be used. */ |
||||
return true;
|
||||
} |
||||
|
||||
/* The following two functions were copied entirely from the Windows file. This
|
||||
* code can run on Windows so it can have the same problem. */ |
||||
static bool inner_maybe_resolve_localhost_manually_locked( |
||||
const char* name, const char* default_port, |
||||
grpc_core::UniquePtr<grpc_core::ServerAddressList>* addrs, char** host, |
||||
char** port) { |
||||
gpr_split_host_port(name, host, port); |
||||
if (*host == nullptr) { |
||||
gpr_log(GPR_ERROR, |
||||
"Failed to parse %s into host:port during Windows localhost " |
||||
"resolution check.", |
||||
name); |
||||
return false; |
||||
} |
||||
if (*port == nullptr) { |
||||
if (default_port == nullptr) { |
||||
gpr_log(GPR_ERROR, |
||||
"No port or default port for %s during Windows localhost " |
||||
"resolution check.", |
||||
name); |
||||
return false; |
||||
} |
||||
*port = gpr_strdup(default_port); |
||||
} |
||||
if (gpr_stricmp(*host, "localhost") == 0) { |
||||
GPR_ASSERT(*addrs == nullptr); |
||||
*addrs = grpc_core::MakeUnique<grpc_core::ServerAddressList>(); |
||||
uint16_t numeric_port = grpc_strhtons(*port); |
||||
// Append the ipv6 loopback address.
|
||||
struct sockaddr_in6 ipv6_loopback_addr; |
||||
memset(&ipv6_loopback_addr, 0, sizeof(ipv6_loopback_addr)); |
||||
((char*)&ipv6_loopback_addr.sin6_addr)[15] = 1; |
||||
ipv6_loopback_addr.sin6_family = AF_INET6; |
||||
ipv6_loopback_addr.sin6_port = numeric_port; |
||||
(*addrs)->emplace_back(&ipv6_loopback_addr, sizeof(ipv6_loopback_addr), |
||||
nullptr /* args */); |
||||
// Append the ipv4 loopback address.
|
||||
struct sockaddr_in ipv4_loopback_addr; |
||||
memset(&ipv4_loopback_addr, 0, sizeof(ipv4_loopback_addr)); |
||||
((char*)&ipv4_loopback_addr.sin_addr)[0] = 0x7f; |
||||
((char*)&ipv4_loopback_addr.sin_addr)[3] = 0x01; |
||||
ipv4_loopback_addr.sin_family = AF_INET; |
||||
ipv4_loopback_addr.sin_port = numeric_port; |
||||
(*addrs)->emplace_back(&ipv4_loopback_addr, sizeof(ipv4_loopback_addr), |
||||
nullptr /* args */); |
||||
// Let the address sorter figure out which one should be tried first.
|
||||
grpc_cares_wrapper_address_sorting_sort(addrs->get()); |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
bool grpc_ares_maybe_resolve_localhost_manually_locked( |
||||
const char* name, const char* default_port, |
||||
grpc_core::UniquePtr<grpc_core::ServerAddressList>* addrs) { |
||||
char* host = nullptr; |
||||
char* port = nullptr; |
||||
bool out = inner_maybe_resolve_localhost_manually_locked(name, default_port, |
||||
addrs, &host, &port); |
||||
gpr_free(host); |
||||
gpr_free(port); |
||||
return out; |
||||
} |
||||
|
||||
#endif /* GRPC_ARES == 1 && defined(GRPC_UV) */ |
Loading…
Reference in new issue