Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LoginServer.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <stdio.h>
- #include <conio.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <string.h>
- #include <stdlib.h>
- #define _CRT_SECURE_NO_WARNINGS
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #define SERVER_PORT 7225
- #define SERVER_ADDR "127.0.0.1"
- #define BUFF_SIZE 2048
- #define MAX_UID_LENGTH 2048
- #define MAX_PWD_LENGHT 2048
- #undef UNICODE
- //Error code for USERID
- #define USER_OK "00"
- #define USER_VOID "01" // User doesnot exist
- #define USER_DISABLED "02" // User is locked
- #define USER_NULL "03" // User has not entered UID
- // Error code for PASSWORD
- #define PASS_OK "10"
- #define PASS_INCORRECT "11"
- #define PASS_NULL "12"
- // Error code for LOGOUT
- #define LOGOUT_SUCCESS "20"
- #define USER_NOT_AUTHENTICATED "21"
- // Error code for LOGIN
- #define LOGIN_SUCCESS "30"
- #define USER_AUTHENTICATED "31"
- #define USER_IDENTIFIED "32"
- #define USER_ONLINE "33"
- #define USER_OFFLINE "34"
- // Error code for EXIT
- #define EXIT_OK "40"
- #define EXIT_FAIL "41"
- // Other error code
- #define SYNTAX_ERROR "51"
- #define NO_OPTION "52"
- #define UNKNOW_ERROR "53"
- // Code for kind of message
- #define USER_PREFIX "USER"
- #define PASSWORD_PREFIX "PASS"
- #define LOGOUT_PREFIX "LOUT"
- #define LOGIN_PREFIX "LGIN"
- #define EXIT_PREFIX "EXIT"
- #define OTHER_OPTION "OTHR"
- // Status for connection
- #define AUTHENTICATED 0
- #define NOT_AUTHENTICATED 1
- #define NOT_IDENTIFIED 2
- #define NO_CONNECTION 3
- // link with Ws2_32.lib
- #pragma comment (lib, "Ws2_32.lib")
- #pragma warning(disable : 4996)
- // Declare struct for Account
- struct Account {
- char *userID;
- char *password;
- int status; //0: active, 1: disabled
- Account *next;
- };
- Account *head = NULL;
- Account *current = NULL;
- // All function needed to complete the program
- Account *getDataFromFile(FILE *file);
- void saveDataToFile(FILE *file, Account *aPtr);
- char *checkValidUser(Account *aPtr, char *userID);
- char *checkValidPassword(Account *aPtr, char *userID, char *password);
- void lockUser(FILE *file, char *userID, Account *aPtr, char *oldName);
- void printList(Account *aPtr);
- char *checkLogout();
- char *getPrefix(char *message);
- void chopNChars(char *str, int n);
- bool isUserID(char *message);
- bool isPassword(char *message);
- bool isLogOut(char *message);
- int main(int argc, char* argv[]) {
- // Open all necessary files
- char fileName[] = "account.txt";
- FILE *file = fopen(fileName, "r+");
- // Create a linked list with the data
- // of the original file
- current = getDataFromFile(file);
- // Initiate WinSock
- WSADATA wsaData;
- WORD wVersion = MAKEWORD(2, 2);
- if (WSAStartup(wVersion, &wsaData)) {
- printf("Version is not support!\n");
- }
- // Construct socket
- SOCKET listenSock;
- listenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- // Bind address to socket
- sockaddr_in serverAddr;
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons(SERVER_PORT);
- serverAddr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
- if (bind(listenSock, (sockaddr *)&serverAddr, sizeof(serverAddr))) {
- printf("Error! Cannot bind this address!\n");
- return 0;
- }
- // Listen request from client
- if (listen(listenSock, 10)) {
- printf("Error! Cannot listen!\n");
- return 0;
- }
- printf("Server started!\n");
- // Communicate with client
- sockaddr_in clientAddr;
- int ret, clientAddrLen = sizeof(clientAddr);
- int connStat = NO_CONNECTION;
- printList(current);
- while (1) {
- SOCKET connSock;
- // accept request
- connSock = accept(listenSock, (sockaddr *)&clientAddr, &clientAddrLen);
- printf("Client connected!\n");
- connStat = NOT_IDENTIFIED;
- char uid[BUFF_SIZE] = { '\0' };
- do {
- // Listen menu request from client
- char buff[BUFF_SIZE] = {'\0'};
- int countWrong;
- ret = recv(connSock, buff, BUFF_SIZE, 0);
- if (ret == SOCKET_ERROR)
- break;
- buff[ret] = 0;
- // Consider the state of the connection
- // User hasnot entered any information
- if (connStat == NOT_IDENTIFIED) {
- if (strcmp(getPrefix(buff), USER_PREFIX) == 0) {
- chopNChars(buff, 4);
- strcpy(uid, buff);
- printf("UID %s\n", uid);
- // Handle the message
- // Username is not null
- if (strlen(buff) > 0) {
- printf("Receive from client1 %s\n", buff);
- // Check if the UserID is valid
- if ((strcmp(checkValidUser(current, buff), USER_OK) == 0)) {
- printf("Valid UserID!\n");
- connStat = NOT_AUTHENTICATED;
- countWrong = 0;
- // Echo success code to client
- ret = send(connSock, USER_OK, strlen(USER_OK), 0);
- if (ret == SOCKET_ERROR)
- printf("Error 2 : %i\n", WSAGetLastError());
- }
- // UserID doesnot not exist
- else if (strcmp(checkValidUser(current, buff), USER_VOID) == 0) {
- printf("UserID does not exist!\n");
- // Echo error code to client
- ret = send(connSock, USER_VOID, strlen(USER_VOID), 0);
- if (ret == SOCKET_ERROR)
- printf("Error 3 : %i\n", WSAGetLastError());
- }
- // UserID is disabled
- else if (strcmp(checkValidUser(current, buff), USER_DISABLED) == 0) {
- printf("User is disabled!\n");
- // Echo error code to client
- ret = send(connSock, USER_DISABLED, strlen(USER_DISABLED), 0);
- if (ret == SOCKET_ERROR)
- printf("Error 3 : %i\n", WSAGetLastError());
- }
- }
- // Username is null
- else if (strlen(buff) <= 0) {
- printf("Receive from client %s\n", buff);
- printf("UserID is NULL!\n");
- ret = send(connSock, USER_NULL, strlen(USER_NULL), 0);
- // printf("8\n");
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- // printf("9\n");
- }
- }
- else if (strcmp(getPrefix(buff), PASSWORD_PREFIX) == 0) {
- ret = send(connSock, SYNTAX_ERROR, strlen(SYNTAX_ERROR), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- else if (strcmp(getPrefix(buff), LOGOUT_PREFIX) == 0) {
- ret = send(connSock, USER_NOT_AUTHENTICATED, strlen(USER_NOT_AUTHENTICATED), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- else if (strcmp(getPrefix(buff), EXIT_PREFIX) == 0) {
- ret = send(connSock, EXIT_OK, strlen(EXIT_OK), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- connStat = NO_CONNECTION;
- break;
- }
- else if (strcmp(getPrefix(buff), OTHER_OPTION) == 0) {
- ret = send(connSock, NO_OPTION, strlen(NO_OPTION), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- else {
- ret = send(connSock, UNKNOW_ERROR, strlen(UNKNOW_ERROR), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- }
- // User enter the userid and being identified
- else if (connStat == NOT_AUTHENTICATED) {
- if (strcmp(getPrefix(buff), PASSWORD_PREFIX) == 0) {
- printf("%s\n", buff);
- chopNChars(buff, 4);
- printf("%s\n", buff);
- printf("uid %s\n", uid);
- // Check if the received password is NULL
- if (strlen(buff) > 0) {
- printf("Receive from client2 %s\n", buff);
- // Check if the password is correct
- if (strcmp(checkValidPassword(current, uid, buff), PASS_OK) == 0) {
- printf("Correct password!\n");
- connStat = AUTHENTICATED;
- countWrong = 0;
- // Echo success code to client
- ret = send(connSock, PASS_OK, strlen(PASS_OK), 0);
- if (ret == SOCKET_ERROR)
- printf("Error 2.2 : %i\n", WSAGetLastError());
- break;
- }
- // The password is incorrect
- else if ((strcmp(checkValidPassword(current, uid, buff), PASS_INCORRECT) == 0) && (countWrong < 2)) {
- printf("Count wrong %d\n", countWrong);
- printf("Incorrect password!\n");
- countWrong++;
- // Echo error code to client
- ret = send(connSock, PASS_INCORRECT, strlen(PASS_INCORRECT), 0);
- if (ret == SOCKET_ERROR)
- printf("Error 2.3 : %i\n", WSAGetLastError());
- }
- // The userID is disabled after 3 times entering wrong password
- else if (countWrong == 2) {
- printf("UserID is disabled!\n");
- connStat = NOT_IDENTIFIED;
- lockUser(file, uid, current, fileName);
- ret = send(connSock, USER_DISABLED, strlen(USER_DISABLED), 0);
- if (ret == SOCKET_ERROR)
- printf("Error 2.4 : %i\n", WSAGetLastError());
- }
- // UserID is disabled
- else if (strcmp(checkValidUser(current, uid), USER_DISABLED) == 0) {
- printf("User is disabled!\n");
- // Echo error code to client
- ret = send(connSock, USER_DISABLED, strlen(USER_DISABLED), 0);
- if (ret == SOCKET_ERROR)
- printf("Error 3 : %i\n", WSAGetLastError());
- }
- }
- else if (strlen(buff) <= 0) {
- printf("Receive from client %s\n", buff);
- printf("Password is NULL!\n");
- ret = send(connSock, PASS_INCORRECT, strlen(PASS_INCORRECT), 0);
- // printf("8\n");
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- }
- else if (strcmp(getPrefix(buff), USER_PREFIX) == 0) {
- ret = send(connSock, SYNTAX_ERROR, strlen(SYNTAX_ERROR), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- else if (strcmp(getPrefix(buff), LOGOUT_PREFIX) == 0) {
- ret = send(connSock, USER_NOT_AUTHENTICATED, strlen(USER_NOT_AUTHENTICATED), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- else if (strcmp(getPrefix(buff), EXIT_PREFIX) == 0) {
- ret = send(connSock, EXIT_OK, strlen(EXIT_OK), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- connStat = NO_CONNECTION;
- break;
- }
- else if (strcmp(getPrefix(buff), OTHER_OPTION) == 0) {
- ret = send(connSock, NO_OPTION, strlen(NO_OPTION), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- else {
- ret = send(connSock, UNKNOW_ERROR, strlen(UNKNOW_ERROR), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- }
- // Userid has been logged in to the system
- else if (connStat == AUTHENTICATED) {
- if (strcmp(getPrefix(buff), USER_PREFIX) == 0) {
- ret = send(connSock, USER_AUTHENTICATED, strlen(USER_AUTHENTICATED), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- else if (strcmp(getPrefix(buff), PASSWORD_PREFIX) == 0) {
- ret = send(connSock, USER_AUTHENTICATED, strlen(USER_AUTHENTICATED), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- else if (strcmp(getPrefix(buff), LOGOUT_PREFIX) == 0) {
- ret = send(connSock, LOGOUT_SUCCESS, strlen(LOGOUT_SUCCESS), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- connStat = NOT_IDENTIFIED;
- }
- else if (strcmp(getPrefix(buff), EXIT_PREFIX) == 0) {
- ret = send(connSock, EXIT_OK, strlen(EXIT_OK), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- connStat = NO_CONNECTION;
- break;
- }
- else if (strcmp(getPrefix(buff), OTHER_OPTION) == 0) {
- ret = send(connSock, NO_OPTION, strlen(NO_OPTION), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- else {
- ret = send(connSock, UNKNOW_ERROR, strlen(UNKNOW_ERROR), 0);
- if (ret == SOCKET_ERROR)
- printf("Error : %i\n", WSAGetLastError());
- }
- }
- } while (connStat != NO_CONNECTION);
- closesocket(connSock);
- } // end accepting
- // Close socket
- closesocket(listenSock);
- // Terminate Winsock
- WSACleanup();
- return 0;
- }
- // Return a singly linked list with the
- // data as from pointed file
- Account *getDataFromFile(FILE *file) {
- Account *current; // pointer points to the current data
- char userID[MAX_UID_LENGTH];
- char password[MAX_PWD_LENGHT];
- int status; // status of the account (0 is active, 1 is deactive)
- current = NULL;
- // Traverse to the end of file
- while (!feof(file)) {
- // Extract data from file
- fscanf(file, "%s %s %d", userID, password, &status);
- // Assign data to the current pointer
- Account *node = new Account;
- node->userID = strdup(userID);
- node->password = strdup(password);
- node->status = status;
- node->next = NULL;
- if (head == NULL)
- head = current = node;
- else
- current = current->next = node;
- }
- return current;
- }
- // Save the data from the pointed linked
- // list to a specific file
- void saveDataToFile(FILE *file, Account *aPtr) {
- for (aPtr = head; aPtr; aPtr = aPtr->next) {
- // This if - else is to make the last character not '\n'
- if (aPtr->next != NULL)
- fprintf(file, "%s %s %d\n", aPtr->userID, aPtr->password, aPtr->status);
- else
- fprintf(file, "%s %s %d", aPtr->userID, aPtr->password, aPtr->status);
- }
- }
- // Print the data of the linked list
- // (for testing only)
- void printList(Account *aPtr) {
- for (aPtr = head; aPtr; aPtr = aPtr->next)
- printf("%s %s %d\n", aPtr->userID, aPtr->password, aPtr->status);
- }
- // Return a signal to send back userID
- // handled result to the client
- // The parameters is a data pointer and userID
- char *checkValidUser(Account *aPtr, char *userID) {
- for (aPtr = head; aPtr; aPtr = aPtr->next)
- if ((strcmp(aPtr->userID, userID) == 0) && (aPtr->status == 0))
- return USER_OK;
- else if ((strcmp(aPtr->userID, userID) == 0) && (aPtr->status == 1))
- return USER_DISABLED;
- return USER_VOID;
- }
- // Return a signal to send back password
- // handled result to the client
- // The parameters is a data pointer, userID and password
- char *checkValidPassword(Account *aPtr, char *userID, char *password) {
- for (aPtr = head; aPtr; aPtr = aPtr->next)
- if ((strcmp(aPtr->userID, userID) == 0) && (aPtr->status == 0) && (strcmp(aPtr->password, password) == 0))
- return PASS_OK;
- return PASS_INCORRECT;
- }
- // This function is to lock user when
- // the password is incorrect 3 times
- // The parameter includes: file pointer,
- // userID to be disabled, data pointer
- // and the name of the pointed file
- void lockUser(FILE *file, char *userID, Account *aPtr, char *oldName) {
- // Change the status to 1 (disabled)
- freopen(oldName, "w+", file);
- for (aPtr = head; aPtr; aPtr = aPtr->next) {
- if (strcmp(aPtr->userID, userID) == 0) {
- aPtr->status = 1;
- }
- }
- printf("\tLOCKED ACCOUNT %s !\n", userID);
- saveDataToFile(file, aPtr);
- freopen(oldName, "r+", file);
- }
- // Check if the logout command is available
- char *checkLogout() {
- return LOGOUT_SUCCESS;
- }
- // Get prefix of a message from client
- char *getPrefix(char *message) {
- char *pre = new char[5];
- int c;
- if (pre == NULL) {
- printf("Error!\n");
- exit(1);
- }
- for (c = 0; c < 4; c++) {
- *(pre + c) = *(message);
- message++;
- }
- *(pre + c) = '\0';
- return pre;
- }
- // Check if the message is UserID
- bool isUserID(char *message) {
- if (strcmp(getPrefix(message), USER_PREFIX) == 0) {
- return true;
- }
- return false;
- }
- // Check if the message is Password
- bool isPassword(char *message) {
- if (strcmp(getPrefix(message), PASSWORD_PREFIX) == 0)
- return true;
- return false;
- }
- // Check if the message is Logout cmd
- bool isLogOut(char *message) {
- if (strcmp(getPrefix(message), LOGOUT_PREFIX) == 0)
- return true;
- return false;
- }
- // Function to cut n first character of
- // a pointed string
- void chopNChars(char *str, int n) {
- int strLen = strlen(str);
- if (n > strLen)
- return;
- memmove(str, str + n, strLen - n + 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement