Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmap WebSocket remote IPv4-mapped IPv6 addresses #1097

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading