Skip to content

Commit

Permalink
DEV9: Better error handling on UDP_FixedPort creation
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastRar authored and stenzek committed May 9, 2024
1 parent d7101c3 commit 85888a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
28 changes: 20 additions & 8 deletions pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,14 @@ namespace Sessions

UDP_FixedPort::UDP_FixedPort(ConnectionKey parKey, IP_Address parAdapterIP, u16 parPort)
: BaseSession(parKey, parAdapterIP)
, client{socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)}
, port(parPort)
, port{parPort}
{
}

void UDP_FixedPort::Init()
{
int ret;
client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (client == INVALID_SOCKET)
{
Console.Error("DEV9: UDP: Failed to open socket. Error: %d",
Expand All @@ -57,11 +61,7 @@ namespace Sessions
#elif defined(__POSIX__)
errno);
#endif
/*
* TODO: Currently error is not correctly handled here
* We would need to call RaiseEventConnectionClosed()
* and also deal with the follow up call to NewClientSession()
*/
RaiseEventConnectionClosed();
return;
}

Expand Down Expand Up @@ -90,17 +90,23 @@ namespace Sessions
sockaddr_in endpoint{};
endpoint.sin_family = AF_INET;
endpoint.sin_addr = std::bit_cast<in_addr>(adapterIP);
endpoint.sin_port = htons(parPort);
endpoint.sin_port = htons(port);

ret = bind(client, reinterpret_cast<const sockaddr*>(&endpoint), sizeof(endpoint));

if (ret == SOCKET_ERROR)
{
Console.Error("DEV9: UDP: Failed to bind socket. Error: %d",
#ifdef _WIN32
WSAGetLastError());
#elif defined(__POSIX__)
errno);
#endif
RaiseEventConnectionClosed();
return;
}

open.store(true);
}

IP_Payload* UDP_FixedPort::Recv()
Expand Down Expand Up @@ -228,6 +234,9 @@ namespace Sessions

UDP_Session* UDP_FixedPort::NewClientSession(ConnectionKey parNewKey, bool parIsBrodcast, bool parIsMulticast)
{
if (!open.load())
return nullptr;

UDP_Session* s = new UDP_Session(parNewKey, adapterIP, parIsBrodcast, parIsMulticast, client);

s->AddConnectionClosedHandler([&](BaseSession* session) { HandleChildConnectionClosed(session); });
Expand All @@ -247,7 +256,10 @@ namespace Sessions
{
connections.erase(index);
if (connections.size() == 0)
{
open.store(false);
RaiseEventConnectionClosed();
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Sessions
class UDP_FixedPort : public BaseSession
{
private:
std::atomic<bool> open{true};
std::atomic<bool> open{false};

#ifdef _WIN32
SOCKET client = INVALID_SOCKET;
Expand All @@ -38,6 +38,8 @@ namespace Sessions
public:
UDP_FixedPort(ConnectionKey parKey, PacketReader::IP::IP_Address parAdapterIP, u16 parPort);

void Init();

virtual PacketReader::IP::IP_Payload* Recv();
virtual bool Send(PacketReader::IP::IP_Payload* payload);
virtual void Reset();
Expand Down
8 changes: 8 additions & 0 deletions pcsx2/DEV9/sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,20 @@ bool SocketAdapter::SendUDP(ConnectionKey Key, IP_Packet* ipPkt)

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

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);

if (s == nullptr)
{
Console.Error("DEV9: Socket: Failed to Create New UDP Connection from FixedPort");
return false;
}
}
else
{
Expand Down

0 comments on commit 85888a9

Please sign in to comment.