feat: Enable ephemeral port usage (#231)

* Port 0 allows to request ephemeral port
This commit is contained in:
Nils Ohlmeier 2021-07-13 13:24:12 -07:00 committed by GitHub
parent 924f40c4e0
commit 10346acade
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 10 deletions

View File

@ -195,30 +195,29 @@ public abstract class AbstractUdpListener
/** /**
* Initializes a new <tt>SinglePortUdpHarvester</tt> instance which is to * Initializes a new <tt>SinglePortUdpHarvester</tt> instance which is to
* bind on the specified local address. * 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. * @throws IOException if initialization fails.
*/ */
protected AbstractUdpListener(TransportAddress localAddress) protected AbstractUdpListener(TransportAddress localAddress)
throws IOException throws IOException
{ {
TransportAddress tempAddress = localAddress;
boolean bindWildcard = StackProperties.getBoolean( boolean bindWildcard = StackProperties.getBoolean(
StackProperties.BIND_WILDCARD, StackProperties.BIND_WILDCARD,
false); false);
if (bindWildcard) if (bindWildcard)
{ {
this.localAddress = new TransportAddress( tempAddress = new TransportAddress(
(InetAddress) null, (InetAddress) null,
localAddress.getPort(), localAddress.getPort(),
localAddress.getTransport() localAddress.getTransport()
); );
} }
else
{
this.localAddress = localAddress;
}
socket = new DatagramSocket( this.localAddress ); socket = new DatagramSocket( tempAddress );
Integer receiveBufferSize = config.udpReceiveBufferSize(); Integer receiveBufferSize = config.udpReceiveBufferSize();
if (receiveBufferSize != null) if (receiveBufferSize != null)
@ -226,6 +225,17 @@ public abstract class AbstractUdpListener
socket.setReceiveBufferSize(receiveBufferSize); 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 String logMessage
= "Initialized AbstractUdpListener with address " + this.localAddress; = "Initialized AbstractUdpListener with address " + this.localAddress;
logMessage += ". Receive buffer size " + socket.getReceiveBufferSize(); logMessage += ". Receive buffer size " + socket.getReceiveBufferSize();

View File

@ -101,7 +101,8 @@ public class SinglePortUdpHarvester
/** /**
* Initializes a new <tt>SinglePortUdpHarvester</tt> instance which is to * Initializes a new <tt>SinglePortUdpHarvester</tt> instance which is to
* bind on the specified local address. * 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. * @throws IOException if initialization fails.
*/ */
public SinglePortUdpHarvester(TransportAddress localAddress) public SinglePortUdpHarvester(TransportAddress localAddress)
@ -109,7 +110,7 @@ public class SinglePortUdpHarvester
{ {
super(localAddress); super(localAddress);
logger.info("Initialized SinglePortUdpHarvester with address " logger.info("Initialized SinglePortUdpHarvester with address "
+ localAddress); + this.localAddress);
} }
/** /**

View File

@ -101,7 +101,7 @@ public class SinglePortUdpHarvesterTest
// Verify results. // Verify results.
catch ( BindException ex ) 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. // 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();
}
} }