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
diff --git a/include/async_resolve.hpp b/include/async_resolve.hpp
index 5e526ca..2bf5112 100644
--- a/include/async_resolve.hpp
+++ b/include/async_resolve.hpp
@@ -12,12 +12,37 @@
#include <iostream>
#include <memory>
-namespace crow
-{
-
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
{
public:
@@ -75,36 +100,15 @@
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
- {
- BMCWEB_LOG_ERROR
- << "Resolve failed to fetch the IP address";
- handler(ec, endpointList);
- return;
- }
endpoint.port(portNum);
+ if (!endpointFromResolveTuple(std::get<2>(resolveList),
+ endpoint))
+ {
+ boost::system::error_code ecErr = make_error_code(
+ boost::system::errc::address_not_available);
+ handler(ecErr, endpointList);
+ }
BMCWEB_LOG_DEBUG << "resolved endpoint is : " << endpoint;
endpointList.push_back(endpoint);
}
@@ -118,5 +122,4 @@
};
} // namespace async_resolve
-} // namespace crow
#endif