-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
536 lines (462 loc) · 17.6 KB
/
Copy pathserver.cpp
File metadata and controls
536 lines (462 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include <string>
#include <string.h>
#include <bitset>
#include <thread>
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unordered_map>
#include <fcntl.h>
#include <netdb.h>
#include <chrono>
#include <ctime>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include "server.hpp"
#include "constants.hpp"
#include "utilities.hpp"
#include "tcp.hpp"
// SERVER IMPLEMENTATION
// CONSTRUCTORS
Server::Server(char *port, std::string saveFolder)
{
m_folderName = saveFolder;
struct addrinfo hints, *myAddrInfo, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; // set to AF_INET to use IPv4
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE; // use my IP
int ret;
if ((ret = getaddrinfo(NULL, port, &hints, &myAddrInfo)) != 0)
{
perror("getaddrinfo");
exit(1);
}
for (p = myAddrInfo; p != NULL; p = p->ai_next)
{
if ((m_sockFd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
{
perror("listener: socket");
continue;
}
if (bind(m_sockFd, p->ai_addr, p->ai_addrlen) == -1)
{
close(m_sockFd);
perror("listener: bind");
continue;
}
break;
}
if (p == NULL)
{
perror("listener: failed to bind socket");
exit(1);
}
}
Server::~Server()
{
}
void Server::run()
{
handleConnection();
}
/**
* @brief Enumerate through timers and if any timer has timed out, release resources for that connection
*
*/
void Server::closeTimedOutConnectionsAndRetransmitFIN()
{
for (auto it = m_connectionIdToTCB.begin(); it != m_connectionIdToTCB.end(); it++)
{
// close connection if connection inactive for 10s
if (!checkTimer(it->first, CONNECTION_TIMEOUT)) // check timer by connection id
{ // timer run out
closeConnection(it->first);
}
// retransmit fin packet if ACK not received after server FIN-ACK
else if (it->second->connectionState == FIN_RECEIVED && !checkTimer(it->first, RETRANSMISSION_TIMEOUT))
{
sendPacket(it->second->clientInfo, it->second->clientInfoLen, it->second->finPacket);
setTimer(it->first);
}
}
}
/**
* @brief Shift the window a certain number of bytes ahead for a connection
*
* @param id connection id
* @param bytes number of bytes to shift winodw by
*/
void Server::moveWindow(int connId, int bytes)
{
using namespace std;
vector<char> &connectionBuffer = m_connectionIdToTCB[connId]->connectionBuffer;
bitset<RWND_BYTES> &connectionBitset = m_connectionIdToTCB[connId]->connectionBitvector;
if (bytes >= (int)connectionBuffer.size())
return;
for (int i = 0; i < RWND_BYTES - bytes; i++) // last value of i will be RWND_BYTES - bytesToWriite - 1
{
connectionBuffer[i] = connectionBuffer[i + bytes];
connectionBitset[i] = connectionBitset[i + bytes];
}
for (int i = RWND_BYTES - bytes; i < RWND_BYTES; i++)
{
connectionBuffer[i] = 0;
connectionBitset[i] = 0;
}
}
/**
* @brief Function that handles all of the incoming connections
*
*/
void Server::handleConnection()
{
using namespace std;
while (true) // since server will run indefinitely, and we're not using multithreading/forking
{
closeTimedOutConnectionsAndRetransmitFIN(); // check and close any timed out connection every iteration
// prepare to read incoming packet
char packetBuffer[MAX_PACKET_LENGTH + 1]; // last byte nullbyte
struct sockaddr clientInfo; // needed to send response
socklen_t clientInfoLen = sizeof(clientInfo);
int bytesRead = recvfrom(m_sockFd, packetBuffer, MAX_PACKET_LENGTH, 0, &clientInfo, &clientInfoLen);
packetBuffer[MAX_PACKET_LENGTH] = 0; // mark the end with a null byte
// convert C string to std::string
std::string packet = convertCStringtoStandardString(packetBuffer, bytesRead);
TCPPacket *p = new TCPPacket(packet); // create new packet from string
int packetConnId = p->getConnId(); // get the connection ID of the packet
/* everything will go through addNewConnection and handlefIN as if the packet is
relevant to them they will update connection state */
packetConnId = addNewConnection(p, &clientInfo, clientInfoLen);
bool finHandled = handleFin(p, packetConnId);
// if packet is not in map then discard it
if (m_connectionIdToTCB.find(packetConnId) == m_connectionIdToTCB.end())
{
// there should be no other reason for us to find a SYN unless we're not adding a new connection
// do nothing, packet will be deleted
}
else if (!finHandled)
{
// set timer for packets to detect 10s inactivity of connection
setTimer(packetConnId);
int returnValue = addPacketToBuffer(packetConnId, p);
flushBuffer(packetConnId);
// check if the a SYN-ACK needs to be sent
bool synFlag = false;
if (m_connectionIdToTCB[packetConnId]->connectionState == AWAITING_ACK)
{
synFlag = true;
}
bool isDup = m_connectionIdToTCB[packetConnId]->connectionExpectedSeqNum == m_connectionIdToTCB[packetConnId]->previousExpectedSeqNum;
m_connectionIdToTCB[packetConnId]->previousExpectedSeqNum = m_connectionIdToTCB[packetConnId]->connectionExpectedSeqNum;
bool isDropped = returnValue == PACKET_DROPPED;
printPacket(p, true, isDropped, false); // for receipt of the packet receive
if (returnValue == PACKET_ADDED || returnValue == PACKET_DUPLICATE || returnValue == PACKET_DROPPED)
{
// if reached this block, then packet was valid and ACK should be sent
TCPPacket *ackPacket = new TCPPacket(
m_connectionIdToTCB[packetConnId]->connectionServerSeqNum, // sequence number
m_connectionIdToTCB[packetConnId]->connectionExpectedSeqNum, // ack number
packetConnId, // connection id
true, // is an ACK
synFlag, // decided by synFlag
false, // is not FIN
0, // no payload
"");
if (synFlag)
++m_connectionIdToTCB[packetConnId]->connectionServerSeqNum;
sendPacket(&clientInfo, clientInfoLen, ackPacket);
printPacket(ackPacket, false, false, isDup); // for receipt of the packet send
delete ackPacket;
ackPacket = nullptr;
}
}
// delete the packet
delete p;
p = nullptr;
}
}
/**
* @brief Adds packet to the buffer
*
* @param p pointer to the TCPPacket
* @return True if packet was successfully added to the buffer, false if no space was available,the pointer was nullptr, or it was a duplicate
*/
int Server::addPacketToBuffer(int connId, TCPPacket *p)
{
/*
Several implementations could have been used here in the case that we
receive a packet that has fills a gap AND also overwrites on either side.
One approach would be that if any byte in the range of the packet in the window
has already been written to then drop the packet, removing the chance of an overwrite.
However, the counter argument to that was -- what's the guarantee that the first packet
I got was not corrupted (since such a case would only arise in the case of a corruption)
Thus, the current implementation is that if there is an overlap in bits, overwrite.
Packets that have overlapping bytes should result in undefined behavior, which is
exactly what this implementation would provide, since the order of arrival of the
packets is not definite, and the result is therefore indeterminate.
*/
using namespace std;
if (p == nullptr)
return PACKET_NULL;
if (p->isSYN())
return PACKET_ADDED;
vector<char> &connectionBuffer = m_connectionIdToTCB[connId]->connectionBuffer;
bitset<RWND_BYTES> &connectionBitset = m_connectionIdToTCB[connId]->connectionBitvector;
int nextExpectedSeqNum = m_connectionIdToTCB[connId]->connectionExpectedSeqNum;
int packetSeqNum = p->getSeqNum();
int payloadLen = p->getPayloadLength();
string payloadBuffer = p->getPayload();
if (p->isFIN() || m_connectionIdToTCB[connId]->connectionState == FIN_RECEIVED)
return PACKET_DROPPED;
/*
HANDLING OF WRAP AROUND:
The only time we will see a wrap around in action is if the the sequence number of the packet arrived
is less than the nextExpectedSequnceNumber
What about the case if there is a wrap around of the sequence number within the packet's payload?
We don't care; the nextExpectedSeqNum would be an offset such that all the payload should have space.
If that runs out of bounds, we should DROP the packet, not accomodate for it by wrapping out in the buffer.
We NEVER wrap around in index to the start of the buffer. We ONLY wrap around in terms of the next packet's sequence number.
In such a case that there is a wrap around, then for all we are concerned, we only need to shift the offset
This logic sets the code as follows:
*/
int offset = packetSeqNum - nextExpectedSeqNum;
if (packetSeqNum < nextExpectedSeqNum)
{
offset = MAX_SEQ_NUM + packetSeqNum - nextExpectedSeqNum + 1;
/*
RWND_BYTES + packetSeqNum => shift the packets sequence number as if it is moving beyond the maximum sequence number
- nextExpectedSeqNum => shift back based on the base offset
*/
}
// when to drop?
// we have an adjusted base offset, all we have to see now is if it runs above or below bounds
// (because RWND_BYTES + packetSeqNum - nextExpectedSeqNum) can be less than 0 or it can go beyond the buffer
// a neat way to think of offset is an "adjusted sequence number"
if (offset + payloadLen > RWND_BYTES)
return PACKET_DROPPED;
for (int i = 0; i < payloadLen; i++)
{
connectionBuffer[offset + i] = payloadBuffer[i];
connectionBitset[offset + i] = 1; // now mark as used, regardless of overwrite
}
return PACKET_ADDED;
}
/**
* @brief Writes consecutive packets that have arrived in the buffer from the start, updates next expected sequence number, and initializes next slots in buffer to nullptr
*
* @return number of bytes that were written
*/
int Server::flushBuffer(int connId)
{
using namespace std;
vector<char> &connectionBuffer = m_connectionIdToTCB[connId]->connectionBuffer;
bitset<RWND_BYTES> &connectionBitset = m_connectionIdToTCB[connId]->connectionBitvector;
int &nextExpectedSeqNum = m_connectionIdToTCB[connId]->connectionExpectedSeqNum;
// find the number of bytes to write
int bytesToWrite = 0;
for (; bytesToWrite < RWND_BYTES && connectionBitset[bytesToWrite] == 1; bytesToWrite++)
;
char *outputBuffer = new char[bytesToWrite + 1];
std::copy(connectionBuffer.begin(), connectionBuffer.begin() + bytesToWrite, outputBuffer);
outputBuffer[bytesToWrite] = 0; // just for safety
writeToFile(connId, outputBuffer, bytesToWrite);
nextExpectedSeqNum += bytesToWrite; // update the next expected sequence number
nextExpectedSeqNum %= MAX_SEQ_NUM + 1;
delete[] outputBuffer;
outputBuffer = nullptr;
moveWindow(connId, bytesToWrite);
return bytesToWrite;
}
/**
* @brief Adds a new connection and sets the correct connection State
*/
int Server::addNewConnection(TCPPacket *p, sockaddr *clientInfo, socklen_t clientInfoLen)
{
if (p->isSYN() && p->getConnId() == 0) // new connection id
{
// Get the connection ID
int packetConnId = m_nextAvailableConnectionId;
// create an output file
// TODO: assumed existance of save directory
std::string pathName = m_folderName + "/" + std::to_string(packetConnId) + ".file";
int fd = open(pathName.c_str(), O_CREAT | O_WRONLY, 0644);
// set up TCB and start timer
m_connectionIdToTCB[packetConnId] = new TCB(p->getSeqNum() + 1, fd, ConnectionState::AWAITING_ACK, true, clientInfo, clientInfoLen); // +1 in constructer as SYN == 1byte
m_connectionIdToTCB[packetConnId]->clientInfo = clientInfo;
m_connectionIdToTCB[packetConnId]->clientInfoLen = clientInfoLen;
m_connectionIdToTCB[packetConnId]->connectionFileDescriptor = fd;
setTimer(packetConnId);
++m_nextAvailableConnectionId; // update the next available connection Id
return packetConnId;
}
// Update Connection state in case of an ACK
else if (p->isACK() && m_connectionIdToTCB[p->getConnId()]->connectionState == AWAITING_ACK) // new connection id
{
m_connectionIdToTCB[p->getConnId()]->connectionState = ConnectionState::CONNECTION_SET;
return p->getConnId();
}
return p->getConnId();
}
/**
* @brief close connection and remove entry from unordered map
*/
void Server::closeConnection(int connId)
{
// delete TCB Block
delete m_connectionIdToTCB[connId];
m_connectionIdToTCB[connId] = nullptr;
m_connectionIdToTCB.erase(connId);
}
/**
* @brief set timer by saving current timestamp
*/
void Server::setTimer(int connId)
{
m_connectionIdToTCB[connId]->connectionTimer = std::chrono::system_clock::now();
}
/**
* @brief check timer by comparing present timestamp with timerLimit
*/
bool Server::checkTimer(int connId, float timerLimit)
{
c_time current_time = std::chrono::system_clock::now();
c_time start_time = m_connectionIdToTCB[connId]->connectionTimer;
std::chrono::duration<double> elapsed_time = current_time - start_time;
int elapsed_seconds = elapsed_time.count();
return (elapsed_seconds < timerLimit);
}
/**
* @brief handleFin
* @return boolean if fin was handled or not
*/
bool Server::handleFin(TCPPacket *p, int connId)
{
if (p->getSeqNum() < m_connectionIdToTCB[connId]->connectionExpectedSeqNum)
return false;
// if fin packet update state and send fin from server
else if (p->isFIN())
{
// output the packet
bool duplicate = false;
if (m_connectionIdToTCB[connId]->connectionState == ConnectionState::FIN_RECEIVED)
{
// the FIN-ACK was already sent, but I got it again
duplicate = true;
}
printPacket(p, true, false, false);
// change state to FIN_RECEIVED -> wait for ACK for FIN-ACK
m_connectionIdToTCB[connId]->connectionState = ConnectionState::FIN_RECEIVED;
m_connectionIdToTCB[connId]->connectionExpectedSeqNum = (p->getSeqNum() + 1) % (MAX_SEQ_NUM + 1);
TCPPacket *finPacket = new TCPPacket(
m_connectionIdToTCB[connId]->connectionServerSeqNum, // sequence number
m_connectionIdToTCB[connId]->connectionExpectedSeqNum, // ack number
connId, // connection id
true, // is ACK
false, // is not SYN
true, // is FIN
0, // no payload
"");
sendPacket(m_connectionIdToTCB[connId]->clientInfo, m_connectionIdToTCB[connId]->clientInfoLen, finPacket);
// save the FIN packet
m_connectionIdToTCB[connId]->finPacket = finPacket;
printPacket(finPacket, false, false, duplicate);
setTimer(connId); // set timer
return true;
}
/*
If Ack recieved and the connection state is awaiting for ack then 4 way handwave
complete and so close connection
*/
else if (p->isACK() && m_connectionIdToTCB[p->getConnId()]->connectionState == FIN_RECEIVED)
{
// write out the packet
printPacket(p, true, false, false);
// assuming that the received expected sequence number is the same as the one received
closeConnection(p->getConnId());
return true;
}
return false;
}
void Server::outputToStdout(std::string message)
{
std::cout << message << std::endl;
}
void Server::outputToStderr(std::string message)
{
std::cerr << message << std::endl;
}
int Server::sendPacket(sockaddr *clientInfo, int clientInfoLen, TCPPacket *p)
{
int packetLength;
char *packetCString;
int bytesSent;
packetCString = p->getCString(packetLength);
if (p == nullptr)
{
return -1;
}
if ((bytesSent = sendto(m_sockFd, packetCString, packetLength, 0, clientInfo, clientInfoLen) == -1))
{
std::string errorMessage = "Packet send Error: " + std::string(strerror(errno));
outputToStderr(errorMessage);
}
return bytesSent;
}
int Server::writeToFile(int connId, char *message, int len)
{
TCB *currentBlock = m_connectionIdToTCB[connId];
int fd = currentBlock->connectionFileDescriptor;
int bytesWrote;
if ((bytesWrote = write(fd, message, len)) == -1)
{
std::string errorMessage = "File write Error: " + std::string(strerror(errno));
outputToStderr(errorMessage);
return -1;
}
return bytesWrote;
}
void Server::printPacket(TCPPacket *p, bool recvd, bool dropped, bool dup)
{
std::string message;
if (recvd)
message = "RECV";
else
message = "SEND";
if (recvd && dropped)
message = "DROP";
message = message + " " + std::to_string(p->getSeqNum()) + " " + std::to_string(p->getAckNum()) + " " + std::to_string(p->getConnId()) + " ";
if (p->isACK())
message += "ACK ";
if (p->isSYN())
message += "SYN ";
if (p->isFIN())
message += "FIN ";
if (dup && !recvd)
message += "DUP ";
message.pop_back(); // remove the trailing space
outputToStdout(message);
}
int main(int argc, char *argv[])
{
using namespace std;
if (argc != 3)
{
cerr << "ERROR: Incorrect number of arguments provided!" << endl;
exit(1);
}
if (!(atoi(argv[1]) && strcmp(argv[1], "0")))
{
cerr << "ERROR: Incorrect format of ports provided" << endl;
exit(1); //TODO: need to change and implement the exact exit functions with different exit codes.
}
if (atoi(argv[1]) > 65535 || atoi(argv[1]) < 0)
{
cerr << "ERROR: Port invalid" << endl;
exit(1);
}
Server server(argv[1], argv[2]);
server.run();
}