Advertisement
Guest User

Untitled

a guest
May 26th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. bool verifyAccount(struct account *acc) {
  2.     SOCKET ListenSocket = INVALID_SOCKET, ClientSocket = INVALID_SOCKET;
  3.     SOCKET someSocket = INVALID_SOCKET;
  4.     struct dataSet dS;
  5.     dS.s = INVALID_SOCKET;
  6.     struct sockaddr_in ip;
  7.     struct sockaddr_in addr;
  8.     char servIp[255];
  9.     struct addrinfo *ptr = NULL, hints;
  10.     WSADATA wsaData;
  11.     int iResult;
  12.     struct addrinfo *result = NULL;
  13.  
  14.     ZeroMemory( &hints, sizeof(hints) );
  15.     hints.ai_family = AF_UNSPEC;
  16.     hints.ai_socktype = SOCK_STREAM;
  17.     hints.ai_protocol = IPPROTO_TCP;
  18.  
  19.     // Resolve the server address and port
  20.     iResult = getaddrinfo(getINIstring("Client.ini", "SERVER", "SERVIP", servIp), "54231", &hints, &result);
  21.     if ( iResult != 0 ) {
  22.         printf("getaddrinfo failed: %d\n", iResult);
  23.         WSACleanup();
  24.         return 1;
  25.     }
  26.  
  27.      for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
  28.  
  29.         // Create a SOCKET for connecting to server
  30.         dS.s = socket(ptr->ai_family, ptr->ai_socktype,
  31.             ptr->ai_protocol);
  32.         if (dS.s == INVALID_SOCKET) {
  33.             printf("Error at socket(): %ld\n", WSAGetLastError());
  34.             freeaddrinfo(result);
  35.             WSACleanup();
  36.             return 0;
  37.         }
  38.  
  39.  
  40.         // Connect to server.
  41.         iResult = connect( dS.s, ptr->ai_addr, (int)ptr->ai_addrlen);
  42.         if (iResult == SOCKET_ERROR) {
  43.             CConsole::outErr("Couldn't find server! %s", servIp);
  44.             closesocket(dS.s);
  45.             dS.s = INVALID_SOCKET;
  46.             return 0;
  47.         }
  48.         break;
  49.     }
  50.  
  51.     char hostname[255];
  52.     char *szIPAddress;
  53.     WSADATA wData;
  54.     PHOSTENT hostinfo;
  55.    
  56.     // Find the clients ip
  57.     if( gethostname ( hostname, sizeof(hostname)) == 0) {
  58.         if((hostinfo = gethostbyname(hostname)) != NULL) {
  59.             szIPAddress = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
  60.         }
  61.     }
  62.  
  63.        
  64.     CConsole::outTime("Connected to server.");
  65.    
  66.     dS.ip = inet_addr(szIPAddress);
  67.     dS.servip = inet_addr(servIp);
  68.     struct sockaddr_in client;
  69.     unsigned int socksize;
  70.     socksize = sizeof(client);
  71.     char sendbuf[64];
  72.     char recvbuf[64];
  73.     char temppass[16];
  74.     char iniuser[20];
  75.     char inipass[20];
  76.     int autoLogin = getINIint("Client.ini", "SERVER", "AUTOLOGIN");
  77.     memset(iniuser,0,20);
  78.     memset(inipass,0,20);
  79.  
  80.     getINIstring("Client.ini", "SERVER", "USERNAME", acc->userName);
  81.     getINIstring("Client.ini", "SERVER", "PASSWORD", acc->password);
  82.  
  83.     if((autoLogin == 1) && firstStart){
  84.  
  85.         CConsole::outTime("Autologin activated!");
  86.    
  87.     } else {
  88.         autoLogin = 0;
  89.     }
  90.  
  91.     int i = 0;
  92.     char ch;
  93.  
  94.     char choise;
  95.     CConsole::outHr();
  96.  
  97.     if(!autoLogin) {
  98.    
  99.         menu:
  100.             CConsole::outTime("What do you want to do?");
  101.             CConsole::outTime("1) Log in using an existing account");
  102.             CConsole::outTime("2) Create a new account");
  103.             CConsole::outHr();
  104.             printf("->");
  105.         login:
  106.             scanf("%s",&choise);
  107.             CConsole::outHr();
  108.             switch(choise) {
  109.                 case '1':
  110.                     {
  111.                     printf("Enter your username and password to log in.\n");
  112.                     printf("Username: ");
  113.                     scanf("%s", acc->userName);
  114.                     printf("Password: ");
  115.                     char c;
  116.                     int i = 0;
  117.                     int test = 0;
  118.                     while (1) {
  119.                          c = getch();
  120.                          test = c;
  121.                          if(test == 8) {
  122.                             acc->password[i] = 0;
  123.                             i --;
  124.                             continue;
  125.                          }
  126.                          acc->password[i] = c;
  127.                          printf("*",acc->password[i]);
  128.                          if(test == 13) {
  129.                              acc->password[i] = 0;
  130.                              break;
  131.                          }
  132.                          i++;
  133.                     }
  134.                     printf("\n");
  135.                     sendbuf[32] = 0x10;
  136.                     break;
  137.                     }
  138.                 case '2':
  139.         newuser:
  140.                     CConsole::outTime("Enter your desired username and password.\n");
  141.                     printf("Username: ");
  142.                     scanf("%s", acc->userName);
  143.                     printf("Password: ");
  144.                     scanf("%s", acc->password);
  145.                     printf("Repeat password: ");
  146.                     scanf("%s", temppass);
  147.                     if(stricmp(acc->password,temppass)) {
  148.                         CConsole::outErr("Passwords do not match, please try again!\n");
  149.                         goto newuser;
  150.                     }
  151.                     sendbuf[32] = 0x20;
  152.                     break;
  153.                 default:
  154.                     CConsole::outErr("Invalid choise, try again!\n->");
  155.                     goto login;
  156.                     break;
  157.             }
  158.     } else {
  159.  
  160.         sendbuf[32] = 0x10;
  161.         autoLogin = 0;
  162.         firstStart = false;
  163.  
  164.     }
  165.  
  166.     //CConsole::outHr();
  167.     memcpy(sendbuf   , acc->userName,16);
  168.     memcpy(sendbuf+16, acc->password,16);
  169.     iResult = send(dS.s, sendbuf, 33, 0);
  170.     Sleep(100);
  171.     recv(dS.s, recvbuf, 16, 0);
  172.     switch(recvbuf[0]) {
  173.         case SUCCESS_LOGIN:
  174.             memcpy(&uAcc,recvbuf+1,4);
  175.             CConsole::outTime("Successfully logged in as %s!\n",acc->userName);
  176.             closesocket(dS.s);
  177.             return true;
  178.             break;
  179.         case ERROR_LOGIN:
  180.             CConsole::outErr("You could not be logged in!\nDid you enter the right password?\n");
  181.             closesocket(dS.s);
  182.             return false;
  183.             break;
  184.         case SUCCESS_CREATE_ACCOUNT:
  185.             CConsole::outTime("Account successfully created!\n");
  186.             closesocket(dS.s);
  187.             return false;
  188.             break;
  189.         case ERROR_CREATE_ACCOUNT:
  190.             CConsole::outErr("Account could not be created!\nThe username is already taken!\n");
  191.             closesocket(dS.s);
  192.             return false;
  193.             break;
  194.     }
  195.     closesocket(dS.s);
  196.     return false;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement