From ac914d44157d6a44cd981108354aba20f8209739 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Fri, 1 Sep 2017 13:28:27 -0400 Subject: [PATCH] avoid port mapping of local addresses (#2296) --- src/broadcast_socket.cpp | 7 +++++++ src/session_impl.cpp | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/broadcast_socket.cpp b/src/broadcast_socket.cpp index fbc816493..41449e65e 100644 --- a/src/broadcast_socket.cpp +++ b/src/broadcast_socket.cpp @@ -69,10 +69,17 @@ namespace libtorrent { #if TORRENT_USE_IPV6 if (a.is_v6()) { + // NOTE: site local is deprecated but by + // https://www.ietf.org/rfc/rfc3879.txt: + // routers SHOULD be configured to prevent + // routing of this prefix by default. + address_v6 const a6 = a.to_v6(); return a6.is_loopback() || a6.is_link_local() + || a6.is_site_local() || a6.is_multicast_link_local() + || a6.is_multicast_site_local() // fc00::/7, unique local address || (a6.to_bytes()[0] & 0xfe) == 0xfc; } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 2611d81d7..0c037bcdd 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -2172,12 +2172,20 @@ namespace { if (map_handle != -1) m.delete_mapping(map_handle); map_handle = -1; +#if TORRENT_USE_IPV6 + address const addr = ep.address(); + // with IPv4 the interface might be behind NAT so we can't skip them + // based on the scope of the local address + if (addr.is_v6() && is_local(addr)) + return; +#endif + // only update this mapping if we actually have a socket listening if (ep != EndpointType()) map_handle = m.add_mapping(protocol, ep.port(), ep); } - tcp::endpoint to_tcp(udp::endpoint const ep) + tcp::endpoint to_tcp(udp::endpoint const& ep) { return tcp::endpoint(ep.address(), ep.port()); }