2525#define MSG_NOSIGNAL 0x00
2626#endif
2727
28+ #define MIN_VALID_SOCKET 20
29+
2830int inetLastErrno = 0 ; // TODO: since errno can only be read once, we should keep track the value to be used on sceNetInetGetErrno
2931
3032bool netInetInited = false ;
@@ -33,7 +35,7 @@ bool netInetInited = false;
3335InetSocket g_inetSockets[256 ];
3436
3537static int AllocInetSocket () {
36- for (int i = 1 ; i < ARRAY_SIZE (g_inetSockets); i++) {
38+ for (int i = MIN_VALID_SOCKET ; i < ARRAY_SIZE (g_inetSockets); i++) {
3739 if (g_inetSockets[i].state == SocketState::Unused) {
3840 return i;
3941 }
@@ -44,7 +46,7 @@ static int AllocInetSocket() {
4446}
4547
4648static bool GetInetSocket (int sock, InetSocket **inetSocket) {
47- if (sock < 1 || sock >= ARRAY_SIZE (g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) {
49+ if (sock < MIN_VALID_SOCKET || sock >= ARRAY_SIZE (g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) {
4850 *inetSocket = nullptr ;
4951 return false ;
5052 }
@@ -53,22 +55,8 @@ static bool GetInetSocket(int sock, InetSocket **inetSocket) {
5355}
5456
5557// Simplified mappers, only really useful in select/poll
56- static int GetInetSocketFromHostSocket (SOCKET hostSock) {
57- for (int i = 1 ; i < ARRAY_SIZE (g_inetSockets); i++) {
58- if (g_inetSockets[i].state == SocketState::Used && g_inetSockets[i].sock == hostSock) {
59- return i;
60- }
61- }
62- if (hostSock == 0 ) {
63- // Map 0 to 0, special case.
64- return 0 ;
65- }
66- _dbg_assert_ (false );
67- return -1 ;
68- }
69-
7058static SOCKET GetHostSocketFromInetSocket (int sock) {
71- if (sock < 1 || sock >= ARRAY_SIZE (g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) {
59+ if (sock < MIN_VALID_SOCKET || sock >= ARRAY_SIZE (g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) {
7260 _dbg_assert_ (false );
7361 return -1 ;
7462 }
@@ -265,7 +253,7 @@ int sceNetInetSelect(int nfds, u32 readfdsPtr, u32 writefdsPtr, u32 exceptfdsPtr
265253 // Save the mapping during setup.
266254 SOCKET hostSockets[256 ]{};
267255
268- for (int i = 0 ; i < nfds; i++) {
256+ for (int i = MIN_VALID_SOCKET ; i < nfds; i++) {
269257 if (readfds && (NetInetFD_ISSET (i, readfds))) {
270258 SOCKET sock = GetHostSocketFromInetSocket (i);
271259 hostSockets[i] = sock;
@@ -314,7 +302,7 @@ int sceNetInetSelect(int nfds, u32 readfdsPtr, u32 writefdsPtr, u32 exceptfdsPtr
314302 if (readfds != NULL ) NetInetFD_ZERO (readfds);
315303 if (writefds != NULL ) NetInetFD_ZERO (writefds);
316304 if (exceptfds != NULL ) NetInetFD_ZERO (exceptfds);
317- for (int i = 0 ; i < nfds; i++) {
305+ for (int i = MIN_VALID_SOCKET ; i < nfds; i++) {
318306 if (readfds && hostSockets[i] != 0 && FD_ISSET (hostSockets[i], &rdfds)) {
319307 NetInetFD_SET (i, readfds);
320308 }
0 commit comments