Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV9: Always bind UDP ports in sockets #12042

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,18 @@ namespace Sessions

void UDP_FixedPort::Reset()
{
std::lock_guard numberlock(connectionSentry);
// Reseting a session may cause that session to close itself,
// when that happens, the connections vector gets modified via our close handler.
// Duplicate the vector to avoid iterating over a modified collection,
// this also avoids the issue of recursive locking when our close handler takes a lock.
std::vector<UDP_BaseSession*> connectionsCopy;
{
std::lock_guard numberlock(connectionSentry);
connectionsCopy = connections;
}

for (size_t i = 0; i < connections.size(); i++)
connections[i]->Reset();
for (size_t i = 0; i < connectionsCopy.size(); i++)
connectionsCopy[i]->Reset();
}

UDP_Session* UDP_FixedPort::NewClientSession(ConnectionKey parNewKey, bool parIsBrodcast, bool parIsMulticast)
Expand Down
72 changes: 31 additions & 41 deletions pcsx2/DEV9/sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,55 +464,45 @@ bool SocketAdapter::SendUDP(ConnectionKey Key, IP_Packet* ipPkt)
return false;
else
{
UDP_Session* s = nullptr;

if (abs(udp.sourcePort - udp.destinationPort) <= 10 || //Used for games that assume the destination/source port
ipPkt->destinationIP == dhcpServer.broadcastIP || //Broadcast packets
ipPkt->destinationIP == IP_Address{{{255, 255, 255, 255}}} || //Limited Broadcast packets
(ipPkt->destinationIP.bytes[0] & 0xF0) == 0xE0) //Multicast address start with 0b1110
// Always bind the UDP source port
// PS2 software can run into issues if the source port is not preserved
UDP_FixedPort* fPort = nullptr;
BaseSession* fSession;
if (fixedUDPPorts.TryGetValue(udp.sourcePort, &fSession))
{
UDP_FixedPort* fPort = nullptr;
BaseSession* fSession;
if (fixedUDPPorts.TryGetValue(udp.sourcePort, &fSession))
{
//DevCon.WriteLn("DEV9: Socket: Using Existing UDPFixedPort");
fPort = static_cast<UDP_FixedPort*>(fSession);
}
else
{
ConnectionKey fKey{};
fKey.protocol = (u8)IP_Type::UDP;
fKey.ps2Port = udp.sourcePort;
fKey.srvPort = 0;
fPort = static_cast<UDP_FixedPort*>(fSession);
}
else
{
ConnectionKey fKey{};
fKey.protocol = static_cast<u8>(IP_Type::UDP);
fKey.ps2Port = udp.sourcePort;
fKey.srvPort = 0;

Console.WriteLn("DEV9: Socket: Creating New UDPFixedPort with port %d", udp.sourcePort);
Console.WriteLn("DEV9: Socket: Binding UDP fixed port %d", udp.sourcePort);

fPort = new UDP_FixedPort(fKey, adapterIP, udp.sourcePort);
fPort->AddConnectionClosedHandler([&](BaseSession* session) { HandleFixedPortClosed(session); });
fPort = new UDP_FixedPort(fKey, adapterIP, udp.sourcePort);
fPort->AddConnectionClosedHandler([&](BaseSession* session) { HandleFixedPortClosed(session); });

fPort->destIP = {};
fPort->sourceIP = dhcpServer.ps2IP;
fPort->destIP = {};
fPort->sourceIP = dhcpServer.ps2IP;

connections.Add(fKey, fPort);
fixedUDPPorts.Add(udp.sourcePort, fPort);
connections.Add(fKey, fPort);
fixedUDPPorts.Add(udp.sourcePort, fPort);

fPort->Init();
}
fPort->Init();
}

Console.WriteLn("DEV9: Socket: Creating New UDP Connection from FixedPort %d to %d", udp.sourcePort, udp.destinationPort);
s = fPort->NewClientSession(Key,
ipPkt->destinationIP == dhcpServer.broadcastIP || ipPkt->destinationIP == IP_Address{{{255, 255, 255, 255}}},
(ipPkt->destinationIP.bytes[0] & 0xF0) == 0xE0);
Console.WriteLn("DEV9: Socket: Creating New UDP Connection from fixed port %d to %d", udp.sourcePort, udp.destinationPort);
UDP_Session* s = fPort->NewClientSession(Key,
ipPkt->destinationIP == dhcpServer.broadcastIP || ipPkt->destinationIP == IP_Address{{{255, 255, 255, 255}}},
(ipPkt->destinationIP.bytes[0] & 0xF0) == 0xE0);

if (s == nullptr)
{
Console.Error("DEV9: Socket: Failed to Create New UDP Connection from FixedPort");
return false;
}
}
else
// If we are unable to bind to the port, fall back to a dynamic port
if (s == nullptr)
{
Console.WriteLn("DEV9: Socket: Creating New UDP Connection to %d", udp.destinationPort);
Console.Error("DEV9: Socket: Failed to Create New UDP Connection from fixed port");
Console.WriteLn("DEV9: Socket: Retrying with dynamic port to %d", udp.destinationPort);
s = new UDP_Session(Key, adapterIP);
}

Expand Down Expand Up @@ -577,7 +567,7 @@ void SocketAdapter::HandleFixedPortClosed(BaseSession* sender)
else
deleteQueueRecvThread.push_back(sender);

Console.WriteLn("DEV9: Socket: Closed Dead UDP Fixed Port to %d", key.ps2Port);
Console.WriteLn("DEV9: Socket: Unbound fixed port %d", key.ps2Port);
}

void SocketAdapter::close()
Expand Down