Util { public static InetSocketAddress computeLocalEndpoint( NetworkInterface ni, InetAddress localAddress, Integer localPort, InetAddress address, Integer port) throws UserErrorException { InetAddress effectiveAddress = null; if (ni != null) { for(Enumeration<InetAddress> e = ni.getInetAddresses(); e.hasMoreElements(); ) { InetAddress a = e.nextElement(); if (effectiveAddress == null) { effectiveAddress = a; } else if (Util.isIPv6(effectiveAddress) && Util.isIPv4(a)) { effectiveAddress = a; } } if (localAddress != null) { if (!localAddress.equals(effectiveAddress)) { throw new UserErrorException( "incompatible values: network interface " + effectiveAddress + " and local address " + localAddress); } } } else { if (localAddress != null && address == null) { effectiveAddress = localAddress; } else if (localAddress == null && address != null) { effectiveAddress = address; } else if (localAddress != null) { if (!localAddress.equals(address)) { throw new UserErrorException( "incompatible values: address " + address + " and local address " + localAddress); } effectiveAddress = localAddress; } else { effectiveAddress = null; } } Integer effectivePort; if (localPort != null && port == null) { effectivePort = localPort; } else if (localPort == null && port != null) { effectivePort = port; } else if (localPort != null && !localPort.equals(port)) { throw new UserErrorException("incompatible values: local port " + localPort + " and port " + port); } else { effectivePort = localPort; } if (effectiveAddress == null) { if (effectivePort == null) { return null; } return new InetSocketAddress(effectivePort); } if (effectivePort == null) { effectivePort = 0; } validatePort(effectivePort); return new InetSocketAddress(effectiveAddress, effectivePort); } private  Util(); static byte[] addressToBytes(String address); static boolean identical(byte[] b, byte[] b2); static void validatePort(int port); static InetSocketAddress computeLocalEndpoint(
            NetworkInterface ni, InetAddress localAddress, Integer localPort, InetAddress address, Integer port); static boolean isIPv4(InetAddress a); static boolean isIPv6(InetAddress a); static String inetAddressToString(InetAddress a); static void displayInfo(); static void dumpState(Configuration c, DatagramSocket s, DatagramPacket p); static String truncate(byte[] payload, int length); static final Pattern IPv4_FORMAT; }
