2021-07-05 02:31:23 +00:00
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2015-02-02 09:56:53 +00:00
|
|
|
|
2015-02-02 10:08:58 +00:00
|
|
|
#include "Common/TraversalClient.h"
|
2017-09-09 19:52:35 +00:00
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
2015-09-26 21:13:07 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2018-05-21 13:48:17 +00:00
|
|
|
#include "Common/Random.h"
|
2018-10-18 08:33:05 +00:00
|
|
|
#include "Core/NetPlayProto.h"
|
2015-02-02 09:56:53 +00:00
|
|
|
|
2015-02-15 03:51:08 +00:00
|
|
|
TraversalClient::TraversalClient(ENetHost* netHost, const std::string& server, const u16 port)
|
2018-04-16 20:24:09 +00:00
|
|
|
: m_NetHost(netHost), m_Server(server), m_port(port)
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
|
|
|
netHost->intercept = TraversalClient::InterceptCallback;
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
|
|
|
|
ReconnectToServer();
|
|
|
|
}
|
|
|
|
|
2018-04-16 20:02:21 +00:00
|
|
|
TraversalClient::~TraversalClient() = default;
|
|
|
|
|
|
|
|
TraversalHostId TraversalClient::GetHostID() const
|
|
|
|
{
|
|
|
|
return m_HostId;
|
|
|
|
}
|
|
|
|
|
|
|
|
TraversalClient::State TraversalClient::GetState() const
|
|
|
|
{
|
|
|
|
return m_State;
|
|
|
|
}
|
|
|
|
|
|
|
|
TraversalClient::FailureReason TraversalClient::GetFailureReason() const
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
2018-04-16 20:02:21 +00:00
|
|
|
return m_FailureReason;
|
2015-02-02 09:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::ReconnectToServer()
|
|
|
|
{
|
|
|
|
if (enet_address_set_host(&m_ServerAddress, m_Server.c_str()))
|
|
|
|
{
|
2017-08-08 22:29:47 +00:00
|
|
|
OnFailure(FailureReason::BadHost);
|
2015-02-02 09:56:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-02-15 03:51:08 +00:00
|
|
|
m_ServerAddress.port = m_port;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-01-19 19:00:01 +00:00
|
|
|
m_State = State::Connecting;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
TraversalPacket hello = {};
|
2021-01-19 19:11:39 +00:00
|
|
|
hello.type = TraversalPacketType::HelloFromClient;
|
2015-02-02 09:56:53 +00:00
|
|
|
hello.helloFromClient.protoVersion = TraversalProtoVersion;
|
|
|
|
SendTraversalPacket(hello);
|
|
|
|
if (m_Client)
|
|
|
|
m_Client->OnTraversalStateChanged();
|
|
|
|
}
|
|
|
|
|
2021-01-19 19:21:04 +00:00
|
|
|
static ENetAddress MakeENetAddress(const TraversalInetAddress& address)
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
2021-01-19 19:21:04 +00:00
|
|
|
ENetAddress eaddr{};
|
|
|
|
if (address.isIPV6)
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
|
|
|
eaddr.port = 0; // no support yet :(
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-19 19:21:04 +00:00
|
|
|
eaddr.host = address.address[0];
|
|
|
|
eaddr.port = ntohs(address.port);
|
2015-02-02 09:56:53 +00:00
|
|
|
}
|
|
|
|
return eaddr;
|
|
|
|
}
|
|
|
|
|
2021-01-19 19:26:04 +00:00
|
|
|
void TraversalClient::ConnectToClient(std::string_view host)
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
|
|
|
if (host.size() > sizeof(TraversalHostId))
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Host too long");
|
2015-02-02 09:56:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
TraversalPacket packet = {};
|
2021-01-19 19:11:39 +00:00
|
|
|
packet.type = TraversalPacketType::ConnectPlease;
|
2021-01-19 19:26:04 +00:00
|
|
|
memcpy(packet.connectPlease.hostId.data(), host.data(), host.size());
|
2015-02-02 09:56:53 +00:00
|
|
|
m_ConnectRequestId = SendTraversalPacket(packet);
|
|
|
|
m_PendingConnect = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TraversalClient::TestPacket(u8* data, size_t size, ENetAddress* from)
|
|
|
|
{
|
|
|
|
if (from->host == m_ServerAddress.host && from->port == m_ServerAddress.port)
|
|
|
|
{
|
|
|
|
if (size < sizeof(TraversalPacket))
|
|
|
|
{
|
2020-10-23 18:41:30 +00:00
|
|
|
ERROR_LOG_FMT(NETPLAY, "Received too-short traversal packet.");
|
2015-02-02 09:56:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HandleServerPacket((TraversalPacket*)data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--Temporary until more of the old netplay branch is moved over
|
|
|
|
void TraversalClient::Update()
|
|
|
|
{
|
|
|
|
ENetEvent netEvent;
|
|
|
|
if (enet_host_service(m_NetHost, &netEvent, 4) > 0)
|
|
|
|
{
|
|
|
|
switch (netEvent.type)
|
|
|
|
{
|
|
|
|
case ENET_EVENT_TYPE_RECEIVE:
|
|
|
|
TestPacket(netEvent.packet->data, netEvent.packet->dataLength, &netEvent.peer->address);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
enet_packet_destroy(netEvent.packet);
|
|
|
|
break;
|
2015-02-02 10:08:58 +00:00
|
|
|
default:
|
|
|
|
break;
|
2015-02-02 09:56:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
HandleResends();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::HandleServerPacket(TraversalPacket* packet)
|
|
|
|
{
|
|
|
|
u8 ok = 1;
|
|
|
|
switch (packet->type)
|
|
|
|
{
|
2021-01-19 19:11:39 +00:00
|
|
|
case TraversalPacketType::Ack:
|
2015-02-02 09:56:53 +00:00
|
|
|
if (!packet->ack.ok)
|
|
|
|
{
|
2017-08-08 22:29:47 +00:00
|
|
|
OnFailure(FailureReason::ServerForgotAboutUs);
|
2015-02-02 09:56:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
for (auto it = m_OutgoingTraversalPackets.begin(); it != m_OutgoingTraversalPackets.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it->packet.requestId == packet->requestId)
|
|
|
|
{
|
|
|
|
m_OutgoingTraversalPackets.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-01-19 19:11:39 +00:00
|
|
|
case TraversalPacketType::HelloFromServer:
|
2021-01-19 19:00:01 +00:00
|
|
|
if (!IsConnecting())
|
2015-02-02 09:56:53 +00:00
|
|
|
break;
|
|
|
|
if (!packet->helloFromServer.ok)
|
|
|
|
{
|
2017-08-08 22:29:47 +00:00
|
|
|
OnFailure(FailureReason::VersionTooOld);
|
2015-02-02 09:56:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_HostId = packet->helloFromServer.yourHostId;
|
2021-01-19 19:00:01 +00:00
|
|
|
m_State = State::Connected;
|
2015-02-02 09:56:53 +00:00
|
|
|
if (m_Client)
|
|
|
|
m_Client->OnTraversalStateChanged();
|
|
|
|
break;
|
2021-01-19 19:11:39 +00:00
|
|
|
case TraversalPacketType::PleaseSendPacket:
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
|
|
|
// security is overrated.
|
2021-01-19 19:21:04 +00:00
|
|
|
ENetAddress addr = MakeENetAddress(packet->pleaseSendPacket.address);
|
2015-02-02 09:56:53 +00:00
|
|
|
if (addr.port != 0)
|
|
|
|
{
|
|
|
|
char message[] = "Hello from Dolphin Netplay...";
|
|
|
|
ENetBuffer buf;
|
|
|
|
buf.data = message;
|
|
|
|
buf.dataLength = sizeof(message) - 1;
|
|
|
|
enet_socket_send(m_NetHost->socket, &addr, &buf, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// invalid IPV6
|
|
|
|
ok = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-01-19 19:11:39 +00:00
|
|
|
case TraversalPacketType::ConnectReady:
|
|
|
|
case TraversalPacketType::ConnectFailed:
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
|
|
|
if (!m_PendingConnect || packet->connectReady.requestId != m_ConnectRequestId)
|
|
|
|
break;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
m_PendingConnect = false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
if (!m_Client)
|
|
|
|
break;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2021-01-19 19:11:39 +00:00
|
|
|
if (packet->type == TraversalPacketType::ConnectReady)
|
2021-01-19 19:21:04 +00:00
|
|
|
m_Client->OnConnectReady(MakeENetAddress(packet->connectReady.address));
|
2015-02-02 09:56:53 +00:00
|
|
|
else
|
|
|
|
m_Client->OnConnectFailed(packet->connectFailed.reason);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2020-10-23 18:41:30 +00:00
|
|
|
WARN_LOG_FMT(NETPLAY, "Received unknown packet with type {}", packet->type);
|
2015-02-02 09:56:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-01-19 19:11:39 +00:00
|
|
|
if (packet->type != TraversalPacketType::Ack)
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
|
|
|
TraversalPacket ack = {};
|
2021-01-19 19:11:39 +00:00
|
|
|
ack.type = TraversalPacketType::Ack;
|
2015-02-02 09:56:53 +00:00
|
|
|
ack.requestId = packet->requestId;
|
|
|
|
ack.ack.ok = ok;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
ENetBuffer buf;
|
|
|
|
buf.data = &ack;
|
|
|
|
buf.dataLength = sizeof(ack);
|
|
|
|
if (enet_socket_send(m_NetHost->socket, &m_ServerAddress, &buf, 1) == -1)
|
2017-08-08 22:29:47 +00:00
|
|
|
OnFailure(FailureReason::SocketSendError);
|
2015-02-02 09:56:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 06:53:50 +00:00
|
|
|
void TraversalClient::OnFailure(FailureReason reason)
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
2021-01-19 19:00:01 +00:00
|
|
|
m_State = State::Failure;
|
2015-02-02 09:56:53 +00:00
|
|
|
m_FailureReason = reason;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-02-02 09:56:53 +00:00
|
|
|
if (m_Client)
|
|
|
|
m_Client->OnTraversalStateChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::ResendPacket(OutgoingTraversalPacketInfo* info)
|
|
|
|
{
|
|
|
|
info->sendTime = enet_time_get();
|
|
|
|
info->tries++;
|
|
|
|
ENetBuffer buf;
|
|
|
|
buf.data = &info->packet;
|
|
|
|
buf.dataLength = sizeof(info->packet);
|
|
|
|
if (enet_socket_send(m_NetHost->socket, &m_ServerAddress, &buf, 1) == -1)
|
2017-08-08 22:29:47 +00:00
|
|
|
OnFailure(FailureReason::SocketSendError);
|
2015-02-02 09:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::HandleResends()
|
|
|
|
{
|
2018-04-16 20:28:48 +00:00
|
|
|
const u32 now = enet_time_get();
|
2015-02-02 09:56:53 +00:00
|
|
|
for (auto& tpi : m_OutgoingTraversalPackets)
|
|
|
|
{
|
|
|
|
if (now - tpi.sendTime >= (u32)(300 * tpi.tries))
|
|
|
|
{
|
|
|
|
if (tpi.tries >= 5)
|
|
|
|
{
|
2017-08-08 22:29:47 +00:00
|
|
|
OnFailure(FailureReason::ResendTimeout);
|
2015-02-02 09:56:53 +00:00
|
|
|
m_OutgoingTraversalPackets.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ResendPacket(&tpi);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HandlePing();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::HandlePing()
|
|
|
|
{
|
2018-04-16 20:28:48 +00:00
|
|
|
const u32 now = enet_time_get();
|
2021-01-19 19:00:01 +00:00
|
|
|
if (IsConnected() && now - m_PingTime >= 500)
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
2015-02-17 06:53:50 +00:00
|
|
|
TraversalPacket ping = {};
|
2021-01-19 19:11:39 +00:00
|
|
|
ping.type = TraversalPacketType::Ping;
|
2015-02-02 09:56:53 +00:00
|
|
|
ping.ping.hostId = m_HostId;
|
|
|
|
SendTraversalPacket(ping);
|
|
|
|
m_PingTime = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TraversalRequestId TraversalClient::SendTraversalPacket(const TraversalPacket& packet)
|
|
|
|
{
|
|
|
|
OutgoingTraversalPacketInfo info;
|
|
|
|
info.packet = packet;
|
2018-06-14 14:26:11 +00:00
|
|
|
info.packet.requestId = Common::Random::GenerateValue<TraversalRequestId>();
|
2015-02-02 09:56:53 +00:00
|
|
|
info.tries = 0;
|
|
|
|
m_OutgoingTraversalPackets.push_back(info);
|
|
|
|
ResendPacket(&m_OutgoingTraversalPackets.back());
|
|
|
|
return info.packet.requestId;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraversalClient::Reset()
|
|
|
|
{
|
|
|
|
m_PendingConnect = false;
|
|
|
|
m_Client = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ENET_CALLBACK TraversalClient::InterceptCallback(ENetHost* host, ENetEvent* event)
|
|
|
|
{
|
|
|
|
auto traversalClient = g_TraversalClient.get();
|
2015-03-14 14:19:18 +00:00
|
|
|
if (traversalClient->TestPacket(host->receivedData, host->receivedDataLength,
|
|
|
|
&host->receivedAddress) ||
|
|
|
|
(host->receivedDataLength == 1 && host->receivedData[0] == 0))
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
|
|
|
event->type = (ENetEventType)42;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<TraversalClient> g_TraversalClient;
|
|
|
|
std::unique_ptr<ENetHost> g_MainNetHost;
|
|
|
|
|
|
|
|
// The settings at the previous TraversalClient reset - notably, we
|
|
|
|
// need to know not just what port it's on, but whether it was
|
|
|
|
// explicitly requested.
|
|
|
|
static std::string g_OldServer;
|
2015-09-20 11:08:37 +00:00
|
|
|
static u16 g_OldServerPort;
|
|
|
|
static u16 g_OldListenPort;
|
2015-02-02 09:56:53 +00:00
|
|
|
|
2015-09-20 11:08:37 +00:00
|
|
|
bool EnsureTraversalClient(const std::string& server, u16 server_port, u16 listen_port)
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
2015-09-20 11:08:37 +00:00
|
|
|
if (!g_MainNetHost || !g_TraversalClient || server != g_OldServer ||
|
|
|
|
server_port != g_OldServerPort || listen_port != g_OldListenPort)
|
2015-02-02 09:56:53 +00:00
|
|
|
{
|
|
|
|
g_OldServer = server;
|
2015-09-20 11:08:37 +00:00
|
|
|
g_OldServerPort = server_port;
|
|
|
|
g_OldListenPort = listen_port;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-09-20 11:08:37 +00:00
|
|
|
ENetAddress addr = {ENET_HOST_ANY, listen_port};
|
2018-10-18 08:33:05 +00:00
|
|
|
ENetHost* host = enet_host_create(&addr, // address
|
|
|
|
50, // peerCount
|
|
|
|
NetPlay::CHANNEL_COUNT, // channelLimit
|
|
|
|
0, // incomingBandwidth
|
|
|
|
0); // outgoingBandwidth
|
2015-02-02 09:56:53 +00:00
|
|
|
if (!host)
|
|
|
|
{
|
|
|
|
g_MainNetHost.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
g_MainNetHost.reset(host);
|
2015-09-20 11:08:37 +00:00
|
|
|
g_TraversalClient.reset(new TraversalClient(g_MainNetHost.get(), server, server_port));
|
2015-02-02 09:56:53 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseTraversalClient()
|
|
|
|
{
|
|
|
|
if (!g_TraversalClient)
|
|
|
|
return;
|
|
|
|
|
2017-07-22 22:19:22 +00:00
|
|
|
g_TraversalClient.reset();
|
|
|
|
g_MainNetHost.reset();
|
2015-03-01 15:17:32 +00:00
|
|
|
}
|