|
| 1 | +/** |
| 2 | + * Copyright Soramitsu Co., Ltd. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +#ifndef LIBP2P_INCLUDE_LIBP2P_NETWORK_CARES_CARES_HPP |
| 7 | +#define LIBP2P_INCLUDE_LIBP2P_NETWORK_CARES_CARES_HPP |
| 8 | + |
| 9 | +#include <atomic> |
| 10 | +#include <functional> |
| 11 | +#include <list> |
| 12 | +#include <map> |
| 13 | +#include <memory> |
| 14 | +#include <string> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +#include <ares.h> |
| 18 | +#include <arpa/nameser.h> |
| 19 | +#include <sys/select.h> |
| 20 | +#include <boost/asio.hpp> |
| 21 | +#include <boost/optional.hpp> |
| 22 | +#include <libp2p/common/logger.hpp> |
| 23 | +#include <libp2p/outcome/outcome.hpp> |
| 24 | + |
| 25 | +namespace libp2p::network::c_ares { |
| 26 | + |
| 27 | + /** |
| 28 | + * |
| 29 | + * Only one instance is allowed to exist. |
| 30 | + * Has to be initialized prior any threads spawn. |
| 31 | + * Designed for use only via Boost injector passing by a reference. |
| 32 | + */ |
| 33 | + class Ares final { |
| 34 | + public: |
| 35 | + using TxtCallback = |
| 36 | + std::function<void(outcome::result<std::vector<std::string>>)>; |
| 37 | + |
| 38 | + enum class Error { |
| 39 | + NOT_INITIALIZED = 1, |
| 40 | + CHANNEL_INIT_FAILURE, |
| 41 | + THREAD_FAILED, |
| 42 | + // the following are the codes returned to callback by ::ares_query |
| 43 | + E_NO_DATA, |
| 44 | + E_BAD_QUERY, |
| 45 | + E_SERVER_FAIL, |
| 46 | + E_NOT_FOUND, |
| 47 | + E_SERVER_NOTIMP, |
| 48 | + E_REFUSED, |
| 49 | + E_BAD_NAME, |
| 50 | + E_QUERY_TIMEOUT, |
| 51 | + E_NS_CONN_REFUSED, |
| 52 | + E_NO_MEM, |
| 53 | + E_CANCELLED, |
| 54 | + E_CHANNEL_DESTROYED, |
| 55 | + E_BAD_RESPONSE, |
| 56 | + }; |
| 57 | + |
| 58 | + static inline const std::map<int, Ares::Error> kQueryErrors = { |
| 59 | + {ARES_ENODATA, Error::E_NO_DATA}, |
| 60 | + {ARES_EFORMERR, Error::E_BAD_QUERY}, |
| 61 | + {ARES_ESERVFAIL, Error::E_SERVER_FAIL}, |
| 62 | + {ARES_ENOTFOUND, Error::E_NOT_FOUND}, |
| 63 | + {ARES_ENOTIMP, Error::E_SERVER_NOTIMP}, |
| 64 | + {ARES_EREFUSED, Error::E_REFUSED}, |
| 65 | + {ARES_EBADNAME, Error::E_BAD_NAME}, |
| 66 | + {ARES_ETIMEOUT, Error::E_QUERY_TIMEOUT}, |
| 67 | + {ARES_ECONNREFUSED, Error::E_NS_CONN_REFUSED}, |
| 68 | + {ARES_ENOMEM, Error::E_NO_MEM}, |
| 69 | + {ARES_ECANCELLED, Error::E_CANCELLED}, |
| 70 | + {ARES_EDESTRUCTION, Error::E_CHANNEL_DESTROYED}, |
| 71 | + {ARES_EBADRESP, Error::E_BAD_RESPONSE}, |
| 72 | + }; |
| 73 | + |
| 74 | + Ares(); |
| 75 | + ~Ares(); |
| 76 | + |
| 77 | + // make it non-copyable |
| 78 | + Ares(const Ares &) = delete; |
| 79 | + Ares(Ares &&) = delete; |
| 80 | + void operator=(const Ares &) = delete; |
| 81 | + void operator=(Ares &&) = delete; |
| 82 | + |
| 83 | + static void resolveTxt( |
| 84 | + const std::string &uri, |
| 85 | + const std::weak_ptr<boost::asio::io_context> &io_context, |
| 86 | + TxtCallback callback); |
| 87 | + |
| 88 | + private: |
| 89 | + struct RequestContext { |
| 90 | + std::weak_ptr<boost::asio::io_context> io_context; |
| 91 | + std::string uri; |
| 92 | + TxtCallback callback; |
| 93 | + |
| 94 | + RequestContext(std::weak_ptr<boost::asio::io_context> io_context_, |
| 95 | + std::string uri_, TxtCallback callback_) |
| 96 | + : io_context{std::move(io_context_)}, |
| 97 | + uri{std::move(uri_)}, |
| 98 | + callback{std::move(callback_)} {} |
| 99 | + }; |
| 100 | + |
| 101 | + /// schedules to user's io_context the call of callback with specified error |
| 102 | + static void reportError( |
| 103 | + const std::weak_ptr<boost::asio::io_context> &io_context, |
| 104 | + TxtCallback callback, Error error); |
| 105 | + |
| 106 | + static void txtCallback(void *arg, int status, int timeouts, |
| 107 | + unsigned char *abuf, int alen); |
| 108 | + |
| 109 | + /// does ares sockets processing for the channel, to be in a separate thread |
| 110 | + static void waitAresChannel(::ares_channel channel); |
| 111 | + |
| 112 | + static void removeRequest(RequestContext *request_ptr); |
| 113 | + |
| 114 | + static std::atomic_bool initialized_; |
| 115 | + static std::list<std::shared_ptr<RequestContext>> requests_; |
| 116 | + static common::Logger log_; |
| 117 | + }; |
| 118 | + |
| 119 | +} // namespace libp2p::network::c_ares |
| 120 | + |
| 121 | +OUTCOME_HPP_DECLARE_ERROR(libp2p::network::c_ares, Ares::Error); |
| 122 | + |
| 123 | +#endif // LIBP2P_INCLUDE_LIBP2P_NETWORK_CARES_CARES_HPP |
0 commit comments