summaryrefslogtreecommitdiff
path: root/include/async_resolve.hpp
diff options
context:
space:
mode:
authorEd Tanous <edtanous@google.com>2021-10-05 03:03:52 +0300
committerEd Tanous <ed@tanous.net>2023-07-14 20:00:46 +0300
commite1452bea2809f7189a69ee562e5078e452f0a251 (patch)
tree1286ff33b8c03a083af928f905f8f1e908d1abee /include/async_resolve.hpp
parent4a2e485d4d846ef7b6e03749795e9dc827b50ba1 (diff)
downloadbmcweb-e1452bea2809f7189a69ee562e5078e452f0a251.tar.xz
AsyncResolve cleanups and error handling
The Async DBus resolver really has nothing to do with crow, which is our core http library namespace and has some opportunistic cleanups that can be done. This commit moves it into the bmcweb namespace (unimportantly) and breaks out one of the larger functions such that it can be unit tested, and unit tests it. Tested: Unit tests pass. Signed-off-by: Ed Tanous <edtanous@google.com> Change-Id: Ie3cfbb0ef81a027a1ad42358c04967a517471117
Diffstat (limited to 'include/async_resolve.hpp')
-rw-r--r--include/async_resolve.hpp63
1 files changed, 33 insertions, 30 deletions
diff --git a/include/async_resolve.hpp b/include/async_resolve.hpp
index 5e526cabc5..2bf511299d 100644
--- a/include/async_resolve.hpp
+++ b/include/async_resolve.hpp
@@ -12,11 +12,36 @@
#include <iostream>
#include <memory>
-namespace crow
+namespace async_resolve
{
-namespace async_resolve
+inline bool endpointFromResolveTuple(const std::vector<uint8_t>& ipAddress,
+ 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";
+ return false;
+ }
+ return true;
+}
class Resolver
{
@@ -75,36 +100,15 @@ class Resolver
for (const std::tuple<int32_t, int32_t, std::vector<uint8_t>>&
resolveList : resp)
{
- const 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
+ endpoint.port(portNum);
+ if (!endpointFromResolveTuple(std::get<2>(resolveList),
+ endpoint))
{
- BMCWEB_LOG_ERROR
- << "Resolve failed to fetch the IP address";
- handler(ec, endpointList);
- return;
+ boost::system::error_code ecErr = make_error_code(
+ boost::system::errc::address_not_available);
+ handler(ecErr, endpointList);
}
- endpoint.port(portNum);
BMCWEB_LOG_DEBUG << "resolved endpoint is : " << endpoint;
endpointList.push_back(endpoint);
}
@@ -118,5 +122,4 @@ class Resolver
};
} // namespace async_resolve
-} // namespace crow
#endif