Valhalla Legends Forums Archive | C/C++ Programming | winsock data recieve, from within a dll

AuthorMessageTime
MysT_DooMI found this hilarious, last two kids were the best

YouTube Challenge - I Told My Kids I Ate All Their Halloween Candy
November 06, 2011, 07:19 PM
SixenI lol'd.November 07, 2011, 06:48 PM
l)ragonSomething I came across while searching my cd's from some time ago.

Code: [Select]
// BNLS.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "packetbuffer.h"

#define BNLS_NULL                       (0x00) //supported
#define BNLS_CDKEY                      (0x01) //supported
#define BNLS_LOGONCHALLENGE             (0x02) //supported
#define BNLS_LOGONPROOF                 (0x03) //supported
#define BNLS_CREATEACCOUNT              (0x04) //supported
#define BNLS_CHANGECHALLENGE            (0x05) //supported
#define BNLS_CHANGEPROOF                (0x06) //supported
#define BNLS_UPGRADECHALLENGE           (0x07) //supported
#define BNLS_UPGRADEPROOF               (0x08) //supported
#define BNLS_VERSIONCHECK               (0x09)
#define BNLS_CONFIRMLOGON               (0x0a) //supported
#define BNLS_HASHDATA                   (0x0b) //supported
#define BNLS_CDKEY_EX                   (0x0c) //supported, not fully
#define BNLS_CHOOSENLSREVISION          (0x0d) //supported
#define BNLS_AUTHORIZE                  (0x0e) //supported
#define BNLS_AUTHORIZEPROOF             (0x0f) //supported
#define BNLS_REQUESTVERSIONBYTE         (0x10) //supported
#define BNLS_VERIFYSERVER               (0x11) //supported
#define BNLS_RESERVESERVERSLOTS         (0x12)
#define BNLS_SERVERLOGONCHALLENGE       (0x13)
#define BNLS_SERVERLOGONPROOF           (0x14)
#define BNLS_RESERVED0                  (0x15)
#define BNLS_RESERVED1                  (0x16)
#define BNLS_RESERVED2                  (0x17)
#define BNLS_VERSIONCHECKEX             (0x18)
#define BNLS_RESERVED3                  (0x19)
#define BNLS_VERSIONCHECKEX2            (0x1a) //supported

#define PRODUCT_STARCRAFT             (0x01)
#define PRODUCT_BROODWAR              (0x02)
#define PRODUCT_WAR2BNE               (0x03)
#define PRODUCT_DIABLO2               (0x04)
#define PRODUCT_LORDOFDESTRUCTION     (0x05)
#define PRODUCT_JAPANSTARCRAFT        (0x06)
#define PRODUCT_WARCRAFT3             (0x07)
#define PRODUCT_THEFROZENTHRONE       (0x08)

const char *STARCRAFT =("RATS");
const char *BROODWAR =("PXES");
const char *WAR2BNE =("NB2W");
const char *DIABLO2 =("VD2D");
const char *LORDOFDESTRUCTION =("PX2D");
const char *JAPANSTARCRAFT =("RTSJ");
const char *WARCRAFT3 =("3RAW");
const char *THEFROZENTHRONE =("PX3W");

#define HASHDATA_FLAG_UNUSED          (0x01)
#define HASHDATA_FLAG_DOUBLEHASH      (0x02)
#define HASHDATA_FLAG_COOKIE          (0x04)

#define STATUS_AUTHORIZED               (0x00)
#define STATUS_UNAUTHORIZED             (0x01)
#define STATUS_UNKNOWEN_NAME            (-2)
#define STATUS_UNKNOWEN_PASS            (-3)
#define STATUS_UNKNOWEN_AUTHFAIL        (-4)
#define STATUS_UNSUPPORTED_GAME         (-5)
#define STATUS_CDKEY_FAILURE (-6)
#define FUNCTION_FAIL             (-1)

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
)
{
    return TRUE;
}

