I tried so...
GameServer Main.cpp
Code:
#include "headers.h"
#include "Header.h"
#include "xD.h"
#include "mysql.h"
// Winsock for our network communication
#include <winsock2.h>
#include <iostream>
using namespace std;
int StartServerListening(unsigned short port);
void EndServer(int socket);
HANDLE Hthread; // Used to handle our threads
HANDLE Hmutex; // A mutex is bascially a memory spot we can lock so nothing writes to it as we read form it.. That could cause issues
FD_SET masterSet; // This basically is our list of sockets that we will lock and unlock with out mutexHandle
// A FD_SET has a few data members:
// - fd_count - the number of file descriptors in the set
// - fd_array - the array of file descriptors
// We can use these little macros to help edit the FD_SET
// - FD_SET(a socket, FD_SET to manipulate) - add a scoket to the set
// - FD_ZERO(FD_SET to manipulate) - zero a set
// - FD_CLR(a socket, FD_SET to manipulate) - remove a socket from the set
// - FD_ISSET(a socket, FD_SET to manipulate) - checks if the socket is already in the set
bool gQuitFlag = false;// A flag ill use to terminate the program if it goes terribly wrong
// AcceptThread() - a This function will accept ALL incomming connections Then add to the FD_SET list
void AcceptThread(int* serverSocket)
{
int mySocket = *serverSocket; // Copy my socket over to a local variable
for (;;)// Run forever, why stop? O_o
{
unsigned int clientSocket = accept(mySocket, 0, 0); // Do a normal accept
if (clientSocket == SOCKET_ERROR)
{
cout << "Server: I shall not accept them!\n";
gQuitFlag = true; // Quit this thread
return;
}
else
{
WaitForSingleObject(Hmutex, INFINITE);// Lock the mutex and wait for it
FD_SET(clientSocket, &masterSet);// Add this socket to the master set using FD_SET()
ReleaseMutex(Hmutex);// Unlock the mutex
cout << "client on " << clientSocket <<" connected.\n" << endl;// Let everyone know someone just joined
}
}
}
int main()
{
std::cout << "##################NostaleExtremeX - Game Server######################" << std::endl;
std::cout << "# NostaleExtremeX version Beta #" << std::endl;
std::cout << "# IP: 127.0.0.1 #" << std::endl;
std::cout << "# Port: 1337 #" << std::endl;
std::cout << "# NEX - Private Server of Nostale #" << std::endl;
std::cout << "# NosPrivate #" << std::endl;
std::cout << "##################NostaleExtremeX - Game Server######################" << std::endl;
cout << "Server: Congratulations!\n";
cout << "Server: Server Online\n\n";
int serverSocket;
serverSocket = StartServerListening(1337);
if (serverSocket == -1)
{
cout << "Server: I failed you master! Im so ashamed!\n";
return 1;
}
Hmutex = CreateMutex(NULL, false, NULL);// Create the mutex
if (Hmutex == NULL)
{
cout << "Server: Sorry Sir! Could not make our Mutex! FORGIVE ME!\n";
EndServer(serverSocket);
return 1;
}
int thread;// Create the thread
Hthread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)AcceptThread, &serverSocket, 0, (LPDWORD)&thread);
if (Hthread == NULL)
{
cout << "Server: SORRY SIR! Could not beging Accepting!\n";
EndServer(serverSocket);
return 1;
}
Sleep(100);// Let the AcceptThread start up, cheap method, but oh well -.-;
FD_ZERO(&masterSet); // Always zero your sets before you use them, you know.. just incase...
for (;;) // Forever!!!!
{
if (gQuitFlag)
{
break;
}
WaitForSingleObject(Hmutex, INFINITE); // Lock the mutex
FD_SET TempSet = masterSet; // Make a temp location so we can unlock our mutex and let our accept thread run ASAP
ReleaseMutex(Hmutex); // Unlock the mutex
if (TempSet.fd_count == 0) // If a set is empty and we call form it, it returns a error, thats too much coding for me
{ // So we will simply check if there is anything in it, if so we will do something, else screw it -.-;
continue;
}
// select() has 5 member datas of use to us they are
// - the number of file descriptors
// - a FD_SET checks readability
// - a FD_SET checks for writeability
// - a FD_SET checks for errors
// - a wait time
timeval waitTime; // Set up our interval
waitTime.tv_sec = 0; // Set it to 0
waitTime.tv_usec = 0; // Set it to 0
int result = select(TempSet.fd_count, &TempSet, NULL, NULL, &waitTime); // Select a socket in out TempSet
if (result == 0) // Check for 0's agian
{
continue; // = Empty
}
if (result == SOCKET_ERROR)
{
cout << "Server: Sorry Sir! there was a error in the Select() macro!\n";
continue;
}
for (unsigned int i = 0; i < TempSet.fd_count; i++)
{
unsigned int clientSocket = TempSet.fd_array[i];
int nBytes;
#define MAX_MESSAGE_SIZE 4096
char buffer[MAX_MESSAGE_SIZE];
unsigned long messageSize;
nBytes = recv(clientSocket, (char*)&messageSize, sizeof(messageSize), 0);
if (nBytes == SOCKET_ERROR)
{
int error = WSAGetLastError();// Main error you will get here is if the other client suddenly disconnects or loses power or something similar
if (error == WSAECONNRESET)
{
WaitForSingleObject(Hmutex, INFINITE); // Lock our mutex
FD_CLR(clientSocket, &masterSet); // Remove the socket from our master set
ReleaseMutex(Hmutex); // Unlock our mutex
closesocket(clientSocket); // Close the socket on our side so our computer cleans up properly
cout << "Server: Client on "<< clientSocket << " has Disconnected." << endl; // Tell everyone the person left
continue;
}
else
{
cout << "Server: Sorry Sir... Its a fatel wound.. Oh god it hurts... *Breaths slowly* *Stops breathing*\n";// Error we wernt expecting, terminate the server in this case
gQuitFlag = true;
break;
}
}
if (nBytes == 0) // Called if the user exits and calls closesocket() on THEIR side. They stops ending data, thus 0 bytes, ets jsut remove them from the set
{
WaitForSingleObject(Hmutex, INFINITE); // Lock our mutex
FD_CLR(clientSocket, &masterSet); // Remove the socket from our master set
ReleaseMutex(Hmutex); // Unlock our mutex
closesocket(clientSocket); // Close the socket on our side, so our computer cleans up properly
cout << "Server: Client on " << clientSocket << " has disconnected" << "\n";// Quick Message to the rest in the room
continue;// On to the next one!
}
messageSize = ntohl(messageSize);
nBytes = recv(clientSocket, buffer, messageSize, 0);
if (nBytes == SOCKET_ERROR)
{
cout << "Server: I failed the message sir!\n";
gQuitFlag = true;
break;
}
buffer[messageSize] = 'xD';
cout << "Server: Client [" << clientSocket << "]: " << buffer << "\n";
}
}
EndServer(serverSocket);
cout << "Server: Press any key to shut me down....\n";
getchar();
return 0;
}
int StartServerListening(unsigned short port)
{
int error;
WSAData wsaData;
if ((error = WSAStartup(MAKEWORD(2, 2), &wsaData)) == SOCKET_ERROR)
{
cout << "Server: Winsock is being a bitch Sir!\n";
return -1;
}
int mySocket = socket(AF_INET, SOCK_STREAM, 0);
if (mySocket == SOCKET_ERROR)
{
cout << "Server: ****ing Socket is stuck closed!\n";
return -1;
}
struct sockaddr_in server;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = INADDR_ANY;
if (bind(mySocket, (sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
cout << "Server: Sorry Sir! My Bind Failed!\n";
closesocket(mySocket);
return -1;
}
if (listen(mySocket, 5) == SOCKET_ERROR)
{
cout << "Server: I failed to listen -.-;\n";
closesocket(mySocket);
return -1;
}
cout << "Server: Starting Up.. READY!\n";
return mySocket;
}
void EndServer(int socket)
{
WaitForSingleObject(Hthread, INFINITE);// Kill thread and handle
CloseHandle(Hthread);
CloseHandle(Hmutex);
closesocket(socket);
WSACleanup();
cout << "Server: Shuting down master.\n";
}
int _tmain(int argc, _TCHAR* argv[])
{
MYSQL *nosdb;
if ((nosdb = mysql_init (NULL)) == NULL) {
cout << " CRITICAL ERROR: Can't start mysql" << endl;
getchar();
exit(1);
}else{
if(!mysql_real_connect(nosdb, "127.0.0.1" ,"root", "1009" ,"nostalexD", 3306, 0,0))
{cout << " CRITICAL ERROR: Can't connect to DB" << endl;
getchar();
exit(1);}
else
{cout << "Service MySQL Started" << endl;}
}
MYSQL_ROW mrow;
MYSQL_RES *mres;
}
GameServer headers.h
Code:
#pragma comment(lib, "Ws2_32.lib")
// Define
#define MAX_SOCKET 10
#define RECV_BUFFER 512
// Includes
#include <iostream>
#include <WinSock2.h>
#include <Windows.h>
#include <vector>
#include <sstream>
using namespace std;
int WsaStart(void); // Simpel WSA-Start Function
std::string DecryptSessionPacket(std::string str); // Decrypt Session Function
std::vector<std::string> split( std::string str, char delimiter ); // Split Function
std::string DecryptGamePacket2(unsigned char str[]); // DecryptGame2 Function
std::string DecryptGamePacket(int session_id, unsigned char *str, int length); // DecryptGame Function
std::string DecryptSessionPacket(std::string str)
{
std::string encrypted_string;
for (int i = 1; i < str.length(); i++)
{
if (str[i] == 0xE) { return encrypted_string; }
unsigned char firstbyte = str[i] - 0xF;
unsigned char secondbyte = firstbyte;
secondbyte &= 0xF0;
firstbyte = firstbyte - secondbyte;
secondbyte >>= 0x4;
switch (secondbyte)
{
case 0:
encrypted_string += ' ';
break;
case 1:
encrypted_string += ' ';
break;
case 2:
encrypted_string += '-';
break;
case 3:
encrypted_string += '.';
break;
default:
secondbyte += 0x2C;
encrypted_string += secondbyte;
break;
}
switch (firstbyte)
{
case 0:
encrypted_string += '\0';
break;
case 1:
encrypted_string += ' ';
break;
case 2:
encrypted_string += '-';
break;
case 3:
encrypted_string += '.';
break;
default:
firstbyte += 0x2C;
encrypted_string += firstbyte;
break;
}
}
return encrypted_string;
}
std::vector<std::string> split( std::string str, char delimiter )
{
std::vector<std::string> ret;
size_t pos = str.find_first_of( delimiter );
while ( !str.empty() )
{
std::string cur = str.substr( 0, pos );
if ( !cur.empty() )
ret.push_back( cur );
if ( pos == std::string::npos )
break;
str = str.substr( pos + 1 );
pos = str.find_first_of( delimiter );
}
return ret;
}
std::string DecryptGamePacket2(unsigned char str[])
{
std::string decrypted_string;
char table[] = {' ','-','.','0','1','2','3','4','5','6','7','8','9','n'};
int count = 0;
for (count = 0; count < strlen((const char*)str); )
{
if (str[count] <= 0x7A)
{
unsigned char len = str[count];
for (int i = 0; i < (int)len; i++)
{
count++;
decrypted_string += str[count] ^ 0xFF;
}
count++;
} else
{
unsigned char len = str[count];
len &= 0x7F;
for (int i = 0; i < (int)len;)
{
count++;
unsigned char highbyte = str[count];
highbyte &= 0xF0;
highbyte >>= 0x4;
unsigned char lowbyte = str[count];
lowbyte &= 0x0F;
if (highbyte != 0x0 && highbyte != 0xF)
{
decrypted_string += table[highbyte-1];
i++;
}
if (lowbyte != 0x0 && lowbyte != 0xF)
{
decrypted_string += table[lowbyte-1];
i++;
}
}
count ++;
}
}
return decrypted_string;
}
std::string DecryptGamePacket(int session_id, unsigned char *str, int length)
{
std::string encrypted_string = "";
int session_key = session_id & 0xFF;
unsigned char session_number = session_id >> 6;
session_number &= 0xFF;
session_number &= 0x80000003;
switch (session_number)
{
case 0:
for (int i = 0; i < length; i++)
{
unsigned char firstbyte = session_key + 0x40;
unsigned char highbyte = str[i] - firstbyte;
encrypted_string += highbyte;
}
break;
case 1:
for (int i = 0; i < length; i++)
{
unsigned char firstbyte = session_key + 0x40;
unsigned char highbyte = str[i] + firstbyte;
encrypted_string += highbyte;
}
break;
case 2:
for (int i = 0; i < length; i++)
{
unsigned char firstbyte = session_key + 0x40;
unsigned char highbyte = str[i] - firstbyte ^ 0xC3;
encrypted_string += highbyte;
}
break;
case 3:
for (int i = 0; i < length; i++)
{
unsigned char firstbyte = session_key + 0x40;
unsigned char highbyte = str[i] + firstbyte ^ 0xC3;
encrypted_string += highbyte;
}
break;
default:
encrypted_string += 0xF;
break;
}
std::vector<std::string> temp = split(encrypted_string, 0xFF);
std::string save;
for (int i = 0; i < temp.size(); i++)
{
save += DecryptGamePacket2((unsigned char*) temp[i].c_str());
save += 0xFF;
}
return save;
}
Code:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#pragma comment(lib, "User32.lib")
void DisplayErrorBox(LPTSTR lpszFunction);
int _main(int argc, TCHAR *argv[])
{
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
TCHAR szDir[MAX_PATH];
size_t length_of_arg;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError=0;
// If the directory is not specified as a command-line argument,
// print usage.
if(argc != 2)
{
_tprintf(TEXT("\nUsage: %s <directory name>\n"), argv[0]);
return (-1);
}
// Check that the input path plus 3 is not longer than MAX_PATH.
// Three characters are for the "\*" plus NULL appended below.
StringCchLength(argv[1], MAX_PATH, &length_of_arg);
if (length_of_arg > (MAX_PATH - 3))
{
_tprintf(TEXT("\nDirectory path is too long.\n"));
return (-1);
}
_tprintf(TEXT("\nTarget directory is %s\n\n"), argv[1]);
// Prepare string for use with FindFile functions. First, copy the
// string to a buffer, then append '\*' to the directory name.
StringCchCopy(szDir, MAX_PATH, argv[1]);
StringCchCat(szDir, MAX_PATH, TEXT("\config\server.ini"));
StringCchCopy(szDir, MAX_PATH, argv[1]);
StringCchCat(szDir, MAX_PATH, TEXT("\data\*"));
StringCchCopy(szDir, MAX_PATH, argv[1]);
StringCchCat(szDir, MAX_PATH, TEXT("\maps\zones\*"));
// Find the first file in the directory.
hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
{
DisplayErrorBox(TEXT("FindFirstFile"));
return dwError;
}
// List all the files in the directory with some info about them.
do
{
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
_tprintf(TEXT(" %s <DIR>\n"), ffd.cFileName);
}
else
{
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
_tprintf(TEXT(" %s %ld bytes\n"), ffd.cFileName, filesize.QuadPart);
}
}
while (FindNextFile(hFind, &ffd) != 0);
dwError = GetLastError();
if (dwError != ERROR_NO_MORE_FILES)
{
DisplayErrorBox(TEXT("FindFirstFile"));
}
FindClose(hFind);
return dwError;
}
void DisplayErrorBox(LPTSTR lpszFunction)
{
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
// Display the error message and clean up
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
TEXT("%s failed with error %d: %s"),
lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
}
what changes in the login is that...
Code:
#pragma comment(lib, "Ws2_32.lib")
// Includes
#include <iostream>
#include <WinSock2.h>
#include <Windows.h>
#include <vector>
#include <sstream>
#include <tchar.h>
using namespace std;
std::string DecryptLoginPacket(std::string str, int plength)
{
std::string decrypted_string;
for (int i = 0; i < plength; i++) { decrypted_string += str[i] - 0xF ^ 0xC3; }
return decrypted_string;
}
std::string EncryptLoginPacket(std::string str)
{
std::string encrypted_string;
for (int i = 0; i < str.length(); i++) { encrypted_string += str[i] + 0xF; }
return encrypted_string += 0x19;
}
std::string GetPasswordString(std::string str)
{
std::string decrypted_string;
int count = 1;
int convert;
if (str.length() %2 == 0)
{
str.erase(0, 3);
} else
{
str.erase(0, 4);
}
for (int i = 0; i < str.length(); i+=2)
{
decrypted_string += str[i];
if (count %2 == 0)
{
decrypted_string += ' ';
}
count++;
}
std::stringstream ss(decrypted_string);
decrypted_string.clear();
while (ss >> std::hex >> convert)
{
decrypted_string.push_back(convert);
}
return decrypted_string;
}
std::string DecryptHashPassword(std::string str)
{
std::string decrypted_string;
int count = 1;
int convert;
if (str.length() %2 == 0)
{
str.erase(0, 3);
} else
{
str.erase(0, 4);
}
for (int i = 0; i < str.length(); i+=2)
{
decrypted_string += str[i];
if (count %2 == 0)
{
decrypted_string += ' ';
}
count++;
}
std::stringstream ss(decrypted_string);
decrypted_string.clear();
while (ss >> std::hex >> convert)
{
decrypted_string.push_back(convert);
}
return decrypted_string;
}
Login or Game... main.cpp...
Code:
#include "headers.h"
#include "mysql.h"
int _Wmain()
{
std::cout << "##################NostaleExtremeX - Login Server#####################" << std::endl;
std::cout << "# NostaleExtremeX version Beta #" << std::endl;
std::cout << "# IP: 25.81.108.154 #" << std::endl;
std::cout << "# Port: 4001 #" << std::endl;
std::cout << "# NEX - Private Server of Nostale #" << std::endl;
std::cout << "# NosPrivate #" << std::endl;
std::cout << "##################NostaleExtremeX - Login Server#####################" << std::endl;
long answer;
char recvbuf[255];
WSAData wsaData;
WORD DLLVERSION;
DLLVERSION = MAKEWORD(2,1);
answer = WSAStartup(DLLVERSION, &wsaData);
SOCKADDR_IN addr;
int addrlen = sizeof(addr);
SOCKET sListen;
SOCKET sConnect;
sConnect = socket(AF_INET,SOCK_STREAM,0);
addr.sin_addr.s_addr = inet_addr("25.81.108.154");
addr.sin_family = AF_INET;
addr.sin_port = htons(4001);
sListen = socket(AF_INET, SOCK_STREAM,0);
bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
listen(sListen, SOMAXCONN);
int b = 0;
for(;;){
if(sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen))
{
std::cout << "Connection accepted from: "<< std::endl;
answer = recv(sConnect, recvbuf,255,0);
std::cout << recvbuf << std::endl;
}
}
WSACleanup();
std::cin.get();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
MYSQL *nosdb;
if ((nosdb = mysql_init (NULL)) == NULL) {
cout << " CRITICAL ERROR: Can't start mysql" << endl;
getchar();
exit(1);
}else{
if(!mysql_real_connect(nosdb, "127.0.0.1" ,"root", "1009" ,"nostalexD", 3306, 0,0))
{cout << " CRITICAL ERROR: Can't connect to DB" << endl;
getchar();
exit(1);}
else
{cout << "Service MySQL Started" << endl;}
}
MYSQL_ROW mrow;
MYSQL_RES *mres;
}
isn't blocked ports and program, because my firewall is off
plz help






, 