Split networking code into its own target (#2091)

This commit is contained in:
Jesse Talavera 2024-07-14 11:03:21 -04:00 committed by GitHub
parent a812a43bda
commit 94ba7c1594
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
213 changed files with 159 additions and 108 deletions

18
.gitattributes vendored Normal file
View File

@ -0,0 +1,18 @@
# Vendored Dependencies
src/frontend/glad/** linguist-vendored
src/frontend/qt_sdl/gif-h/** linguist-vendored
src/frontend/qt_sdl/toml/** linguist-vendored
src/net/libslirp/** linguist-vendored
src/net/pcap/** linguist-vendored
src/sha1/** linguist-vendored
src/teakra/** linguist-vendored
src/tiny-AES-c/** linguist-vendored
src/xxhash/** linguist-vendored
# A handful of custom files embedded in the vendored dependencies
## Ad-hoc CMakeLists.txt for melonDS
!src/net/libslirp/src/CMakeLists.txt -linguist-vendored
## glib stub
!src/net/libslirp/src/glib/** -linguist-vendored

View File

@ -256,6 +256,9 @@ Semaphore* Semaphore_Create();
void Semaphore_Free(Semaphore* sema);
void Semaphore_Reset(Semaphore* sema);
void Semaphore_Wait(Semaphore* sema);
/// Waits for the semaphore to be signaled, or until the timeout (in milliseconds) expires.
/// If the timeout is 0, then don't wait; return immediately if the semaphore is not signaled.
bool Semaphore_TryWait(Semaphore* sema, int timeout_ms = 0);
void Semaphore_Post(Semaphore* sema, int count = 1);
struct Mutex;

View File

@ -1 +0,0 @@
../IN_ndp/ndp.pcap

View File

@ -1 +0,0 @@
../IN_udp/DNS_freedesktop_1-1-1-1.pcap

View File

@ -1 +0,0 @@
../IN_dhcp/dhcp.pkt

View File

@ -1 +0,0 @@
../IN_dhcp/dhcp_capture.pcap

View File

@ -1 +0,0 @@
../IN_icmp/icmp_capture.pcap

View File

@ -1 +0,0 @@
../IN_tcp/nc-10.0.2.2-8080.pcap

View File

@ -1 +0,0 @@
../IN_tcp/nc-ident.pcap

View File

@ -1 +0,0 @@
../IN_icmp/ping_10-0-2-2.pcap

View File

@ -1 +0,0 @@
../IN_tcp/tcp_qemucapt.pcap

View File

@ -1 +0,0 @@
../IN_tftp/tftp-get-blah.pkt

View File

@ -1 +0,0 @@
../IN_tftp/tftp_capture.pcap

View File

@ -1 +0,0 @@
../IN_tftp/tftp_get_libslirp-txt.pcap

View File

@ -1 +0,0 @@
../IN_udp6/DNS_freedesktop_1-1-1-1.pcap

View File

@ -1 +0,0 @@
../IN_icmp6/icmp_capture.pcap

View File

@ -1 +0,0 @@
../IN_icmp6/ping_10-0-2-2.pcap

View File

@ -1 +0,0 @@
../IN_tcp6/tcp_qemucapt.pcap

View File

@ -1 +0,0 @@
../IN_udp6/tftp_capture.pcap

View File

@ -1 +0,0 @@
../IN_udp6/tftp_get_libslirp-txt.pcap

View File

@ -1 +0,0 @@
IN_tcp

View File

@ -1 +0,0 @@
IN_tcp

View File

@ -1 +0,0 @@
IN_tcp6

View File

@ -1 +0,0 @@
IN_tcp6

View File

@ -1 +0,0 @@
IN_udp

View File

@ -1 +0,0 @@
../IN_dhcp/dhcp.pkt

View File

@ -1 +0,0 @@
../IN_dhcp/dhcp_capture.pcap

View File

@ -1 +0,0 @@
../IN_tftp/tftp-get-blah.pkt

View File

@ -1 +0,0 @@
../IN_tftp/tftp_capture.pcap

View File

@ -1 +0,0 @@
../IN_tftp/tftp_get_libslirp-txt.pcap

View File

@ -1 +0,0 @@
IN_udp6

View File

@ -1 +0,0 @@
../IN_tftp6/tftp_capture.pcap

View File

@ -1 +0,0 @@
../IN_tftp6/tftp_get_libslirp-txt.pcap

View File

@ -31,11 +31,6 @@ set(SOURCES_QT_SDL
ROMInfoDialog.cpp
RAMInfoDialog.cpp
TitleManagerDialog.cpp
PacketDispatcher.cpp
Net.cpp
Net_PCap.cpp
Net_Slirp.cpp
LocalMP.cpp
OSD_shaders.h
font.h
Platform.cpp
@ -93,16 +88,11 @@ add_compile_definitions(ARCHIVE_SUPPORT_ENABLED)
add_executable(melonDS ${SOURCES_QT_SDL})
option(USE_SYSTEM_LIBSLIRP "Use system libslirp instead of the bundled version" OFF)
if (USE_SYSTEM_LIBSLIRP)
pkg_check_modules(Slirp REQUIRED IMPORTED_TARGET slirp)
target_link_libraries(melonDS PRIVATE PkgConfig::Slirp)
else()
add_subdirectory("../libslirp"
"${CMAKE_BINARY_DIR}/libslirp"
EXCLUDE_FROM_ALL)
target_link_libraries(melonDS PRIVATE slirp)
endif()
add_subdirectory("../../net"
"${CMAKE_BINARY_DIR}/net"
)
target_link_libraries(melonDS PRIVATE net-utils)
if (WIN32)
target_link_libraries(melonDS PUBLIC opengl32)

View File

@ -351,6 +351,14 @@ void Semaphore_Wait(Semaphore* sema)
((QSemaphore*) sema)->acquire();
}
bool Semaphore_TryWait(Semaphore* sema, int timeout_ms)
{
if (!timeout_ms)
return ((QSemaphore*)sema)->tryAcquire(1);
return ((QSemaphore*)sema)->tryAcquire(1, timeout_ms);
}
void Semaphore_Post(Semaphore* sema, int count)
{
((QSemaphore*) sema)->release(count);

View File

@ -25,6 +25,7 @@
#include "main.h"
#include "Net.h"
#include "Net_PCap.h"
#include "WifiSettingsDialog.h"
#include "ui_WifiSettingsDialog.h"
@ -109,7 +110,10 @@ void WifiSettingsDialog::done(int r)
}
Net_PCap::DeInit();
Net::Init();
Config::Table cfg = Config::GetGlobalTable();
bool direct = cfg.GetBool("LAN.DirectMode");
std::string devicename = cfg.GetString("LAN.Device");
Net::Init(direct, devicename.c_str());
QDialog::done(r);

View File

@ -274,7 +274,12 @@ int main(int argc, char** argv)
}
LocalMP::Init();
Net::Init();
{
Config::Table cfg = Config::GetGlobalTable();
bool direct = cfg.GetBool("LAN.DirectMode");
std::string devicename = cfg.GetString("LAN.Device");
Net::Init(direct, devicename.c_str());
}
createEmuInstance();

20
src/net/CMakeLists.txt Normal file
View File

@ -0,0 +1,20 @@
add_library(net-utils STATIC
Net.cpp
Net_PCap.cpp
Net_Slirp.cpp
PacketDispatcher.cpp
LocalMP.cpp
)
target_include_directories(net-utils PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(net-utils PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/..")
option(USE_SYSTEM_LIBSLIRP "Use system libslirp instead of the bundled version" OFF)
if (USE_SYSTEM_LIBSLIRP)
pkg_check_modules(Slirp REQUIRED IMPORTED_TARGET slirp)
target_link_libraries(net-utils PRIVATE PkgConfig::Slirp)
else()
add_subdirectory(libslirp EXCLUDE_FROM_ALL)
target_link_libraries(net-utils PRIVATE slirp)
endif()

View File

@ -17,11 +17,10 @@
*/
#include <cstring>
#include <QMutex>
#include <QSemaphore>
#include "LocalMP.h"
#include "Platform.h"
#include "types.h"
using namespace melonDS;
using namespace melonDS::Platform;
@ -54,7 +53,7 @@ const u32 kPacketQueueSize = 0x10000;
const u32 kReplyQueueSize = 0x10000;
const u32 kMaxFrameSize = 0x948;
QMutex MPQueueLock;
Mutex* MPQueueLock;
MPStatusData MPStatus;
u8 MPPacketQueue[kPacketQueueSize];
u8 MPReplyQueue[kReplyQueueSize];
@ -66,39 +65,37 @@ int RecvTimeout;
int LastHostID;
QSemaphore SemPool[32];
Semaphore* SemPool[32];
void SemPoolInit()
{
for (int i = 0; i < 32; i++)
{
SemPool[i].acquire(SemPool[i].available());
SemPool[i] = Semaphore_Create();
}
}
bool SemPost(int num)
{
SemPool[num].release(1);
Semaphore_Post(SemPool[num]);
return true;
}
bool SemWait(int num, int timeout)
{
if (!timeout)
return SemPool[num].tryAcquire(1);
return SemPool[num].tryAcquire(1, timeout);
return Semaphore_TryWait(SemPool[num], timeout);
}
void SemReset(int num)
{
SemPool[num].acquire(SemPool[num].available());
Semaphore_Reset(SemPool[num]);
}
bool Init()
{
MPQueueLock.lock();
MPQueueLock = Mutex_Create();
Mutex_Lock(MPQueueLock);
memset(MPPacketQueue, 0, kPacketQueueSize);
memset(MPReplyQueue, 0, kReplyQueueSize);
@ -106,7 +103,7 @@ bool Init()
memset(PacketReadOffset, 0, sizeof(PacketReadOffset));
memset(ReplyReadOffset, 0, sizeof(ReplyReadOffset));
MPQueueLock.unlock();
Mutex_Unlock(MPQueueLock);
// prepare semaphores
// semaphores 0-15: regular frames; semaphore I is posted when instance I needs to process a new frame
@ -125,6 +122,13 @@ bool Init()
void DeInit()
{
for (int i = 0; i < 32; i++)
{
Semaphore_Free(SemPool[i]);
SemPool[i] = nullptr;
}
Mutex_Free(MPQueueLock);
}
void SetRecvTimeout(int timeout)
@ -134,20 +138,20 @@ void SetRecvTimeout(int timeout)
void Begin(int inst)
{
MPQueueLock.lock();
Mutex_Lock(MPQueueLock);
PacketReadOffset[inst] = MPStatus.PacketWriteOffset;
ReplyReadOffset[inst] = MPStatus.ReplyWriteOffset;
SemReset(inst);
SemReset(16+inst);
MPStatus.ConnectedBitmask |= (1 << inst);
MPQueueLock.unlock();
Mutex_Unlock(MPQueueLock);
}
void End(int inst)
{
MPQueueLock.lock();
Mutex_Lock(MPQueueLock);
MPStatus.ConnectedBitmask &= ~(1 << inst);
MPQueueLock.unlock();
Mutex_Unlock(MPQueueLock);
}
void FIFORead(int inst, int fifo, void* buf, int len)
@ -228,7 +232,7 @@ int SendPacketGeneric(int inst, u32 type, u8* packet, int len, u64 timestamp)
return 0;
}
MPQueueLock.lock();
Mutex_Lock(MPQueueLock);
u16 mask = MPStatus.ConnectedBitmask;
@ -261,7 +265,7 @@ int SendPacketGeneric(int inst, u32 type, u8* packet, int len, u64 timestamp)
MPStatus.MPReplyBitmask |= (1 << inst);
}
MPQueueLock.unlock();
Mutex_Unlock(MPQueueLock);
if (type == 2)
{
@ -288,7 +292,7 @@ int RecvPacketGeneric(int inst, u8* packet, bool block, u64* timestamp)
return 0;
}
MPQueueLock.lock();
Mutex_Lock(MPQueueLock);
MPPacketHeader pktheader;
FIFORead(inst, 0, &pktheader, sizeof(pktheader));
@ -298,7 +302,7 @@ int RecvPacketGeneric(int inst, u8* packet, bool block, u64* timestamp)
Log(LogLevel::Warn, "PACKET FIFO OVERFLOW\n");
PacketReadOffset[inst] = MPStatus.PacketWriteOffset;
SemReset(inst);
MPQueueLock.unlock();
Mutex_Unlock(MPQueueLock);
return 0;
}
@ -309,7 +313,7 @@ int RecvPacketGeneric(int inst, u8* packet, bool block, u64* timestamp)
if (PacketReadOffset[inst] >= kPacketQueueSize)
PacketReadOffset[inst] -= kPacketQueueSize;
MPQueueLock.unlock();
Mutex_Unlock(MPQueueLock);
continue;
}
@ -322,7 +326,7 @@ int RecvPacketGeneric(int inst, u8* packet, bool block, u64* timestamp)
}
if (timestamp) *timestamp = pktheader.Timestamp;
MPQueueLock.unlock();
Mutex_Unlock(MPQueueLock);
return pktheader.Length;
}
}
@ -388,7 +392,7 @@ u16 RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask)
return ret;
}
MPQueueLock.lock();
Mutex_Lock(MPQueueLock);
MPPacketHeader pktheader;
FIFORead(inst, 1, &pktheader, sizeof(pktheader));
@ -398,7 +402,7 @@ u16 RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask)
Log(LogLevel::Warn, "REPLY FIFO OVERFLOW\n");
ReplyReadOffset[inst] = MPStatus.ReplyWriteOffset;
SemReset(16+inst);
MPQueueLock.unlock();
Mutex_Unlock(MPQueueLock);
return 0;
}
@ -410,7 +414,7 @@ u16 RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask)
if (ReplyReadOffset[inst] >= kReplyQueueSize)
ReplyReadOffset[inst] -= kReplyQueueSize;
MPQueueLock.unlock();
Mutex_Unlock(MPQueueLock);
continue;
}
@ -427,11 +431,11 @@ u16 RecvReplies(int inst, u8* packets, u64 timestamp, u16 aidmask)
{
// all the clients have sent their reply
MPQueueLock.unlock();
Mutex_Unlock(MPQueueLock);
return ret;
}
MPQueueLock.unlock();
Mutex_Unlock(MPQueueLock);
}
}