SOCKET __stdcall InitBNLS(SOCKET sckBNLS, char *ServerIP, u_short sPort)
{

if(sckBNLS == INVALID_SOCKET) {
WSADATA wsa;
WSAStartup(MAKEWORD(2,0),&wsa);
SOCKET s = socket(AF_INET, SOCK_STREAM, 0);
sckBNLS = s;
}
else {
closesocket(sckBNLS);
sckBNLS = socket(AF_INET, SOCK_STREAM, 0);
}

SOCKADDR_IN rsin;
if(sckBNLS == INVALID_SOCKET) {
return FUNCTION_FAIL;
}

hostent *hs = gethostbyname(ServerIP);
char *ip = inet_ntoa( *(LPIN_ADDR)*hs->h_addr_list );

rsin.sin_family = AF_INET;
rsin.sin_addr.s_addr = inet_addr(ip);
rsin.sin_port = htons(sPort);

//if(bind(sckBNLS, (PSOCKADDR)&rsin, sizeof(rsin)) == SOCKET_ERROR) {
// return false;
//}

connect(sckBNLS, (PSOCKADDR)&rsin, sizeof(rsin));

switch(int err = GetLastError()) {
//case WSAECONNREFUSED:
//case WSAENETUNREACH:
//case WSAETIMEDOUT:
// return;
default:
//plog("Other connect error %d\n", err);
//sprintf(thismsg, "Other connect error %d\n", err);
//closesocket(sckBNLS);
return FUNCTION_FAIL;
case 0:
//GOOD
return sckBNLS;
}
return true;
}

void DoEvents()
{
MSG Msg;
while(PeekMessage(&Msg,NULL,0,0,PM_REMOVE))
{
if(Msg.message == WM_QUIT) break;
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}

int __stdcall CloseBNLS(SOCKET sckBNLS) {
if(sckBNLS == INVALID_SOCKET) {
//bad bad
return 0;
} else {
closesocket(sckBNLS);
return FUNCTION_FAIL;
}
}

int __stdcall SendBNLSAuth(SOCKET sckBNLS, char *Name, char *Pass)
{
PacketBuffer pb;

pb.insert(Name);
pb.SendBNLSPacket(sckBNLS, BNLS_AUTHORIZE);

char *buf = (char*)malloc(7);
int len = 7;

int rlen = recv(sckBNLS, buf, len, 0);
if(rlen == len) {
u_short l2 = *(u_short *)(buf);
u_char b1 = buf[2];
DWORD sKey = *(unsigned long *)(buf + 3);
DWORD cPass = BNLSChecksum(Pass, sKey);
pb.insert(cPass);
pb.SendBNLSPacket(sckBNLS, BNLS_AUTHORIZEPROOF);

buf = (char*)malloc(len);
int rlen = recv(sckBNLS, buf, len, 0);
if(rlen == len) {
//Are we authorized
u_short l3 = *(u_short *)(buf);
u_char b2 = buf[2];
DWORD dwResult = *(unsigned long *)(buf + 3);
switch(dwResult) {
case STATUS_AUTHORIZED:
return STATUS_AUTHORIZED;
case STATUS_UNAUTHORIZED:
return STATUS_UNAUTHORIZED;
default:
return STATUS_UNKNOWEN_AUTHFAIL;
}
}
return STATUS_UNKNOWEN_PASS;
}
return STATUS_UNKNOWEN_NAME;
}
//(DWORD) Product ID.*
//(DWORD) Flags.**
//(DWORD) Cookie.
//(ULONGLONG) Timestamp for version check archive.
//(String) Version check archive filename.
//(String) Checksum formula.
int __stdcall sendBNLS_VERSIONCHECKEX2(SOCKET sckBNLS,
   char *Product,
   DWORD Flags,
   DWORD Cookie,
   u_char *TS_FN_CF,
   int dLen,
   char *outBuf)
{
PacketBuffer pb;
DWORD ID = PID(Product);
if(ID == -1) { return STATUS_UNSUPPORTED_GAME; }
u_char sl = 0;

pb.insert(ID);
pb.insert(Flags);
pb.insert(Cookie);
for(int i = 0; i<dLen; i++)
{
sl = (u_char)(TS_FN_CF[i]);
pb.insertbyte(sl);
}
pb.SendBNLSPacket(sckBNLS, BNLS_VERSIONCHECKEX2);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
DWORD dwResult = *(unsigned long *)(buf + 3);

if(b2 == BNLS_VERSIONCHECKEX2) {
//Continue on
} else { return FUNCTION_FAIL; }
if(dwResult == 1) {
//Continue on
} else { return FUNCTION_FAIL; }

memcpy(outBuf+2, buf+7, pLength-7);
unsigned short lVal = pLength - 3;
memcpy(outBuf, &lVal, sizeof(lVal));
return 1;
}

void __stdcall sendBNLS_NULL(SOCKET sckBNLS)
{
PacketBuffer pb;
pb.SendBNLSPacket(sckBNLS, BNLS_NULL);
}

int __stdcall sendBNLS_REQUESTVERSIONBYTE(SOCKET sckBNLS, char *Product)
{
PacketBuffer pb;
DWORD ID = PID(Product);
if(ID == -1) { return STATUS_UNSUPPORTED_GAME; }
pb.insert(ID);
pb.SendBNLSPacket(sckBNLS, BNLS_REQUESTVERSIONBYTE);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_REQUESTVERSIONBYTE) {
//Continue on
} else { return FUNCTION_FAIL; }

DWORD dwResult = *(unsigned long *)(buf + 3);
DWORD dwReturn = *(unsigned long *)(buf + 7);

return dwReturn;
}
//BNLS_CDKEY
int __stdcall sendBNLS_CDKEY(SOCKET sckBNLS, DWORD ServerKey, char *CDKey, char *outBuf)
{
PacketBuffer pb;
pb.insert(ServerKey);
pb.insert(CDKey);
pb.SendBNLSPacket(sckBNLS, BNLS_CDKEY);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_CDKEY) {
//Continue on
} else { return FUNCTION_FAIL; }

bool dwResult = *(bool *)(buf + 3);
if(dwResult) {
DWORD dwReturn = *(unsigned long *)(buf + 7);
memcpy(outBuf, buf+11, 9*4);
return dwReturn;
} else { return FUNCTION_FAIL; }

return dwResult;
}

int __stdcall sendBNLS_LOGONCHALLENGE(SOCKET sckBNLS, char *Name, char *Pass, char *outBuf)
{
PacketBuffer pb;
pb.insert(Name);
pb.insert(Pass);
pb.SendBNLSPacket(sckBNLS, BNLS_LOGONCHALLENGE);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_LOGONCHALLENGE) {
//Continue on
} else { return FUNCTION_FAIL; }

memcpy(outBuf, buf+3, 8*4);
return 1;
}

int __stdcall sendBNLS_LOGONPROOF(SOCKET sckBNLS, u_char *dataBuf, char *outBuf)
{
PacketBuffer pb;
u_char sl = 0;
for(int i = 0; i<(16*4); i++)
{
sl = (u_char)(dataBuf[i]);
pb.insertbyte(sl);
}
pb.SendBNLSPacket(sckBNLS, BNLS_LOGONPROOF);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_LOGONPROOF) {
//Continue on
} else { return FUNCTION_FAIL; }

memcpy(outBuf, buf+3, 5*4);
return 1;
}

int __stdcall sendBNLS_CREATEACCOUNT(SOCKET sckBNLS, char *Name, char *Pass, char *outBuf)
{
PacketBuffer pb;
pb.insert(Name);
pb.insert(Pass);
pb.SendBNLSPacket(sckBNLS, BNLS_CREATEACCOUNT);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_CREATEACCOUNT) {
//Continue on
} else { return FUNCTION_FAIL; }

memcpy(outBuf, buf+3, 16*4);
return 1;
}

int __stdcall sendBNLS_CHANGECHALLENGE(SOCKET sckBNLS, char *Name, char *OldPass, char *NewPass, char *outBuf)
{
PacketBuffer pb;
pb.insert(Name);
pb.insert(OldPass);
pb.insert(NewPass);
pb.SendBNLSPacket(sckBNLS, BNLS_CHANGECHALLENGE);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_CHANGECHALLENGE) {
//Continue on
} else { return FUNCTION_FAIL; }

memcpy(outBuf, buf+3, 8*4);
return 1;
}

int __stdcall sendBNLS_CHANGEPROOF(SOCKET sckBNLS, u_char *dataBuf, char *outBuf)
{
PacketBuffer pb;
u_char sl = 0;
for(int i = 0; i<(16*4); i++)
{
sl = (u_char)(dataBuf[i]);
pb.insertbyte(sl);
}
pb.SendBNLSPacket(sckBNLS, BNLS_CHANGEPROOF);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_CHANGEPROOF) {
//Continue on
} else { return FUNCTION_FAIL; }

memcpy(outBuf, buf+3, 21*4);
return 1;
}

int __stdcall sendBNLS_UPGRADECHALLENGE(SOCKET sckBNLS, char *Name, char *OldPass, char *NewPass)
{
PacketBuffer pb;
pb.insert(Name);
pb.insert(OldPass);
pb.insert(NewPass);
pb.SendBNLSPacket(sckBNLS, BNLS_UPGRADECHALLENGE);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_UPGRADECHALLENGE) {
//Continue on
} else { return FUNCTION_FAIL; }

DWORD dwReturn = *(unsigned long *)(buf + 3);
return dwReturn;
}

int __stdcall sendBNLS_UPGRADEPROOF(SOCKET sckBNLS, DWORD ServerKey57, char *outBuf)
{
PacketBuffer pb;
pb.insert(ServerKey57);
pb.SendBNLSPacket(sckBNLS, BNLS_UPGRADEPROOF);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_UPGRADEPROOF) {
//Continue on
} else { return FUNCTION_FAIL; }

memcpy(outBuf, buf+3, 22*4);
return 1;
}

int __stdcall sendBNLS_CONFIRMLOGON(SOCKET sckBNLS, u_char *dataBuf)
{
PacketBuffer pb;
u_char sl = 0;
for(int i = 0; i<(16*4); i++)
{
sl = (u_char)(dataBuf[i]);
pb.insertbyte(sl);
}
pb.SendBNLSPacket(sckBNLS, BNLS_CONFIRMLOGON);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_CONFIRMLOGON) {
//Continue on
} else { return FUNCTION_FAIL; }

DWORD dwReturn = *(unsigned long *)(buf + 3);
return dwReturn;
}

int __stdcall sendBNLS_HASHDATA(SOCKET sckBNLS,
char *outBuf,
DWORD Flags,
u_char *dataBuf,
int bufLength,
DWORD ClientKey,
DWORD ServerKey,
DWORD Cookie)
{
PacketBuffer pb;
pb.insert(bufLength);
pb.insert(Flags);
u_char sl = 0;
for(int i = 0; i<(bufLength); i++)
{
sl = (u_char)(dataBuf[i]);
pb.insertbyte(sl);
}
if(Flags & HASHDATA_FLAG_DOUBLEHASH) {
pb.insert(ClientKey);
pb.insert(ServerKey);
}
if(Flags & HASHDATA_FLAG_COOKIE) {
pb.insert(Cookie);
}
pb.SendBNLSPacket(sckBNLS, BNLS_HASHDATA);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_HASHDATA) {
//Continue on
} else { return FUNCTION_FAIL; }

