aboutsummaryrefslogtreecommitdiffstats
path: root/src/server.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/server.cpp')
-rw-r--r--src/server.cpp86
1 files changed, 36 insertions, 50 deletions
diff --git a/src/server.cpp b/src/server.cpp
index 745b215..d016e30 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -1,3 +1,6 @@
+// Author Name: Matt Strapp
+// Date: 25 April 2022
+// x500: strap012
#include "server.hpp"
#define MAX_CONCURRENCY_LIMIT 40000 // overkill
@@ -95,7 +98,7 @@ void DoServer(int port) {
memset(&connStat[nConns], 0, sizeof(struct CONN_STAT));
ftime(&connStat[nConns].lastTime);
connStat[nConns].recInitHeader =
- false; // waiting to recieve initial header
+ false; // waiting to receive initial header
connStat[nConns].expectingHeader =
true; // message to be read is a header
connStat[nConns].nToDo = HEADER_LEN;
@@ -109,7 +112,6 @@ void DoServer(int port) {
}
for (int i = 1; i <= nConns; i++) {
- // Log("nConns: %d peers[%d].revents: %02X (POLLRDNORM %02X, POLLWRNORM
// %02X, POLLERR %02X, POLLHUP %02X)", nConns, i, peers[i].revents,
// POLLRDNORM, POLLWRNORM, POLLERR, POLLHUP);
double timeDiff =
@@ -118,23 +120,20 @@ void DoServer(int port) {
connStat[i].lastTime.millitm / (double)1000.0f);
if (peers[i].revents &
- (POLLRDNORM | POLLERR |
- POLLHUP)) { // TODO: break out POLLERR and POLLHUP?
- // Log("Servicing connection %d, POLLHUP: %d", i, peers[i].revents &
- // POLLHUP);
+ (POLLRDNORM | POLLERR | POLLHUP)) { // TODO: break out POLLERR and
+ // POLLHUP? POLLHUP);
// Process initial communication
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] Removing connection %d", NAME, i);
RemoveConnection(i);
- goto NEXT_CONNECTION;
+ continue;
}
if (connStat[i].nToDo == 0) {
Header header;
header.decode(buf[i]);
- // Log("Initial communication for connection %d:", i);
if (!((header.m_command == CONNECT) | (header.m_command == SENDF) |
(header.m_command == SENDF2) | (header.m_command == GETF))) {
@@ -146,51 +145,46 @@ void DoServer(int port) {
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] Removing connection %d", NAME, i);
RemoveConnection(i);
- goto NEXT_CONNECTION;
+ continue;
}
}
} // End initial communication
- else { // Standard polling recieve
- // Log("Standard polling recieve/send: connStat[i].direction: %02X",
- // connStat[i].direction);
+ else { // Standard polling
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] Removing connection %d", NAME, i);
RemoveConnection(i);
- goto NEXT_CONNECTION;
+ continue;
}
if (connStat[i].nToDo == 0) {
if (processReception(i) != 0) {
- Log("[%s] Line %d: Removing connection %d", NAME, __LINE__, i);
+ Log("[%s] Removing connection %d", NAME, i);
RemoveConnection(i);
- goto NEXT_CONNECTION;
+ continue;
}
}
} // end C2S
- } // End standard recieve
+ } // End standard receive
} // if POLLRDNORM
// a data socket is writable
if (peers[i].revents & POLLWRNORM) {
- // Log("Sending nonblocking!");
if (Send_NonBlocking(peers[i].fd, buf[i], &connStat[i], &peers[i]) <
0) {
- Log("[%s] Line %d: Removing connection %d", NAME, __LINE__, i);
+ Log("[%s] Removing connection %d", NAME, i);
RemoveConnection(i);
- goto NEXT_CONNECTION;
+ continue;
}
}
- // a s2c connection hasn't been used in a while? Check that the client is
- // still there with a ping
+ // Verify the connection is still valid
if ((connStat[i].direction == S2C) && (timeDiff > S2C_SERVICE_FREQ) &&
(connStat[i].nToDo == 0) && (connStat[i].messageLen == 0) &&
(connStat[i].nBuffered == 0)) {
- // Log("[%s] Pinging [%s] on connection %d due to timeout", SERVER_NAME,
// connStat[i].name, i);
Header header;
header.setFlags(S2C, SUCCESS, PRIV, SIGN);
@@ -223,7 +217,6 @@ int processReception(int i) {
if (connStat[i]
.expectingHeader) { // Expecting a header read sequence was completed
- // Log("\nProcessing header reception. header.m_size: %d", header.m_size);
if (header.m_size > 0) {
connStat[i].expectingHeader = false;
connStat[i].nToDo = header.m_size;
@@ -238,8 +231,7 @@ int processReception(int i) {
return -1; // Error, signal to caller to end connection
}
} else { // expecting a data read sequence was completed
- // Log("\nProcessing data reception.");
- // printServerCommand(i);
+ // printServerCommand(i);
doServerCommand(i);
if (connStat[i].direction == C2S) { // CONNECT events for S2C can get here
connStat[i].expectingHeader = true;
@@ -281,7 +273,7 @@ void doServerCommand(int i) {
switch (header.m_command) {
case CONNECT:
- buf2i(&buf[i][HEADER_LEN], connStat[i].id);
+ BigEndianToLittle(&buf[i][HEADER_LEN], connStat[i].id);
connStat[i].direction = header.m_direction;
connStat[i].linkType = MESSAGE_LINK;
connStat[i].lastTime = currentTime;
@@ -361,7 +353,6 @@ void doServerCommand(int i) {
for (int x = 1; x <= nConns; x++) {
if (connStat[i].id == connStat[x].id) {
memcpy(connStat[x].name, username, MAX_USERNAME_LEN);
- // Log("Connection %d (%s) is loggin in.", x, connStat[x].name);
connStat[x].isLoggedIn = true;
}
}
@@ -390,7 +381,6 @@ void doServerCommand(int i) {
if (connStat[i].isLoggedIn) {
for (int x = 1; x <= nConns; x++) {
if (connStat[i].id == connStat[x].id) {
- // Log("Connection %d (user %s) is logging off.", x,
// connStat[x].name);
connStat[x].isLoggedIn = false;
foundActiveUser = true;
@@ -457,8 +447,7 @@ void doServerCommand(int i) {
break;
case SENDF: // Initial connection
case SENDF2: // Initial connection
- buf2i(&buf[i][HEADER_LEN], connStat[i].id);
- // Log("This connection's id: %d", connStat[i].id);
+ BigEndianToLittle(&buf[i][HEADER_LEN], connStat[i].id);
connStat[i].direction = header.m_direction;
connStat[i].linkType = FILE_LINK;
connStat[i].lastTime = currentTime;
@@ -491,28 +480,29 @@ void doServerCommand(int i) {
outHeader.m_command = SEND; // avoid file transfer logic on client side
memcpy(outHeader.m_name, NAME, strlen(NAME));
strcat(message, "Users must be logged in to '");
- strcat(message, com2str(header.m_command));
+ strncat(message, com2str(header.m_command), MAX_COMMAND_LEN);
strcat(message, "'. Login with '");
- strcat(message, com2str(LOGIN));
+ strncat(message, com2str(LOGIN), MAX_COMMAND_LEN);
strcat(message, " [username] [password]'.");
outHeader.m_size = strlen(message);
sendMessageToId(connStat[i].id, false, outHeader, message);
} else {
// Save the file
nextFileId = getNumEntries(FILE_DATABASE);
- if (nextFileId == -1) {
- Log("Enexpected state. %s next File Id of -1.", FILE_DATABASE);
- } else {
+ if (nextFileId == -1)
+ Log("Unexpected state. %s next File Id of -1.", FILE_DATABASE);
+ else {
snprintf(server_filename, sizeof(server_filename), "%s/%d_", FILES_DIR,
nextFileId);
offset = HEADER_LEN + 4;
- buf2i(&buf[i][offset], filenameLen); // first 4 bytes is client id
+ BigEndianToLittle(&buf[i][offset],
+ filenameLen); // first 4 bytes is client id
offset += 4;
memcpy(&server_filename[strlen(server_filename)], &buf[i][offset],
filenameLen);
memcpy(filename, &buf[i][offset], filenameLen);
offset += filenameLen;
- buf2i(&buf[i][offset], fileLen);
+ BigEndianToLittle(&buf[i][offset], fileLen);
offset += 4;
fp = fopen(FILE_DATABASE, "a");
if (fp == NULL) {
@@ -529,7 +519,7 @@ void doServerCommand(int i) {
SIGN); // will update recipient as necessary
outHeader.m_command = header.m_command;
memcpy(outHeader.m_name, connStat[i].name, strlen(connStat[i].name));
- i2buf(nextFileId, intbuf);
+ LittleEndianToBig(nextFileId, intbuf);
memcpy(message, intbuf, 4);
offset = 4;
memcpy(&message[offset], &buf[i][HEADER_LEN + 8], filenameLen);
@@ -551,7 +541,7 @@ void doServerCommand(int i) {
}
break;
case GETF:
- buf2i(&buf[i][HEADER_LEN], connStat[i].id);
+ BigEndianToLittle(&buf[i][HEADER_LEN], connStat[i].id);
// header name is origional sender. Find client's name with client id
for (int x = 1; x <= nConns; x++) {
if ((connStat[i].id == connStat[x].id) &&
@@ -561,7 +551,7 @@ void doServerCommand(int i) {
break;
}
}
- buf2i(&buf[i][HEADER_LEN + 4], fileId);
+ BigEndianToLittle(&buf[i][HEADER_LEN + 4], fileId);
connStat[i].direction = S2C;
connStat[i].linkType = FILE_LINK;
connStat[i].lastTime = currentTime;
@@ -576,7 +566,6 @@ void doServerCommand(int i) {
memcpy(filename, &buf[i][HEADER_LEN + 8], header.m_size - 8);
filenameLen = strlen(filename);
- // Log("Getting file %s", server_filename);
memset(buf[i], 0, BUF_LEN);
// Check that the file exists
@@ -591,13 +580,13 @@ void doServerCommand(int i) {
memcpy(outHeader.m_name, header.m_name,
MAX_USERNAME_LEN); // pass along origional sender
offset = HEADER_LEN;
- i2buf(filenameLen, &buf[i][offset]); // encode filename length
+ LittleEndianToBig(filenameLen, &buf[i][offset]); // encode filename length
offset += 4;
memcpy(&buf[i][offset], filename,
strlen(filename)); // encode the filename
offset += strlen(filename);
fileLen = file2buf(server_filename, &buf[i][offset + 4]);
- i2buf(fileLen, &buf[i][offset]);
+ LittleEndianToBig(fileLen, &buf[i][offset]);
offset += 4 + fileLen;
outHeader.m_size = offset - HEADER_LEN;
outHeader.encode(buf[i]);
@@ -642,7 +631,7 @@ void doServerCommand(int i) {
// PONG
break;
default:
- Log("No doServerCommand() for command %s", com2str(header.m_command));
+ Log("Unknown command: %s", com2str(header.m_command));
}
}
@@ -697,10 +686,7 @@ void prepareMessage(int i, Header header, char *message) {
if (connStat[i].direction == S2C) {
int offset = connStat[i].nBuffered;
- // Log("Preparing message for connection %d", i);
// header.displayContents(true);
- // Log("Message: |%s|", message);
- // Log("offset: %d, nBuffered: %d, nToDo: %d: messageLen: %d", offset,
// connStat[i].nBuffered, connStat[i].nToDo, connStat[i].messageLen);
header.encode(&buf[i][offset]);