Skip to content

Commit

Permalink
Few changes to make Generals/ZH happier.
Browse files Browse the repository at this point in the history
Adds a few function that were seemingly added to an internal copy of the library? Unclear if they were ever official parts of the interface.
  • Loading branch information
OmniBlade committed Mar 4, 2025
1 parent 1ad41e7 commit a218556
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/gamespy/chat/chat.h
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@ void chatGetBasicUserInfoA(CHAT chat,
CHATBool blocking);
*/

// Stuff that seems to be used by generals/zh
void chatSetLocalIP(unsigned long preferredIP);
unsigned long chatGetLocalIP(void);

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
#ifdef __cplusplus
Expand Down
29 changes: 29 additions & 0 deletions src/chat/chatsocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,24 @@ CHATBool ciSocketInit(ciSocket* sock, const char* nick)
return CHATTrue;
}

// Save off the user's preferred address to use with a future connect().
////////////////////////
static unsigned long bindAddress = INADDR_ANY;
void chatSetLocalIP(unsigned long preferredIP)
{
bindAddress = preferredIP;
}
unsigned long chatGetLocalIP(void)
{
return bindAddress;
}

CHATBool ciSocketConnect(ciSocket* sock, const char* serverAddress, int port)
{
unsigned int ip;
HOSTENT* host;
SOCKADDR_IN address;
SOCKADDR_IN localAddress;
int rcode;

#if !defined(INSOCK) && !defined(_NITRO) && !defined(_REVOLUTION)
Expand Down Expand Up @@ -248,6 +261,13 @@ CHATBool ciSocketConnect(ciSocket* sock, const char* serverAddress, int port)
address.sin_addr.s_addr = ip;
address.sin_port = htons((unsigned short)port);

// Setup the local address.
/////////////////////
memset(&localAddress, 0, sizeof(SOCKADDR_IN));
localAddress.sin_family = AF_INET;
localAddress.sin_addr.s_addr = htonl(bindAddress);
localAddress.sin_port = 0;

// Create the socket.
/////////////////////
sock->sock = socket(AF_INET, SOCK_STREAM, 0);
Expand All @@ -262,6 +282,15 @@ CHATBool ciSocketConnect(ciSocket* sock, const char* serverAddress, int port)
//assert(gsiSocketIsNotError(rcode));
#endif

// Try and bind.
///////////////////
rcode = bind(sock->sock, (SOCKADDR *)&localAddress, sizeof(SOCKADDR_IN));
if(rcode == SOCKET_ERROR)
{
closesocket(sock->sock);
return CHATFalse;
}

// Try and connect.
///////////////////
rcode = connect(sock->sock, (SOCKADDR*)&address, sizeof(SOCKADDR_IN));
Expand Down
13 changes: 13 additions & 0 deletions src/qr2/qr2.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ qr2_error_t qr2_create_socket(/*[out]*/ SOCKET* sock, const char* ip, /*[in/out]
/* PUBLIC FUNCTIONS */
/****************************************************************************/

// debugging
int isQR2Hosting = 0;
int getQR2HostingStatus(void)
{
return isQR2Hosting;
}

/* qr2_init: Initializes the sockets, etc. Returns an error value
if an error occured, or 0 otherwise */
qr2_error_t qr2_init_socketA(/*[out]*/ qr2_t* qrec,
Expand Down Expand Up @@ -252,6 +259,9 @@ qr2_error_t qr2_init_socketA(/*[out]*/ qr2_t* qrec,
}
} else //don't need to look up
ret = 1;

isQR2Hosting = (ret != 0); // debugging

if (!ret) {
gsDebugFormat(GSIDebugCat_QR2,
GSIDebugType_Misc,
Expand Down Expand Up @@ -641,6 +651,9 @@ void qr2_shutdown(qr2_t qrec)
qrec = current_rec;
if (qrec->ispublic)
send_heartbeat(qrec, 2);

isQR2Hosting = 0; // debugging

if (INVALID_SOCKET != qrec->hbsock && qrec->read_socket) //if we own the socket
{
closesocket(qrec->hbsock);
Expand Down

0 comments on commit a218556

Please sign in to comment.