if(Flags & HASHDATA_FLAG_COOKIE) {
memcpy(outBuf, buf+3, 6*4);
} else {
memcpy(outBuf, buf+3, 5*4);
}
return 1;
}

int __stdcall sendBNLS_CDKEY_EX(SOCKET sckBNLS, DWORD Cookie, u_char KeyCount, DWORD Flags, u_char *dataBuf, int dbLength, char *outBuf)
{
PacketBuffer pb;
u_char sl = 0;

pb.insert(Cookie);
pb.insertbyte(KeyCount);
pb.insert(Flags);
for(int i = 0; i<(dbLength); i++)
{
sl = (u_char)(dataBuf[i]);
pb.insertbyte(sl);
}
pb.SendBNLSPacket(sckBNLS, BNLS_CDKEY_EX);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_CDKEY_EX) {
//Continue on
} else { return FUNCTION_FAIL; }

b2 = buf[8];
if(b2 == KeyCount) {
//Continue on
} else { return (*(unsigned int *)(buf + 9)); }

memcpy(outBuf, buf+13, (int)((10*4)*KeyCount));
return 1;
}

int __stdcall sendBNLS_CHOOSENLSREVISION(SOCKET sckBNLS, DWORD NLSVersion)
{
if(NLSVersion == 1) { return 1; } //No need to send bnls defaults to version 1
PacketBuffer pb;

pb.insert(NLSVersion);
pb.SendBNLSPacket(sckBNLS, BNLS_CHOOSENLSREVISION);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_CHOOSENLSREVISION) {
//Continue on
} else { return FUNCTION_FAIL; }

DWORD dwReturn = *(unsigned long *)(buf + 3);
return dwReturn;
}

int __stdcall sendBNLS_VERIFYSERVER(SOCKET sckBNLS, DWORD ServerIP, u_char *dataBuf, int dbLength)
{
PacketBuffer pb;
u_char sl = 0;

pb.insert(ServerIP);
for(int i = 0; i<(dbLength); i++)
{
sl = (u_char)(dataBuf[i]);
pb.insertbyte(sl);
}
pb.SendBNLSPacket(sckBNLS, BNLS_VERIFYSERVER);

char *buf = (char*)malloc(1024);
int rlen = recv(sckBNLS, buf, 1024, 0);
u_short pLength = *(u_short *)buf;
u_char b2 = buf[2];
if(b2 == BNLS_VERIFYSERVER) {
//Continue on
} else { return FUNCTION_FAIL; }

DWORD dwReturn = *(unsigned long *)(buf + 3);
return dwReturn;
}

DWORD PID(char *Product)
{
if(!stricmp(Product, STARCRAFT)){
return PRODUCT_STARCRAFT;
}
if(!stricmp(Product, BROODWAR)){
return PRODUCT_BROODWAR;
}
if(!stricmp(Product, WAR2BNE)){
return PRODUCT_WAR2BNE;
}
if(!stricmp(Product, DIABLO2)){
return PRODUCT_DIABLO2;
}
if(!stricmp(Product, LORDOFDESTRUCTION)){
return PRODUCT_LORDOFDESTRUCTION;
}
if(!stricmp(Product, JAPANSTARCRAFT)){
return PRODUCT_JAPANSTARCRAFT;
}
if(!stricmp(Product, WARCRAFT3)){
return PRODUCT_WARCRAFT3;
}
if(!stricmp(Product, THEFROZENTHRONE)){
return PRODUCT_THEFROZENTHRONE;
}
return FUNCTION_FAIL;
}
was doing this as an experiment before giving up on it back early 06 apparently.

Problem was the (vb6)app that was calling it would lock up and continue on like nothing happened, I figure it was due to waiting on the data to arrive to the functions them selves was what was causing this, thats where i gave up on it.
Now if i was to do something again similar to how im pulling in the remote data, how would I go about stoping these lock ups or atleast making them less noticable lol?
August 15, 2010, 04:26 pm