View File

@ -18,11 +18,11 @@
#include <stdio.h>
#include <string.h>
#include <QMutex>
#include "Net.h"
#include "Net_PCap.h"
#include "Net_Slirp.h"
#include "PacketDispatcher.h"
#include "Platform.h"
#include "Config.h"
using namespace melonDS;
@ -38,18 +38,17 @@ bool DirectMode;
PacketDispatcher Dispatcher;
bool Init()
bool Init(bool direct, const char* devicename)
{
if (Inited) DeInit();
Dispatcher.clear();
Config::Table cfg = Config::GetGlobalTable();
DirectMode = cfg.GetBool("LAN.DirectMode");
DirectMode = direct;
bool ret = false;
if (DirectMode)
ret = Net_PCap::Init();
ret = Net_PCap::Init(devicename);
else
ret = Net_Slirp::Init();

View File

@ -20,14 +20,16 @@
#define NET_H
#include "types.h"
#include "Net_PCap.h"
#include "Net_Slirp.h"
namespace Net
{
using namespace melonDS;
bool Init();
///
/// @param direct Whether to use direct or indirect mode
/// @param devicename The name of the network device to use; ignored if direct is false
/// @return true if initialization succeeded
bool Init(bool direct, const char* devicename);
void DeInit();
void RegisterInstance(int inst);

View File

@ -19,9 +19,8 @@
#include <string.h>
#include <pcap/pcap.h>
#include "Net.h"
#include "Config.h"
#include "Net_PCap.h"
#include "Platform.h"
#include "main.h"
#ifdef __WIN32__
#include <iphlpapi.h>
@ -299,7 +298,7 @@ bool InitAdapterList()
return true;
}
bool Init()
bool Init(std::string_view devicename)
{
if (!PCapLib) PCapAdapter = nullptr;
if (PCapAdapter) pcap_close(PCapAdapter);
@ -307,12 +306,10 @@ bool Init()
InitAdapterList();
// open pcap device
Config::Table cfg = Config::GetGlobalTable();
std::string devicename = cfg.GetString("LAN.Device");
PCapAdapterData = &Adapters[0];
for (int i = 0; i < NumAdapters; i++)
{
if (!strncmp(Adapters[i].DeviceName, devicename.c_str(), 128))
if (!strncmp(Adapters[i].DeviceName, devicename.data(), 128))
PCapAdapterData = &Adapters[i];
}

View File

@ -19,6 +19,7 @@
#ifndef NET_PCAP_H
#define NET_PCAP_H
#include <string_view>
#include "types.h"
namespace Net_PCap
@ -41,7 +42,7 @@ extern int NumAdapters;
bool InitAdapterList();
bool Init();
bool Init(std::string_view devicename);
void DeInit();
int SendPacket(u8* data, int len);

View File

@ -31,7 +31,7 @@ struct PacketHeader
const u32 kPacketMagic = 0x4B504C4D;
PacketDispatcher::PacketDispatcher()
PacketDispatcher::PacketDispatcher() : mutex(Platform::Mutex_Create())
{
instanceMask = 0;
memset(packetQueues, 0, sizeof(packetQueues));
@ -39,34 +39,34 @@ PacketDispatcher::PacketDispatcher()
PacketDispatcher::~PacketDispatcher()
{
//
Platform::Mutex_Free(mutex);
}
void PacketDispatcher::registerInstance(int inst)
{
mutex.lock();
Mutex_Lock(mutex);
instanceMask |= (1 << inst);
packetQueues[inst] = new PacketQueue();
mutex.unlock();
Mutex_Unlock(mutex);
}
void PacketDispatcher::unregisterInstance(int inst)
{
mutex.lock();
Mutex_Lock(mutex);
instanceMask &= ~(1 << inst);
delete packetQueues[inst];
mutex.unlock();
Mutex_Unlock(mutex);
}
void PacketDispatcher::clear()
{
mutex.lock();
Mutex_Lock(mutex);
for (int i = 0; i < 16; i++)
{
if (!(instanceMask & (1 << i)))
@ -75,7 +75,7 @@ void PacketDispatcher::clear()
PacketQueue* queue = packetQueues[i];
queue->Clear();
}
mutex.unlock();
Mutex_Unlock(mutex);
}
@ -99,7 +99,7 @@ void PacketDispatcher::sendPacket(const void* header, int headerlen, const void*
int totallen = sizeof(phdr) + headerlen + datalen;
mutex.lock();
Mutex_Lock(mutex);
for (int i = 0; i < 16; i++)
{
if (!(recv_mask & (1 << i)))
@ -119,7 +119,7 @@ void PacketDispatcher::sendPacket(const void* header, int headerlen, const void*
if (headerlen) queue->Write(header, headerlen);
if (datalen) queue->Write(data, datalen);
}
mutex.unlock();
Mutex_Unlock(mutex);
}
bool PacketDispatcher::recvPacket(void *header, int *headerlen, void *data, int *datalen, int receiver)
@ -127,19 +127,19 @@ bool PacketDispatcher::recvPacket(void *header, int *headerlen, void *data, int
if ((!header) && (!data)) return false;
if (receiver < 0 || receiver > 15) return false;
mutex.lock();
Mutex_Lock(mutex);
PacketQueue* queue = packetQueues[receiver];
PacketHeader phdr;
if (!queue->Read(&phdr, sizeof(phdr)))
{
mutex.unlock();
Mutex_Unlock(mutex);
return false;
}
if (phdr.magic != kPacketMagic)
{
mutex.unlock();
Mutex_Unlock(mutex);
return false;
}
@ -157,6 +157,6 @@ bool PacketDispatcher::recvPacket(void *header, int *headerlen, void *data, int
else queue->Skip(phdr.dataLength);
}
mutex.unlock();
Mutex_Unlock(mutex);
return true;
}

View File

@ -19,7 +19,7 @@
#ifndef PACKETDISPATCHER_H
#define PACKETDISPATCHER_H
#include <QMutex>
#include "Platform.h"
#include "types.h"
#include "FIFO.h"
@ -40,7 +40,7 @@ public:
bool recvPacket(void* header, int* headerlen, void* data, int* datalen, int receiver);
private:
QMutex mutex;
melonDS::Platform::Mutex* mutex;
melonDS::u16 instanceMask;
PacketQueue* packetQueues[16];
};

View File

@ -0,0 +1 @@
../IN_ndp/ndp.pcap

View File

@ -0,0 +1 @@
../IN_udp/DNS_freedesktop_1-1-1-1.pcap

View File

@ -0,0 +1 @@
../IN_dhcp/dhcp.pkt

View File

@ -0,0 +1 @@
../IN_dhcp/dhcp_capture.pcap

View File

@ -0,0 +1 @@
../IN_icmp/icmp_capture.pcap

View File

@ -0,0 +1 @@
../IN_tcp/nc-10.0.2.2-8080.pcap

View File

@ -0,0 +1 @@
../IN_tcp/nc-ident.pcap

View File

@ -0,0 +1 @@
../IN_icmp/ping_10-0-2-2.pcap

View File

@ -0,0 +1 @@
../IN_tcp/tcp_qemucapt.pcap

View File

@ -0,0 +1 @@
../IN_tftp/tftp-get-blah.pkt

View File

@ -0,0 +1 @@
../IN_tftp/tftp_capture.pcap

View File

@ -0,0 +1 @@
../IN_tftp/tftp_get_libslirp-txt.pcap

View File

@ -0,0 +1 @@
../IN_udp6/DNS_freedesktop_1-1-1-1.pcap

View File

@ -0,0 +1 @@
../IN_icmp6/icmp_capture.pcap

View File

@ -0,0 +1 @@
../IN_icmp6/ping_10-0-2-2.pcap

View File

@ -0,0 +1 @@
../IN_tcp6/tcp_qemucapt.pcap

View File

@ -0,0 +1 @@
../IN_udp6/tftp_capture.pcap

View File

@ -0,0 +1 @@
../IN_udp6/tftp_get_libslirp-txt.pcap

1
src/net/libslirp/fuzzing/IN_tcp-d vendored Normal file
View File

@ -0,0 +1 @@
IN_tcp

1
src/net/libslirp/fuzzing/IN_tcp-h vendored Normal file
View File

@ -0,0 +1 @@
IN_tcp

1
src/net/libslirp/fuzzing/IN_tcp6-d vendored Normal file
View File

@ -0,0 +1 @@
IN_tcp6

1
src/net/libslirp/fuzzing/IN_tcp6-h vendored Normal file
View File

@ -0,0 +1 @@
IN_tcp6

1
src/net/libslirp/fuzzing/IN_udp-h vendored Normal file
View File

@ -0,0 +1 @@
IN_udp

View File

@ -0,0 +1 @@
../IN_dhcp/dhcp.pkt

View File

@ -0,0 +1 @@
../IN_dhcp/dhcp_capture.pcap

View File

@ -0,0 +1 @@
../IN_tftp/tftp-get-blah.pkt

Some files were not shown because too many files have changed in this diff Show More