Fix: fix the access to sin/sin6_addr in getRemoteAddress
inet_ntop using 'remoteSockAddr' instead of casting it to 'sockaddr_in/in6'
structure and getting the field 'sin/sin6_addr' access. As result of this
'getRemoteAddress' returns junk from top of the 'sockaddr_storage' structure.
Tested: Verified by calling 'getRemoteAddress' inside net-ipmi lambda
handler.
[handler, this](const boost::system::error_code& ec,
const IpmiDbusRspType& response) {
...
std::string raddr =
handler->getChannel()->getRemoteAddress();
some_func_for_put_addr(raddr);
...
}
Signed-off-by: Ivan Mikhaylov <i.mikhaylov@yadro.com>
Change-Id: Ia8ae2763a2ab372c148323974fe33ac44b1d28f0
diff --git a/socket_channel.hpp b/socket_channel.hpp
index 5742944..df7e3ae 100644
--- a/socket_channel.hpp
+++ b/socket_channel.hpp
@@ -58,14 +58,20 @@
if (sockAddrSize == sizeof(sockaddr_in))
{
char ipv4addr[INET_ADDRSTRLEN];
- retval =
- inet_ntop(AF_INET, &remoteSockAddr, ipv4addr, sizeof(ipv4addr));
+ retval = inet_ntop(
+ AF_INET,
+ &(reinterpret_cast<const sockaddr_in*>(&remoteSockAddr)
+ ->sin_addr),
+ ipv4addr, sizeof(ipv4addr));
}
else if (sockAddrSize == sizeof(sockaddr_in6))
{
char ipv6addr[INET6_ADDRSTRLEN];
- retval = inet_ntop(AF_INET6, &remoteSockAddr, ipv6addr,
- sizeof(ipv6addr));
+ retval = inet_ntop(
+ AF_INET6,
+ &(reinterpret_cast<const sockaddr_in6*>(&remoteSockAddr)
+ ->sin6_addr),
+ ipv6addr, sizeof(ipv6addr));
}
if (retval)
{