From ec60416c00fe5487af7f3b144abb1c7e47d45b74 Mon Sep 17 00:00:00 2001 From: Sepalani Date: Sat, 30 Jul 2022 23:17:34 +0400 Subject: [PATCH] BBA/BuiltIn: Add an ARP table --- Source/Core/Core/HW/EXI/BBA/BuiltIn.cpp | 20 +++++++++++++++++++- Source/Core/Core/HW/EXI/EXI_DeviceEthernet.h | 5 ++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/Source/Core/Core/HW/EXI/BBA/BuiltIn.cpp b/Source/Core/Core/HW/EXI/BBA/BuiltIn.cpp index 9e2a4dcca7..284f9634a5 100644 --- a/Source/Core/Core/HW/EXI/BBA/BuiltIn.cpp +++ b/Source/Core/Core/HW/EXI/BBA/BuiltIn.cpp @@ -174,6 +174,8 @@ void CEXIETHERNET::BuiltInBBAInterface::Deactivate() ref.ip = 0; } + m_arp_table.clear(); + // Wait for read thread to exit. if (m_read_thread.joinable()) m_read_thread.join(); @@ -206,7 +208,8 @@ void CEXIETHERNET::BuiltInBBAInterface::HandleARP(const Common::ARPPacket& packe } else { - response.arp_header = Common::ARPHeader(arpdata.target_ip, m_fake_mac, m_current_ip, bba_mac); + response.arp_header = Common::ARPHeader(arpdata.target_ip, ResolveAddress(arpdata.target_ip), + m_current_ip, bba_mac); } WriteToQueue(response.Build()); @@ -488,6 +491,21 @@ void CEXIETHERNET::BuiltInBBAInterface::HandleUDPFrame(const Common::UDPPacket& ref->udp_socket.send(data.data(), data.size(), target, ntohs(udp_header.destination_port)); } +const Common::MACAddress& CEXIETHERNET::BuiltInBBAInterface::ResolveAddress(u32 inet_ip) +{ + auto it = m_arp_table.lower_bound(inet_ip); + if (it != m_arp_table.end() && it->first == inet_ip) + { + return it->second; + } + else + { + return m_arp_table + .emplace_hint(it, inet_ip, Common::GenerateMacAddress(Common::MACConsumer::BBA)) + ->second; + } +} + bool CEXIETHERNET::BuiltInBBAInterface::SendFrame(const u8* frame, u32 size) { std::lock_guard lock(m_mtx); diff --git a/Source/Core/Core/HW/EXI/EXI_DeviceEthernet.h b/Source/Core/Core/HW/EXI/EXI_DeviceEthernet.h index 3ca1f26b01..e69033ed2b 100644 --- a/Source/Core/Core/HW/EXI/EXI_DeviceEthernet.h +++ b/Source/Core/Core/HW/EXI/EXI_DeviceEthernet.h @@ -4,6 +4,8 @@ #pragma once #include +#include +#include #include #include @@ -13,7 +15,6 @@ #include -#include #include "Common/Flag.h" #include "Common/Network.h" #include "Core/HW/EXI/BBA/BuiltIn.h" @@ -445,6 +446,7 @@ private: std::string m_local_ip; u32 m_current_ip = 0; u32 m_router_ip = 0; + std::map m_arp_table; #if defined(WIN32) || defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \ defined(__OpenBSD__) || defined(__NetBSD__) || defined(__HAIKU__) std::array network_ref{}; // max 10 at same time, i think most gc game had a @@ -464,6 +466,7 @@ private: void HandleTCPFrame(const Common::TCPPacket& packet); void InitUDPPort(u16 port); void HandleUDPFrame(const Common::UDPPacket& packet); + const Common::MACAddress& ResolveAddress(u32 inet_ip); }; std::unique_ptr m_network_interface;