diff options
author | Matt Strapp <matt@mattstrapp.net> | 2022-04-25 18:10:23 -0500 |
---|---|---|
committer | Matt Strapp <matt@mattstrapp.net> | 2022-04-25 18:10:23 -0500 |
commit | 44d6af73fa1dcfd04b8153433da5280387e2f98d (patch) | |
tree | 408e025d415421b5318e442fad64ef4ccaa979b5 /src | |
parent | A (diff) | |
download | csci4211-44d6af73fa1dcfd04b8153433da5280387e2f98d.tar csci4211-44d6af73fa1dcfd04b8153433da5280387e2f98d.tar.gz csci4211-44d6af73fa1dcfd04b8153433da5280387e2f98d.tar.bz2 csci4211-44d6af73fa1dcfd04b8153433da5280387e2f98d.tar.lz csci4211-44d6af73fa1dcfd04b8153433da5280387e2f98d.tar.xz csci4211-44d6af73fa1dcfd04b8153433da5280387e2f98d.tar.zst csci4211-44d6af73fa1dcfd04b8153433da5280387e2f98d.zip |
more
Signed-off-by: Matt Strapp <matt@mattstrapp.net>
Diffstat (limited to 'src')
-rw-r--r-- | src/server.cpp | 105 | ||||
-rw-r--r-- | src/util.cpp | 335 |
2 files changed, 212 insertions, 228 deletions
diff --git a/src/server.cpp b/src/server.cpp index 270a350..b5e5757 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1,10 +1,10 @@ -#include "server.hpp"
+#include "server.hpp"
#define MAX_CONCURRENCY_LIMIT 40000 // overkill
#define POLL_TIMEOUT 1000 // Milliseconds
#define S2C_SERVICE_FREQ 1.0 // Seconds
-#define USER_DATABASE "user_database.txt"
-#define FILE_DATABASE "file_database.txt"
+#define USER_DATABASE "userDB"
+#define FILE_DATABASE "fileDB"
#define FILES_DIR "files"
int nConns; // total # of data sockets
@@ -28,7 +28,7 @@ void RemoveConnection(int i) { Log("[%s] %d active connections after removal", NAME, nConns);
}
-void DoServer(int svrPort) {
+void DoServer(int port) {
int maxConcurrency = 20; // TODO: Change this to be a #define
int listenFD = socket(AF_INET, SOCK_STREAM, 0);
if (listenFD < 0) {
@@ -39,7 +39,7 @@ void DoServer(int svrPort) { struct sockaddr_in serverAddr;
memset(&serverAddr, 0, sizeof(struct sockaddr_in));
serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons((unsigned short)svrPort);
+ serverAddr.sin_port = htons((unsigned short)port);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
int optval = 1;
@@ -52,13 +52,15 @@ void DoServer(int svrPort) { signal(SIGPIPE, SIG_IGN);
if (bind(listenFD, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0) {
- Error("[%s] Cannot bind to port %d.", NAME, svrPort);
+ Error("[%s] Cannot bind to port %d.", NAME, port);
}
if (listen(listenFD, 16) != 0) {
- Error("[%s] Cannot listen to port %d.", NAME, svrPort);
+ Error("[%s] Cannot listen to port %d.", NAME, port);
}
+ Log("[%s] Listening at 127.0.0.1:%d", NAME, port);
+
nConns = 0;
memset(peers, 0, sizeof(peers));
peers[0].fd = listenFD;
@@ -124,8 +126,7 @@ void DoServer(int svrPort) { if (connStat[i].recInitHeader == false) {
if (Recv_NonBlocking(peers[i].fd, buf[i], &connStat[i], &peers[i]) <
0) {
- Log("[%s] Line %d: Removing connection %d", NAME, __LINE__,
- i);
+ Log("[%s] Line %d: Removing connection %d", NAME, __LINE__, i);
RemoveConnection(i);
goto NEXT_CONNECTION;
}
@@ -145,8 +146,7 @@ void DoServer(int svrPort) { connStat[i].expectingHeader =
true; // flag needed for processing below
if (processReception(i) != 0) {
- Log("[%s] Line %d: Removing connection %d", NAME, __LINE__,
- i);
+ Log("[%s] Line %d: Removing connection %d", NAME, __LINE__, i);
RemoveConnection(i);
goto NEXT_CONNECTION;
}
@@ -158,16 +158,14 @@ void DoServer(int svrPort) { if (connStat[i].direction == C2S) {
if (Recv_NonBlocking(peers[i].fd, buf[i], &connStat[i], &peers[i]) <
0) {
- Log("[%s] Line %d: Removing connection %d", NAME, __LINE__,
- i);
+ Log("[%s] Line %d: Removing connection %d", NAME, __LINE__, i);
RemoveConnection(i);
goto NEXT_CONNECTION;
}
if (connStat[i].nToDo == 0) {
if (processReception(i) != 0) {
- Log("[%s] Line %d: Removing connection %d", NAME,
- __LINE__, i);
+ Log("[%s] Line %d: Removing connection %d", NAME, __LINE__, i);
RemoveConnection(i);
goto NEXT_CONNECTION;
}
@@ -315,7 +313,7 @@ void doServerCommand(int i) { outHeader.m_command = REGISTER;
memcpy(outHeader.m_name, NAME, strlen(NAME));
strcat(message, "Cannot register username [");
- strcat(message, username);
+ strncat(message, username, MAX_USERNAME_LEN);
strcat(message, "]. Username already registered.\n");
strcat(message,
"\tUsernames may only contain characters a-z, A-Z, or 0-9.\n");
@@ -323,14 +321,14 @@ void doServerCommand(int i) { outHeader.m_size = strlen(message);
sendMessageToId(connStat[i].id, false, outHeader, message);
} else {
- Log("[---- : %s] Successful registration (usr, pswd): %s %s",
- com2str(header.m_command), username, password);
+ Log("[---- : %s] Successful registration of user %s",
+ com2str(header.m_command), username);
recordEntry(USER_DATABASE, username, password);
outHeader.setFlags(S2C, SUCCESS, PUB, SIGN);
outHeader.m_command = REGISTER;
memcpy(outHeader.m_name, NAME, strlen(NAME));
strcat(message, "Successfully registered ");
- strcat(message, username);
+ strncat(message, username, MAX_USERNAME_LEN);
strcat(message, ".");
outHeader.m_size = strlen(message);
sendMessageToId(connStat[i].id, false, outHeader, message);
@@ -351,7 +349,7 @@ void doServerCommand(int i) { outHeader.m_command = LOGIN;
memcpy(outHeader.m_name, NAME, strlen(NAME));
strcat(message, "Login failed. ");
- strcat(message, username);
+ strncat(message, username, MAX_USERNAME_LEN);
strcat(message, " is logged in on another device.");
outHeader.m_size = strlen(message);
sendMessageToId(connStat[i].id, false, outHeader, message);
@@ -367,20 +365,20 @@ void doServerCommand(int i) { connStat[x].isLoggedIn = true;
}
}
- Log("[%s : %s] Successful login (usr, pswd): %s %s", connStat[i].name,
- com2str(header.m_command), username, password);
+ Log("[%s : %s] Successful login", connStat[i].name,
+ com2str(header.m_command));
outHeader.setFlags(S2C, SUCCESS, PUB, SIGN);
outHeader.m_command = LOGIN;
memcpy(outHeader.m_name, NAME, strlen(NAME));
strcat(message, "Welcome ");
- strcat(message, username);
+ strncat(message, username, MAX_USERNAME_LEN);
strcat(message, ".");
outHeader.m_size = strlen(message);
sendMessageToId(connStat[i].id, true, outHeader, message);
}
} else {
- Log("[---- : %s] Login failed (usr, pswd): %s %s",
- com2str(header.m_command), username, password);
+ Log("[---- : %s] Login failed: %s",
+ com2str(header.m_command), username);
outHeader.setFlags(S2C, FAIL, PUB, SIGN);
outHeader.m_command = LOGIN;
memcpy(outHeader.m_name, NAME, strlen(NAME));
@@ -406,7 +404,7 @@ void doServerCommand(int i) { memcpy(outHeader.m_name, NAME, strlen(NAME));
strcat(message, "Goodbye ");
strcat(message, connStat[i].name);
- strcat(message, ". Come back soon.");
+ strcat(message, ".");
outHeader.m_size = strlen(message);
sendMessageToId(connStat[i].id, false, outHeader, message);
} else {
@@ -475,7 +473,7 @@ void doServerCommand(int i) { if ((connStat[x].direction == C2S) &&
(connStat[x].linkType == MESSAGE_LINK) &&
(connStat[x].id == connStat[i].id)) {
- strcat(connStat[i].name, connStat[x].name);
+ strncat(connStat[i].name, connStat[x].name, MAX_USERNAME_LEN);
connStat[i].isLoggedIn = connStat[x].isLoggedIn;
foundActiveUser = true;
s2cConnNumb = x;
@@ -484,7 +482,7 @@ void doServerCommand(int i) { }
if (s2cConnNumb == -1) {
- Log("Enexpected state. Recieved %s on new connection without S2C "
+ Log("Unexpected state. Received %s on new connection without S2C "
"connection for messages.",
com2str(header.m_command));
} else if (!connStat[i].isLoggedIn) {
@@ -565,8 +563,8 @@ void doServerCommand(int i) { connStat[i].direction = S2C;
connStat[i].linkType = FILE_LINK;
connStat[i].lastTime = currentTime;
- // no need for shouldClose. Client will close connection and periodic ping will
- // detect closure
+ // no need for shouldClose. Client will close connection and periodic ping
+ // will detect closure
snprintf(server_filename, sizeof(server_filename), "%s/%d_", FILES_DIR,
fileId);
@@ -715,8 +713,7 @@ void prepareMessage(int i, Header header, char *message) { peers[i].events |= POLLWRNORM;
} else {
- Log("[%s] WARNING: attempted to write to C2S connection %d", NAME,
- i);
+ Log("[%s] WARNING: attempted to write to C2S connection %d", NAME, i);
}
}
@@ -741,48 +738,18 @@ int main(int argc, char **argv) { }
if (strcmp("reset", argv[1]) == 0) {
- Log("Resetting server databases");
- clearDatabase(USER_DATABASE);
- clearDatabase(FILE_DATABASE);
-
- int status;
- pid_t cpid;
-
- if ((cpid = fork()) == -1) {
- perror("fork");
- return 1;
- }
-
- if (cpid == 0) { // Child process executes "rm -rf users"
- char command[40] = "rm -rf files"; // only way this worked
- char *args[5] = {NULL};
- args[0] = strtok(command, " ");
- for (int i = 1; i < 3; i++)
- args[i] = strtok(NULL, " ");
- execvp(args[0], args);
- return 0;
- } else { // Parent process waits for "rm -rf users" to finish
- pid_t rpid;
- int status;
- if ((rpid = wait(NULL)) == -1)
- perror("wait");
- else {
- Log("Clearing file storage");
- if (mkdir(FILES_DIR, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
- perror("mkdir");
- return -1;
- }
- }
- return 0;
- }
+ Log("Clearing databases");
+ if (remove(USER_DATABASE) != 0)
+ Error("Error deleting %s", USER_DATABASE);
+ if (remove(FILE_DATABASE) != 0)
+ Error("Error deleting %s", FILE_DATABASE);
+ exit(EXIT_SUCCESS);
}
- // Make files directory if it doesn't exist
- DIR *pDir;
+ // Make directory if it doesn't exist
struct stat s;
if (stat("files", &s) != 0) {
- // The files directory doesn't exist
if (mkdir("files", S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
perror("mkdir");
return -1;
diff --git a/src/util.cpp b/src/util.cpp index 142fcbd..dc95441 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,16 +1,17 @@ -#include <string> -#include <string.h> #include <cstddef> -#include <stdio.h> #include <stdarg.h> -#include <time.h> +#include <stdio.h> +#include <string.h> +#include <string> #include <sys/timeb.h> +#include <time.h> #include "util.hpp" -#define VALID_CHARACTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" +#define VALID_CHARACTERS \ + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" -enum Command parse_command(char* line){ +enum Command parse_command(char *line) { /* Find a valid command in the first word of the line (case sensitive) args: line (char*): string of characters from the input file @@ -18,130 +19,154 @@ enum Command parse_command(char* line){ returns: (command): Returns the found command. Returns else if first word is invalid. */ - if (line == NULL){ + if (line == NULL) { return INVALID; } - char* rest; - char* tok = strtok_r(line, " ", &rest); + char *rest; + char *tok = strtok_r(line, " ", &rest); - if (tok == NULL){ + if (tok == NULL) { return INVALID; } // REGISTER [username] [password] - if (strcmp(tok, "REGISTER") == 0){ - char* username = strtok_r(rest, " ", &rest); - if(!isValidUsername(username)) return INVALID; + if (strcmp(tok, "REGISTER") == 0) { + char *username = strtok_r(rest, " ", &rest); + if (!isValidUsername(username)) + return INVALID; - char* password = strtok_r(rest, " ", &rest); - if(password == NULL) return INVALID; - if(!isValidPassword(password)) return INVALID; + char *password = strtok_r(rest, " ", &rest); + if (password == NULL) + return INVALID; + if (!isValidPassword(password)) + return INVALID; - char* misc = strtok_r(rest, " ", &rest); - if(misc != NULL) return INVALID; + char *misc = strtok_r(rest, " ", &rest); + if (misc != NULL) + return INVALID; return REGISTER; } // LOGIN [username] [password] - if (strcmp(tok, "LOGIN") == 0){ - char* username = strtok_r(rest, " ", &rest); - if(!isValidUsername(username)) return INVALID; + if (strcmp(tok, "LOGIN") == 0) { + char *username = strtok_r(rest, " ", &rest); + if (!isValidUsername(username)) + return INVALID; - char* password = strtok_r(rest, " ", &rest); - if(password == NULL) return INVALID; - if(!isValidPassword(password)) return INVALID; + char *password = strtok_r(rest, " ", &rest); + if (password == NULL) + return INVALID; + if (!isValidPassword(password)) + return INVALID; - char* misc = strtok_r(rest, " ", &rest); - if(misc != NULL) return INVALID; + char *misc = strtok_r(rest, " ", &rest); + if (misc != NULL) + return INVALID; return LOGIN; } // LOGOUT - if (strcmp(tok, "LOGOUT") == 0){ - char* misc = strtok_r(rest, " ", &rest); - if(misc != NULL) return INVALID; + if (strcmp(tok, "LOGOUT") == 0) { + char *misc = strtok_r(rest, " ", &rest); + if (misc != NULL) + return INVALID; return LOGOUT; } // SEND [msg] - if (strcmp(tok, "SEND") == 0){ - if (int(strlen(rest) == 0)) return INVALID; + if (strcmp(tok, "SEND") == 0) { + if (int(strlen(rest) == 0)) + return INVALID; return SEND; } // SEND2 [username] [msg] - if (strcmp(tok, "SEND2") == 0){ - char* username = strtok_r(rest, " ", &rest); - if(!isValidUsername(username)) return INVALID; + if (strcmp(tok, "SEND2") == 0) { + char *username = strtok_r(rest, " ", &rest); + if (!isValidUsername(username)) + return INVALID; - if (int(strlen(rest) == 0)) return INVALID; + if (int(strlen(rest) == 0)) + return INVALID; return SEND2; } // SENDA [msg] - if (strcmp(tok, "SENDA") == 0){ - if (int(strlen(rest) == 0)) return INVALID; + if (strcmp(tok, "SENDA") == 0) { + if (int(strlen(rest) == 0)) + return INVALID; return SENDA; } // SENDA2 [username] [msg] - if (strcmp(tok, "SENDA2") == 0){ - char* username = strtok_r(rest, " ", &rest); - if(!isValidUsername(username)) return INVALID; + if (strcmp(tok, "SENDA2") == 0) { + char *username = strtok_r(rest, " ", &rest); + if (!isValidUsername(username)) + return INVALID; - if (int(strlen(rest) == 0)) return INVALID; + if (int(strlen(rest) == 0)) + return INVALID; return SENDA2; } // SENDF [local file] - if (strcmp(tok, "SENDF") == 0){ - char* file_name = strtok_r(rest, " ", &rest); - if(file_name == NULL) return INVALID; + if (strcmp(tok, "SENDF") == 0) { + char *file_name = strtok_r(rest, " ", &rest); + if (file_name == NULL) + return INVALID; - if (int(strlen(rest) != 0)) return INVALID; + if (int(strlen(rest) != 0)) + return INVALID; return SENDF; } // SENDF2 [username] [local file] - if (strcmp(tok, "SENDF2") == 0){ - char* username = strtok_r(rest, " ", &rest); - if(!isValidUsername(username)) return INVALID; + if (strcmp(tok, "SENDF2") == 0) { + char *username = strtok_r(rest, " ", &rest); + if (!isValidUsername(username)) + return INVALID; - char* file_name = strtok_r(rest, " ", &rest); - if(file_name == NULL) return INVALID; + char *file_name = strtok_r(rest, " ", &rest); + if (file_name == NULL) + return INVALID; - if (int(strlen(rest) != 0)) return INVALID; + if (int(strlen(rest) != 0)) + return INVALID; return SENDF2; } // LIST - if (strcmp(tok, "LIST") == 0){ - char* misc = strtok_r(rest, " ", &rest); - if(misc != NULL) return INVALID; + if (strcmp(tok, "LIST") == 0) { + char *misc = strtok_r(rest, " ", &rest); + if (misc != NULL) + return INVALID; return LIST; } // DELAY - if (strcmp(tok, "DELAY") == 0){ - char* delay_time = strtok_r(rest, " ", &rest); - if(delay_time == NULL) return INVALID; + if (strcmp(tok, "DELAY") == 0) { + char *delay_time = strtok_r(rest, " ", &rest); + if (delay_time == NULL) + return INVALID; std::string str(delay_time); std::size_t found = str.find_first_not_of("0123456789"); - if(found != std::string::npos) return INVALID; + if (found != std::string::npos) + return INVALID; - if (int(strlen(rest) != 0)) return INVALID; + if (int(strlen(rest) != 0)) + return INVALID; return DELAY; } @@ -150,8 +175,7 @@ enum Command parse_command(char* line){ return INVALID; } - -bool isValidUsername(char* username){ +bool isValidUsername(char *username) { /* Helper function to check if a username string is valid args: username (char*): Username to be tested. @@ -159,52 +183,58 @@ bool isValidUsername(char* username){ returns: bool: Username is valid ? true, false */ - if(username == NULL) return false; - else if ((strlen(username) > MAX_USERNAME_LEN)||(strlen(username) < MIN_USERNAME_LEN)) return false; - else{ + if (username == NULL) + return false; + else if ((strlen(username) > MAX_USERNAME_LEN) || + (strlen(username) < MIN_USERNAME_LEN)) + return false; + else { std::string str(username); std::size_t found = str.find_first_not_of(VALID_CHARACTERS); - if(found != std::string::npos) return false; + if (found != std::string::npos) + return false; } return true; } - -bool isValidPassword(char* password){ - if(password == NULL) return false; - else if ((strlen(password) > MAX_PASSWORD_LEN)||(strlen(password) < MIN_PASSWORD_LEN)) return false; - else{ +bool isValidPassword(char *password) { + if (password == NULL) + return false; + else if ((strlen(password) > MAX_PASSWORD_LEN) || + (strlen(password) < MIN_PASSWORD_LEN)) + return false; + else { std::string str(password); std::size_t found = str.find_first_not_of(VALID_CHARACTERS); - if(found != std::string::npos) return false; + if (found != std::string::npos) + return false; } return true; } - -void Error(const char * format, ...) { - char msg[4096]; - va_list argptr; - va_start(argptr, format); - vsprintf(msg, format, argptr); - va_end(argptr); - fprintf(stderr, "Error: %s\n", msg); - exit(-1); +void Error(const char *format, ...) { + char msg[4096]; + va_list argptr; + va_start(argptr, format); + vsprintf(msg, format, argptr); + va_end(argptr); + fprintf(stderr, "Error: %s\n", msg); + exit(-1); } -void Log(const char * format, ...) { - char msg[2048]; - va_list argptr; - va_start(argptr, format); - vsprintf(msg, format, argptr); - va_end(argptr); - fprintf(stderr, "%s\n", msg); +void Log(const char *format, ...) { + char msg[2048]; + va_list argptr; + va_start(argptr, format); + vsprintf(msg, format, argptr); + va_end(argptr); + fprintf(stderr, "%s\n", msg); } // convert buf[0:3] to int -void buf2i(BYTE * buf, int &i){ +void buf2i(BYTE *buf, int &i) { // No way to avoid Segmentation fault. Some bytes may be 0 i = 0; i += buf[3]; @@ -214,7 +244,7 @@ void buf2i(BYTE * buf, int &i){ } // convert int to buf[4] where buf[0] contains MSB's and buf[3] contains LSB's -void i2buf(int &i, BYTE * buf){ +void i2buf(int &i, BYTE *buf) { // No way to avoid Segmentation fault. Some bytes may be 0 buf[0] = (BYTE)((i & 0xFF000000) >> 24); buf[1] = (BYTE)((i & 0x00FF0000) >> 16); @@ -222,36 +252,24 @@ void i2buf(int &i, BYTE * buf){ buf[3] = (BYTE)(i & 0x000000FF); } -const char * com2str(enum Command command){ - const char * com2str[] = {"REGISTER", - "LOGIN", - "LOGOUT", - "SEND", - "SEND2", - "SENDA", - "SENDA2", - "SENDF", - "SENDF2", - "LIST", - "DELAY", - "GETF", - "PING", - "CONNECT", - "", - "INVALID" - }; - return com2str[command]; +const char *com2str(enum Command command) { + const char *com2str[] = {"REGISTER", "LOGIN", "LOGOUT", "SEND", + "SEND2", "SENDA", "SENDA2", "SENDF", + "SENDF2", "LIST", "DELAY", "GETF", + "PING", "CONNECT", "", "INVALID"}; + return com2str[command]; } // Computes the difference between times a and b in seconds // a - b = result -double timeDifference(struct timeb a, struct timeb b){ - return (a.time + a.millitm / (double) 1000.0f) - (b.time + b.millitm / (double) 1000.0f); +double timeDifference(struct timeb a, struct timeb b) { + return (a.time + a.millitm / (double)1000.0f) - + (b.time + b.millitm / (double)1000.0f); } -int getNumEntries(const char* database){ - FILE* fp = fopen(database, "r"); - if (!fp){ +int getNumEntries(const char *database) { + FILE *fp = fopen(database, "r"); + if (!fp) { Log("Username check failed to open the file: %s", database); return -1; } @@ -259,9 +277,9 @@ int getNumEntries(const char* database){ int numEntries = 0; char line[LINE_SIZE] = {0}; - while(fgets(line, LINE_SIZE, fp) != NULL){ - char* new_line_pos = strchr(line, '\n'); - if(new_line_pos != NULL) + while (fgets(line, LINE_SIZE, fp) != NULL) { + char *new_line_pos = strchr(line, '\n'); + if (new_line_pos != NULL) *new_line_pos = 0; // Including blank lines as entries. Don't use any blank lines. numEntries++; @@ -272,28 +290,30 @@ int getNumEntries(const char* database){ return numEntries; } -// User/password is in registry: true -// User/password is not in registry: false -bool loginQuery(const char * database, char* username, char* password){ - FILE* fp = fopen(database, "r"); - if (!fp){ - Log("Registry check failed to open the file: %s", database); +bool loginQuery(const char *database, char *username, char *password) { + FILE *fp = fopen(database, "r"); + if (!fp) { + Log("Could not open user file: %s", database); return false; } char line[LINE_SIZE] = {0}; - while(fgets(line, LINE_SIZE, fp) != NULL){ - char* new_line_pos = strchr(line, '\n'); - if(new_line_pos != NULL) + while (fgets(line, LINE_SIZE, fp) != NULL) { + char *new_line_pos = strchr(line, '\n'); + if (new_line_pos != NULL) *new_line_pos = 0; - char* rest; - char* usr = strtok_r(line, " ", &rest); - char* pswd = strtok_r(rest, " ", &rest); + char *rest; + char *usr = strtok_r(line, " ", &rest); + char *pswd = strtok_r(rest, " ", &rest); - //Log("username: |%s| Password: |%s|", usr, pswd); + if (usr == NULL || pswd == NULL) { + Log("Registry check failed to parse the file: %s", database); + fclose(fp); + return false; + } - if((strcmp(username, usr) == 0) && (strcmp(password, pswd) == 0)){ + if ((strcmp(username, usr) == 0) && (strcmp(password, pswd) == 0)) { fclose(fp); return true; } @@ -307,25 +327,26 @@ bool loginQuery(const char * database, char* username, char* password){ // User is in registry: true // User is not in registry: false -bool usernameQuery(const char * database, char* username){ - FILE* fp = fopen(database, "r"); - if (!fp){ +bool usernameQuery(const char *database, char *username) { + FILE *fp = fopen(database, "r"); + if (!fp) { Log("Username check failed to open the file: %s", database); return false; } char line[LINE_SIZE] = {0}; - while(fgets(line, LINE_SIZE, fp) != NULL){ - char* new_line_pos = strchr(line, '\n'); - if(new_line_pos != NULL) + while (fgets(line, LINE_SIZE, fp) != NULL) { + char *new_line_pos = strchr(line, '\n'); + if (new_line_pos != NULL) *new_line_pos = 0; - char* rest; - char* usr = strtok_r(line, " ", &rest); + char *rest; + char *usr = strtok_r(line, " ", &rest); - //Log("username: |%s|", usr); + if (usr == NULL) + continue; - if(strcmp(username, usr) == 0){ + if (strcmp(username, usr) == 0) { fclose(fp); return true; } @@ -337,10 +358,10 @@ bool usernameQuery(const char * database, char* username){ return false; } -void recordEntry(const char * database, char* key, char* value){ - FILE* fp = fopen(database, "a"); +void recordEntry(const char *database, char *key, char *value) { + FILE *fp = fopen(database, "a"); - if (!fp){ + if (!fp) { Log("Registry append failed to open the file: %s", database); return; } @@ -355,41 +376,37 @@ void recordEntry(const char * database, char* key, char* value){ fclose(fp); } -void clearDatabase(const char * database){ - FILE* fp = fopen(database, "w"); - fclose(fp); -} - - -int buf2file(BYTE* buf, int nbytes, char* filename){ - FILE* f = fopen(filename, "w"); - if(!f) return -1; +int buf2file(BYTE *buf, int nbytes, char *filename) { + FILE *f = fopen(filename, "w"); + if (!f) + return -1; fwrite(buf, 1, nbytes, f); fclose(f); return 0; } - -int file2buf(char* filename, BYTE* buf){ - FILE* f = fopen(filename, "r"); - if(!f){ +int file2buf(char *filename, BYTE *buf) { + FILE *f = fopen(filename, "r"); + if (!f) { Log("Error: Cannot open %s", filename); return -1; } fseek(f, 0, SEEK_END); // jump to end of file - long nbytes = ftell(f); // find current offset on file f + long nbytes = ftell(f); // find current offset on file f fseek(f, 0, SEEK_SET); // jump to beginning of file - if(nbytes > MAX_REQUEST_SIZE){ + if (nbytes > MAX_REQUEST_SIZE) { Log("Error: File %s is too big: %d", filename, nbytes); + fclose(f); return -1; } int nread = fread(buf, 1, nbytes, f); - if(nread != nbytes){ + if (nread != nbytes) { Log("Error: Byte count mismatch with fread: %d, %d", nbytes, nread); + fclose(f); return -1; } |