3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <grpc/support/port_platform.h>
20 #include "src/core/lib/iomgr/port.h"
21 #if GRPC_ARES == 1 && defined(GRPC_POSIX_SOCKET_ARES_EV_DRIVER)
25 #include <sys/ioctl.h>
27 #include "absl/strings/str_cat.h"
29 #include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver.h"
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33 #include <grpc/support/string_util.h>
34 #include <grpc/support/time.h>
35 #include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h"
36 #include "src/core/lib/address_utils/sockaddr_utils.h"
37 #include "src/core/lib/gpr/string.h"
38 #include "src/core/lib/iomgr/ev_posix.h"
39 #include "src/core/lib/iomgr/iomgr_internal.h"
43 class GrpcPolledFdPosix : public GrpcPolledFd {
45 GrpcPolledFdPosix(ares_socket_t as, grpc_pollset_set* driver_pollset_set)
46 : name_(absl::StrCat("c-ares fd: ", static_cast<int>(as))), as_(as) {
47 fd_ = grpc_fd_create(static_cast<int>(as), name_.c_str(), false);
48 driver_pollset_set_ = driver_pollset_set;
49 grpc_pollset_set_add_fd(driver_pollset_set_, fd_);
52 ~GrpcPolledFdPosix() override {
53 grpc_pollset_set_del_fd(driver_pollset_set_, fd_);
54 /* c-ares library will close the fd inside grpc_fd. This fd may be picked up
55 immediately by another thread, and should not be closed by the following
58 grpc_fd_orphan(fd_, nullptr, &phony_release_fd, "c-ares query finished");
61 void RegisterForOnReadableLocked(grpc_closure* read_closure) override {
62 grpc_fd_notify_on_read(fd_, read_closure);
65 void RegisterForOnWriteableLocked(grpc_closure* write_closure) override {
66 grpc_fd_notify_on_write(fd_, write_closure);
69 bool IsFdStillReadableLocked() override {
70 size_t bytes_available = 0;
71 return ioctl(grpc_fd_wrapped_fd(fd_), FIONREAD, &bytes_available) == 0 &&
75 void ShutdownLocked(grpc_error_handle error) override {
76 grpc_fd_shutdown(fd_, error);
79 ares_socket_t GetWrappedAresSocketLocked() override { return as_; }
81 const char* GetName() override { return name_.c_str(); }
87 grpc_pollset_set* driver_pollset_set_;
90 class GrpcPolledFdFactoryPosix : public GrpcPolledFdFactory {
92 GrpcPolledFd* NewGrpcPolledFdLocked(
93 ares_socket_t as, grpc_pollset_set* driver_pollset_set,
94 std::shared_ptr<WorkSerializer> /*work_serializer*/) override {
95 return new GrpcPolledFdPosix(as, driver_pollset_set);
98 void ConfigureAresChannelLocked(ares_channel /*channel*/) override {}
101 std::unique_ptr<GrpcPolledFdFactory> NewGrpcPolledFdFactory(
102 std::shared_ptr<WorkSerializer> work_serializer) {
103 (void)work_serializer;
104 return absl::make_unique<GrpcPolledFdFactoryPosix>();
107 } // namespace grpc_core
109 #endif /* GRPC_ARES == 1 && defined(GRPC_POSIX_SOCKET_ARES_EV_DRIVER) */