diff --git a/Core/HLE/proAdhoc.cpp b/Core/HLE/proAdhoc.cpp index 17e675e175..a7619de4a9 100644 --- a/Core/HLE/proAdhoc.cpp +++ b/Core/HLE/proAdhoc.cpp @@ -21,6 +21,10 @@ // This is a direct port of Coldbird's code from http://code.google.com/p/aemu/ // All credit goes to him! +#if !defined(_WIN32) +#include +#endif + #include #include "util/text/parsers.h" #include "Core/Core.h" @@ -63,6 +67,10 @@ bool chatScreenVisible = false; bool updateChatScreen = false; int newChat = 0; +bool isLocalServer = false; +uint8_t PPSSPP_ID = 0; +sockaddr localIP; // This might serves the same purpose with existing "localip" above, but since this is copied from my old code so here it is (too lazy to rewrite the code) + int isLocalMAC(const SceNetEtherAddr * addr) { SceNetEtherAddr saddr; getLocalMac(&saddr); @@ -1345,7 +1353,7 @@ int getActivePeerCount(void) { int getLocalIp(sockaddr_in * SocketAddress){ #if defined(_WIN32) // Get local host name - char szHostName[128] = ""; + char szHostName[256] = ""; if(::gethostname(szHostName, sizeof(szHostName))) { // Error handling @@ -1355,11 +1363,26 @@ int getLocalIp(sockaddr_in * SocketAddress){ pHost = ::gethostbyname(szHostName); if(pHost) { memcpy(&SocketAddress->sin_addr, pHost->h_addr_list[0], pHost->h_length); + if (isLocalServer) { + SocketAddress->sin_addr = ((sockaddr_in*)&localIP)->sin_addr; + } return 0; } return -1; #else memcpy(&SocketAddress->sin_addr, &localip, sizeof(uint32_t)); + char szHostName[256] = ""; + gethostname(szHostName, sizeof(szHostName)); + struct hostent* pHost = 0; + pHost = gethostbyname(szHostName); + if (pHost) { + memcpy(&SocketAddress->sin_addr, pHost->h_addr_list[0], pHost->h_length); + if (isLocalServer) { + SocketAddress->sin_addr = ((sockaddr_in*)&localIP)->sin_addr; + } + return 0; + } + //return -1; return 0; #endif } @@ -1369,12 +1392,19 @@ uint32_t getLocalIp(int sock) { localAddr.sin_addr.s_addr = INADDR_ANY; socklen_t addrLen = sizeof(localAddr); getsockname(sock, (struct sockaddr*)&localAddr, &addrLen); + if (isLocalServer) { + localAddr.sin_addr = ((sockaddr_in*)&localIP)->sin_addr; + } return localAddr.sin_addr.s_addr; } void getLocalMac(SceNetEtherAddr * addr){ // Read MAC Address from config uint8_t mac[ETHER_ADDR_LEN] = {0}; + if (PPSSPP_ID > 1) { + memset(&mac, PPSSPP_ID, sizeof(mac)); + } + else if (!ParseMacAddress(g_Config.sMACAddress.c_str(), mac)) { ERROR_LOG(SCENET, "Error parsing mac address %s", g_Config.sMACAddress.c_str()); } @@ -1486,6 +1516,23 @@ int initNetwork(SceNetAdhocctlAdhocId *adhoc_id){ break; } } + + freeaddrinfo(resultAddr); + + // If Server is at localhost Try to Bind socket to specific adapter before connecting to prevent 2nd instance being recognized as already existing 127.0.0.1 by AdhocServer + // (may not works in WinXP/2003 for IPv4 due to "Weak End System" model) + if (((uint8_t*)&serverIp.s_addr)[0] == 0x7f) { // (serverIp.S_un.S_un_b.s_b1 == 0x7f) + int on = 1; + setsockopt(metasocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)); + setsockopt(metasocket, SOL_SOCKET, SO_DONTROUTE, (const char*)&on, sizeof(on)); + + ((struct sockaddr_in*) & localIP)->sin_port = 0; + // Bind Local Address to Socket + iResult = bind(metasocket, (struct sockaddr*) & localIP, sizeof(sockaddr)); + if (iResult == SOCKET_ERROR) { + ERROR_LOG(SCENET, "Bind to alternate localhost[%s] failed(%i).", inet_ntoa(((struct sockaddr_in*) & localIP)->sin_addr), iResult); + } + } memset(¶meter, 0, sizeof(parameter)); strcpy((char *)¶meter.nickname.data, g_Config.sNickName.c_str()); @@ -1537,7 +1584,7 @@ bool resolveIP(uint32_t ip, SceNetEtherAddr * mac) { getLocalIp(&addr); uint32_t localIp = addr.sin_addr.s_addr; - if (ip == localIp){ + if (ip == localIp || ip == ((sockaddr_in*)&localIP)->sin_addr.s_addr){ getLocalMac(mac); return true; } diff --git a/Core/HLE/proAdhoc.h b/Core/HLE/proAdhoc.h index 1480c03026..21470fc7a3 100644 --- a/Core/HLE/proAdhoc.h +++ b/Core/HLE/proAdhoc.h @@ -797,6 +797,9 @@ extern SceNetAdhocPtpStat * ptp[255]; extern std::map adhocctlHandlers; extern uint16_t portOffset; +extern bool isLocalServer; +extern uint8_t PPSSPP_ID; +extern sockaddr localIP; extern uint32_t fakePoolSize; extern SceNetAdhocMatchingContext * contexts; extern int one; diff --git a/Core/HLE/proAdhocServer.cpp b/Core/HLE/proAdhocServer.cpp index e5e0940a71..54eeefd09f 100644 --- a/Core/HLE/proAdhocServer.cpp +++ b/Core/HLE/proAdhocServer.cpp @@ -498,7 +498,7 @@ void login_user_stream(int fd, uint32_t ip) if (u != NULL) { // IP Already existed uint8_t * ip4 = (uint8_t *)&u->resolver.ip; - INFO_LOG(SCENET, "AdhocServer: Already Existing IP: %u.%u.%u.%u\n", ip4[0], ip4[1], ip4[2], ip4[3]); + WARN_LOG(SCENET, "AdhocServer: Already Existing IP: %u.%u.%u.%u\n", ip4[0], ip4[1], ip4[2], ip4[3]); } // Unique IP Address @@ -567,6 +567,15 @@ void login_user_data(SceNetAdhocctlUserNode * user, SceNetAdhocctlLoginPacketC2S // Valid Packet Data if(valid_product_code == 1 && memcmp(&data->mac, "\xFF\xFF\xFF\xFF\xFF\xFF", sizeof(data->mac)) != 0 && memcmp(&data->mac, "\x00\x00\x00\x00\x00\x00", sizeof(data->mac)) != 0 && data->name.data[0] != 0) { + // Check for duplicated MAC as most games identify Players by MAC + SceNetAdhocctlUserNode* u = _db_user; + while (u != NULL && !IsMatch(u->resolver.mac, data->mac)) u = u->next; + + if (u != NULL) { // MAC Already existed + uint8_t* ip4 = (uint8_t*)&u->resolver.ip; + WARN_LOG(SCENET, "AdhocServer: Already Existing MAC: %02X:%02X:%02X:%02X:%02X:%02X [%u.%u.%u.%u]\n", data->mac.data[0], data->mac.data[1], data->mac.data[2], data->mac.data[3], data->mac.data[4], data->mac.data[5], ip4[0], ip4[1], ip4[2], ip4[3]); + } + // Game Product Override game_product_override(&data->game); @@ -631,7 +640,7 @@ void login_user_data(SceNetAdhocctlUserNode * user, SceNetAdhocctlLoginPacketC2S { // Notify User uint8_t * ip = (uint8_t *)&user->resolver.ip; - INFO_LOG(SCENET, "AdhocServer: Invalid Login Packet Contents from %u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]); + WARN_LOG(SCENET, "AdhocServer: Invalid Login Packet Contents from %u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]); } // Logout User - Out of Memory or Invalid Arguments @@ -694,7 +703,7 @@ void logout_user(SceNetAdhocctlUserNode * user) { // Notify User uint8_t * ip = (uint8_t *)&user->resolver.ip; - INFO_LOG(SCENET, "AdhocServer: Dropped Connection to %u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]); + WARN_LOG(SCENET, "AdhocServer: Dropped Connection to %u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]); } // Free Memory @@ -907,7 +916,7 @@ void connect_user(SceNetAdhocctlUserNode * user, SceNetAdhocctlGroupName * group char safegroupstr2[9]; memset(safegroupstr2, 0, sizeof(safegroupstr2)); strncpy(safegroupstr2, (char *)user->group->group.data, ADHOCCTL_GROUPNAME_LEN); - INFO_LOG(SCENET, "AdhocServer: %s (MAC: %02X:%02X:%02X:%02X:%02X:%02X - IP: %u.%u.%u.%u) attempted to join %s group %s without disconnecting from %s first", (char *)user->resolver.name.data, user->resolver.mac.data[0], user->resolver.mac.data[1], user->resolver.mac.data[2], user->resolver.mac.data[3], user->resolver.mac.data[4], user->resolver.mac.data[5], ip[0], ip[1], ip[2], ip[3], safegamestr, safegroupstr, safegroupstr2); + WARN_LOG(SCENET, "AdhocServer: %s (MAC: %02X:%02X:%02X:%02X:%02X:%02X - IP: %u.%u.%u.%u) attempted to join %s group %s without disconnecting from %s first", (char *)user->resolver.name.data, user->resolver.mac.data[0], user->resolver.mac.data[1], user->resolver.mac.data[2], user->resolver.mac.data[3], user->resolver.mac.data[4], user->resolver.mac.data[5], ip[0], ip[1], ip[2], ip[3], safegamestr, safegroupstr, safegroupstr2); } } @@ -922,7 +931,7 @@ void connect_user(SceNetAdhocctlUserNode * user, SceNetAdhocctlGroupName * group char safegroupstr[9]; memset(safegroupstr, 0, sizeof(safegroupstr)); strncpy(safegroupstr, (char *)group->data, ADHOCCTL_GROUPNAME_LEN); - INFO_LOG(SCENET, "AdhocServer: %s (MAC: %02X:%02X:%02X:%02X:%02X:%02X - IP: %u.%u.%u.%u) attempted to join invalid %s group %s", (char *)user->resolver.name.data, user->resolver.mac.data[0], user->resolver.mac.data[1], user->resolver.mac.data[2], user->resolver.mac.data[3], user->resolver.mac.data[4], user->resolver.mac.data[5], ip[0], ip[1], ip[2], ip[3], safegamestr, safegroupstr); + WARN_LOG(SCENET, "AdhocServer: %s (MAC: %02X:%02X:%02X:%02X:%02X:%02X - IP: %u.%u.%u.%u) attempted to join invalid %s group %s", (char *)user->resolver.name.data, user->resolver.mac.data[0], user->resolver.mac.data[1], user->resolver.mac.data[2], user->resolver.mac.data[3], user->resolver.mac.data[4], user->resolver.mac.data[5], ip[0], ip[1], ip[2], ip[3], safegamestr, safegroupstr); } // Invalid State, Out of Memory or Invalid Group Name @@ -1023,7 +1032,7 @@ void disconnect_user(SceNetAdhocctlUserNode * user) char safegamestr[10]; memset(safegamestr, 0, sizeof(safegamestr)); strncpy(safegamestr, user->game->game.data, PRODUCT_CODE_LENGTH); - INFO_LOG(SCENET, "AdhocServer: %s (MAC: %02X:%02X:%02X:%02X:%02X:%02X - IP: %u.%u.%u.%u) attempted to leave %s group without joining one first", (char *)user->resolver.name.data, user->resolver.mac.data[0], user->resolver.mac.data[1], user->resolver.mac.data[2], user->resolver.mac.data[3], user->resolver.mac.data[4], user->resolver.mac.data[5], ip[0], ip[1], ip[2], ip[3], safegamestr); + WARN_LOG(SCENET, "AdhocServer: %s (MAC: %02X:%02X:%02X:%02X:%02X:%02X - IP: %u.%u.%u.%u) attempted to leave %s group without joining one first", (char *)user->resolver.name.data, user->resolver.mac.data[0], user->resolver.mac.data[1], user->resolver.mac.data[2], user->resolver.mac.data[3], user->resolver.mac.data[4], user->resolver.mac.data[5], ip[0], ip[1], ip[2], ip[3], safegamestr); } // Delete User @@ -1099,7 +1108,7 @@ void send_scan_results(SceNetAdhocctlUserNode * user) char safegroupstr[9]; memset(safegroupstr, 0, sizeof(safegroupstr)); strncpy(safegroupstr, (char *)user->group->group.data, ADHOCCTL_GROUPNAME_LEN); - INFO_LOG(SCENET, "AdhocServer: %s (MAC: %02X:%02X:%02X:%02X:%02X:%02X - IP: %u.%u.%u.%u) attempted to scan for %s groups without disconnecting from %s first", (char *)user->resolver.name.data, user->resolver.mac.data[0], user->resolver.mac.data[1], user->resolver.mac.data[2], user->resolver.mac.data[3], user->resolver.mac.data[4], user->resolver.mac.data[5], ip[0], ip[1], ip[2], ip[3], safegamestr, safegroupstr); + WARN_LOG(SCENET, "AdhocServer: %s (MAC: %02X:%02X:%02X:%02X:%02X:%02X - IP: %u.%u.%u.%u) attempted to scan for %s groups without disconnecting from %s first", (char *)user->resolver.name.data, user->resolver.mac.data[0], user->resolver.mac.data[1], user->resolver.mac.data[2], user->resolver.mac.data[3], user->resolver.mac.data[4], user->resolver.mac.data[5], ip[0], ip[1], ip[2], ip[3], safegamestr, safegroupstr); } // Delete User @@ -1213,7 +1222,7 @@ void spread_message(SceNetAdhocctlUserNode *user, const char *message) char safegamestr[10]; memset(safegamestr, 0, sizeof(safegamestr)); strncpy(safegamestr, user->game->game.data, PRODUCT_CODE_LENGTH); - INFO_LOG(SCENET, "AdhocServer: %s (MAC: %02X:%02X:%02X:%02X:%02X:%02X - IP: %u.%u.%u.%u) attempted to send a text message without joining a %s group first", (char *)user->resolver.name.data, user->resolver.mac.data[0], user->resolver.mac.data[1], user->resolver.mac.data[2], user->resolver.mac.data[3], user->resolver.mac.data[4], user->resolver.mac.data[5], ip[0], ip[1], ip[2], ip[3], safegamestr); + WARN_LOG(SCENET, "AdhocServer: %s (MAC: %02X:%02X:%02X:%02X:%02X:%02X - IP: %u.%u.%u.%u) attempted to send a text message without joining a %s group first", (char *)user->resolver.name.data, user->resolver.mac.data[0], user->resolver.mac.data[1], user->resolver.mac.data[2], user->resolver.mac.data[3], user->resolver.mac.data[4], user->resolver.mac.data[5], ip[0], ip[1], ip[2], ip[3], safegamestr); } // Delete User @@ -1786,6 +1795,13 @@ int create_listen_socket(uint16_t port) local.sin_addr.s_addr = INADDR_ANY; local.sin_port = htons(port); + //Should only bind to specific IP for the 2nd or more instance of PPSSPP to prevent communication interference issue when sharing the same port. Doesn't work well when PPSSPP_ID reseted everytime emulation restarted. + /* + if (PPSSPP_ID > 1) { + local.sin_addr = ((sockaddr_in *)&localIP)->sin_addr; + } + */ + // Bind Local Address to Socket int bindresult = bind(fd, (struct sockaddr *)&local, sizeof(local)); @@ -1857,6 +1873,7 @@ int server_loop(int server) // Login User (Stream) if (loginresult != -1) { u32_le sip = addr.sin_addr.s_addr; + /* // Replacing 127.0.0.x with Ethernet IP will cause issue with multiple-instance of localhost (127.0.0.x) if (sip == 0x0100007f) { //127.0.0.1 should be replaced with LAN/WAN IP whenever available char str[100]; gethostname(str, 100); @@ -1865,6 +1882,7 @@ int server_loop(int server) sip = *(u32_le*)pip; WARN_LOG(SCENET, "AdhocServer: Replacing IP %s with %u.%u.%u.%u", inet_ntoa(addr.sin_addr), pip[0], pip[1], pip[2], pip[3]); } + */ login_user_stream(loginresult, sip); } } while(loginresult != -1); @@ -1925,7 +1943,7 @@ int server_loop(int server) { // Notify User uint8_t * ip = (uint8_t *)&user->resolver.ip; - INFO_LOG(SCENET, "AdhocServer: Invalid Opcode 0x%02X in Waiting State from %u.%u.%u.%u", user->rx[0], ip[0], ip[1], ip[2], ip[3]); + WARN_LOG(SCENET, "AdhocServer: Invalid Opcode 0x%02X in Waiting State from %u.%u.%u.%u", user->rx[0], ip[0], ip[1], ip[2], ip[3]); // Logout User logout_user(user); @@ -2009,7 +2027,7 @@ int server_loop(int server) { // Notify User uint8_t * ip = (uint8_t *)&user->resolver.ip; - INFO_LOG(SCENET, "AdhocServer: Invalid Opcode 0x%02X in Logged-In State from %s (MAC: %02X:%02X:%02X:%02X:%02X:%02X - IP: %u.%u.%u.%u)", user->rx[0], (char *)user->resolver.name.data, user->resolver.mac.data[0], user->resolver.mac.data[1], user->resolver.mac.data[2], user->resolver.mac.data[3], user->resolver.mac.data[4], user->resolver.mac.data[5], ip[0], ip[1], ip[2], ip[3]); + WARN_LOG(SCENET, "AdhocServer: Invalid Opcode 0x%02X in Logged-In State from %s (MAC: %02X:%02X:%02X:%02X:%02X:%02X - IP: %u.%u.%u.%u)", user->rx[0], (char *)user->resolver.name.data, user->resolver.mac.data[0], user->resolver.mac.data[1], user->resolver.mac.data[2], user->resolver.mac.data[3], user->resolver.mac.data[4], user->resolver.mac.data[5], ip[0], ip[1], ip[2], ip[3]); // Logout User logout_user(user); diff --git a/Core/HLE/sceNet.cpp b/Core/HLE/sceNet.cpp index 4a01534baf..dacb2904a8 100644 --- a/Core/HLE/sceNet.cpp +++ b/Core/HLE/sceNet.cpp @@ -15,6 +15,13 @@ // Official git repository and contact information can be found at // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. +#if __linux__ || __APPLE__ +#include +#include +#include +#include +#endif + #include "net/resolve.h" #include "util/text/parsers.h" @@ -49,6 +56,172 @@ static struct SceNetMallocStat netMallocStat; static std::map apctlHandlers; +#ifdef _WIN32 +static HANDLE hIDMapFile = NULL; +#elif __linux__ || __APPLE__ +static int hIDMapFile = 0; +#endif +static int32_t* pIDBuf = NULL; +#define ID_SHM_NAME "/PPSSPP_ID" + +// Get current number of instance of PPSSPP running. +// Must be called only once during init. +static uint8_t getPPSSPPInstanceNumber() { +#ifdef _WIN32 + uint32_t BUF_SIZE = 4096; + SYSTEM_INFO sysInfo; + + GetSystemInfo(&sysInfo); + int gran = sysInfo.dwAllocationGranularity ? sysInfo.dwAllocationGranularity : 0x10000; + BUF_SIZE = (BUF_SIZE + gran - 1) & ~(gran - 1); + + hIDMapFile = CreateFileMapping( + INVALID_HANDLE_VALUE, // use paging file + NULL, // default security + PAGE_READWRITE, // read/write access + 0, // maximum object size (high-order DWORD) + BUF_SIZE, // maximum object size (low-order DWORD) + TEXT(ID_SHM_NAME)); // name of mapping object + + DWORD lasterr = GetLastError(); + if (hIDMapFile == NULL) + { + ERROR_LOG(SCENET, "Could not create %s file mapping object (%d).", ID_SHM_NAME, lasterr); + return 1; + } + pIDBuf = (int32_t*)MapViewOfFile(hIDMapFile, // handle to map object + FILE_MAP_ALL_ACCESS, // read/write permission + 0, + 0, + sizeof(int32_t)); //BUF_SIZE + + if (pIDBuf == NULL) + { + ERROR_LOG(SCENET, "Could not map view of file %s (%d).", ID_SHM_NAME, GetLastError()); + //CloseHandle(hIDMapFile); + return 1; + } + + (*pIDBuf)++; + int id = *pIDBuf; + UnmapViewOfFile(pIDBuf); + //CloseHandle(hIDMapFile); //Should be called when program exits + //hIDMapFile = NULL; + + return id; +#elif __ANDROID__ + // TODO : replace shm_open & shm_unlink with ashmem or android-shmem + return 1; +#elif __linux__ || __APPLE__ + long BUF_SIZE = 4096; + //caddr_t pIDBuf; + int status; + + // Create shared memory object + + hIDMapFile = shm_open(ID_SHM_NAME, O_CREAT | O_RDWR, 0); + BUF_SIZE = (BUF_SIZE < sysconf(_SC_PAGE_SIZE)) ? sysconf(_SC_PAGE_SIZE) : BUF_SIZE; + + if ((ftruncate(hIDMapFile, BUF_SIZE)) == -1) { // Set the size + ERROR_LOG(SCENET, "ftruncate(%s) failure.", ID_SHM_NAME); + return 1; + } + + pIDBuf = (int32_t*)mmap(0, BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, hIDMapFile, 0); + if (pIDBuf == MAP_FAILED) { // Set the size + ERROR_LOG(SCENET, "mmap(%s) failure.", ID_SHM_NAME); + pIDBuf = NULL; + return 1; + } + + int id = 1; + if (mlock(pIDBuf, BUF_SIZE) == 0) { + (*pIDBuf)++; + id = *pIDBuf; + munlock(pIDBuf, BUF_SIZE); + } + + status = munmap(pIDBuf, BUF_SIZE); // Unmap the page + //status = close(hIDMapFile); // Close file, should be called when program exits? + //status = shm_unlink(ID_SHM_NAME); // Unlink [& delete] shared-memory object, should be called when program exits + + return id; +#else + return 1; +#endif +} + +static void PPSSPPIDCleanup() { +#ifdef _WIN32 + if (hIDMapFile != NULL) { + CloseHandle(hIDMapFile); // If program exited(or crashed?) or the last handle reference closed the shared memory object will be deleted. + hIDMapFile = NULL; + } +#elif __ANDROID__ + if (hIDMapFile != 0) { + close(hIDMapFile); + // TODO : replace shm_unlink with ashmem or android-shmem + //shm_unlink(ID_SHM_NAME); // If program exited or crashed before unlinked the shared memory object and it's contents will persist. + hIDMapFile = 0; + } +#elif __linux__ || __APPLE__ + // TODO : This unlink should be called when program exits instead of everytime the game reset. + if (hIDMapFile != 0) { + close(hIDMapFile); + shm_unlink(ID_SHM_NAME); // If program exited or crashed before unlinked the shared memory object and it's contents will persist. + hIDMapFile = 0; + } +#endif +} + +static int InitLocalIP() { + // find local IP + addrinfo* localAddr; + addrinfo* ptr; + char ipstr[256]; + sprintf(ipstr, "127.0.0.%u", PPSSPP_ID); + int iResult = getaddrinfo(ipstr, 0, NULL, &localAddr); + if (iResult != 0) { + ERROR_LOG(SCENET, "DNS Error (%s) result: %d\n", ipstr, iResult); + //osm.Show("DNS Error, can't resolve client bind " + ipstr, 8.0f); + ((sockaddr_in*)&localIP)->sin_family = AF_INET; + ((sockaddr_in*)&localIP)->sin_addr.s_addr = inet_addr(ipstr); //"127.0.0.1" + ((sockaddr_in*)&localIP)->sin_port = 0; + return iResult; + } + for (ptr = localAddr; ptr != NULL; ptr = ptr->ai_next) { + switch (ptr->ai_family) { + case AF_INET: + memcpy(&localIP, ptr->ai_addr, sizeof(sockaddr)); + break; + } + } + ((sockaddr_in*)&localIP)->sin_port = 0; + freeaddrinfo(localAddr); + + // Resolve server dns + addrinfo* resultAddr; + in_addr serverIp; + serverIp.s_addr = INADDR_NONE; + + iResult = getaddrinfo(g_Config.proAdhocServer.c_str(), 0, NULL, &resultAddr); + if (iResult != 0) { + ERROR_LOG(SCENET, "DNS Error (%s)\n", g_Config.proAdhocServer.c_str()); + return iResult; + } + for (ptr = resultAddr; ptr != NULL; ptr = ptr->ai_next) { + switch (ptr->ai_family) { + case AF_INET: + serverIp = ((sockaddr_in*)ptr->ai_addr)->sin_addr; + break; + } + } + freeaddrinfo(resultAddr); + isLocalServer = (((uint8_t*)&serverIp.s_addr)[0] == 0x7f); // (serverIp.S_un.S_un_b.s_b1 = 0x7f); + + return 0; +} + static void __ResetInitNetLib() { netInited = false; netApctlInited = false; @@ -59,11 +232,23 @@ static void __ResetInitNetLib() { void __NetInit() { portOffset = g_Config.iPortOffset; + if (PPSSPP_ID == 0) // For persistent ID/IP, New instances may have a possibility to have the same ID/IP if PPSSPPIDCleanup() being called on every reset + { + PPSSPP_ID = getPPSSPPInstanceNumber(); // This should be called when program started instead of when the game started/reseted + } + + net::Init(); + InitLocalIP(); + INFO_LOG(SCENET, "LocalHost IP will be %s", inet_ntoa(((sockaddr_in*)&localIP)->sin_addr)); + //net::Init(); __ResetInitNetLib(); } void __NetShutdown() { __ResetInitNetLib(); + + net::Shutdown(); + //PPSSPPIDCleanup(); // To make the ID/IP persistent on every reset, we should just let the OS closes all open handles instead of calling PPSSPPIDCleanup() on every reset } static void __UpdateApctlHandlers(int oldState, int newState, int flag, int error) { @@ -186,6 +371,10 @@ static u32 sceWlanGetEtherAddr(u32 addrAddr) { } u8 *addr = Memory::GetPointer(addrAddr); + if (PPSSPP_ID > 1) { + Memory::Memset(addrAddr, PPSSPP_ID, 6); + } + else // Read MAC Address from config if (!ParseMacAddress(g_Config.sMACAddress.c_str(), addr)) { ERROR_LOG(SCENET, "Error parsing mac address %s", g_Config.sMACAddress.c_str()); @@ -377,7 +566,7 @@ int sceNetInetPoll(void *fds, u32 nfds, int timeout) { // timeout in miliseconds DEBUG_LOG(SCENET, "UNTESTED sceNetInetPoll(%p, %d, %i) at %08x", fds, nfds, timeout, currentMIPS->pc); int retval = -1; SceNetInetPollfd *fdarray = (SceNetInetPollfd *)fds; // SceNetInetPollfd/pollfd, sceNetInetPoll() have similarity to BSD poll() but pollfd have different size on 64bit -//#ifdef _MSC_VER +//#ifdef _WIN32 //WSAPoll only available for Vista or newer, so we'll use an alternative way for XP since Windows doesn't have poll function like *NIX if (nfds > FD_SETSIZE) return -1; fd_set readfds, writefds, exceptfds; diff --git a/Core/HLE/sceNetAdhoc.cpp b/Core/HLE/sceNetAdhoc.cpp index 6786b123dc..705e438ace 100644 --- a/Core/HLE/sceNetAdhoc.cpp +++ b/Core/HLE/sceNetAdhoc.cpp @@ -270,6 +270,9 @@ static int sceNetAdhocPdpCreate(const char *mac, u32 port, int bufferSize, u32 u sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; + if (isLocalServer) { + getLocalIp(&addr); + } //if (port < 7) addr.sin_port = htons(port + 1341); else // <= 443 addr.sin_port = htons(port + portOffset ); // This not safe in any way... @@ -1658,6 +1661,9 @@ static int sceNetAdhocPtpOpen(const char *srcmac, int sport, const char *dstmac, // addr.sin_len = sizeof(addr); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; + if (isLocalServer) { + getLocalIp(&addr); + } addr.sin_port = htons(sport + portOffset); // Bound Socket to local Port @@ -2130,6 +2136,9 @@ static int sceNetAdhocPtpListen(const char *srcmac, int sport, int bufsize, int sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; + if (isLocalServer) { + getLocalIp(&addr); + } addr.sin_port = htons(sport + portOffset); int iResult = 0;