diff --git a/src/network_utils.cpp b/src/network_utils.cpp index 2a87d8ef..ecb0d844 100644 --- a/src/network_utils.cpp +++ b/src/network_utils.cpp @@ -197,10 +197,24 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6) #if 1 bound6 = {}; if (bind6) { - try { - s6 = bindSocket(bind6, bound6); - } catch (const DhtException& e) { - logger.e("Can't bind inet6 socket: %s", e.what()); + if (bind6.getPort() == 0) { + // Attempt to use the same port as IPv4 with IPv6 + if (auto p4 = bound4.getPort()) { + auto b6 = bind6; + b6.setPort(p4); + try { + s6 = bindSocket(b6, bound6); + } catch (const DhtException& e) { + logger.e("Can't bind inet6 socket: %s", e.what()); + } + } + } + if (s6 == -1) { + try { + s6 = bindSocket(bind6, bound6); + } catch (const DhtException& e) { + logger.e("Can't bind inet6 socket: %s", e.what()); + } } } #endif diff --git a/src/utils.cpp b/src/utils.cpp index 29e7173e..4c70873d 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -216,11 +216,11 @@ SockAddr::getMappedIPv4() } SockAddr -SockAddr::getMappedIPv6() const +SockAddr::getMappedIPv6() { auto family = getFamily(); if (family != AF_INET) - return *this; + return std::move(*this); SockAddr ret; ret.setFamily(AF_INET6); ret.setPort(getPort());