Fix UDP simulation bug with duplicate ports

Occasionally, a second UDP socket would be created with a randomly generated
port equal to the randomly generated port of an earlier socket.
This commit is contained in:
Lukas Joswiak 2020-12-08 17:38:32 +00:00
parent a1f7c74ac4
commit 7ac5aa48ed
1 changed files with 4 additions and 0 deletions

View File

@ -1796,6 +1796,7 @@ public:
: id(deterministicRandom()->randomUniqueID()), process(g_simulator.getCurrentProcess()), peerAddress(peerAddress),
actors(false), _localAddress(localAddress) {
g_sim2.addressMap.emplace(_localAddress, process);
ASSERT(process->boundUDPSockets.find(localAddress) == process->boundUDPSockets.end());
process->boundUDPSockets.emplace(localAddress, this);
}
~UDPSimSocket() {
@ -1909,6 +1910,9 @@ Future<Reference<IUDPSocket>> Sim2::createUDPSocket(NetworkAddress toAddr) {
localAddress.ip = IPAddress(process->address.ip.toV4() + deterministicRandom()->randomInt(0, 256));
}
localAddress.port = deterministicRandom()->randomInt(40000, 60000);
while (process->boundUDPSockets.find(localAddress) != process->boundUDPSockets.end()) {
localAddress.port = deterministicRandom()->randomInt(40000, 60000);
}
return Reference<IUDPSocket>(new UDPSimSocket(localAddress, toAddr));
}