Skip to content
Snippets Groups Projects
Commit 2ac5157a authored by Adrien Béraud's avatar Adrien Béraud
Browse files

net: attempt to use the same port for IPv4 and IPv6 if 0

parent fbe8834e
Branches
Tags
No related merge requests found
...@@ -197,12 +197,26 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6) ...@@ -197,12 +197,26 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6)
#if 1 #if 1
bound6 = {}; bound6 = {};
if (bind6) { if (bind6) {
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 { try {
s6 = bindSocket(bind6, bound6); s6 = bindSocket(bind6, bound6);
} catch (const DhtException& e) { } catch (const DhtException& e) {
logger.e("Can't bind inet6 socket: %s", e.what()); logger.e("Can't bind inet6 socket: %s", e.what());
} }
} }
}
#endif #endif
if (s4 == -1 && s6 == -1) { if (s4 == -1 && s6 == -1) {
......
...@@ -216,11 +216,11 @@ SockAddr::getMappedIPv4() ...@@ -216,11 +216,11 @@ SockAddr::getMappedIPv4()
} }
SockAddr SockAddr
SockAddr::getMappedIPv6() const SockAddr::getMappedIPv6()
{ {
auto family = getFamily(); auto family = getFamily();
if (family != AF_INET) if (family != AF_INET)
return *this; return std::move(*this);
SockAddr ret; SockAddr ret;
ret.setFamily(AF_INET6); ret.setFamily(AF_INET6);
ret.setPort(getPort()); ret.setPort(getPort());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment