EventService : Support async_resolve for subscribers
The http client at bmcweb does not resolve the client's
hostname asynchronously
This commit implements the async_resolve by using systemd resolved.
The async dbus message to resolvd.service is sent when a subscriber
successfully subscribes for events. The method ResolveHostname is
used to resolve the subscriber's hostname
Tested by:
Subscribe for the events at BMC using DMTF event listener
Generate an event and see the same is received at the listener's console
Signed-off-by: Sunitha Harish <sunithaharish04@gmail.com>
Change-Id: I3ab8206ac4764cfa025e94c06407524d6ba220e0
diff --git a/http/http_client.hpp b/http/http_client.hpp
index 1ac890c..992ac2b 100644
--- a/http/http_client.hpp
+++ b/http/http_client.hpp
@@ -14,11 +14,14 @@
// limitations under the License.
*/
#pragma once
+#include <boost/asio/ip/address.hpp>
+#include <boost/asio/ip/basic_endpoint.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/beast/core/flat_buffer.hpp>
#include <boost/beast/core/tcp_stream.hpp>
#include <boost/beast/http/message.hpp>
#include <boost/beast/version.hpp>
+#include <include/async_resolve.hpp>
#include <cstdlib>
#include <functional>
@@ -35,6 +38,8 @@
enum class ConnState
{
initialized,
+ resolveInProgress,
+ resolveFailed,
connectInProgress,
connectFailed,
connected,
@@ -50,12 +55,12 @@
class HttpClient : public std::enable_shared_from_this<HttpClient>
{
private:
+ crow::async_resolve::Resolver resolver;
boost::beast::tcp_stream conn;
boost::asio::steady_timer timer;
boost::beast::flat_buffer buffer;
boost::beast::http::request<boost::beast::http::string_body> req;
boost::beast::http::response<boost::beast::http::string_body> res;
- boost::asio::ip::tcp::resolver::results_type endpoint;
std::vector<std::pair<std::string, std::string>> headers;
std::queue<std::string> requestDataQueue;
ConnState state;
@@ -69,7 +74,36 @@
std::string retryPolicyAction;
bool runningTimer;
- void doConnect()
+ void doResolve()
+ {
+ if (state == ConnState::resolveInProgress)
+ {
+ return;
+ }
+ state = ConnState::resolveInProgress;
+
+ BMCWEB_LOG_DEBUG << "Trying to resolve: " << host << ":" << port;
+
+ auto respHandler =
+ [self(shared_from_this())](
+ const boost::beast::error_code ec,
+ const std::vector<boost::asio::ip::tcp::endpoint>&
+ endpointList) {
+ if (ec)
+ {
+ BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message();
+ self->state = ConnState::resolveFailed;
+ self->checkQueue();
+ return;
+ }
+ BMCWEB_LOG_DEBUG << "Resolved";
+ self->doConnect(endpointList);
+ };
+ resolver.asyncResolve(host, port, std::move(respHandler));
+ }
+
+ void doConnect(
+ const std::vector<boost::asio::ip::tcp::endpoint>& endpointList)
{
if (state == ConnState::connectInProgress)
{
@@ -78,26 +112,25 @@
state = ConnState::connectInProgress;
BMCWEB_LOG_DEBUG << "Trying to connect to: " << host << ":" << port;
- // Set a timeout on the operation
+
conn.expires_after(std::chrono::seconds(30));
+ conn.async_connect(
+ endpointList, [self(shared_from_this())](
+ const boost::beast::error_code ec,
+ const boost::asio::ip::tcp::endpoint& endpoint) {
+ if (ec)
+ {
+ BMCWEB_LOG_ERROR << "Connect " << endpoint
+ << " failed: " << ec.message();
+ self->state = ConnState::connectFailed;
+ self->checkQueue();
+ return;
+ }
+ self->state = ConnState::connected;
+ BMCWEB_LOG_DEBUG << "Connected to: " << endpoint;
- conn.async_connect(endpoint, [self(shared_from_this())](
- const boost::beast::error_code& ec,
- const boost::asio::ip::tcp::resolver::
- results_type::endpoint_type& ep) {
- if (ec)
- {
- BMCWEB_LOG_ERROR << "Connect " << ep
- << " failed: " << ec.message();
- self->state = ConnState::connectFailed;
self->checkQueue();
- return;
- }
- self->state = ConnState::connected;
- BMCWEB_LOG_DEBUG << "Connected to: " << ep;
-
- self->checkQueue();
- });
+ });
}
void sendMessage(const std::string& data)
@@ -274,6 +307,7 @@
{
switch (state)
{
+ case ConnState::resolveInProgress:
case ConnState::connectInProgress:
case ConnState::sendInProgress:
case ConnState::suspended:
@@ -285,10 +319,9 @@
case ConnState::connectFailed:
case ConnState::sendFailed:
case ConnState::recvFailed:
+ case ConnState::resolveFailed:
{
- // After establishing the connection, checkQueue() will
- // get called and it will attempt to send data.
- doConnect();
+ doResolve();
break;
}
case ConnState::connected:
@@ -310,8 +343,6 @@
retryCount(0), maxRetryAttempts(5), retryIntervalSecs(0),
retryPolicyAction("TerminateAfterRetries"), runningTimer(false)
{
- boost::asio::ip::tcp::resolver resolver(ioc);
- endpoint = resolver.resolve(host, port);
state = ConnState::initialized;
}
diff --git a/include/async_resolve.hpp b/include/async_resolve.hpp
new file mode 100644
index 0000000..306a499
--- /dev/null
+++ b/include/async_resolve.hpp
@@ -0,0 +1,99 @@
+#pragma once
+#include <boost/asio/ip/address.hpp>
+#include <boost/asio/ip/basic_endpoint.hpp>
+#include <sdbusplus/message.hpp>
+
+#include <charconv>
+#include <iostream>
+#include <memory>
+
+namespace crow
+{
+
+namespace async_resolve
+{
+
+class Resolver
+{
+ public:
+ Resolver() = default;
+
+ ~Resolver() = default;
+
+ template <typename ResolveHandler>
+ void asyncResolve(const std::string& host, const std::string& port,
+ ResolveHandler&& handler)
+ {
+ BMCWEB_LOG_DEBUG << "Trying to resolve: " << host << ":" << port;
+ uint64_t flag = 0;
+ crow::connections::systemBus->async_method_call(
+ [host, port, handler{std::move(handler)}](
+ const boost::system::error_code ec,
+ const std::vector<
+ std::tuple<int32_t, int32_t, std::vector<uint8_t>>>& resp,
+ const std::string& hostName, const uint64_t flagNum) {
+ std::vector<boost::asio::ip::tcp::endpoint> endpointList;
+ if (ec)
+ {
+ BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message();
+ handler(ec, endpointList);
+ return;
+ }
+ BMCWEB_LOG_DEBUG << "ResolveHostname returned: " << hostName
+ << ":" << flagNum;
+ // Extract the IP address from the response
+ for (auto resolveList : resp)
+ {
+ std::vector<uint8_t> ipAddress = std::get<2>(resolveList);
+ boost::asio::ip::tcp::endpoint endpoint;
+ if (ipAddress.size() == 4) // ipv4 address
+ {
+ BMCWEB_LOG_DEBUG << "ipv4 address";
+ boost::asio::ip::address_v4 ipv4Addr(
+ {ipAddress[0], ipAddress[1], ipAddress[2],
+ ipAddress[3]});
+ endpoint.address(ipv4Addr);
+ }
+ else if (ipAddress.size() == 16) // ipv6 address
+ {
+ BMCWEB_LOG_DEBUG << "ipv6 address";
+ boost::asio::ip::address_v6 ipv6Addr(
+ {ipAddress[0], ipAddress[1], ipAddress[2],
+ ipAddress[3], ipAddress[4], ipAddress[5],
+ ipAddress[6], ipAddress[7], ipAddress[8],
+ ipAddress[9], ipAddress[10], ipAddress[11],
+ ipAddress[12], ipAddress[13], ipAddress[14],
+ ipAddress[15]});
+ endpoint.address(ipv6Addr);
+ }
+ else
+ {
+ BMCWEB_LOG_ERROR
+ << "Resolve failed to fetch the IP address";
+ handler(ec, endpointList);
+ return;
+ }
+ uint16_t portNum;
+ auto it = std::from_chars(
+ port.data(), port.data() + port.size(), portNum);
+ if (it.ec != std::errc())
+ {
+ BMCWEB_LOG_ERROR << "Failed to get the Port";
+ handler(ec, endpointList);
+ return;
+ }
+ endpoint.port(portNum);
+ BMCWEB_LOG_DEBUG << "resolved endpoint is : " << endpoint;
+ endpointList.push_back(endpoint);
+ }
+ // All the resolved data is filled in the endpointList
+ handler(ec, endpointList);
+ },
+ "org.freedesktop.resolve1", "/org/freedesktop/resolve1",
+ "org.freedesktop.resolve1.Manager", "ResolveHostname", 0, host,
+ AF_UNSPEC, flag);
+ }
+};
+
+} // namespace async_resolve
+} // namespace crow