|
19 | 19 | import io.netty.channel.socket.nio.NioDatagramChannel; |
20 | 20 | import io.netty.util.NetUtil; |
21 | 21 | import io.netty.util.internal.PlatformDependent; |
| 22 | +import io.netty.util.internal.SuppressJava6Requirement; |
22 | 23 |
|
23 | | -import java.net.Inet4Address; |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.BindException; |
24 | 26 | import java.net.InetAddress; |
25 | 27 | import java.net.InetSocketAddress; |
26 | 28 | import java.net.SocketAddress; |
| 29 | +import java.net.StandardProtocolFamily; |
27 | 30 | import java.net.UnknownHostException; |
28 | 31 |
|
| 32 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 33 | + |
29 | 34 | public class DatagramUnicastIPv6MappedTest extends DatagramUnicastIPv6Test { |
30 | 35 |
|
| 36 | + private static final InetAddress IPV4_ANY; |
| 37 | + private static final int MAX_PORT_SELECTION_ATTEMPTS = 10; |
| 38 | + |
| 39 | + static { |
| 40 | + try { |
| 41 | + IPV4_ANY = InetAddress.getByAddress(new byte[] {0, 0, 0, 0}); |
| 42 | + } catch (UnknownHostException e) { |
| 43 | + throw new ExceptionInInitializerError(e); |
| 44 | + } |
| 45 | + } |
| 46 | + |
31 | 47 | @Override |
32 | 48 | protected SocketAddress newSocketAddress() { |
33 | 49 | // Explicit use IPv4 wildcard for bind while the channel is using SocketProtocolFamily.INET6. |
34 | | - try { |
35 | | - return new InetSocketAddress(InetAddress.getByAddress(new byte[] {0, 0 , 0, 0}), 0); |
36 | | - } catch (UnknownHostException e) { |
37 | | - throw new IllegalStateException(e); |
| 50 | + // |
| 51 | + // Don't use port 0: when an IPv6 socket binds to the wildcard address, macOS only checks the ports of other |
| 52 | + // IPv6 sockets when it selects the ephemeral port, so it might select a port that an IPv4 socket is |
| 53 | + // bound to. Datagrams that are sent to the IPv4 loopback address are then delivered to the IPv4 socket and |
| 54 | + // never reach the channel, no matter how often they are resent: |
| 55 | + // |
| 56 | + // - in6_pcbsetport() checks if the port is used with in6_pcblookup_local()... |
| 57 | + // https://github.com/apple-oss-distributions/xnu/blob/xnu-12377.121.6/bsd/netinet6/in6_src.c#L1779-L1780 |
| 58 | + // - ...which skips all sockets that are not IPv6 sockets. |
| 59 | + // https://github.com/apple-oss-distributions/xnu/blob/xnu-12377.121.6/bsd/netinet6/in6_pcb.c#L1297-L1301 |
| 60 | + // - The netns_reserve_in() check that follows only fails if Skywalk reserved the port, not BSD sockets. |
| 61 | + // https://github.com/apple-oss-distributions/xnu/blob/xnu-12377.121.6/bsd/skywalk/namespace/netns.c#L774-L783 |
| 62 | + // - When an IPv4 datagram is received, in_pcblookup_hash_locked() prefers IPv4 sockets bound to the |
| 63 | + // destination or the wildcard address over IPv6 sockets bound to the wildcard address. |
| 64 | + // https://github.com/apple-oss-distributions/xnu/blob/xnu-12377.121.6/bsd/netinet/in_pcb.c#L2634-L2666 |
| 65 | + return new InetSocketAddress(IPV4_ANY, unusedPort()); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns a port that is neither used by an IPv4 nor by an IPv6 socket. |
| 70 | + */ |
| 71 | + @SuppressJava6Requirement(reason = "Guarded by java version check") |
| 72 | + private static int unusedPort() { |
| 73 | + assumeTrue(PlatformDependent.javaVersion() >= 7); |
| 74 | + BindException cause = null; |
| 75 | + for (int i = 0; i < MAX_PORT_SELECTION_ATTEMPTS; i++) { |
| 76 | + int port; |
| 77 | + // The IPv4 stack selects a port that is unused by IPv4 sockets and by IPv6 sockets that accept IPv4 |
| 78 | + // datagrams. |
| 79 | + java.nio.channels.DatagramChannel ipv4 = null; |
| 80 | + try { |
| 81 | + ipv4 = java.nio.channels.DatagramChannel.open(StandardProtocolFamily.INET); |
| 82 | + ipv4.bind(new InetSocketAddress(IPV4_ANY, 0)); |
| 83 | + port = ((InetSocketAddress) ipv4.getLocalAddress()).getPort(); |
| 84 | + } catch (IOException e) { |
| 85 | + throw new IllegalStateException(e); |
| 86 | + } finally { |
| 87 | + closeQuietly(ipv4); |
| 88 | + } |
| 89 | + // Bind to the port like the channel will do, which fails if an IPv6 socket that only accepts IPv6 |
| 90 | + // datagrams uses it. The IPv4 channel must be closed by now, as this binds to the IPv4 wildcard address |
| 91 | + // as well on some platforms (like Linux). |
| 92 | + java.nio.channels.DatagramChannel ipv6 = null; |
| 93 | + try { |
| 94 | + ipv6 = java.nio.channels.DatagramChannel.open(StandardProtocolFamily.INET6); |
| 95 | + ipv6.bind(new InetSocketAddress(IPV4_ANY, port)); |
| 96 | + return port; |
| 97 | + } catch (BindException e) { |
| 98 | + cause = e; |
| 99 | + } catch (IOException e) { |
| 100 | + throw new IllegalStateException(e); |
| 101 | + } finally { |
| 102 | + closeQuietly(ipv6); |
| 103 | + } |
| 104 | + } |
| 105 | + throw new IllegalStateException("Failed to find a port that is unused by IPv4 and IPv6 sockets", cause); |
| 106 | + } |
| 107 | + |
| 108 | + private static void closeQuietly(java.nio.channels.DatagramChannel channel) { |
| 109 | + if (channel != null) { |
| 110 | + try { |
| 111 | + channel.close(); |
| 112 | + } catch (IOException ignore) { |
| 113 | + // ignore |
| 114 | + } |
38 | 115 | } |
39 | 116 | } |
40 | 117 |
|
|