diff --git a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java index ae16371..ba05d0b 100644 --- a/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java +++ b/src/main/java/org/ice4j/ice/harvest/AbstractUdpListener.java @@ -195,30 +195,29 @@ public abstract class AbstractUdpListener /** * Initializes a new SinglePortUdpHarvester instance which is to * bind on the specified local address. - * @param localAddress the address to bind to. + * @param localAddress the address to bind to. If the port is 0 an ephemeral port will be chosen by the OS and the + * AbstractUdpListener.localAddress will reflect the allocated port. * @throws IOException if initialization fails. */ protected AbstractUdpListener(TransportAddress localAddress) throws IOException { + TransportAddress tempAddress = localAddress; + boolean bindWildcard = StackProperties.getBoolean( StackProperties.BIND_WILDCARD, false); if (bindWildcard) { - this.localAddress = new TransportAddress( + tempAddress = new TransportAddress( (InetAddress) null, localAddress.getPort(), localAddress.getTransport() ); } - else - { - this.localAddress = localAddress; - } - socket = new DatagramSocket( this.localAddress ); + socket = new DatagramSocket( tempAddress ); Integer receiveBufferSize = config.udpReceiveBufferSize(); if (receiveBufferSize != null) @@ -226,6 +225,17 @@ public abstract class AbstractUdpListener socket.setReceiveBufferSize(receiveBufferSize); } + /* Update the port number if needed. */ + if (localAddress.getPort() == 0) + { + tempAddress = new TransportAddress( + tempAddress.getAddress(), + socket.getLocalPort(), + tempAddress.getTransport() + ); + } + this.localAddress = tempAddress; + String logMessage = "Initialized AbstractUdpListener with address " + this.localAddress; logMessage += ". Receive buffer size " + socket.getReceiveBufferSize(); diff --git a/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java b/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java index 90622c0..50ed6a9 100644 --- a/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java @@ -101,7 +101,8 @@ public class SinglePortUdpHarvester /** * Initializes a new SinglePortUdpHarvester instance which is to * bind on the specified local address. - * @param localAddress the address to bind to. + * @param localAddress the address to bind to. If the port is 0 an ephemeral port is chosen by the OS and the + * SinglePortUdpHarvester.localAddress will reflect the allocated port number. * @throws IOException if initialization fails. */ public SinglePortUdpHarvester(TransportAddress localAddress) @@ -109,7 +110,7 @@ public class SinglePortUdpHarvester { super(localAddress); logger.info("Initialized SinglePortUdpHarvester with address " - + localAddress); + + this.localAddress); } /** diff --git a/src/test/java/org/ice4j/ice/harvest/SinglePortUdpHarvesterTest.java b/src/test/java/org/ice4j/ice/harvest/SinglePortUdpHarvesterTest.java index 2c11749..3535e22 100644 --- a/src/test/java/org/ice4j/ice/harvest/SinglePortUdpHarvesterTest.java +++ b/src/test/java/org/ice4j/ice/harvest/SinglePortUdpHarvesterTest.java @@ -101,7 +101,7 @@ public class SinglePortUdpHarvesterTest // Verify results. catch ( BindException ex ) { - fail( "A bind exception should not have been thrown, as the original harvester was propertly closed."); + fail( "A bind exception should not have been thrown, as the original harvester was properly closed."); } // Tear down. @@ -113,4 +113,29 @@ public class SinglePortUdpHarvesterTest } } } + + @Test + public void testBindWithPortZero() throws Exception + { + // Setup test fixture. + final TransportAddress address = new TransportAddress("127.0.0.1", 0, Transport.UDP); + SinglePortUdpHarvester harvester; + try + { + harvester = new SinglePortUdpHarvester(address); + } + catch (BindException ex) + { + // This is not expected at this stage. + // Rethrow as a different exception than the BindException, that is expected to be thrown later in + // this test. + throw new Exception("Test fixture is invalid.", ex); + } + + assertFalse(harvester.localAddress.getPort() == 0, + "A random port number not equal to zero should have been chosen by the OS"); + + // Tear down + harvester.close(); + } }