Windows networking (#453)
* adds network adapter discovery * adds a pc-all network implementation * tidy cmake rules --------- Co-authored-by: Jeff Harris <jeff@1amstudios.com>
This commit is contained in:
parent
a2cdd1f061
commit
bcbecb372b
|
@ -161,12 +161,23 @@ target_sources(dethrace_obj PRIVATE
|
||||||
dr_types.h
|
dr_types.h
|
||||||
pd/net.h
|
pd/net.h
|
||||||
pd/sys.h
|
pd/sys.h
|
||||||
pc-win95/win95sys.c
|
|
||||||
|
# dethrace-added cross platform sys and network implementation
|
||||||
|
pc-all/allnet.c
|
||||||
|
# added in future PR
|
||||||
|
# pc-all/allsys.c
|
||||||
|
|
||||||
|
# original win95 sys and network
|
||||||
|
# pc-win95/win95sys.c
|
||||||
|
# pc-win95/ssdx.c
|
||||||
|
# pc-win95/ssdx.h
|
||||||
|
|
||||||
|
# todo remove from normal compile
|
||||||
pc-win95/dinput.h
|
pc-win95/dinput.h
|
||||||
pc-win95/ssdx.c
|
|
||||||
pc-win95/ssdx.h
|
# original dos sys and network
|
||||||
pc-win95/win95net.c
|
#pc-dos/dosnet.c
|
||||||
pc-dos/dosnet.c
|
# todo remove from normal compile
|
||||||
pc-dos/dossys.c
|
pc-dos/dossys.c
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -272,10 +272,10 @@ void dr_dprintf(char* fmt_string, ...) {
|
||||||
fflush(gDiagnostic_file);
|
fflush(gDiagnostic_file);
|
||||||
|
|
||||||
// Added by dethrace for debugging
|
// Added by dethrace for debugging
|
||||||
// va_start(args, fmt_string);
|
va_start(args, fmt_string);
|
||||||
// vprintf(fmt_string, args);
|
vprintf(fmt_string, args);
|
||||||
// va_end(args);
|
va_end(args);
|
||||||
// printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// IDA: int __usercall DoErrorInterface@<EAX>(int pMisc_text_index@<EAX>)
|
// IDA: int __usercall DoErrorInterface@<EAX>(int pMisc_text_index@<EAX>)
|
||||||
|
|
|
@ -1598,7 +1598,7 @@ void RequestCarDetails(tNet_game_details* pNet_game) {
|
||||||
gNet_mode = eNet_mode_thinking_about_it;
|
gNet_mode = eNet_mode_thinking_about_it;
|
||||||
}
|
}
|
||||||
message = NetBuildMessage(NETMSGID_CARDETAILSREQ, 0);
|
message = NetBuildMessage(NETMSGID_CARDETAILSREQ, 0);
|
||||||
NetGuaranteedSendMessageToAddress(pNet_game, message, &pNet_game->pd_net_info.addr_in, NULL);
|
NetGuaranteedSendMessageToAddress(pNet_game, message, &pNet_game->pd_net_info, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// IDA: int __cdecl PickARandomCar()
|
// IDA: int __cdecl PickARandomCar()
|
||||||
|
|
|
@ -12,6 +12,10 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// required for platform-specific network structs
|
||||||
|
// if needed for a different platform, make this conditional
|
||||||
|
#include "pc-all/net_types.h"
|
||||||
|
|
||||||
typedef unsigned char tU8;
|
typedef unsigned char tU8;
|
||||||
typedef signed char tS8;
|
typedef signed char tS8;
|
||||||
typedef uint16_t tU16;
|
typedef uint16_t tU16;
|
||||||
|
@ -30,9 +34,6 @@ typedef tU32 tPlayer_ID;
|
||||||
typedef void tPipe_reset_proc(void);
|
typedef void tPipe_reset_proc(void);
|
||||||
typedef struct tPowerup tPowerup;
|
typedef struct tPowerup tPowerup;
|
||||||
|
|
||||||
// FIXME? hardcoding pc-win95 here
|
|
||||||
#include "pc-win95/win95net_types.h"
|
|
||||||
|
|
||||||
#ifdef DETHRACE_FIX_BUGS
|
#ifdef DETHRACE_FIX_BUGS
|
||||||
typedef int tGot_proc(tPowerup*, tCar_spec*);
|
typedef int tGot_proc(tPowerup*, tCar_spec*);
|
||||||
typedef void tLose_proc(tPowerup*, tCar_spec*);
|
typedef void tLose_proc(tPowerup*, tCar_spec*);
|
||||||
|
@ -1383,6 +1384,7 @@ typedef union tNet_contents { // size: 0x160
|
||||||
} data; // @0x0
|
} data; // @0x0
|
||||||
} tNet_contents;
|
} tNet_contents;
|
||||||
|
|
||||||
|
#pragma pack(push, 4)
|
||||||
typedef struct tNet_message { // size: 0x17c
|
typedef struct tNet_message { // size: 0x17c
|
||||||
tU32 pd_stuff_so_DO_NOT_USE; // @0x0
|
tU32 pd_stuff_so_DO_NOT_USE; // @0x0
|
||||||
tU32 magic_number; // @0x4
|
tU32 magic_number; // @0x4
|
||||||
|
@ -1394,6 +1396,7 @@ typedef struct tNet_message { // size: 0x17c
|
||||||
tU16 overall_size; // @0x1a
|
tU16 overall_size; // @0x1a
|
||||||
tNet_contents contents; // @0x1c
|
tNet_contents contents; // @0x1c
|
||||||
} tNet_message;
|
} tNet_message;
|
||||||
|
#pragma pack(pop)
|
||||||
|
|
||||||
typedef struct tCar_detail_info {
|
typedef struct tCar_detail_info {
|
||||||
tCar_detail_ownership ownership;
|
tCar_detail_ownership ownership;
|
||||||
|
|
|
@ -0,0 +1,679 @@
|
||||||
|
#include "pd/net.h"
|
||||||
|
|
||||||
|
#include "brender.h"
|
||||||
|
#include "dr_types.h"
|
||||||
|
#include "errors.h"
|
||||||
|
#include "globvrpb.h"
|
||||||
|
#include "harness/config.h"
|
||||||
|
#include "harness/hooks.h"
|
||||||
|
#include "harness/os.h"
|
||||||
|
#include "harness/trace.h"
|
||||||
|
#include "net_types.h"
|
||||||
|
#include "network.h"
|
||||||
|
#include "pd/net.h"
|
||||||
|
#include "pd/sys.h"
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#define _WINSOCK_DEPRECATED_NO_WARNINGS
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
|
||||||
|
#define EWOULDBLOCK WSAEWOULDBLOCK
|
||||||
|
|
||||||
|
#else /* Assume posix style sockets on non-windows */
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// dethrace: have switched out IPX implementation for IP
|
||||||
|
|
||||||
|
tPD_net_game_info* gJoinable_games;
|
||||||
|
int gMatts_PC;
|
||||||
|
tU16 gSocket_number_pd_format;
|
||||||
|
char gLocal_addr_string[32];
|
||||||
|
char gReceive_buffer[512];
|
||||||
|
tPD_net_player_info gRemote_net_player_info;
|
||||||
|
tPD_net_player_info gLocal_net_player_info;
|
||||||
|
char gSend_buffer[512];
|
||||||
|
tU8* gSend_packet;
|
||||||
|
tU8* gSend_packet_ptr;
|
||||||
|
tU8* gListen_packet;
|
||||||
|
tU8* gListen_packet_ptr;
|
||||||
|
size_t gMsg_header_strlen;
|
||||||
|
int gNumber_of_networks;
|
||||||
|
int gNumber_of_hosts;
|
||||||
|
|
||||||
|
struct sockaddr_in gAny_addr;
|
||||||
|
struct sockaddr_in gLocal_addr;
|
||||||
|
struct sockaddr_in gRemote_addr;
|
||||||
|
struct sockaddr_in gBroadcast_addr;
|
||||||
|
struct sockaddr_in gLast_received_addr;
|
||||||
|
|
||||||
|
tCopyable_sockaddr_in gLocal_addr_copyable;
|
||||||
|
tCopyable_sockaddr_in gLast_received_addr_copyable;
|
||||||
|
|
||||||
|
int gSocket;
|
||||||
|
int gPlayer_id;
|
||||||
|
|
||||||
|
#define MESSAGE_HEADER_STR "CW95MSG"
|
||||||
|
#define JOINABLE_GAMES_CAPACITY 16
|
||||||
|
#define PORT 12286
|
||||||
|
|
||||||
|
DR_STATIC_ASSERT(offsetof(tNet_message, pd_stuff_so_DO_NOT_USE) == 0);
|
||||||
|
DR_STATIC_ASSERT(offsetof(tNet_message, magic_number) == 4);
|
||||||
|
DR_STATIC_ASSERT(offsetof(tNet_message, guarantee_number) == 8);
|
||||||
|
DR_STATIC_ASSERT(offsetof(tNet_message, sender) == 12);
|
||||||
|
DR_STATIC_ASSERT(offsetof(tNet_message, version) == 16);
|
||||||
|
DR_STATIC_ASSERT(offsetof(tNet_message, senders_time_stamp) == 20);
|
||||||
|
DR_STATIC_ASSERT(offsetof(tNet_message, num_contents) == 24);
|
||||||
|
DR_STATIC_ASSERT(offsetof(tNet_message, overall_size) == 26);
|
||||||
|
DR_STATIC_ASSERT(offsetof(tNet_message, contents) == 28);
|
||||||
|
|
||||||
|
// dethrace added
|
||||||
|
void PDNetCopyFromNative(tCopyable_sockaddr_in* pAddress, struct sockaddr_in* sock);
|
||||||
|
void PDNetCopyToNative(struct sockaddr_in* sock, tCopyable_sockaddr_in* pAddress);
|
||||||
|
|
||||||
|
// IDA: void __cdecl ClearupPDNetworkStuff()
|
||||||
|
void ClearupPDNetworkStuff(void) {
|
||||||
|
LOG_TRACE("()");
|
||||||
|
NOT_IMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: void __usercall MATTMessageCheck(char *pFunction_name@<EAX>, tNet_message *pMessage@<EDX>, int pAlleged_size@<EBX>)
|
||||||
|
void MATTMessageCheck(char* pFunction_name, tNet_message* pMessage, int pAlleged_size) {
|
||||||
|
LOG_TRACE("(\"%s\", %p, %d)", pFunction_name, pMessage, pAlleged_size);
|
||||||
|
NOT_IMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __usercall GetProfileText@<EAX>(char *pDest@<EAX>, int pDest_len@<EDX>, char *pFname@<EBX>, char *pKeyname@<ECX>)
|
||||||
|
int GetProfileText(char* pDest, int pDest_len, char* pFname, char* pKeyname) {
|
||||||
|
FILE* fp;
|
||||||
|
char in_buf[256];
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
int len;
|
||||||
|
LOG_TRACE("(\"%s\", %d, \"%s\", \"%s\")", pDest, pDest_len, pFname, pKeyname);
|
||||||
|
NOT_IMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __cdecl GetSocketNumberFromProfileFile()
|
||||||
|
int GetSocketNumberFromProfileFile(void) {
|
||||||
|
char str[256];
|
||||||
|
int sscanf_res;
|
||||||
|
tU32 socknum;
|
||||||
|
LOG_TRACE("()");
|
||||||
|
NOT_IMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// in dossys, this is called `NetNowIPXLocalTarget2String`
|
||||||
|
void SockAddrToString(char* pString, struct sockaddr_in* pSock_addr) {
|
||||||
|
LOG_TRACE("(\"%s\", %p)", pString, pSock_addr);
|
||||||
|
|
||||||
|
char portbuf[10];
|
||||||
|
|
||||||
|
inet_ntop(AF_INET, &pSock_addr->sin_addr, pString, 32);
|
||||||
|
sprintf(portbuf, ":%d", ntohs(pSock_addr->sin_port));
|
||||||
|
strcat(pString, portbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
// added by dethrace
|
||||||
|
void CopyableSockAddrToString(char* pString, tCopyable_sockaddr_in* pSock_addr) {
|
||||||
|
LOG_TRACE("(\"%s\", %p)", pString, pSock_addr);
|
||||||
|
|
||||||
|
char portbuf[10];
|
||||||
|
|
||||||
|
inet_ntop(AF_INET, &pSock_addr->address, pString, 32);
|
||||||
|
sprintf(portbuf, ":%d", ntohs(pSock_addr->port));
|
||||||
|
strcat(pString, portbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __usercall GetMessageTypeFromMessage@<EAX>(char *pMessage_str@<EAX>)
|
||||||
|
int GetMessageTypeFromMessage(char* pMessage_str) {
|
||||||
|
char* real_msg;
|
||||||
|
int msg_type_int;
|
||||||
|
LOG_TRACE("(\"%s\")", pMessage_str);
|
||||||
|
|
||||||
|
real_msg = &pMessage_str[4];
|
||||||
|
msg_type_int = 0;
|
||||||
|
|
||||||
|
// FIXME: "CW95MSG" value is used in and depends on platform
|
||||||
|
if (strncmp(real_msg, MESSAGE_HEADER_STR, gMsg_header_strlen) == 0) {
|
||||||
|
if (isdigit(real_msg[gMsg_header_strlen])) {
|
||||||
|
msg_type_int = real_msg[gMsg_header_strlen] - '0';
|
||||||
|
}
|
||||||
|
if (msg_type_int != 0 && msg_type_int < 3) {
|
||||||
|
return msg_type_int;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 999;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SameEthernetAddress(struct sockaddr_in* pAddr_1, struct sockaddr_in* pAddr_2) {
|
||||||
|
LOG_TRACE("(%p, %p)", pAddr_1, pAddr_2);
|
||||||
|
|
||||||
|
return memcmp(pAddr_1, pAddr_2, sizeof(struct sockaddr_in)) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// added by dethrace
|
||||||
|
int SameEthernetAddress2(tCopyable_sockaddr_in* pAddr_1, struct sockaddr_in* pAddr_2) {
|
||||||
|
LOG_TRACE("(%p, %p)", pAddr_1, pAddr_2);
|
||||||
|
|
||||||
|
return pAddr_1->port == pAddr_2->sin_port && pAddr_1->address == pAddr_2->sin_addr.s_addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*SOCKADDR_IPX_* */ void GetIPXAddrFromPlayerID(tPlayer_ID pPlayer_id) {
|
||||||
|
int i;
|
||||||
|
tU8* nodenum;
|
||||||
|
NOT_IMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: void __usercall MakeMessageToSend(int pMessage_type@<EAX>)
|
||||||
|
void MakeMessageToSend(int pMessage_type) {
|
||||||
|
LOG_TRACE("(%d)", pMessage_type);
|
||||||
|
|
||||||
|
#ifdef DETHRACE_FIX_BUGS
|
||||||
|
sprintf(gSend_buffer, "XXXX%s%1d", MESSAGE_HEADER_STR, pMessage_type);
|
||||||
|
#else
|
||||||
|
sprintf(gSend_buffer, "XXXX%s%0.1d", MESSAGE_HEADER_STR, pMessage_type);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __cdecl ReceiveHostResponses()
|
||||||
|
int ReceiveHostResponses(void) {
|
||||||
|
char str[256];
|
||||||
|
int i;
|
||||||
|
int already_registered;
|
||||||
|
LOG_TRACE("()");
|
||||||
|
|
||||||
|
char addr_string[32];
|
||||||
|
unsigned int sa_len;
|
||||||
|
int error;
|
||||||
|
|
||||||
|
sa_len = sizeof(gRemote_addr);
|
||||||
|
while (1) {
|
||||||
|
if (recvfrom(gSocket, gReceive_buffer, sizeof(gReceive_buffer), 0, (struct sockaddr*)&gRemote_addr, &sa_len) == -1) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
SockAddrToString(addr_string, &gRemote_addr);
|
||||||
|
dr_dprintf("ReceiveHostResponses(): Received string '%s' from %s", gReceive_buffer, addr_string);
|
||||||
|
|
||||||
|
if (SameEthernetAddress(&gLocal_addr, &gRemote_addr)) {
|
||||||
|
dr_dprintf("*** Discounting the above 'cos we sent it ***");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (GetMessageTypeFromMessage(gReceive_buffer) != 2) {
|
||||||
|
dr_dprintf("*** Discounting the above 'cos it's not a host reply ***");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
dr_dprintf("*** It's a host reply! ***");
|
||||||
|
already_registered = 0;
|
||||||
|
for (i = 0; i < gNumber_of_hosts; i++) {
|
||||||
|
if (SameEthernetAddress2(&gJoinable_games[i].addr_in, &gRemote_addr)) {
|
||||||
|
already_registered = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (already_registered) {
|
||||||
|
dr_dprintf("That game already registered");
|
||||||
|
gJoinable_games[i].last_response = PDGetTotalTime();
|
||||||
|
} else {
|
||||||
|
dr_dprintf("Adding joinable game to slot #%d", gNumber_of_hosts);
|
||||||
|
PDNetCopyFromNative(&gJoinable_games[gNumber_of_hosts].addr_in, &gRemote_addr);
|
||||||
|
gJoinable_games[gNumber_of_hosts].last_response = PDGetTotalTime();
|
||||||
|
gNumber_of_hosts++;
|
||||||
|
dr_dprintf("Number of games found so far: %d", gNumber_of_hosts);
|
||||||
|
}
|
||||||
|
if (gNumber_of_hosts) {
|
||||||
|
dr_dprintf("Currently registered net games:");
|
||||||
|
for (i = 0; i < gNumber_of_hosts; i++) {
|
||||||
|
CopyableSockAddrToString(str, &gJoinable_games[i].addr_in);
|
||||||
|
dr_dprintf("%d: Host addr %s", i, str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
error = OS_GetLastSocketError() != EWOULDBLOCK;
|
||||||
|
if (error == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
dr_dprintf("ReceiveHostResponses(): Error on recvfrom() - WSAGetLastError=%d", error);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __cdecl BroadcastMessage()
|
||||||
|
int BroadcastMessage(void) {
|
||||||
|
int i;
|
||||||
|
int errors;
|
||||||
|
char broadcast_addr_string[32];
|
||||||
|
char* real_msg;
|
||||||
|
LOG_TRACE("()");
|
||||||
|
|
||||||
|
errors = 0;
|
||||||
|
for (i = 0; i < gNumber_of_networks; i++) {
|
||||||
|
SockAddrToString(broadcast_addr_string, &gBroadcast_addr);
|
||||||
|
dr_dprintf("Broadcasting on address '%s'", broadcast_addr_string);
|
||||||
|
if (sendto(gSocket, gSend_buffer, strlen(gSend_buffer) + 1, 0, (struct sockaddr*)&gBroadcast_addr, sizeof(gBroadcast_addr)) == -1) {
|
||||||
|
dr_dprintf("BroadcastMessage(): Error on sendto() - WSAGetLastError=%d", OS_GetLastSocketError());
|
||||||
|
errors = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return errors == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __cdecl PDNetInitialise()
|
||||||
|
int PDNetInitialise(void) {
|
||||||
|
tU32 timenow;
|
||||||
|
char profile_string[32];
|
||||||
|
char key_name[32];
|
||||||
|
int sscanf_res;
|
||||||
|
int i;
|
||||||
|
tU32 netnum;
|
||||||
|
char str[256];
|
||||||
|
int mess_num;
|
||||||
|
|
||||||
|
struct linger so_linger;
|
||||||
|
unsigned int sa_len;
|
||||||
|
|
||||||
|
LOG_TRACE("()");
|
||||||
|
|
||||||
|
gPlayer_id = PDGetTotalTime();
|
||||||
|
sa_len = sizeof(struct sockaddr_in);
|
||||||
|
dr_dprintf("PDNetInitialise()");
|
||||||
|
|
||||||
|
memset(&gAny_addr, 0, sizeof(gAny_addr));
|
||||||
|
memset(&gLocal_addr, 0, sizeof(gLocal_addr));
|
||||||
|
memset(&gRemote_addr, 0, sizeof(gRemote_addr));
|
||||||
|
memset(&gBroadcast_addr, 0, sizeof(gBroadcast_addr));
|
||||||
|
|
||||||
|
gAny_addr.sin_family = AF_INET;
|
||||||
|
gAny_addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
gAny_addr.sin_port = htons(PORT);
|
||||||
|
|
||||||
|
int found = OS_GetAdapterAddress(harness_game_config.network_adapter_name, &gLocal_addr);
|
||||||
|
if (!found) {
|
||||||
|
gLocal_addr.sin_addr.s_addr = INADDR_LOOPBACK;
|
||||||
|
}
|
||||||
|
|
||||||
|
gLocal_addr.sin_port = htons(PORT);
|
||||||
|
|
||||||
|
// advertise that we are connectable on this address
|
||||||
|
PDNetCopyFromNative(&gLocal_addr_copyable, &gLocal_addr);
|
||||||
|
|
||||||
|
SockAddrToString(str, &gLocal_addr);
|
||||||
|
LOG_INFO("Advertising on %s", str);
|
||||||
|
|
||||||
|
// actually listen on any address
|
||||||
|
gLocal_addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
|
SockAddrToString(str, &gLocal_addr);
|
||||||
|
LOG_INFO("Listening on %s", str);
|
||||||
|
|
||||||
|
gRemote_addr.sin_family = AF_INET;
|
||||||
|
gRemote_addr.sin_port = htons(PORT);
|
||||||
|
gBroadcast_addr.sin_family = AF_INET;
|
||||||
|
gBroadcast_addr.sin_port = htons(PORT);
|
||||||
|
gBroadcast_addr.sin_addr.s_addr = INADDR_BROADCAST;
|
||||||
|
|
||||||
|
if (OS_InitSockets() == -1) {
|
||||||
|
dr_dprintf("PDNetInitialise(): WSAStartup() failed");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
gSocket = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
if (gSocket == -1) {
|
||||||
|
dr_dprintf("PDNetInitialise(): Failed to create socket - WSAGetLastError=%d", OS_GetLastSocketError());
|
||||||
|
OS_CleanupSockets();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
int broadcast = 1;
|
||||||
|
setsockopt(gSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast));
|
||||||
|
so_linger.l_onoff = 1;
|
||||||
|
so_linger.l_linger = 0;
|
||||||
|
setsockopt(gSocket, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(so_linger));
|
||||||
|
|
||||||
|
unsigned long nobio = 1;
|
||||||
|
if (OS_SetSocketNonBlocking(gSocket) == -1) {
|
||||||
|
dr_dprintf("Error on ioctlsocket() - WSAGetLastError=%d", OS_GetLastSocketError());
|
||||||
|
OS_CloseSocket(gSocket);
|
||||||
|
OS_CleanupSockets();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (harness_game_config.no_bind == 0) {
|
||||||
|
if (bind(gSocket, (struct sockaddr*)&gAny_addr, sizeof(gAny_addr)) == -1) {
|
||||||
|
dr_dprintf("Error on bind() - WSAGetLastError=%d", OS_GetLastSocketError());
|
||||||
|
OS_CloseSocket(gSocket);
|
||||||
|
OS_CleanupSockets();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SockAddrToString(gLocal_addr_string, &gLocal_addr);
|
||||||
|
|
||||||
|
dr_dprintf("Socket bound OK; local address is '%s'", gLocal_addr_string);
|
||||||
|
gMsg_header_strlen = 7;
|
||||||
|
if (strstr(gLocal_addr_string, "00a0240f9fac")) {
|
||||||
|
gMatts_PC = 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __cdecl PDNetShutdown()
|
||||||
|
int PDNetShutdown(void) {
|
||||||
|
LOG_TRACE("()");
|
||||||
|
|
||||||
|
dr_dprintf("PDNetShutdown()");
|
||||||
|
if (gSocket != -1) {
|
||||||
|
OS_CloseSocket(gSocket);
|
||||||
|
}
|
||||||
|
gSocket = -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: void __cdecl PDNetStartProducingJoinList()
|
||||||
|
void PDNetStartProducingJoinList(void) {
|
||||||
|
LOG_TRACE("()");
|
||||||
|
|
||||||
|
dr_dprintf("PDNetStartProducingJoinList()");
|
||||||
|
gNumber_of_hosts = 0;
|
||||||
|
gJoinable_games = BrMemAllocate(sizeof(tPD_net_game_info) * JOINABLE_GAMES_CAPACITY, 0x80u);
|
||||||
|
if (gJoinable_games == NULL) {
|
||||||
|
PDFatalError("Can't allocate memory for joinable games");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: void __cdecl PDNetEndJoinList()
|
||||||
|
void PDNetEndJoinList(void) {
|
||||||
|
LOG_TRACE("()");
|
||||||
|
|
||||||
|
dr_dprintf("PDNetEndJoinList()");
|
||||||
|
if (gJoinable_games) {
|
||||||
|
BrMemFree(gJoinable_games);
|
||||||
|
}
|
||||||
|
gJoinable_games = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __usercall PDNetGetNextJoinGame@<EAX>(tNet_game_details *pGame@<EAX>, int pIndex@<EDX>)
|
||||||
|
int PDNetGetNextJoinGame(tNet_game_details* pGame, int pIndex) {
|
||||||
|
static tU32 next_broadcast_time = 0;
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
int number_of_hosts_has_changed;
|
||||||
|
char str[256];
|
||||||
|
LOG_TRACE9("(%p, %d)", pGame, pIndex);
|
||||||
|
|
||||||
|
dr_dprintf("PDNetGetNextJoinGame(): pIndex is %d", pIndex);
|
||||||
|
if (pIndex == 0) {
|
||||||
|
do {
|
||||||
|
number_of_hosts_has_changed = 0;
|
||||||
|
for (i = 0; i < gNumber_of_hosts; i++) {
|
||||||
|
if (gJoinable_games[i].last_response + 10000 < PDGetTotalTime()) {
|
||||||
|
number_of_hosts_has_changed = 1;
|
||||||
|
for (j = i; j < gNumber_of_hosts - 1; j++) {
|
||||||
|
memcpy(&gJoinable_games[j], &gJoinable_games[j + 1], sizeof(tPD_net_game_info));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (number_of_hosts_has_changed) {
|
||||||
|
gNumber_of_hosts--;
|
||||||
|
}
|
||||||
|
} while (number_of_hosts_has_changed);
|
||||||
|
if (PDGetTotalTime() > next_broadcast_time) {
|
||||||
|
next_broadcast_time = PDGetTotalTime() + 3000;
|
||||||
|
MakeMessageToSend(1);
|
||||||
|
if (BroadcastMessage() == 0) {
|
||||||
|
dr_dprintf("PDNetGetNextJoinGame(): Error on BroadcastMessage()");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ReceiveHostResponses();
|
||||||
|
if (gNumber_of_hosts <= pIndex) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
dr_dprintf("PDNetGetNextJoinGame(): Adding game.");
|
||||||
|
// danger: copying a tPD_net_game_info into a tPD_net_player_info
|
||||||
|
pGame->pd_net_info = *(tPD_net_player_info*)&gJoinable_games[pIndex];
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: void __usercall PDNetDisposeGameDetails(tNet_game_details *pDetails@<EAX>)
|
||||||
|
void PDNetDisposeGameDetails(tNet_game_details* pDetails) {
|
||||||
|
LOG_TRACE("(%p)", pDetails);
|
||||||
|
NOT_IMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __usercall PDNetHostGame@<EAX>(tNet_game_details *pDetails@<EAX>, char *pHost_name@<EDX>, void **pHost_address@<EBX>)
|
||||||
|
int PDNetHostGame(tNet_game_details* pDetails, char* pHost_name, void** pHost_address) {
|
||||||
|
LOG_TRACE("(%p, \"%s\", %p)", pDetails, pHost_name, pHost_address);
|
||||||
|
|
||||||
|
dr_dprintf("PDNetHostGame()");
|
||||||
|
//*pHost_address = &gLocal_addr;
|
||||||
|
*pHost_address = &gLocal_addr_copyable;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __usercall PDNetJoinGame@<EAX>(tNet_game_details *pDetails@<EAX>, char *pPlayer_name@<EDX>)
|
||||||
|
int PDNetJoinGame(tNet_game_details* pDetails, char* pPlayer_name) {
|
||||||
|
LOG_TRACE("(%p, \"%s\")", pDetails, pPlayer_name);
|
||||||
|
|
||||||
|
dr_dprintf("PDNetJoinGame()");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: void __usercall PDNetLeaveGame(tNet_game_details *pDetails@<EAX>)
|
||||||
|
void PDNetLeaveGame(tNet_game_details* pDetails) {
|
||||||
|
LOG_TRACE("(%p)", pDetails);
|
||||||
|
|
||||||
|
dr_dprintf("PDNetLeaveGame()");
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: void __usercall PDNetHostFinishGame(tNet_game_details *pDetails@<EAX>)
|
||||||
|
void PDNetHostFinishGame(tNet_game_details* pDetails) {
|
||||||
|
LOG_TRACE("(%p)", pDetails);
|
||||||
|
|
||||||
|
dr_dprintf("PDNetHostFinishGame()");
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: tU32 __usercall PDNetExtractGameID@<EAX>(tNet_game_details *pDetails@<EAX>)
|
||||||
|
tU32 PDNetExtractGameID(tNet_game_details* pDetails) {
|
||||||
|
LOG_TRACE("(%p)", pDetails);
|
||||||
|
|
||||||
|
dr_dprintf("PDNetExtractGameID()");
|
||||||
|
// dethrace changed
|
||||||
|
// return ntohs(gLocal_addr.sin_addr.s_addr);
|
||||||
|
return gPlayer_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: tPlayer_ID __usercall PDNetExtractPlayerID@<EAX>(tNet_game_details *pDetails@<EAX>)
|
||||||
|
tPlayer_ID PDNetExtractPlayerID(tNet_game_details* pDetails) {
|
||||||
|
LOG_TRACE("(%p)", pDetails);
|
||||||
|
|
||||||
|
dr_dprintf("PDNetExtractPlayerID()");
|
||||||
|
// dethrace changed
|
||||||
|
// return ntohs(gLocal_addr.sin_addr.s_addr);
|
||||||
|
return gPlayer_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: void __usercall PDNetObtainSystemUserName(char *pName@<EAX>, int pMax_length@<EDX>)
|
||||||
|
void PDNetObtainSystemUserName(char* pName, int pMax_length) {
|
||||||
|
int result;
|
||||||
|
char* found;
|
||||||
|
dr_dprintf("PDNetObtainSystemUserName()");
|
||||||
|
|
||||||
|
result = gethostname(pName, pMax_length);
|
||||||
|
if (result == 0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
found = strpbrk(pName, "_=(){}[]<>!$%^&*/:@~;'#,?\\|`\"");
|
||||||
|
if (found == NULL) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
*found = '-';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __usercall PDNetSendMessageToPlayer@<EAX>(tNet_game_details *pDetails@<EAX>, tNet_message *pMessage@<EDX>, tPlayer_ID pPlayer@<EBX>)
|
||||||
|
int PDNetSendMessageToPlayer(tNet_game_details* pDetails, tNet_message* pMessage, tPlayer_ID pPlayer) {
|
||||||
|
char str[256];
|
||||||
|
LOG_TRACE("(%p, %p, %d)", pDetails, pMessage, pPlayer);
|
||||||
|
NOT_IMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __usercall PDNetSendMessageToAllPlayers@<EAX>(tNet_game_details *pDetails@<EAX>, tNet_message *pMessage@<EDX>)
|
||||||
|
int PDNetSendMessageToAllPlayers(tNet_game_details* pDetails, tNet_message* pMessage) {
|
||||||
|
char str[256];
|
||||||
|
int i;
|
||||||
|
LOG_TRACE("(%p, %p)", pDetails, pMessage);
|
||||||
|
|
||||||
|
struct sockaddr_in someaddr;
|
||||||
|
|
||||||
|
for (i = 0; i < gNumber_of_net_players; ++i) {
|
||||||
|
if (i == gThis_net_player_index) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
PDNetCopyToNative(&someaddr, &gNet_players[i].pd_net_info.addr_in);
|
||||||
|
SockAddrToString(str, &someaddr);
|
||||||
|
LOG_DEBUG(str);
|
||||||
|
if (sendto(gSocket, (const char*)pMessage, pMessage->overall_size, 0, (struct sockaddr*)&someaddr, sizeof(someaddr)) == -1) {
|
||||||
|
dr_dprintf("PDNetSendMessageToAllPlayers(): Error on sendto() - WSAGetLastError=%d", OS_GetLastSocketError());
|
||||||
|
NetDisposeMessage(pDetails, pMessage);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NetDisposeMessage(pDetails, pMessage);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: tNet_message* __usercall PDNetGetNextMessage@<EAX>(tNet_game_details *pDetails@<EAX>, void **pSender_address@<EDX>)
|
||||||
|
tNet_message* PDNetGetNextMessage(tNet_game_details* pDetails, void** pSender_address) {
|
||||||
|
char* receive_buffer;
|
||||||
|
char str[256];
|
||||||
|
int msg_type;
|
||||||
|
LOG_TRACE("(%p, %p)", pDetails, pSender_address);
|
||||||
|
|
||||||
|
char addr_str[32];
|
||||||
|
unsigned int sa_len;
|
||||||
|
int res;
|
||||||
|
tNet_message* msg;
|
||||||
|
|
||||||
|
sa_len = sizeof(gRemote_addr);
|
||||||
|
msg = NetAllocateMessage(512);
|
||||||
|
receive_buffer = (char*)msg;
|
||||||
|
res = recvfrom(gSocket, receive_buffer, 512, 0, (struct sockaddr*)&gRemote_addr, &sa_len);
|
||||||
|
res = res != -1;
|
||||||
|
if (res == 0) {
|
||||||
|
res = OS_GetLastSocketError() != EWOULDBLOCK;
|
||||||
|
if (res) {
|
||||||
|
sprintf(str, "PDNetGetNextMessage(): Error on recvfrom() - WSAGetLastError=%d", res);
|
||||||
|
PDFatalError(str);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
SockAddrToString(addr_str, &gRemote_addr);
|
||||||
|
if (!SameEthernetAddress(&gLocal_addr, &gRemote_addr)) {
|
||||||
|
msg_type = GetMessageTypeFromMessage(receive_buffer);
|
||||||
|
switch (msg_type) {
|
||||||
|
case 1:
|
||||||
|
if (gNet_mode == eNet_mode_host) {
|
||||||
|
dr_dprintf("PDNetGetNextMessage(): Received '%s' from '%s', replying to joiner", receive_buffer, addr_str);
|
||||||
|
MakeMessageToSend(2);
|
||||||
|
if (sendto(gSocket, gSend_buffer, strlen(gSend_buffer) + 1, 0, (struct sockaddr*)&gRemote_addr, sizeof(gRemote_addr)) == -1) {
|
||||||
|
dr_dprintf("PDNetGetNextMessage(): Error on sendto() - WSAGetLastError=%d", OS_GetLastSocketError());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
// no-op
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
dr_dprintf("PDNetGetNextMessage(): res is %d, received message type %d from '%s', passing up", res, msg->contents.header.type, addr_str);
|
||||||
|
memcpy(&gLast_received_addr, &gRemote_addr, sizeof(gLast_received_addr));
|
||||||
|
|
||||||
|
// Changed by dethrace
|
||||||
|
// *pSender_address = &gLast_received_addr;
|
||||||
|
PDNetCopyFromNative(&gLast_received_addr_copyable, &gLast_received_addr);
|
||||||
|
*pSender_address = &gLast_received_addr_copyable;
|
||||||
|
return msg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
msg->guarantee_number = 0;
|
||||||
|
NetDisposeMessage(pDetails, msg);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: tNet_message* __usercall PDNetAllocateMessage@<EAX>(tU32 pSize@<EAX>, tS32 pSize_decider@<EDX>)
|
||||||
|
tNet_message* PDNetAllocateMessage(tU32 pSize, tS32 pSize_decider) {
|
||||||
|
LOG_TRACE("(%d, %d)", pSize, pSize_decider);
|
||||||
|
NOT_IMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: void __usercall PDNetDisposeMessage(tNet_game_details *pDetails@<EAX>, tNet_message *pMessage@<EDX>)
|
||||||
|
void PDNetDisposeMessage(tNet_game_details* pDetails, tNet_message* pMessage) {
|
||||||
|
LOG_TRACE("(%p, %p)", pDetails, pMessage);
|
||||||
|
NOT_IMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: void __usercall PDNetSetPlayerSystemInfo(tNet_game_player_info *pPlayer@<EAX>, void *pSender_address@<EDX>)
|
||||||
|
void PDNetSetPlayerSystemInfo(tNet_game_player_info* pPlayer, void* pSender_address) {
|
||||||
|
LOG_TRACE("(%p, %p)", pPlayer, pSender_address);
|
||||||
|
|
||||||
|
dr_dprintf("PDNetSetPlayerSystemInfo()");
|
||||||
|
memcpy(&pPlayer->pd_net_info, pSender_address, sizeof(pPlayer->pd_net_info));
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: void __usercall PDNetDisposePlayer(tNet_game_player_info *pPlayer@<EAX>)
|
||||||
|
void PDNetDisposePlayer(tNet_game_player_info* pPlayer) {
|
||||||
|
LOG_TRACE("(%p)", pPlayer);
|
||||||
|
NOT_IMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __usercall PDNetSendMessageToAddress@<EAX>(tNet_game_details *pDetails@<EAX>, tNet_message *pMessage@<EDX>, void *pAddress@<EBX>)
|
||||||
|
int PDNetSendMessageToAddress(tNet_game_details* pDetails, tNet_message* pMessage, void* pAddress) {
|
||||||
|
char str[256];
|
||||||
|
LOG_TRACE("(%p, %p, %p)", pDetails, pMessage, pAddress);
|
||||||
|
struct sockaddr_in someaddr;
|
||||||
|
|
||||||
|
PDNetCopyToNative(&someaddr, pAddress);
|
||||||
|
|
||||||
|
SockAddrToString(str, &someaddr);
|
||||||
|
|
||||||
|
if (sendto(gSocket, (const char*)pMessage, pMessage->overall_size, 0, (struct sockaddr*)&someaddr, sizeof(someaddr)) == -1) {
|
||||||
|
dr_dprintf("PDNetSendMessageToAddress(): Error on sendto() - WSAGetLastError=%d", OS_GetLastSocketError());
|
||||||
|
NetDisposeMessage(pDetails, pMessage);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
NetDisposeMessage(pDetails, pMessage);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __usercall PDNetInitClient@<EAX>(tNet_game_details *pDetails@<EAX>)
|
||||||
|
int PDNetInitClient(tNet_game_details* pDetails) {
|
||||||
|
LOG_TRACE("(%p)", pDetails);
|
||||||
|
NOT_IMPLEMENTED();
|
||||||
|
}
|
||||||
|
|
||||||
|
// IDA: int __cdecl PDNetGetHeaderSize()
|
||||||
|
int PDNetGetHeaderSize(void) {
|
||||||
|
LOG_TRACE("()");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PDNetCopyFromNative(tCopyable_sockaddr_in* pAddress, struct sockaddr_in* sock) {
|
||||||
|
pAddress->port = sock->sin_port;
|
||||||
|
pAddress->address = sock->sin_addr.s_addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PDNetCopyToNative(struct sockaddr_in* sock, tCopyable_sockaddr_in* pAddress) {
|
||||||
|
sock->sin_addr.s_addr = pAddress->address;
|
||||||
|
sock->sin_port = pAddress->port;
|
||||||
|
sock->sin_family = AF_INET;
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef PC_ALL_NET_TYPES_H
|
||||||
|
#define PC_ALL_NET_TYPES_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// This file added dethrace
|
||||||
|
// - have switched out IPX implementation for IP
|
||||||
|
// - cross-platform instead of per-platform implementation
|
||||||
|
|
||||||
|
// cannot be a regular sockaddr_in because it is transmitted between OS's
|
||||||
|
typedef struct tCopyable_sockaddr_in {
|
||||||
|
uint64_t address;
|
||||||
|
uint32_t port;
|
||||||
|
} tCopyable_sockaddr_in;
|
||||||
|
|
||||||
|
typedef struct tPD_net_player_info {
|
||||||
|
// cannot be a regular sockaddr_in because it is transmitted between OS's
|
||||||
|
tCopyable_sockaddr_in addr_in;
|
||||||
|
|
||||||
|
} tPD_net_player_info;
|
||||||
|
|
||||||
|
// has to match `tPD_net_player_info` - see `PDNetGetNextJoinGame`
|
||||||
|
typedef struct tPD_net_game_info {
|
||||||
|
// cannot be a regular sockaddr_in because it is transmitted between OS's
|
||||||
|
tCopyable_sockaddr_in addr_in;
|
||||||
|
|
||||||
|
uint32_t last_response;
|
||||||
|
} tPD_net_game_info;
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,3 +1,5 @@
|
||||||
|
#ifdef 0
|
||||||
|
|
||||||
#include "pd/net.h"
|
#include "pd/net.h"
|
||||||
|
|
||||||
#include "brender.h"
|
#include "brender.h"
|
||||||
|
@ -673,3 +675,5 @@ int PDNetGetHeaderSize(void) {
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -1,25 +0,0 @@
|
||||||
#ifndef WIN95_NET_TYPES_H
|
|
||||||
#define WIN95_NET_TYPES_H
|
|
||||||
|
|
||||||
// #include "dr_types.h"
|
|
||||||
#include "harness/win95_polyfill.h"
|
|
||||||
#include "harness/winsock.h"
|
|
||||||
|
|
||||||
// dethrace: have switched out IPX implementation for IP
|
|
||||||
|
|
||||||
typedef struct tPD_net_player_info {
|
|
||||||
// struct sockaddr_ipx addr_ipx;
|
|
||||||
struct sockaddr_in addr_in;
|
|
||||||
} tPD_net_player_info;
|
|
||||||
|
|
||||||
typedef struct tPD_net_game_info {
|
|
||||||
// struct sockaddr_ipx addr_ipx;
|
|
||||||
struct sockaddr_in addr_in;
|
|
||||||
tU32 last_response;
|
|
||||||
} tPD_net_game_info;
|
|
||||||
|
|
||||||
// typedef struct tIPX_netnum {
|
|
||||||
// unsigned char bNetwork[4];
|
|
||||||
// } tIPX_netnum;
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -40,8 +40,6 @@ target_sources(harness PRIVATE
|
||||||
include/harness/trace.h
|
include/harness/trace.h
|
||||||
include/harness/config.h
|
include/harness/config.h
|
||||||
include/harness/os.h
|
include/harness/os.h
|
||||||
include/harness/win95_polyfill.h
|
|
||||||
include/harness/win95_polyfill_defs.h
|
|
||||||
include/harness/audio.h
|
include/harness/audio.h
|
||||||
# cameras/debug_camera.c
|
# cameras/debug_camera.c
|
||||||
# cameras/debug_camera.h
|
# cameras/debug_camera.h
|
||||||
|
@ -50,8 +48,6 @@ target_sources(harness PRIVATE
|
||||||
harness.c
|
harness.c
|
||||||
harness.h
|
harness.h
|
||||||
audio/miniaudio.c
|
audio/miniaudio.c
|
||||||
win95/polyfill.c
|
|
||||||
win95/winsock.c
|
|
||||||
platforms/null.c
|
platforms/null.c
|
||||||
platforms/null.h
|
platforms/null.h
|
||||||
|
|
||||||
|
|
|
@ -340,9 +340,6 @@ int Harness_ProcessCommandLine(tArgument_config* config, int* argc, char* argv[]
|
||||||
} else if (strcasecmp(argv[i], "--sound-options") == 0) {
|
} else if (strcasecmp(argv[i], "--sound-options") == 0) {
|
||||||
harness_game_config.sound_options = 1;
|
harness_game_config.sound_options = 1;
|
||||||
consumed = 1;
|
consumed = 1;
|
||||||
} else if (strcasecmp(argv[i], "--no-bind") == 0) {
|
|
||||||
harness_game_config.no_bind = 1;
|
|
||||||
consumed = 1;
|
|
||||||
} else if (strcasecmp(argv[i], "--opengl") == 0) {
|
} else if (strcasecmp(argv[i], "--opengl") == 0) {
|
||||||
config->platform_capabilityies &= ~ePlatform_cap_video_mask;
|
config->platform_capabilityies &= ~ePlatform_cap_video_mask;
|
||||||
config->platform_capabilityies |= ePlatform_cap_opengl;
|
config->platform_capabilityies |= ePlatform_cap_opengl;
|
||||||
|
@ -354,6 +351,13 @@ int Harness_ProcessCommandLine(tArgument_config* config, int* argc, char* argv[]
|
||||||
} else if (strcasecmp(argv[i], "--game-completed") == 0) {
|
} else if (strcasecmp(argv[i], "--game-completed") == 0) {
|
||||||
harness_game_config.game_completed = 1;
|
harness_game_config.game_completed = 1;
|
||||||
consumed = 1;
|
consumed = 1;
|
||||||
|
} else if (strcasecmp(argv[i], "--no-bind") == 0) {
|
||||||
|
harness_game_config.no_bind = 1;
|
||||||
|
consumed = 1;
|
||||||
|
} else if (strstr(argv[i], "--network-adapter-name=") != NULL) {
|
||||||
|
char* s = strstr(argv[i], "=");
|
||||||
|
strcpy(harness_game_config.network_adapter_name, s + 1);
|
||||||
|
consumed = 1;
|
||||||
} else if (strcmp(argv[i], "--platform") == 0) {
|
} else if (strcmp(argv[i], "--platform") == 0) {
|
||||||
if (i < *argc + 1) {
|
if (i < *argc + 1) {
|
||||||
config->platform_name = argv[i + 1];
|
config->platform_name = argv[i + 1];
|
||||||
|
|
|
@ -45,11 +45,15 @@ typedef struct tHarness_game_config {
|
||||||
int start_full_screen;
|
int start_full_screen;
|
||||||
int gore_check;
|
int gore_check;
|
||||||
int sound_options;
|
int sound_options;
|
||||||
int no_bind;
|
|
||||||
int no_music;
|
int no_music;
|
||||||
int verbose;
|
int verbose;
|
||||||
int opengl_3dfx_mode;
|
int opengl_3dfx_mode;
|
||||||
int game_completed;
|
int game_completed;
|
||||||
|
|
||||||
|
int install_signalhandler;
|
||||||
|
int no_bind;
|
||||||
|
char network_adapter_name[256];
|
||||||
} tHarness_game_config;
|
} tHarness_game_config;
|
||||||
|
|
||||||
extern tHarness_game_info harness_game_info;
|
extern tHarness_game_info harness_game_info;
|
||||||
|
|
|
@ -40,4 +40,16 @@ char* OS_Basename(const char* path);
|
||||||
|
|
||||||
char* OS_GetWorkingDirectory(char* argv0);
|
char* OS_GetWorkingDirectory(char* argv0);
|
||||||
|
|
||||||
|
int OS_GetAdapterAddress(char* name, void* pSockaddr_in);
|
||||||
|
|
||||||
|
int OS_InitSockets(void);
|
||||||
|
|
||||||
|
void OS_CleanupSockets(void);
|
||||||
|
|
||||||
|
int OS_GetLastSocketError(void);
|
||||||
|
|
||||||
|
int OS_SetSocketNonBlocking(int socket);
|
||||||
|
|
||||||
|
int OS_CloseSocket(int socket);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
#include "harness/config.h"
|
#include "harness/config.h"
|
||||||
#include "harness/os.h"
|
#include "harness/os.h"
|
||||||
|
#include <arpa/inet.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
|
@ -10,6 +11,7 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <execinfo.h>
|
#include <execinfo.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
#include <ifaddrs.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <link.h>
|
#include <link.h>
|
||||||
|
@ -19,6 +21,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
|
@ -348,3 +351,54 @@ char* OS_Basename(const char* path) {
|
||||||
char* OS_GetWorkingDirectory(char* argv0) {
|
char* OS_GetWorkingDirectory(char* argv0) {
|
||||||
return OS_Dirname(argv0);
|
return OS_Dirname(argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int OS_GetAdapterAddress(char* name, void* pSockaddr_in) {
|
||||||
|
struct ifaddrs *ifaddr, *ifa;
|
||||||
|
int found = 0;
|
||||||
|
|
||||||
|
// Get the list of network interfaces
|
||||||
|
if (getifaddrs(&ifaddr) == -1) {
|
||||||
|
perror("getifaddrs");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop through all interfaces
|
||||||
|
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
||||||
|
if (ifa->ifa_addr == NULL)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (strlen(name) == 0 || strcmp(name, ifa->ifa_name) == 0) {
|
||||||
|
if (ifa->ifa_addr->sa_family == AF_INET) {
|
||||||
|
*((struct sockaddr_in*)pSockaddr_in) = *(struct sockaddr_in*)ifa->ifa_addr;
|
||||||
|
found = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Free the memory allocated by getifaddrs
|
||||||
|
freeifaddrs(ifaddr);
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
int OS_InitSockets(void) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int OS_GetLastSocketError(void) {
|
||||||
|
return errno;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OS_CleanupSockets(void) {
|
||||||
|
// no-op
|
||||||
|
}
|
||||||
|
|
||||||
|
int OS_SetSocketNonBlocking(int socket) {
|
||||||
|
int flags = fcntl(socket, F_GETFL);
|
||||||
|
flags |= O_NONBLOCK;
|
||||||
|
return fcntl(socket, F_SETFL, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
int OS_CloseSocket(int socket) {
|
||||||
|
return close(socket);
|
||||||
|
}
|
||||||
|
|
|
@ -2,24 +2,32 @@
|
||||||
|
|
||||||
#include "harness/config.h"
|
#include "harness/config.h"
|
||||||
#include "harness/os.h"
|
#include "harness/os.h"
|
||||||
|
#include <arpa/inet.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <err.h>
|
#include <err.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <execinfo.h>
|
#include <execinfo.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <ifaddrs.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <mach-o/dyld.h>
|
#include <mach-o/dyld.h>
|
||||||
|
#include <netdb.h> // for getaddrinfo() and freeaddrinfo()
|
||||||
|
#include <netinet/in.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <unistd.h> // for close()
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
|
#define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
|
||||||
|
@ -284,3 +292,54 @@ char* OS_GetWorkingDirectory(char* argv0) {
|
||||||
}
|
}
|
||||||
return OS_Dirname(argv0);
|
return OS_Dirname(argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int OS_GetAdapterAddress(char* name, void* pSockaddr_in) {
|
||||||
|
struct ifaddrs *ifaddr, *ifa;
|
||||||
|
int found = 0;
|
||||||
|
|
||||||
|
// Get the list of network interfaces
|
||||||
|
if (getifaddrs(&ifaddr) == -1) {
|
||||||
|
perror("getifaddrs");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Loop through all interfaces
|
||||||
|
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
||||||
|
if (ifa->ifa_addr == NULL)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (strlen(name) == 0 || strcmp(name, ifa->ifa_name) == 0) {
|
||||||
|
if (ifa->ifa_addr->sa_family == AF_INET) {
|
||||||
|
*((struct sockaddr_in*)pSockaddr_in) = *(struct sockaddr_in*)ifa->ifa_addr;
|
||||||
|
found = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Free the memory allocated by getifaddrs
|
||||||
|
freeifaddrs(ifaddr);
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
int OS_InitSockets(void) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int OS_GetLastSocketError(void) {
|
||||||
|
return errno;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OS_CleanupSockets(void) {
|
||||||
|
// no-op
|
||||||
|
}
|
||||||
|
|
||||||
|
int OS_SetSocketNonBlocking(int socket) {
|
||||||
|
int flags = fcntl(socket, F_GETFL);
|
||||||
|
flags |= O_NONBLOCK;
|
||||||
|
return fcntl(socket, F_SETFL, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
int OS_CloseSocket(int socket) {
|
||||||
|
return close(socket);
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,17 @@
|
||||||
// Based on https://gist.github.com/jvranish/4441299
|
// Based on https://gist.github.com/jvranish/4441299
|
||||||
|
|
||||||
// this has to be first
|
#define _WIN32_WINNT 0x0600 // or higher (e.g., 0x0A00 for Windows 10)
|
||||||
#include <windows.h>
|
|
||||||
|
#include <winsock2.h>
|
||||||
|
|
||||||
|
#include <ws2tcpip.h> // for getaddrinfo, inet_pton, etc.
|
||||||
|
|
||||||
|
#include <iphlpapi.h> // for GetAdaptersAddresses
|
||||||
|
|
||||||
|
#include <ipifcons.h>
|
||||||
|
|
||||||
//
|
//
|
||||||
|
#include <windows.h> // only after winsock2.h
|
||||||
|
|
||||||
#include <dbghelp.h>
|
#include <dbghelp.h>
|
||||||
|
|
||||||
|
@ -12,10 +21,15 @@
|
||||||
|
|
||||||
#include <errno.h> /* errno, strerror */
|
#include <errno.h> /* errno, strerror */
|
||||||
#include <io.h> /* _access_s, F_OK */
|
#include <io.h> /* _access_s, F_OK */
|
||||||
|
#include <locale.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h> /* errno_t, FILE, fgets, fopen_s, fprintf*/
|
#include <stdio.h> /* errno_t, FILE, fgets, fopen_s, fprintf*/
|
||||||
#include <stdlib.h> /* _splitpath */
|
#include <stdlib.h> /* _splitpath */
|
||||||
#include <string.h> /* strcpy, strerror, strlen, strrchr */
|
#include <string.h> /* strcpy, strerror, strlen, strrchr */
|
||||||
|
#include <wchar.h>
|
||||||
|
|
||||||
|
#pragma comment(lib, "iphlpapi.lib")
|
||||||
|
#pragma comment(lib, "ws2_32.lib")
|
||||||
|
|
||||||
#ifndef F_OK
|
#ifndef F_OK
|
||||||
#define F_OK 0
|
#define F_OK 0
|
||||||
|
@ -391,3 +405,75 @@ char* OS_Basename(const char* path) {
|
||||||
char* OS_GetWorkingDirectory(char* argv0) {
|
char* OS_GetWorkingDirectory(char* argv0) {
|
||||||
return OS_Dirname(argv0);
|
return OS_Dirname(argv0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int OS_GetAdapterAddress(char* name, void* pSockaddr_in) {
|
||||||
|
DWORD ret, bufLen = 15000;
|
||||||
|
IP_ADAPTER_ADDRESSES* adapter_addrs = (IP_ADAPTER_ADDRESSES*)malloc(bufLen);
|
||||||
|
int found = 0;
|
||||||
|
|
||||||
|
if (!adapter_addrs) {
|
||||||
|
fprintf(stderr, "Memory allocation failed.\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert input name to wide char
|
||||||
|
wchar_t wideName[256] = { 0 };
|
||||||
|
if (name && strlen(name) > 0) {
|
||||||
|
mbstowcs(wideName, name, sizeof(wideName) / sizeof(wideName[0]) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, NULL, adapter_addrs, &bufLen);
|
||||||
|
if (ret == ERROR_BUFFER_OVERFLOW) {
|
||||||
|
free(adapter_addrs);
|
||||||
|
adapter_addrs = (IP_ADAPTER_ADDRESSES*)malloc(bufLen);
|
||||||
|
if (!adapter_addrs)
|
||||||
|
return 0;
|
||||||
|
ret = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, NULL, adapter_addrs, &bufLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret != NO_ERROR) {
|
||||||
|
free(adapter_addrs);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (IP_ADAPTER_ADDRESSES* aa = adapter_addrs; aa != NULL; aa = aa->Next) {
|
||||||
|
LOG_DEBUG("name: %s", aa->FriendlyName); // Skip if name is provided and doesn't match FriendlyName
|
||||||
|
if (wcslen(wideName) > 0 && wcscmp(aa->FriendlyName, wideName) != 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
for (IP_ADAPTER_UNICAST_ADDRESS* ua = aa->FirstUnicastAddress; ua != NULL; ua = ua->Next) {
|
||||||
|
if (ua->Address.lpSockaddr->sa_family == AF_INET) {
|
||||||
|
struct sockaddr_in* sa_in = (struct sockaddr_in*)ua->Address.lpSockaddr;
|
||||||
|
*((struct sockaddr_in*)pSockaddr_in) = *sa_in;
|
||||||
|
found = 1;
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
free(adapter_addrs);
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
|
int OS_InitSockets(void) {
|
||||||
|
WSADATA wsadata;
|
||||||
|
return WSAStartup(MAKEWORD(2, 2), &wsadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
int OS_GetLastSocketError(void) {
|
||||||
|
return WSAGetLastError();
|
||||||
|
}
|
||||||
|
|
||||||
|
void OS_CleanupSockets(void) {
|
||||||
|
WSACleanup();
|
||||||
|
}
|
||||||
|
|
||||||
|
int OS_SetSocketNonBlocking(int socket) {
|
||||||
|
unsigned long nobio = 1;
|
||||||
|
return ioctlsocket(socket, FIONBIO, &nobio);
|
||||||
|
}
|
||||||
|
|
||||||
|
int OS_CloseSocket(int socket) {
|
||||||
|
return closesocket(socket);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue