-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
sceNetInet socket remap #19827
Changes from 1 commit
303a03c
08f2bee
0f2bd65
8505e24
fcb3d63
c33ea84
3dc2a10
2c3f7f6
5026e92
155a9f9
a1744a6
b97b0e4
c9c5944
698b73d
728268b
77fcb18
f84ec05
cddd3d4
440fa80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "Core/HLE/SocketManager.h" | ||
#include "Common/Log.h" | ||
|
||
#include <mutex> | ||
|
||
// We use this array from 1 and forward. It's probably not a good idea to return 0 as a socket. | ||
InetSocket g_inetSockets[256]; | ||
static std::mutex g_socketMutex; // TODO: Remove once the adhoc thread is gone | ||
|
||
int AllocInetSocket() { | ||
std::lock_guard<std::mutex> guard(g_socketMutex); | ||
for (int i = MIN_VALID_INET_SOCKET; i < ARRAY_SIZE(g_inetSockets); i++) { | ||
if (g_inetSockets[i].state == SocketState::Unused) { | ||
return i; | ||
} | ||
} | ||
_dbg_assert_(false); | ||
ERROR_LOG(Log::sceNet, "Ran out of socket handles! This is BAD."); | ||
return 0; | ||
} | ||
|
||
bool GetInetSocket(int sock, InetSocket **inetSocket) { | ||
std::lock_guard<std::mutex> guard(g_socketMutex); | ||
if (sock < MIN_VALID_INET_SOCKET || sock >= ARRAY_SIZE(g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) { | ||
*inetSocket = nullptr; | ||
return false; | ||
} | ||
*inetSocket = &g_inetSockets[sock]; | ||
return true; | ||
} | ||
|
||
// Simplified mappers, only really useful in select/poll | ||
SOCKET GetHostSocketFromInetSocket(int sock) { | ||
std::lock_guard<std::mutex> guard(g_socketMutex); | ||
if (sock < MIN_VALID_INET_SOCKET || sock >= ARRAY_SIZE(g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) { | ||
_dbg_assert_(false); | ||
return -1; | ||
} | ||
if (sock == 0) { | ||
// Map 0 to 0, special case. | ||
return 0; | ||
} | ||
return g_inetSockets[sock].sock; | ||
} | ||
|
||
void CloseAllSockets() { | ||
for (auto &sock : g_inetSockets) { | ||
if (sock.state != SocketState::Unused) { | ||
closesocket(sock.sock); | ||
} | ||
sock.state = SocketState::Unused; | ||
sock.sock = 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include "Common/Net/SocketCompat.h" | ||
|
||
// Keep track of who's using a socket. | ||
enum class SocketState { | ||
Unused, | ||
UsedNetInet, | ||
UsedProAdhoc, | ||
}; | ||
|
||
// Internal socket state tracking | ||
struct InetSocket { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 PS: Sockets are in blocking mode by default after creation, thus the default value should be nonblocking = false. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
SocketState state; | ||
// NOTE: These are the PSP types for now | ||
int domain; | ||
int type; | ||
int protocol; | ||
// These are the host types for convenience. | ||
int hostDomain; | ||
int hostType; | ||
int hostProtocol; | ||
}; | ||
|
||
#define MIN_VALID_INET_SOCKET 20 | ||
#define VALID_INET_SOCKET_COUNT 256 | ||
|
||
extern InetSocket g_inetSockets[VALID_INET_SOCKET_COUNT]; | ||
|
||
int AllocInetSocket(); | ||
bool GetInetSocket(int sock, InetSocket **inetSocket); | ||
SOCKET GetHostSocketFromInetSocket(int sock); | ||
void CloseAllSockets(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we returned -1 in the case of failure to create a socket? just like a posix
socket
syscall:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The caller is responsible for the actual syscall return value. But yeah this will be made clearer in an upcoming commit.
(either way, haven't hit this yet)