Skip to content

sceNetInet socket remap #19827

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

Merged
merged 19 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
ImDebugger: Add Np and Sockets debugger windows, on a new Network menu
  • Loading branch information
hrydgard committed Jan 8, 2025
commit 698b73dd15e33e70e3da86cc56d4e04f0c4a9502
10 changes: 10 additions & 0 deletions Core/HLE/SocketManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,13 @@ void SocketManager::CloseAll() {
sock.sock = 0;
}
}

const char *SocketStateToString(SocketState state) {
switch (state) {
case SocketState::Unused: return "unused";
case SocketState::UsedNetInet: return "netInet";
case SocketState::UsedProAdhoc: return "proAdhoc";
default:
return "N/A";
}
}
2 changes: 2 additions & 0 deletions Core/HLE/SocketManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ enum class SocketState {
UsedProAdhoc,
};

const char *SocketStateToString(SocketState state);

// Internal socket state tracking
struct InetSocket {
Copy link
Collaborator

@anr2me anr2me Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should store non-blocking state here too, and probably some other value that can be set using SetSockOpt too just like non-blocking state, but non-blocking state alone should be sufficient (at least for TCP/UDP socket, while PDP/PTP sockets might need some additional info to be stored, you can checked the AdhocSocket struct for this), since we will need to retrieve the non-blocking state in order to simulate blocking mode later.

PS: Sockets are in blocking mode by default after creation, thus the default value should be nonblocking = false.

Copy link
Collaborator

@anr2me anr2me Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, since we have stored the socket protocol here, we can replace these code at sceNetInet.cpp by forwarding only TCP or UDP instead of forwarding both protocol:

// Enable Port-forwarding
	// TODO: Check the socket type/protocol for SOCK_STREAM/SOCK_DGRAM or IPPROTO_TCP/IPPROTO_UDP instead of forwarding both protocol
	// InetSocket* sock = pspSockets.Get<InetSocket>(socket, error);
	// UPnP_Add((sock->type == SOCK_STREAM)? IP_PROTOCOL_TCP: IP_PROTOCOL_UDP, port, port);	
	unsigned short port = ntohs(saddr.in.sin_port);
	UPnP_Add(IP_PROTOCOL_UDP, port, port);
	UPnP_Add(IP_PROTOCOL_TCP, port, port);

I opened both TCP and UDP because i use the native socket directly and there is noway to retrieve the socket protocol (at least the easy way as i remembered).

Edit: oops, you already did this >.<

SOCKET sock; // native socket
Expand Down
81 changes: 81 additions & 0 deletions UI/ImDebugger/ImDebugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "Core/Debugger/SymbolMap.h"
#include "Core/MemMap.h"
#include "Core/HLE/HLE.h"
#include "Core/HLE/SocketManager.h"
#include "Core/HLE/NetInetConstants.h"
#include "Core/HLE/sceNp.h"
#include "Common/System/Request.h"

#include "Core/HLE/sceAtrac.h"
Expand Down Expand Up @@ -475,6 +478,69 @@ static void DrawKernelObjects(ImConfig &cfg) {
ImGui::End();
}

static void DrawNp(ImConfig &cfg) {
if (!ImGui::Begin("NP", &cfg.npOpen)) {
ImGui::End();
return;
}

ImGui::Text("Signed in: %d", npSigninState);
ImGui::Text("Title ID: %s", npTitleId.data);

SceNpId id{};
NpGetNpId(&id);
ImGui::Text("User Handle: %s", id.handle);
ImGui::End();
}

static void DrawSockets(ImConfig &cfg) {
if (!ImGui::Begin("Sockets", &cfg.socketsOpen)) {
ImGui::End();
return;
}
if (ImGui::BeginTable("sock", 7, ImGuiTableFlags_RowBg | ImGuiTableFlags_BordersH | ImGuiTableFlags_Resizable)) {
ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Host", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Non-blocking", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Created by", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Domain", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Type", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableSetupColumn("Protocol", ImGuiTableColumnFlags_WidthStretch);

ImGui::TableHeadersRow();

for (int i = SocketManager::MIN_VALID_INET_SOCKET; i < SocketManager::VALID_INET_SOCKET_COUNT; i++) {
InetSocket *inetSocket;
if (!g_socketManager.GetInetSocket(i, &inetSocket)) {
continue;
}

ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::Text("%d", i);
ImGui::TableNextColumn();
ImGui::Text("%d", (int)inetSocket->sock);
ImGui::TableNextColumn();
ImGui::TextUnformatted(inetSocket->nonblocking ? "Non-blocking" : "Blocking");
ImGui::TableNextColumn();
ImGui::TextUnformatted(SocketStateToString(inetSocket->state));
ImGui::TableNextColumn();
std::string str = inetSocketDomain2str(inetSocket->domain);
ImGui::TextUnformatted(str.c_str());
ImGui::TableNextColumn();
str = inetSocketType2str(inetSocket->type);
ImGui::TextUnformatted(str.c_str());
ImGui::TableNextColumn();
str = inetSocketProto2str(inetSocket->protocol);
ImGui::TextUnformatted(str.c_str());
ImGui::TableNextColumn();
}

ImGui::EndTable();
}
ImGui::End();
}

static const char *MemCheckConditionToString(MemCheckCondition cond) {
switch (cond) {
case MEMCHECK_READ: return "Read";
Expand Down Expand Up @@ -1070,6 +1136,11 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebu
ImGui::MenuItem("Decoder contexts", nullptr, &cfg_.audioDecodersOpen);
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Network")) {
ImGui::MenuItem("Sockets", nullptr, &cfg_.socketsOpen);
ImGui::MenuItem("NP", nullptr, &cfg_.npOpen);
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Tools")) {
ImGui::MenuItem("Debug stats", nullptr, &cfg_.debugStatsOpen);
ImGui::MenuItem("Struct viewer", nullptr, &cfg_.structViewerOpen);
Expand Down Expand Up @@ -1188,6 +1259,14 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebu
}
}

if (cfg_.socketsOpen) {
DrawSockets(cfg_);
}

if (cfg_.npOpen) {
DrawNp(cfg_);
}

// Process UI commands
switch (control.command.cmd) {
case ImCmd::SHOW_IN_CPU_DISASM:
Expand Down Expand Up @@ -1543,6 +1622,8 @@ void ImConfig::SyncConfig(IniFile *ini, bool save) {
sync.Sync("geDebuggerOpen", &geDebuggerOpen, false);
sync.Sync("geStateOpen", &geStateOpen, false);
sync.Sync("schedulerOpen", &schedulerOpen, false);
sync.Sync("socketsOpen", &socketsOpen, false);
sync.Sync("npOpen", &npOpen, false);
sync.Sync("pixelViewerOpen", &pixelViewerOpen, false);
for (int i = 0; i < 4; i++) {
char name[64];
Expand Down
3 changes: 3 additions & 0 deletions UI/ImDebugger/ImDebugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Common/System/Request.h"

#include "Core/Core.h"
#include "Core/HLE/SocketManager.h"

#include "Core/Debugger/DisassemblyManager.h"
#include "Core/Debugger/DebugInterface.h"
Expand Down Expand Up @@ -140,6 +141,8 @@ struct ImConfig {
bool schedulerOpen;
bool watchOpen;
bool pixelViewerOpen;
bool npOpen;
bool socketsOpen;
bool memViewOpen[4];

// HLE explorer settings
Expand Down