Skip to content

Commit

Permalink
Merge pull request #1097 from paullouisageneau/unmap-ws-remote-addr
Browse files Browse the repository at this point in the history
Unmap WebSocket remote IPv4-mapped IPv6 addresses
  • Loading branch information
paullouisageneau authored Jan 19, 2024
2 parents c92f6e2 + a8d5274 commit cd99a1a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/impl/tcptransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ using namespace std::chrono_literals;
using std::chrono::duration_cast;
using std::chrono::milliseconds;

namespace {

bool unmap_inet6_v4mapped(struct sockaddr *sa, socklen_t *len) {
if (sa->sa_family != AF_INET6)
return false;

const auto *sin6 = reinterpret_cast<struct sockaddr_in6 *>(sa);
if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
return false;

struct sockaddr_in6 copy = *sin6;
sin6 = &copy;

auto *sin = reinterpret_cast<struct sockaddr_in *>(sa);
std::memset(sin, 0, sizeof(*sin));
sin->sin_family = AF_INET;
sin->sin_port = sin6->sin6_port;
std::memcpy(&sin->sin_addr, reinterpret_cast<const uint8_t *>(&sin6->sin6_addr) + 12, 4);
*len = sizeof(*sin);
return true;
}

}

TcpTransport::TcpTransport(string hostname, string service, state_callback callback)
: Transport(nullptr, std::move(callback)), mIsActive(true), mHostname(std::move(hostname)),
mService(std::move(service)), mSock(INVALID_SOCKET) {
Expand All @@ -47,6 +71,8 @@ TcpTransport::TcpTransport(socket_t sock, state_callback callback)
if (::getpeername(mSock, reinterpret_cast<struct sockaddr *>(&addr), &addrlen) < 0)
throw std::runtime_error("getsockname failed");

unmap_inet6_v4mapped(reinterpret_cast<struct sockaddr *>(&addr), &addrlen);

char node[MAX_NUMERICNODE_LEN];
char serv[MAX_NUMERICSERV_LEN];
if (::getnameinfo(reinterpret_cast<struct sockaddr *>(&addr), addrlen, node,
Expand Down

0 comments on commit cd99a1a

Please sign in to comment.