@Test public void computeLocalEndpoint_SendingEndpoint_NetworkInterface() throws Exception { NetworkInterface ni = getLocalhostNetworkInterface(); InetSocketAddress sa = Util.computeLocalEndpoint(ni, null, null, null, null); assertNotNull(sa); assertEquals(InetAddress.getByName("127.0.0.1"), sa.getAddress()); assertEquals(0, sa.getPort()); }
@Test public void computeLocalEndpoint_SendingEndpoint_NetworkInterface_And_LocalAddress_Same() throws Exception { NetworkInterface ni = getLocalhostNetworkInterface(); InetSocketAddress sa = Util.computeLocalEndpoint(ni, InetAddress.getByName("127.0.0.1"), null, null, null); assertNotNull(sa); assertEquals(InetAddress.getByName("127.0.0.1"), sa.getAddress()); assertEquals(0, sa.getPort()); }
@Test public void computeLocalEndpoint_SendingEndpoint_NetworkInterface_And_LocalAddress_Different() throws Exception { NetworkInterface ni = getLocalhostNetworkInterface(); try { Util.computeLocalEndpoint(ni, InetAddress.getByName("127.0.0.2"), null, null, null); fail("should have thrown exception"); } catch(UserErrorException e) { String msg = e.getMessage(); assertTrue(msg.contains("incompatible values")); assertTrue(msg.contains("local address")); assertTrue(msg.contains("network interface")); } }
@Test public void computeLocalEndpoint_SendingEndpoint_NoNetworkInterface_LocalAddress() throws Exception { InetSocketAddress sa = Util.computeLocalEndpoint(null, InetAddress.getByName("127.0.0.5"), null, null, null); assertNotNull(sa); assertEquals(InetAddress.getByName("127.0.0.5"), sa.getAddress()); assertEquals(0, sa.getPort()); }
@Test public void computeLocalEndpoint_SendingEndpoint_NoNetworkInterface_Address() throws Exception { InetSocketAddress sa = Util.computeLocalEndpoint(null, null, null, InetAddress.getByName("127.0.0.6"), null); assertNotNull(sa); assertEquals(InetAddress.getByName("127.0.0.6"), sa.getAddress()); assertEquals(0, sa.getPort()); }
@Test public void computeLocalEndpoint_SendingEndpoint_NoNetworkInterface_LocalAddress_And_Address_Same() throws Exception { InetSocketAddress sa = Util.computeLocalEndpoint( null, InetAddress.getByName("127.0.0.7"), null, InetAddress.getByName("127.0.0.7"), null); assertNotNull(sa); assertEquals(InetAddress.getByName("127.0.0.7"), sa.getAddress()); assertEquals(0, sa.getPort()); }
@Test public void computeLocalEndpoint_SendingEndpoint_NoNetworkInterface_LocalPort() throws Exception { InetSocketAddress sa = Util.computeLocalEndpoint(null, null, 12345, null, null); assertNotNull(sa); InetAddress a = sa.getAddress(); assertTrue(a.isAnyLocalAddress()); assertEquals(12345, sa.getPort()); }
@Test public void computeLocalEndpoint_SendingEndpoint_NoNetworkInterface_LocalAddress_And_Address_NotSame() throws Exception { try { Util.computeLocalEndpoint( null, InetAddress.getByName("127.0.0.8"), null, InetAddress.getByName("127.0.0.9"), null); fail("should have thrown exception"); } catch(UserErrorException e) { String msg = e.getMessage(); assertTrue(msg.contains("incompatible values: address")); assertTrue(msg.contains("local address")); } }
@Test public void computeLocalEndpoint_ReceivingEndpoint_NetworkInterface_Port() throws Exception { NetworkInterface ni = getLocalhostNetworkInterface(); InetSocketAddress sa = Util.computeLocalEndpoint(ni, null, 5555, null, null); assertNotNull(sa); assertEquals(InetAddress.getByName("127.0.0.1"), sa.getAddress()); assertEquals(5555, sa.getPort()); }
@Test public void computeLocalEndpoint_ReceivingEndpoint_NetworkInterface_LocalPort() throws Exception { NetworkInterface ni = getLocalhostNetworkInterface(); InetSocketAddress sa = Util.computeLocalEndpoint(ni, null, null, null, 6666); assertNotNull(sa); assertEquals(InetAddress.getByName("127.0.0.1"), sa.getAddress()); assertEquals(6666, sa.getPort()); }
@Test public void computeLocalEndpoint_ReceivingEndpoint_NetworkInterface_LocalPortAndPortSameValues() throws Exception { NetworkInterface ni = getLocalhostNetworkInterface(); InetSocketAddress sa = Util.computeLocalEndpoint(ni, null, 7777, null, 7777); assertNotNull(sa); assertEquals(InetAddress.getByName("127.0.0.1"), sa.getAddress()); assertEquals(7777, sa.getPort()); }
@Test public void computeLocalEndpoint_ReceivingEndpoint_NetworkInterface_LocalPortAndPortDifferentValues() throws Exception { NetworkInterface ni = getLocalhostNetworkInterface(); try { Util.computeLocalEndpoint(ni, null, 8888, null, 9999); fail("should have thrown exception"); } catch(UserErrorException e) { String msg = e.getMessage(); assertTrue(msg.contains("incompatible values: local port")); assertTrue(msg.contains("port 9999")); } }
@Test public void computeLocalEndpoint_Null() throws Exception { SocketAddress sa = Util.computeLocalEndpoint(null, null, null, null, null); assertNull(sa); }