Subversion Repositories Games.Carmageddon

Rev

Rev 1 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include "pd/net.h"
  2.  
  3. #include "brender.h"
  4. #include "dr_types.h"
  5. #include "errors.h"
  6. #include "globvrpb.h"
  7. #include "harness/config.h"
  8. #include "harness/hooks.h"
  9. #include "harness/trace.h"
  10. #include "harness/winsock.h"
  11. #include "network.h"
  12. #include "pd/net.h"
  13. #include "pd/sys.h"
  14. #include <ctype.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17.  
  18. // dethrace: have switched out IPX implementation for IP
  19.  
  20. tU32 gNetwork_init_flags;
  21. tPD_net_game_info* gJoinable_games;
  22. int gMatts_PC;
  23. tU16 gSocket_number_pd_format;
  24. //_IPX_ELEMENT gListen_elements[16];
  25. char gLocal_ipx_addr_string[32];
  26. //_IPX_ELEMENT gSend_elements[16];
  27. struct sockaddr_in* gLocal_addr_ipx;
  28. char gReceive_buffer[512];
  29. tPD_net_player_info gRemote_net_player_info;
  30. struct sockaddr_in* gBroadcast_addr_ipx;
  31. tPD_net_player_info gLocal_net_player_info;
  32. char gSend_buffer[512];
  33. tIPX_netnum gNetworks[16];
  34. struct sockaddr_in* gRemote_addr_ipx;
  35. tU8* gSend_packet;
  36. // W32 gListen_segment;
  37. tU8* gSend_packet_ptr;
  38. // W32 gSend_segment;
  39. tU8* gListen_packet;
  40. tU8* gListen_packet_ptr;
  41. size_t gMsg_header_strlen;
  42. int gNumber_of_networks;
  43. int gNumber_of_hosts;
  44. //_IPX_HEADER gLast_received_IPX_header;
  45. tU16 gSocket_number_network_order;
  46. // unsigned short gECB_offset;
  47. tU16 gListen_selector;
  48. tU16 gSend_selector;
  49.  
  50. struct sockaddr_in gLocal_addr;
  51. struct sockaddr_in gRemote_addr;
  52. struct sockaddr_in gBroadcast_addr;
  53. struct sockaddr_in gLast_received_addr;
  54. int gSocket;
  55.  
  56. #define MESSAGE_HEADER_STR "CW95MSG"
  57. #define JOINABLE_GAMES_CAPACITY 16
  58.  
  59. DR_STATIC_ASSERT(offsetof(tNet_message, pd_stuff_so_DO_NOT_USE) == 0);
  60. DR_STATIC_ASSERT(offsetof(tNet_message, magic_number) == 4);
  61. DR_STATIC_ASSERT(offsetof(tNet_message, guarantee_number) == 8);
  62. DR_STATIC_ASSERT(offsetof(tNet_message, sender) == 12);
  63. DR_STATIC_ASSERT(offsetof(tNet_message, version) == 16);
  64. DR_STATIC_ASSERT(offsetof(tNet_message, senders_time_stamp) == 20);
  65. DR_STATIC_ASSERT(offsetof(tNet_message, num_contents) == 24);
  66. DR_STATIC_ASSERT(offsetof(tNet_message, overall_size) == 26);
  67. DR_STATIC_ASSERT(offsetof(tNet_message, contents) == 28);
  68.  
  69. // IDA: void __cdecl ClearupPDNetworkStuff()
  70. void ClearupPDNetworkStuff(void) {
  71.     LOG_TRACE("()");
  72.     NOT_IMPLEMENTED();
  73. }
  74.  
  75. // IDA: void __usercall MATTMessageCheck(char *pFunction_name@<EAX>, tNet_message *pMessage@<EDX>, int pAlleged_size@<EBX>)
  76. void MATTMessageCheck(char* pFunction_name, tNet_message* pMessage, int pAlleged_size) {
  77.     LOG_TRACE("(\"%s\", %p, %d)", pFunction_name, pMessage, pAlleged_size);
  78.     NOT_IMPLEMENTED();
  79. }
  80.  
  81. // IDA: int __usercall GetProfileText@<EAX>(char *pDest@<EAX>, int pDest_len@<EDX>, char *pFname@<EBX>, char *pKeyname@<ECX>)
  82. int GetProfileText(char* pDest, int pDest_len, char* pFname, char* pKeyname) {
  83.     //FILE* fp; // Pierre-Marie Baty -- unused variable
  84.     //char in_buf[256]; // Pierre-Marie Baty -- unused variable
  85.     //int i; // Pierre-Marie Baty -- unused variable
  86.     //int j; // Pierre-Marie Baty -- unused variable
  87.     //int len; // Pierre-Marie Baty -- unused variable
  88.     LOG_TRACE("(\"%s\", %d, \"%s\", \"%s\")", pDest, pDest_len, pFname, pKeyname);
  89.     NOT_IMPLEMENTED();
  90. }
  91.  
  92. // IDA: int __cdecl GetSocketNumberFromProfileFile()
  93. int GetSocketNumberFromProfileFile(void) {
  94.     //char str[256]; // Pierre-Marie Baty -- unused variable
  95.     //int sscanf_res; // Pierre-Marie Baty -- unused variable
  96.     //tU32 socknum; // Pierre-Marie Baty -- unused variable
  97.     LOG_TRACE("()");
  98.     NOT_IMPLEMENTED();
  99. }
  100.  
  101. // tU32 EthernetAddressToU32(SOCKADDR_IPX_* pAddr_ipx) {
  102. //     LOG_TRACE("(%p)", pAddr_ipx);
  103. //     NOT_IMPLEMENTED();
  104. // }
  105.  
  106. void NetNowIPXLocalTarget2String(char* pString, struct sockaddr_in* pSock_addr_ipx) {
  107.     LOG_TRACE("(\"%s\", %p)", pString, pSock_addr_ipx);
  108.  
  109.     char portbuf[10];
  110.  
  111.     inet_ntop(AF_INET, &pSock_addr_ipx->sin_addr, pString, 32);
  112.     sprintf(portbuf, ":%d", ntohs(pSock_addr_ipx->sin_port));
  113.     strcat(pString, portbuf);
  114. }
  115.  
  116. // IDA: int __usercall GetMessageTypeFromMessage@<EAX>(char *pMessage_str@<EAX>)
  117. int GetMessageTypeFromMessage(char* pMessage_str) {
  118.     char* real_msg;
  119.     int msg_type_int;
  120.     LOG_TRACE("(\"%s\")", pMessage_str);
  121.  
  122.     real_msg = &pMessage_str[4];
  123.     msg_type_int = 0;
  124.  
  125.     // FIXME: "CW95MSG" value is used in and depends on platform
  126.     if (strncmp(real_msg, MESSAGE_HEADER_STR, gMsg_header_strlen) == 0) {
  127.         if (isdigit(real_msg[gMsg_header_strlen])) {
  128.             msg_type_int = real_msg[gMsg_header_strlen] - '0';
  129.         }
  130.         if (msg_type_int != 0 && msg_type_int < 3) {
  131.             return msg_type_int;
  132.         }
  133.     }
  134.     return 999;
  135. }
  136.  
  137. int SameEthernetAddress(struct sockaddr_in* pAddr_ipx1, struct sockaddr_in* pAddr_ipx2) {
  138.     LOG_TRACE("(%p, %p)", pAddr_ipx1, pAddr_ipx2);
  139.  
  140.     return memcmp(pAddr_ipx1, pAddr_ipx2, sizeof(struct sockaddr_in)) == 0;
  141. }
  142.  
  143. /*SOCKADDR_IPX_* */ void* GetIPXAddrFromPlayerID(tPlayer_ID pPlayer_id) {
  144.     //int i; // Pierre-Marie Baty -- unused variable
  145.     //tU8* nodenum; // Pierre-Marie Baty -- unused variable
  146.     NOT_IMPLEMENTED();
  147. }
  148.  
  149. // IDA: void __usercall MakeMessageToSend(int pMessage_type@<EAX>)
  150. void MakeMessageToSend(int pMessage_type) {
  151.     LOG_TRACE("(%d)", pMessage_type);
  152.  
  153. #ifdef DETHRACE_FIX_BUGS
  154.     sprintf(gSend_buffer, "XXXX%s%1d", MESSAGE_HEADER_STR, pMessage_type);
  155. #else
  156.     sprintf(gSend_buffer, "XXXX%s%0.1d", MESSAGE_HEADER_STR, pMessage_type);
  157. #endif
  158. }
  159.  
  160. // IDA: int __cdecl ReceiveHostResponses()
  161. int ReceiveHostResponses(void) {
  162.     char str[256];
  163.     int i;
  164.     int already_registered;
  165.     LOG_TRACE("()");
  166.  
  167.     char addr_string[32];
  168.     unsigned int sa_len;
  169.     int wsa_error;
  170.  
  171.     sa_len = sizeof(gRemote_addr);
  172.     while (1) {
  173.         if (recvfrom(gSocket, gReceive_buffer, sizeof(gReceive_buffer), 0, (struct sockaddr*)&gRemote_addr, &sa_len) == -1) {
  174.             break;
  175.         }
  176.         NetNowIPXLocalTarget2String(addr_string, gRemote_addr_ipx);
  177.         dr_dprintf("ReceiveHostResponses(): Received string '%s' from %s", gReceive_buffer, addr_string);
  178.  
  179.         if (SameEthernetAddress(gLocal_addr_ipx, gRemote_addr_ipx)) {
  180.             dr_dprintf("*** Discounting the above 'cos we sent it ***");
  181.             continue;
  182.         }
  183.         if (GetMessageTypeFromMessage(gReceive_buffer) != 2) {
  184.             dr_dprintf("*** Discounting the above 'cos it's not a host reply ***");
  185.             continue;
  186.         }
  187.  
  188.         dr_dprintf("*** It's a host reply! ***");
  189.         already_registered = 0;
  190.         for (i = 0; i < gNumber_of_hosts; i++) {
  191.             if (SameEthernetAddress(&gJoinable_games[i].addr_in, gRemote_addr_ipx)) {
  192.                 already_registered = 1;
  193.                 break;
  194.             }
  195.         }
  196.         if (already_registered) {
  197.             dr_dprintf("That game already registered");
  198.             gJoinable_games[i].last_response = PDGetTotalTime();
  199.         } else {
  200.             dr_dprintf("Adding joinable game to slot #%d", gNumber_of_hosts);
  201.             gJoinable_games[gNumber_of_hosts].addr_in = *gRemote_addr_ipx;
  202.             gJoinable_games[gNumber_of_hosts].last_response = PDGetTotalTime();
  203.             gNumber_of_hosts++;
  204.             dr_dprintf("Number of games found so far: %d", gNumber_of_hosts);
  205.         }
  206.         if (gNumber_of_hosts) {
  207.             dr_dprintf("Currently registered net games:");
  208.             for (i = 0; i < gNumber_of_hosts; i++) {
  209.                 NetNowIPXLocalTarget2String(str, &gJoinable_games[i].addr_in);
  210.                 dr_dprintf("%d: Host addr %s", i, str);
  211.             }
  212.         }
  213.     }
  214.     wsa_error = WSAGetLastError() != WSAEWOULDBLOCK;
  215.     if (wsa_error == 0) {
  216.         return 1;
  217.     }
  218.     dr_dprintf("ReceiveHostResponses(): Error on recvfrom() - WSAGetLastError=%d", wsa_error);
  219.     return 0;
  220. }
  221.  
  222. // IDA: int __cdecl BroadcastMessage()
  223. int BroadcastMessage(void) {
  224.     int i;
  225.     int errors;
  226.     char broadcast_addr_string[32];
  227.     //char* real_msg; // Pierre-Marie Baty -- unused variable
  228.     LOG_TRACE("()");
  229.  
  230.     errors = 0;
  231.     for (i = 0; i < gNumber_of_networks; i++) {
  232.         //*(_DWORD*)gBroadcast_addr_ipx->sa_netnum = gNetworks[i];
  233.         NetNowIPXLocalTarget2String(broadcast_addr_string, gBroadcast_addr_ipx);
  234.         dr_dprintf("Broadcasting on address '%s'", broadcast_addr_string);
  235.         if (sendto(gSocket, gSend_buffer, strlen(gSend_buffer) + 1, 0, (struct sockaddr*)&gBroadcast_addr, sizeof(gBroadcast_addr)) == -1) {
  236.             dr_dprintf("BroadcastMessage(): Error on sendto() - WSAGetLastError=%d", WSAGetLastError());
  237.             errors = 1;
  238.         }
  239.     }
  240.     return errors == 0;
  241. }
  242.  
  243. // IDA: int __cdecl PDNetInitialise()
  244. int PDNetInitialise(void) {
  245.     //tU32 timenow; // Pierre-Marie Baty -- unused variable
  246.     //char profile_string[32]; // Pierre-Marie Baty -- unused variable
  247.     //char key_name[32]; // Pierre-Marie Baty -- unused variable
  248.     //int sscanf_res; // Pierre-Marie Baty -- unused variable
  249.     //int i; // Pierre-Marie Baty -- unused variable
  250.     //tU32 netnum; // Pierre-Marie Baty -- unused variable
  251.     //char str[256]; // Pierre-Marie Baty -- unused variable
  252.     //int mess_num; // Pierre-Marie Baty -- unused variable
  253.  
  254.     struct linger so_linger;
  255.     unsigned int sa_len;
  256.     WSADATA wsadata;
  257.     LOG_TRACE("()");
  258.  
  259.     sa_len = sizeof(struct sockaddr_in);
  260.     dr_dprintf("PDNetInitialise()");
  261.     int ipx_socket_num = 12286;
  262.     // Dont bother to handle network.ini
  263.     // if (gWin32_should_load_network_ini) {
  264.     //     if (GetPrivateProfileStringA(
  265.     //             "Network",
  266.     //             "IPXSocket",
  267.     //             Default,
  268.     //             (LPSTR)ReturnedString,
  269.     //             0x20u,
  270.     //             gNetwork_profile_fname)) {
  271.     //         v11 = sscanf(ReturnedString, "%x", &v9);
  272.     //         if (v11) {
  273.     //             if (v11 != -1) {
  274.     //                 dr_dprintf("New IPX socket %4.4x", v9);
  275.     //                 ipx_socket_num = sub_454930(v9);
  276.     //             }
  277.     //         }
  278.     //     }
  279.     // }
  280.     gLocal_addr_ipx = &gLocal_addr;
  281.     gRemote_addr_ipx = &gRemote_addr;
  282.     gBroadcast_addr_ipx = &gBroadcast_addr;
  283.     memset(&gLocal_addr, 0, sizeof(struct sockaddr_in));
  284.     memset(&gRemote_addr, 0, sizeof(struct sockaddr_in));
  285.     memset(&gBroadcast_addr, 0, sizeof(struct sockaddr_in));
  286.  
  287.     // gLocal_addr_ipx->sa_family = AF_IPX;
  288.     // gRemote_addr_ipx->sa_family = AF_IPX;
  289.     // gBroadcast_addr_ipx->sa_family = AF_IPX;
  290.     // gLocal_addr_ipx->sa_socket = ipx_socket_num;
  291.     // gRemote_addr_ipx->sa_socket = ipx_socket_num;
  292.     // gBroadcast_addr_ipx->sa_socket = ipx_socket_num;
  293.  
  294.     // // IPX broadcast
  295.     // gBroadcast_addr_ipx->sa_nodenum[0] = -1;
  296.     // gBroadcast_addr_ipx->sa_nodenum[1] = -1;
  297.     // gBroadcast_addr_ipx->sa_nodenum[2] = -1;
  298.     // gBroadcast_addr_ipx->sa_nodenum[3] = -1;
  299.     // gBroadcast_addr_ipx->sa_nodenum[4] = -1;
  300.     // gBroadcast_addr_ipx->sa_nodenum[5] = -1;
  301.  
  302.     gLocal_addr.sin_family = AF_INET;
  303.     gLocal_addr.sin_port = htons(12286);
  304.     gLocal_addr.sin_addr.s_addr = INADDR_ANY;
  305.     gRemote_addr.sin_family = AF_INET;
  306.     gRemote_addr.sin_port = htons(12286);
  307.     gBroadcast_addr.sin_family = AF_INET;
  308.     gBroadcast_addr.sin_port = htons(12286);
  309.  
  310.     // original code was using MAKEWORD(1, 1)
  311.     if (WSAStartup(MAKEWORD(2, 2), &wsadata) == -1) {
  312.         dr_dprintf("PDNetInitialise(): WSAStartup() failed");
  313.         return -1;
  314.     }
  315.  
  316.     // gSocket = socket(6, 2, 1000);
  317.     gSocket = socket(AF_INET, SOCK_DGRAM, 0);
  318.     if (gSocket == -1) {
  319.         dr_dprintf("PDNetInitialise(): Failed to create socket - WSAGetLastError=%d", WSAGetLastError());
  320.         WSACleanup();
  321.         return -1;
  322.     }
  323.     int broadcast = 1;
  324.     setsockopt(gSocket, SOL_SOCKET, SO_BROADCAST, (void *) &broadcast, sizeof(broadcast)); // Pierre-Marie Baty -- added portable type cast
  325.     so_linger.l_onoff = 1;
  326.     so_linger.l_linger = 0;
  327.     setsockopt(gSocket, SOL_SOCKET, SO_LINGER, (void *) &so_linger, sizeof(so_linger)); // Pierre-Marie Baty -- added portable type cast
  328.  
  329.     unsigned long nobio = 1;
  330.     if (ioctlsocket(gSocket, FIONBIO, &nobio) == -1) {
  331.         dr_dprintf("Error on ioctlsocket() - WSAGetLastError=%d", WSAGetLastError());
  332.         closesocket(gSocket);
  333.         WSACleanup();
  334.         return -1;
  335.     }
  336.  
  337.     if (harness_game_config.no_bind == 0) {
  338.         if (bind(gSocket, (struct sockaddr*)&gLocal_addr, sizeof(gLocal_addr)) == -1) {
  339.             dr_dprintf("Error on bind() - WSAGetLastError=%d", WSAGetLastError());
  340.             closesocket(gSocket);
  341.             WSACleanup();
  342.             return -1;
  343.         }
  344.     }
  345.  
  346.     int res = getsockname(gSocket, (struct sockaddr*)&gLocal_addr, &sa_len);
  347.     NetNowIPXLocalTarget2String(gLocal_ipx_addr_string, gLocal_addr_ipx);
  348.     // gNetworks[0] = *(tIPX_netnum*)gLocal_addr_ipx->sa_netnum;
  349.     gNumber_of_networks = 1;
  350.  
  351.     // if (gWin32_should_load_network_ini) {
  352.     //     for (i = 1; i <= 15; ++i) {
  353.     //         sprintf(KeyName, "%s%d", "Network", i);
  354.     //         if (GetPrivateProfileStringA(
  355.     //                 "Network",
  356.     //                 KeyName,
  357.     //                 Default,
  358.     //                 (LPSTR)ReturnedString,
  359.     //                 0x20u,
  360.     //                 gNetwork_profile_fname)) {
  361.     //             v11 = sscanf(ReturnedString, "%x", &network_nbr);
  362.     //             if (v11) {
  363.     //                 if (v11 != -1) {
  364.     //                     dr_dprintf("Found network number '%x' in INI file", network_nbr);
  365.     //                     gNetworks[gNumber_of_networks].bNetwork[0] = HIBYTE(network_nbr);
  366.     //                     gNetworks[gNumber_of_networks].bNetwork[1] = (network_nbr & 0xFF0000u) >> 16;
  367.     //                     gNetworks[gNumber_of_networks].bNetwork[2] = BYTE1(network_nbr);
  368.     //                     gNetworks[gNumber_of_networks].bNetwork[3] = network_nbr;
  369.     //                     if (gNetworks[gNumber_of_networks].bNetwork[0] != gLocal_addr_ipx->sa_netnum[0]
  370.     //                         || gNetworks[gNumber_of_networks].bNetwork[1] != gLocal_addr_ipx->sa_netnum[1]
  371.     //                         || gNetworks[gNumber_of_networks].bNetwork[2] != gLocal_addr_ipx->sa_netnum[2]
  372.     //                         || gNetworks[gNumber_of_networks].bNetwork[3] != gLocal_addr_ipx->sa_netnum[3]) {
  373.     //                         ++gNumber_of_networks;
  374.     //                     }
  375.     //                 }
  376.     //             }
  377.     //         }
  378.     //     }
  379.     //     dr_dprintf("Total networks = %d", gNumber_of_networks);
  380.     // }
  381.     // byte_536440 = *gLocal_addr_ipx;
  382.  
  383.     dr_dprintf("Socket bound OK; local address is '%s'", gLocal_ipx_addr_string);
  384.     gMsg_header_strlen = 7;
  385.     if (strstr(gLocal_ipx_addr_string, "00a0240f9fac")) {
  386.         gMatts_PC = 1;
  387.     }
  388.     return 0;
  389. }
  390.  
  391. // IDA: int __cdecl PDNetShutdown()
  392. int PDNetShutdown(void) {
  393.     LOG_TRACE("()");
  394.  
  395.     dr_dprintf("PDNetShutdown()");
  396.     if (gSocket != -1) {
  397.         closesocket(gSocket);
  398.     }
  399.     gSocket = -1;
  400.     return 0;
  401. }
  402.  
  403. // IDA: void __cdecl PDNetStartProducingJoinList()
  404. void PDNetStartProducingJoinList(void) {
  405.     LOG_TRACE("()");
  406.  
  407.     dr_dprintf("PDNetStartProducingJoinList()");
  408.     gNumber_of_hosts = 0;
  409.     gJoinable_games = BrMemAllocate(sizeof(tPD_net_game_info) * JOINABLE_GAMES_CAPACITY, 0x80u);
  410.     if (gJoinable_games == NULL) {
  411.         PDFatalError("Can't allocate memory for joinable games");
  412.     }
  413. }
  414.  
  415. // IDA: void __cdecl PDNetEndJoinList()
  416. void PDNetEndJoinList(void) {
  417.     LOG_TRACE("()");
  418.  
  419.     dr_dprintf("PDNetEndJoinList()");
  420.     if (gJoinable_games) {
  421.         BrMemFree(gJoinable_games);
  422.     }
  423.     gJoinable_games = NULL;
  424. }
  425.  
  426. // IDA: int __usercall PDNetGetNextJoinGame@<EAX>(tNet_game_details *pGame@<EAX>, int pIndex@<EDX>)
  427. int PDNetGetNextJoinGame(tNet_game_details* pGame, int pIndex) {
  428.     static tU32 next_broadcast_time = 0;
  429.     int i;
  430.     int j;
  431.     int number_of_hosts_has_changed;
  432.     //char str[256]; // Pierre-Marie Baty -- unused variable
  433.     LOG_TRACE9("(%p, %d)", pGame, pIndex);
  434.  
  435.     dr_dprintf("PDNetGetNextJoinGame(): pIndex is %d", pIndex);
  436.     if (pIndex == 0) {
  437.         do {
  438.             number_of_hosts_has_changed = 0;
  439.             for (i = 0; i < gNumber_of_hosts; i++) {
  440.                 if (gJoinable_games[i].last_response + 10000 < (tU32) PDGetTotalTime()) { // Pierre-Marie Baty -- added type cast
  441.                     number_of_hosts_has_changed = 1;
  442.                     for (j = i; j < gNumber_of_hosts - 1; j++) {
  443.                         memcpy(&gJoinable_games[j], &gJoinable_games[j + 1], sizeof(tPD_net_game_info));
  444.                     }
  445.                 }
  446.             }
  447.             if (number_of_hosts_has_changed) {
  448.                 gNumber_of_hosts--;
  449.             }
  450.         } while (number_of_hosts_has_changed);
  451.         if ((tU32) PDGetTotalTime() > next_broadcast_time) { // Pierre-Marie Baty -- added type cast
  452.             next_broadcast_time = PDGetTotalTime() + 3000;
  453.             MakeMessageToSend(1);
  454.             if (BroadcastMessage() == 0) {
  455.                 dr_dprintf("PDNetGetNextJoinGame(): Error on BroadcastMessage()");
  456.             }
  457.         }
  458.     }
  459.     ReceiveHostResponses();
  460.     if (gNumber_of_hosts <= pIndex) {
  461.         return 0;
  462.     }
  463.     dr_dprintf("PDNetGetNextJoinGame(): Adding game.");
  464.     memcpy(&pGame->pd_net_info.addr_in, &gJoinable_games[pIndex].addr_in, sizeof(pGame->pd_net_info.addr_in));
  465.     return 1;
  466. }
  467.  
  468. // IDA: void __usercall PDNetDisposeGameDetails(tNet_game_details *pDetails@<EAX>)
  469. void PDNetDisposeGameDetails(tNet_game_details* pDetails) {
  470.     LOG_TRACE("(%p)", pDetails);
  471.     NOT_IMPLEMENTED();
  472. }
  473.  
  474. // IDA: int __usercall PDNetHostGame@<EAX>(tNet_game_details *pDetails@<EAX>, char *pHost_name@<EDX>, void **pHost_address@<EBX>)
  475. int PDNetHostGame(tNet_game_details* pDetails, char* pHost_name, void** pHost_address) {
  476.     LOG_TRACE("(%p, \"%s\", %p)", pDetails, pHost_name, pHost_address);
  477.  
  478.     dr_dprintf("PDNetHostGame()");
  479.     *pHost_address = &gLocal_addr;
  480.     return 1;
  481. }
  482.  
  483. // IDA: int __usercall PDNetJoinGame@<EAX>(tNet_game_details *pDetails@<EAX>, char *pPlayer_name@<EDX>)
  484. int PDNetJoinGame(tNet_game_details* pDetails, char* pPlayer_name) {
  485.     LOG_TRACE("(%p, \"%s\")", pDetails, pPlayer_name);
  486.  
  487.     dr_dprintf("PDNetJoinGame()");
  488.     return 0;
  489. }
  490.  
  491. // IDA: void __usercall PDNetLeaveGame(tNet_game_details *pDetails@<EAX>)
  492. void PDNetLeaveGame(tNet_game_details* pDetails) {
  493.     LOG_TRACE("(%p)", pDetails);
  494.  
  495.     dr_dprintf("PDNetLeaveGame()");
  496. }
  497.  
  498. // IDA: void __usercall PDNetHostFinishGame(tNet_game_details *pDetails@<EAX>)
  499. void PDNetHostFinishGame(tNet_game_details* pDetails) {
  500.     LOG_TRACE("(%p)", pDetails);
  501.  
  502.     dr_dprintf("PDNetHostFinishGame()");
  503. }
  504.  
  505. // IDA: tU32 __usercall PDNetExtractGameID@<EAX>(tNet_game_details *pDetails@<EAX>)
  506. tU32 PDNetExtractGameID(tNet_game_details* pDetails) {
  507.     LOG_TRACE("(%p)", pDetails);
  508.  
  509.     dr_dprintf("PDNetExtractGameID()");
  510.     return ntohs(pDetails->pd_net_info.addr_in.sin_port); // PDGetTotalTime();
  511. }
  512.  
  513. // IDA: tPlayer_ID __usercall PDNetExtractPlayerID@<EAX>(tNet_game_details *pDetails@<EAX>)
  514. tPlayer_ID PDNetExtractPlayerID(tNet_game_details* pDetails) {
  515.     LOG_TRACE("(%p)", pDetails);
  516.  
  517.     dr_dprintf("PDNetExtractPlayerID()");
  518.     return ntohs(gLocal_addr_ipx->sin_port);
  519. }
  520.  
  521. // IDA: void __usercall PDNetObtainSystemUserName(char *pName@<EAX>, int pMax_length@<EDX>)
  522. void PDNetObtainSystemUserName(char* pName, int pMax_length) {
  523.     int result;
  524.     char* found;
  525.     dr_dprintf("PDNetObtainSystemUserName()");
  526.  
  527.     result = gethostname(pName, pMax_length);
  528.     if (result == 0) {
  529.     }
  530.  
  531.     while (1) {
  532.         found = strpbrk(pName, "_=(){}[]<>!$%^&*/:@~;'#,?\\|`\"");
  533.         if (found == NULL) {
  534.             break;
  535.         }
  536.         *found = '-';
  537.     }
  538. }
  539.  
  540. // IDA: int __usercall PDNetSendMessageToPlayer@<EAX>(tNet_game_details *pDetails@<EAX>, tNet_message *pMessage@<EDX>, tPlayer_ID pPlayer@<EBX>)
  541. int PDNetSendMessageToPlayer(tNet_game_details* pDetails, tNet_message* pMessage, tPlayer_ID pPlayer) {
  542.     //char str[256]; // Pierre-Marie Baty -- unused variable
  543.     // SOCKADDR_IPX_* remote_addr_ipx;
  544.     LOG_TRACE("(%p, %p, %d)", pDetails, pMessage, pPlayer);
  545.     NOT_IMPLEMENTED();
  546. }
  547.  
  548. // IDA: int __usercall PDNetSendMessageToAllPlayers@<EAX>(tNet_game_details *pDetails@<EAX>, tNet_message *pMessage@<EDX>)
  549. int PDNetSendMessageToAllPlayers(tNet_game_details* pDetails, tNet_message* pMessage) {
  550.     //char str[256]; // Pierre-Marie Baty -- unused variable
  551.     int i;
  552.     LOG_TRACE("(%p, %p)", pDetails, pMessage);
  553.  
  554.     for (i = 0; i < gNumber_of_net_players; ++i) {
  555.         if (i == gThis_net_player_index) {
  556.             continue;
  557.         }
  558.         if (sendto(gSocket, (const char*)pMessage, pMessage->overall_size, 0, (struct sockaddr*)&gNet_players[i].pd_net_info.addr_in, sizeof(gNet_players[i].pd_net_info.addr_in)) == -1) {
  559.             dr_dprintf("PDNetSendMessageToAllPlayers(): Error on sendto() - WSAGetLastError=%d", WSAGetLastError());
  560.             NetDisposeMessage(pDetails, pMessage);
  561.             return 1;
  562.         }
  563.     }
  564.     NetDisposeMessage(pDetails, pMessage);
  565.     return 0;
  566. }
  567.  
  568. // IDA: tNet_message* __usercall PDNetGetNextMessage@<EAX>(tNet_game_details *pDetails@<EAX>, void **pSender_address@<EDX>)
  569. tNet_message* PDNetGetNextMessage(tNet_game_details* pDetails, void** pSender_address) {
  570.     char* receive_buffer;
  571.     char str[256];
  572.     int msg_type;
  573.     LOG_TRACE("(%p, %p)", pDetails, pSender_address);
  574.  
  575.     char addr_str[32];
  576.     unsigned int sa_len;
  577.     int res;
  578.     tNet_message* msg;
  579.  
  580.     sa_len = sizeof(gRemote_addr);
  581.     msg = NetAllocateMessage(512);
  582.     receive_buffer = (char*)msg;
  583.     res = recvfrom(gSocket, receive_buffer, 512, 0, (struct sockaddr*)&gRemote_addr, &sa_len);
  584.     res = res != -1;
  585.     if (res == 0) {
  586.         res = WSAGetLastError() != WSAEWOULDBLOCK;
  587.         if (res) {
  588.             sprintf(str, "PDNetGetNextMessage(): Error on recvfrom() - WSAGetLastError=%d", res);
  589.             PDFatalError(str);
  590.         }
  591.     } else {
  592.         NetNowIPXLocalTarget2String(addr_str, gRemote_addr_ipx);
  593.         if (!SameEthernetAddress(gLocal_addr_ipx, gRemote_addr_ipx)) {
  594.             msg_type = GetMessageTypeFromMessage(receive_buffer);
  595.             switch (msg_type) {
  596.             case 1:
  597.                 if (gNet_mode == eNet_mode_host) {
  598.                     dr_dprintf("PDNetGetNextMessage(): Received '%s' from '%s', replying to joiner", receive_buffer, addr_str);
  599.                     MakeMessageToSend(2);
  600.                     if (sendto(gSocket, gSend_buffer, strlen(gSend_buffer) + 1, 0, (struct sockaddr*)&gRemote_addr, sizeof(gRemote_addr)) == -1) {
  601.                         dr_dprintf("PDNetGetNextMessage(): Error on sendto() - WSAGetLastError=%d", WSAGetLastError());
  602.                     }
  603.                 }
  604.                 break;
  605.             case 2:
  606.                 // no-op
  607.                 break;
  608.             default:
  609.                 dr_dprintf("PDNetGetNextMessage(): res is %d, received message type %d from '%s', passing up", res, msg->contents.header.type, addr_str);
  610.                 memcpy(&gLast_received_addr, gRemote_addr_ipx, sizeof(gLast_received_addr));
  611.                 *pSender_address = &gLast_received_addr;
  612.                 return msg;
  613.             }
  614.         }
  615.     }
  616.     msg->guarantee_number = 0;
  617.     NetDisposeMessage(pDetails, msg);
  618.     return 0;
  619. }
  620.  
  621. // IDA: tNet_message* __usercall PDNetAllocateMessage@<EAX>(tU32 pSize@<EAX>, tS32 pSize_decider@<EDX>)
  622. tNet_message* PDNetAllocateMessage(tU32 pSize, tS32 pSize_decider) {
  623.     LOG_TRACE("(%d, %d)", pSize, pSize_decider);
  624.     NOT_IMPLEMENTED();
  625. }
  626.  
  627. // IDA: void __usercall PDNetDisposeMessage(tNet_game_details *pDetails@<EAX>, tNet_message *pMessage@<EDX>)
  628. void PDNetDisposeMessage(tNet_game_details* pDetails, tNet_message* pMessage) {
  629.     LOG_TRACE("(%p, %p)", pDetails, pMessage);
  630.     NOT_IMPLEMENTED();
  631. }
  632.  
  633. // IDA: void __usercall PDNetSetPlayerSystemInfo(tNet_game_player_info *pPlayer@<EAX>, void *pSender_address@<EDX>)
  634. void PDNetSetPlayerSystemInfo(tNet_game_player_info* pPlayer, void* pSender_address) {
  635.     LOG_TRACE("(%p, %p)", pPlayer, pSender_address);
  636.  
  637.     dr_dprintf("PDNetSetPlayerSystemInfo()");
  638.     memcpy(&pPlayer->pd_net_info, pSender_address, sizeof(pPlayer->pd_net_info));
  639. }
  640.  
  641. // IDA: void __usercall PDNetDisposePlayer(tNet_game_player_info *pPlayer@<EAX>)
  642. void PDNetDisposePlayer(tNet_game_player_info* pPlayer) {
  643.     LOG_TRACE("(%p)", pPlayer);
  644.     NOT_IMPLEMENTED();
  645. }
  646.  
  647. // IDA: int __usercall PDNetSendMessageToAddress@<EAX>(tNet_game_details *pDetails@<EAX>, tNet_message *pMessage@<EDX>, void *pAddress@<EBX>)
  648. int PDNetSendMessageToAddress(tNet_game_details* pDetails, tNet_message* pMessage, void* pAddress) {
  649.     char str[256];
  650.     LOG_TRACE("(%p, %p, %p)", pDetails, pMessage, pAddress);
  651.  
  652.     NetNowIPXLocalTarget2String(str, (struct sockaddr_in*)pAddress);
  653.  
  654.     if (sendto(gSocket, (const char*)pMessage, pMessage->overall_size, 0, (const struct sockaddr*)pAddress, sizeof(struct sockaddr)) == -1) {
  655.         dr_dprintf("PDNetSendMessageToAddress(): Error on sendto() - WSAGetLastError=%d", WSAGetLastError());
  656.         NetDisposeMessage(pDetails, pMessage);
  657.         return 1;
  658.     }
  659.  
  660.     NetDisposeMessage(pDetails, pMessage);
  661.     return 0;
  662. }
  663.  
  664. // IDA: int __usercall PDNetInitClient@<EAX>(tNet_game_details *pDetails@<EAX>)
  665. int PDNetInitClient(tNet_game_details* pDetails) {
  666.     LOG_TRACE("(%p)", pDetails);
  667.     NOT_IMPLEMENTED();
  668. }
  669.  
  670. // IDA: int __cdecl PDNetGetHeaderSize()
  671. int PDNetGetHeaderSize(void) {
  672.     LOG_TRACE("()");
  673.  
  674.     return 0;
  675. }
  676.