diff options
author | Matt Strapp <matt@mattstrapp.net> | 2022-04-25 19:09:12 -0500 |
---|---|---|
committer | Matt Strapp <matt@mattstrapp.net> | 2022-04-25 19:21:02 -0500 |
commit | 51247ec41133e04e0b0ee04cbf2e290ff26be03d (patch) | |
tree | bfca271e8365c624cc90695343530179a39e899c /src | |
parent | e (diff) | |
download | csci4211-51247ec41133e04e0b0ee04cbf2e290ff26be03d.tar csci4211-51247ec41133e04e0b0ee04cbf2e290ff26be03d.tar.gz csci4211-51247ec41133e04e0b0ee04cbf2e290ff26be03d.tar.bz2 csci4211-51247ec41133e04e0b0ee04cbf2e290ff26be03d.tar.lz csci4211-51247ec41133e04e0b0ee04cbf2e290ff26be03d.tar.xz csci4211-51247ec41133e04e0b0ee04cbf2e290ff26be03d.tar.zst csci4211-51247ec41133e04e0b0ee04cbf2e290ff26be03d.zip |
Signed-off-by: Matt Strapp <matt@mattstrapp.net>
Diffstat (limited to 'src')
-rw-r--r-- | src/client.cpp | 71 | ||||
-rw-r--r-- | src/comms.cpp | 21 | ||||
-rw-r--r-- | src/server.cpp | 86 | ||||
-rw-r--r-- | src/util.cpp | 15 |
4 files changed, 74 insertions, 119 deletions
diff --git a/src/client.cpp b/src/client.cpp index d2b7562..b6f49b5 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1,3 +1,6 @@ +// Author Name: Matt Strapp +// Date: 25 April 2022 +// x500: strap012 #include "client.hpp" // Connection 0: Client to Server data communication @@ -31,7 +34,6 @@ void RemoveConnection(int i) { memmove(buf + i, buf + i + 1, (nConns - i) * sizeof(BYTE *)); } nConns--; - // Log("[%s] %d active connections after removal", SERVER_NAME, nConns); } int Send_Blocking(int sockFD, const BYTE *data, int len) { @@ -65,7 +67,6 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { struct timeb currentTime; ftime(¤tTime); id = currentTime.time ^ (((int)getpid() & 0x0000FFFF) << 16); - // Log("My identifier is %d (%08X)", id, id); // create the data communication sockets for (int i = 0; i < 2; i++) { @@ -75,7 +76,7 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { if (connect(fd, (const struct sockaddr *)&serverAddr, sizeof(serverAddr)) != 0) { - Error("C2s cannot connect to server %s:%d.", svrIP, svrPort); + Error("Cannot connect to server %s:%d.", svrIP, svrPort); } enum Direction direction; @@ -93,7 +94,7 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { memcpy(header.m_name, "--------", MAX_USERNAME_LEN); header.m_size = 4; header.encode(connectBuf); - i2buf(id, connectBuf + HEADER_LEN); + LittleEndianToBig(id, connectBuf + HEADER_LEN); // header.displayContents(true); if (Send_Blocking(fd, connectBuf, HEADER_LEN + header.m_size) < 0) { @@ -116,7 +117,6 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { connStat[i].messageLen = 0; if (i == S2C_DATA) { - // Log("S2C_DATA is setting POLLRDNORM"); connStat[i].expectingHeader = true; connStat[i].nToDo = HEADER_LEN; peers[i].events |= POLLRDNORM; // S2C reads @@ -150,7 +150,7 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { if (fgets(line, LINE_SIZE, fp) == NULL) { Log("End of command file."); endOfFile = true; - goto DO_POLL; + exit(0); } new_line_pos = strchr(line, '\n'); @@ -264,10 +264,10 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { } offset = 0; - i2buf(id, &stagingBuf[offset]); // stagingBuf skips header - offset += 4; // unique id + LittleEndianToBig(id, &stagingBuf[offset]); // stagingBuf skips header + offset += 4; // unique id filenameLen = strlen(tok); - i2buf(filenameLen, &stagingBuf[offset]); + LittleEndianToBig(filenameLen, &stagingBuf[offset]); offset += 4; // int length of file name memcpy(&stagingBuf[offset], tok, strlen(tok)); @@ -276,7 +276,7 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { nbytes = file2buf(tok, &stagingBuf[offset + 4]); if (nbytes == -1) break; - i2buf(nbytes, &stagingBuf[offset]); + LittleEndianToBig(nbytes, &stagingBuf[offset]); offset += 4; // file size int offset += nbytes; // file length (offest now tells how many bytes to // copy into buf) @@ -294,8 +294,8 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { Log("Cannot connect to server for file transfer"); } else { if (command == SENDF) - Log("[%s] Sending %s to everyone currently logged in.", - NAME, tok); + Log("[%s] Sending %s to everyone currently logged in.", NAME, + tok); else Log("[%s] Sending %s to %s.", NAME, tok, header.m_name); @@ -323,46 +323,39 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { free(stagingBuf); } - DO_POLL: int nReady = poll(peers, nConns, POLL_TIMEOUT); - // Log("nReady %d", nReady); if (nConns == 0) { - Log("[%s] The server has gone offline.", NAME); + Log("[%s] The server has gone offline, exiting.", NAME); return; } for (int i = 0; i < nConns; i++) { // Handle reading data (s2c) if (peers[i].revents & (POLLRDNORM | POLLERR | POLLHUP)) { - // Log("Recieving nonblocking!"); if (connStat[i].direction == S2C) { if (Recv_NonBlocking(peers[i].fd, buf[i], &connStat[i], &peers[i]) < 0) { - // Log("Line %d: Removing connection %d", __LINE__, i); RemoveConnection(i); - goto NEXT_CONNECTION; + continue; } if (connStat[i].nToDo == 0) { if (processReception(i) != 0) { - // Log("Line %d: Removing connection %d", __LINE__, i); RemoveConnection(i); } - goto NEXT_CONNECTION; + continue; } } } // Handle sending data (c2s) if (peers[i].revents & (POLLWRNORM | POLLERR | POLLHUP)) { - // Log("Sending nonblocking!"); if (connStat[i].direction == C2S) { if (Send_NonBlocking(peers[i].fd, buf[i], &connStat[i], &peers[i]) < 0) { - // Log("Line %d: Removing connection %d", __LINE__, i); RemoveConnection(i); - goto NEXT_CONNECTION; + continue; } } } @@ -373,7 +366,6 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { C2S_SERVICE_FREQ) && (connStat[i].nToDo == 0) && (connStat[i].messageLen == 0) && (connStat[i].nBuffered == 0)) { - // Log("[%s] Pinging server on connection %d due to timeout", // SERVER_NAME, i); Header header; header.setFlags(C2S, SUCCESS, PRIV, SIGN); @@ -388,9 +380,6 @@ void DoClient(const char *svrIP, int svrPort, const char *fileName) { peers[i].events |= POLLWRNORM; } - - NEXT_CONNECTION: - asm("nop"); } } @@ -411,16 +400,14 @@ int processReception(int i) { Header header; header.decode(buf[i]); - if (connStat[i] - .expectingHeader) { // Expecting a header read sequence was completed - // Log("\nProcessing header reception."); - // header.displayContents(true); + if (connStat[i].expectingHeader) { // Expecting a header read sequence was + // completed header.displayContents(true); if (header.m_size > 0) { connStat[i].expectingHeader = false; connStat[i].nToDo = header.m_size; } else if (header.m_size == 0) { // printMessage(i); - doClientCommand(i); + DoCommand(i); connStat[i].expectingHeader = true; connStat[i].nBuffered = 0; connStat[i].nToDo = HEADER_LEN; @@ -429,9 +416,8 @@ 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."); - // printMessage(i); - doClientCommand(i); + // printMessage(i); + DoCommand(i); connStat[i].expectingHeader = true; connStat[i].nBuffered = 0; connStat[i].nToDo = HEADER_LEN; @@ -439,7 +425,6 @@ int processReception(int i) { } if (connStat[i].shouldClose) { - // Log("[%s] Line %d: Removing connection %d due to shouldClose", SERVER_NAME, // __LINE__, i); RemoveConnection(i); } @@ -447,7 +432,7 @@ int processReception(int i) { return 0; } -void doClientCommand(int i) { +void DoCommand(int i) { Header header; header.decode(buf[i]); @@ -476,7 +461,7 @@ void doClientCommand(int i) { case SENDF2: // "[person] File transfer: <file name>" // First 4 bytes are file id - buf2i(&buf[i][HEADER_LEN], file_id); + BigEndianToLittle(&buf[i][HEADER_LEN], file_id); if (header.m_command == SENDF) Log("[%s] Initiating file transfer: %s", header.m_name, &buf[i][HEADER_LEN + 4]); @@ -520,7 +505,7 @@ void doClientCommand(int i) { 4 + header.m_size; // [client id] [file id] [file name] outHeader.encode(buf[nConns - 1]); offset = HEADER_LEN; - i2buf(id, buf[nConns - 1] + offset); // encode client id + LittleEndianToBig(id, buf[nConns - 1] + offset); // encode client id offset += 4; memcpy(&buf[nConns - 1][offset], &buf[i][HEADER_LEN], header.m_size); // encode file id and file name @@ -535,9 +520,9 @@ void doClientCommand(int i) { break; case GETF: if (header.m_flag == SUCCESS) { - buf2i(&buf[i][HEADER_LEN], filenameLen); + BigEndianToLittle(&buf[i][HEADER_LEN], filenameLen); memcpy(filename, &buf[i][HEADER_LEN + 4], filenameLen); - buf2i(&buf[i][HEADER_LEN + 4 + filenameLen], fileLen); + BigEndianToLittle(&buf[i][HEADER_LEN + 4 + filenameLen], fileLen); buf2file(&buf[i][HEADER_LEN + 4 + filenameLen + 4], fileLen, filename); if (header.m_recipient == PUB) Log("[%s] File transfer complete: %s", header.m_name, filename); @@ -552,12 +537,10 @@ void doClientCommand(int i) { connStat[i].shouldClose = true; break; case PING: - // Log("Ping from %s", header.m_name); asm("nop"); // do nothing break; default: - Log("[%s] No doClientCommand() for %s", NAME, - com2str(header.m_command)); + Log("[%s] No DoCommand() for %s", NAME, com2str(header.m_command)); } } diff --git a/src/comms.cpp b/src/comms.cpp index 14fcd61..7661155 100644 --- a/src/comms.cpp +++ b/src/comms.cpp @@ -1,3 +1,6 @@ +// Author Name: Matt Strapp +// Date: 25 April 2022 +// x500: strap012 #include "comms.hpp" int Send_NonBlocking(int sockFD, BYTE *data, struct CONN_STAT *pStat, @@ -8,17 +11,14 @@ int Send_NonBlocking(int sockFD, BYTE *data, struct CONN_STAT *pStat, ftime(&(pStat->lastTime)); int n = send(sockFD, data + pStat->messageLen - pStat->nToDo, pStat->nToDo, 0); - // Log("Send_NonBlocking n: %d", n); if (n >= 0) { pStat->nToDo -= n; } else if (n < 0 && (errno == ECONNRESET || errno == EPIPE)) { - // Log("Connection closed."); close(sockFD); return -1; } else if (n < 0 && (errno == EWOULDBLOCK)) { // The socket becomes non-writable. Exit now to prevent blocking. // OS will notify us when we can write - // Log("Writing would block. Waiting for OS to notify send."); pPeer->events |= POLLWRNORM; return 0; } else { @@ -26,7 +26,6 @@ int Send_NonBlocking(int sockFD, BYTE *data, struct CONN_STAT *pStat, } } - // Log("Message was completely written out (messagelen was message written // out)"); Log("What's currently in there: nBuffered %d, messageLen: %d, nToDo // %d", pStat->nBuffered, pStat->messageLen, pStat->nToDo); memcpy(data, data + pStat->messageLen, BUF_LEN - pStat->messageLen); // Good @@ -36,22 +35,18 @@ int Send_NonBlocking(int sockFD, BYTE *data, struct CONN_STAT *pStat, if (pStat->nBuffered > 0) { // start off the next send if one is queued Header nextHeader; nextHeader.decode(data); - // Log("Another message is queued of size 13 + %d", nextHeader.m_size); pStat->messageLen = HEADER_LEN + nextHeader.m_size; pStat->nToDo = HEADER_LEN + nextHeader.m_size; } else { - // Log("No other messages are queued"); pStat->messageLen = 0; pStat->nToDo = 0; pPeer->events &= ~POLLWRNORM; // no more bytes to send? Stop listening for // the writable cue. } - // Log("What's in there now: nBuffered %d, messageLen: %d, nToDo %d", // pStat->nBuffered, pStat->messageLen, pStat->nToDo); - // Change the connection from a sender to a reciever (used by client) + // Change the connection from a sender to a receiver (used by client) if (pStat->changeDirection && (pStat->nToDo == 0)) { - // Log("Changing direction!: nBuffered: %d", pStat->nBuffered); pStat->direction = (pStat->direction == C2S) ? S2C : C2S; pStat->expectingHeader = true; pStat->nToDo = HEADER_LEN; @@ -71,16 +66,13 @@ int Recv_NonBlocking(int sockFD, BYTE *data, struct CONN_STAT *pStat, // pStat->nToDo %d",pStat->nToDo); while (pStat->nToDo > 0) { int n = recv(sockFD, data + pStat->nBuffered, pStat->nToDo, 0); - // Log("Rev_NonBlocking recieved %d bytes", n); if (n > 0) { pStat->nBuffered += n; pStat->nToDo -= n; } else if (n == 0 || (n < 0 && errno == ECONNRESET)) { - // Log("\nConnection closed. n = %d", n); close(sockFD); return -1; } else if (n < 0 && (errno == EWOULDBLOCK)) { - // Log("\nWould block waiting for read..."); // The socket becomes non-readable. Exit now to prevent blocking. // OS will notify us when we can read return 0; @@ -125,8 +117,7 @@ void Header::encode(BYTE *buf) { memcpy(&buf[index], m_name, 8); index += 8; - // NOTE: There was a problem encoding very large sizes. - i2buf(m_size, &buf[index]); + LittleEndianToBig(m_size, &buf[index]); index += 4; } @@ -144,7 +135,7 @@ void Header::decode(BYTE *buf) { for (int x = 0; x < MAX_USERNAME_LEN; x++) m_name[x] = buf[index++]; - buf2i(&buf[index], m_size); + BigEndianToLittle(&buf[index], m_size); index += 4; } 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]);
diff --git a/src/util.cpp b/src/util.cpp index dc95441..e6f0902 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1,11 +1,6 @@ -#include <cstddef> -#include <stdarg.h> -#include <stdio.h> -#include <string.h> -#include <string> -#include <sys/timeb.h> -#include <time.h> - +// Author Name: Matt Strapp +// Date: 25 April 2022 +// x500: strap012 #include "util.hpp" #define VALID_CHARACTERS \ @@ -234,7 +229,7 @@ void Log(const char *format, ...) { } // convert buf[0:3] to int -void buf2i(BYTE *buf, int &i) { +void BigEndianToLittle(BYTE *buf, int &i) { // No way to avoid Segmentation fault. Some bytes may be 0 i = 0; i += buf[3]; @@ -244,7 +239,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 LittleEndianToBig(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); |