Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool verifyAccount(struct account *acc) {
- SOCKET ListenSocket = INVALID_SOCKET, ClientSocket = INVALID_SOCKET;
- SOCKET someSocket = INVALID_SOCKET;
- struct dataSet dS;
- dS.s = INVALID_SOCKET;
- struct sockaddr_in ip;
- struct sockaddr_in addr;
- char servIp[255];
- struct addrinfo *ptr = NULL, hints;
- WSADATA wsaData;
- int iResult;
- struct addrinfo *result = NULL;
- ZeroMemory( &hints, sizeof(hints) );
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
- // Resolve the server address and port
- iResult = getaddrinfo(getINIstring("Client.ini", "SERVER", "SERVIP", servIp), "54231", &hints, &result);
- if ( iResult != 0 ) {
- printf("getaddrinfo failed: %d\n", iResult);
- WSACleanup();
- return 1;
- }
- for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
- // Create a SOCKET for connecting to server
- dS.s = socket(ptr->ai_family, ptr->ai_socktype,
- ptr->ai_protocol);
- if (dS.s == INVALID_SOCKET) {
- printf("Error at socket(): %ld\n", WSAGetLastError());
- freeaddrinfo(result);
- WSACleanup();
- return 0;
- }
- // Connect to server.
- iResult = connect( dS.s, ptr->ai_addr, (int)ptr->ai_addrlen);
- if (iResult == SOCKET_ERROR) {
- CConsole::outErr("Couldn't find server! %s", servIp);
- closesocket(dS.s);
- dS.s = INVALID_SOCKET;
- return 0;
- }
- break;
- }
- char hostname[255];
- char *szIPAddress;
- WSADATA wData;
- PHOSTENT hostinfo;
- // Find the clients ip
- if( gethostname ( hostname, sizeof(hostname)) == 0) {
- if((hostinfo = gethostbyname(hostname)) != NULL) {
- szIPAddress = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
- }
- }
- CConsole::outTime("Connected to server.");
- dS.ip = inet_addr(szIPAddress);
- dS.servip = inet_addr(servIp);
- struct sockaddr_in client;
- unsigned int socksize;
- socksize = sizeof(client);
- char sendbuf[64];
- char recvbuf[64];
- char temppass[16];
- char iniuser[20];
- char inipass[20];
- int autoLogin = getINIint("Client.ini", "SERVER", "AUTOLOGIN");
- memset(iniuser,0,20);
- memset(inipass,0,20);
- getINIstring("Client.ini", "SERVER", "USERNAME", acc->userName);
- getINIstring("Client.ini", "SERVER", "PASSWORD", acc->password);
- if((autoLogin == 1) && firstStart){
- CConsole::outTime("Autologin activated!");
- } else {
- autoLogin = 0;
- }
- int i = 0;
- char ch;
- char choise;
- CConsole::outHr();
- if(!autoLogin) {
- menu:
- CConsole::outTime("What do you want to do?");
- CConsole::outTime("1) Log in using an existing account");
- CConsole::outTime("2) Create a new account");
- CConsole::outHr();
- printf("->");
- login:
- scanf("%s",&choise);
- CConsole::outHr();
- switch(choise) {
- case '1':
- {
- printf("Enter your username and password to log in.\n");
- printf("Username: ");
- scanf("%s", acc->userName);
- printf("Password: ");
- char c;
- int i = 0;
- int test = 0;
- while (1) {
- c = getch();
- test = c;
- if(test == 8) {
- acc->password[i] = 0;
- i --;
- continue;
- }
- acc->password[i] = c;
- printf("*",acc->password[i]);
- if(test == 13) {
- acc->password[i] = 0;
- break;
- }
- i++;
- }
- printf("\n");
- sendbuf[32] = 0x10;
- break;
- }
- case '2':
- newuser:
- CConsole::outTime("Enter your desired username and password.\n");
- printf("Username: ");
- scanf("%s", acc->userName);
- printf("Password: ");
- scanf("%s", acc->password);
- printf("Repeat password: ");
- scanf("%s", temppass);
- if(stricmp(acc->password,temppass)) {
- CConsole::outErr("Passwords do not match, please try again!\n");
- goto newuser;
- }
- sendbuf[32] = 0x20;
- break;
- default:
- CConsole::outErr("Invalid choise, try again!\n->");
- goto login;
- break;
- }
- } else {
- sendbuf[32] = 0x10;
- autoLogin = 0;
- firstStart = false;
- }
- //CConsole::outHr();
- memcpy(sendbuf , acc->userName,16);
- memcpy(sendbuf+16, acc->password,16);
- iResult = send(dS.s, sendbuf, 33, 0);
- Sleep(100);
- recv(dS.s, recvbuf, 16, 0);
- switch(recvbuf[0]) {
- case SUCCESS_LOGIN:
- memcpy(&uAcc,recvbuf+1,4);
- CConsole::outTime("Successfully logged in as %s!\n",acc->userName);
- closesocket(dS.s);
- return true;
- break;
- case ERROR_LOGIN:
- CConsole::outErr("You could not be logged in!\nDid you enter the right password?\n");
- closesocket(dS.s);
- return false;
- break;
- case SUCCESS_CREATE_ACCOUNT:
- CConsole::outTime("Account successfully created!\n");
- closesocket(dS.s);
- return false;
- break;
- case ERROR_CREATE_ACCOUNT:
- CConsole::outErr("Account could not be created!\nThe username is already taken!\n");
- closesocket(dS.s);
- return false;
- break;
- }
- closesocket(dS.s);
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement