aboutsummaryrefslogtreecommitdiffstats
path: root/src/server.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/server.cpp')
-rw-r--r--src/server.cpp105
1 files changed, 36 insertions, 69 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;