From d438d4ef1b648ff2a3b4edcef8ce544de5edcd83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Fri, 21 Jun 2019 11:00:48 -0400 Subject: [PATCH] sockaddr: add getMappedIPv6 --- include/opendht/sockaddr.h | 1 + src/utils.cpp | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/opendht/sockaddr.h b/include/opendht/sockaddr.h index 143a3981..98a82009 100644 --- a/include/opendht/sockaddr.h +++ b/include/opendht/sockaddr.h @@ -249,6 +249,7 @@ public: bool isMappedIPv4() const; SockAddr getMappedIPv4() const; + SockAddr getMappedIPv6() const; /** * A comparator to classify IP addresses, only considering the diff --git a/src/utils.cpp b/src/utils.cpp index 9b7aabbb..3c9bc207 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -215,6 +215,22 @@ SockAddr::getMappedIPv4() const return ret; } +SockAddr +SockAddr::getMappedIPv6() const +{ + auto family = getFamily(); + if (family != AF_INET) + return *this; + SockAddr ret; + ret.setFamily(AF_INET6); + ret.setPort(getPort()); + auto addr4 = reinterpret_cast(&getIPv4().sin_addr); + auto addr6 = reinterpret_cast(&ret.getIPv6().sin6_addr); + std::copy(MAPPED_IPV4_PREFIX.begin(), MAPPED_IPV4_PREFIX.end(), addr6); + std::copy_n(addr4, sizeof(in_addr), addr6 + MAPPED_IPV4_PREFIX.size()); + return ret; +} + bool operator==(const SockAddr& a, const SockAddr& b) { return a.equals(b); }