-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.cpp
267 lines (229 loc) · 8.84 KB
/
server.cpp
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
/* A simple server in the internet domain using TCP
The port number is passed as an argument
This version runs forever, forking off a separate
process for each connection
*/
#include <stdio.h>
#include <sys/types.h> // definitions of a number of data types used in socket.h and netinet/in.h
#include <sys/socket.h> // definitions of structures needed for sockets, e.g. sockaddr
#include <netinet/in.h> // constants and structures needed for internet domain addresses, e.g. sockaddr_in
#include <stdlib.h>
#include <strings.h>
#include <sys/wait.h> /* for the waitpid() system call */
#include <signal.h> /* signal name macros, and the kill() prototype */
#include <unistd.h>
#include <sys/time.h>
#include <iostream>
#include <sys/fcntl.h>
#include <errno.h>
#include "Packet.h"
using namespace std;
int eofPosition = -1;
void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
void error(char *msg)
{
perror(msg);
exit(1);
}
char*
file_to_str(FILE* file, int* len) {
fseek(file, 0L, SEEK_END);
*len = ftell(file);
fseek(file, 0L, SEEK_SET);
char* fileContents = (char*)malloc(*len+1);
bzero(fileContents, *len+1);
fread(fileContents, 1, *len, file);
return fileContents;
}
bool
send_pkt_with_seq_num(int seqNum, char* fileContents, int fileLength, int sockfd, struct sockaddr_in cliAddr, int windowEnd) {
if (seqNum + DATA_LEN > fileLength && fileLength <= windowEnd) {
eofPosition = fileLength;
Packet pkt(seqNum, EOF_PACKET, fileContents + seqNum, fileLength - seqNum);
printf("Sending data packet with SEQUENCE NUMBER: %d\n", pkt.getSeqNum());
int serializedLength;
char* buffer = pkt.serialize(&serializedLength);
int err = sendto(sockfd, (void*)buffer, serializedLength, 0, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
free(buffer);
if (err < 0) {
error("ERROR on send_pkt_with_seq_num");
}
return false;
}
else {
int pktLength;
if (seqNum + DATA_LEN > windowEnd)
pktLength = windowEnd - seqNum;
else
pktLength = DATA_LEN;
Packet pkt(seqNum, DATA_PACKET, fileContents + seqNum, pktLength);
printf("Sending data packet with SEQUENCE number: %d\n", pkt.getSeqNum());
int serializedLength;
char* buffer = pkt.serialize(&serializedLength);
int err = sendto(sockfd, (void*)buffer, serializedLength, 0, (struct sockaddr *) &cliAddr, sizeof(cliAddr));
free(buffer);
if (err < 0) {
error("ERROR on send_pkt_with_seq_num");
}
}
return true;
}
int main(int argc, char *argv[])
{
srand(time(0));
int sockfd, newsockfd, portno, pid;
socklen_t clilen;
struct sockaddr_in serv_addr, cliAddr;
struct sigaction sa; // for signal SIGCHLD
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
fcntl(sockfd, F_SETFL, O_NONBLOCK);
if (sockfd < 0) {
error("ERROR opening socket");
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
error("ERROR on binding");
}
clilen = sizeof(cliAddr);
/****** Kill Zombie Processes ******/
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
/*********************************/
char* fileContents;
int fileLength;
int windowStart = 0;
int currentClientPort = -1;
unsigned long currentClientIp = -1;
struct timeval timeVal;
long t;
while (1) {
char packetData[2048];
int packetDataLength = recvfrom(sockfd, packetData, 2048, 0, (struct sockaddr *) &cliAddr, &clilen);
if (packetDataLength < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
error("ERROR on recvfrom");
}
gettimeofday(&timeVal, NULL);
long now = timeVal.tv_usec/1000 + timeVal.tv_sec*1000;
if (currentClientPort != -1 && now - t > TIMEOUT) {
printf("TIMEOUT on ACK\n");
int stop;
if (eofPosition != -1)
stop = eofPosition;
else
stop = windowStart + WINDOW_SIZE;
t = now;
for (int i = windowStart; i < stop; i+=DATA_LEN) {
printf("RETRANSMISSION: ");
if (!send_pkt_with_seq_num(i, fileContents, fileLength, sockfd, cliAddr, stop))
break;
}
}
continue;
}
Packet rcvdPacket(packetData, packetDataLength);
// simulate corruption and packet loss
int r = (rand() % 100) + 1;
if (r <= PROBABILITY_PACKET_LOST * 100) {
continue;
}
r = (rand() % 100) + 1;
if (r <= PROBABILITY_PACKET_CORRUPT * 100) {
rcvdPacket.setAckNum(rcvdPacket.getAckNum() + 1);
}
if (rcvdPacket.isCorrupt()) {
printf("corrupt\n");
continue;
}
// potentially need to handle receiving a request packet in the middle of handling a request
if (rcvdPacket.isRequest() && currentClientPort == -1) {
string filePath(rcvdPacket.getData(), rcvdPacket.getDataLen());
printf("File Path: %s\n", filePath.c_str());
FILE* file = fopen(filePath.c_str(), "rb");
if (!file) {
// FILE NOT FOUND
Packet responsePacket(0, NOT_FOUND_PACKET, NULL, 0);
int serializedLength;
char* buffer = responsePacket.serialize(&serializedLength);
int err = sendto(sockfd, (void*)buffer, serializedLength, 0, (struct sockaddr *) &cliAddr, clilen);
if (err < 0)
error("ERROR on FNF sendto");
free(buffer);
continue;
}
currentClientPort = htons(cliAddr.sin_port);
currentClientIp = cliAddr.sin_addr.s_addr;
fileContents = file_to_str(file, &fileLength);
fclose(file);
printf("File Length: %d\n", fileLength);
gettimeofday(&timeVal, NULL);
t = timeVal.tv_usec/1000 + timeVal.tv_sec*1000;
for (int i = 0; i < WINDOW_SIZE; i+=DATA_LEN) {
if (!send_pkt_with_seq_num(i, fileContents, fileLength, sockfd, cliAddr, windowStart + WINDOW_SIZE)) {
break;
}
}
}
else if (rcvdPacket.isEOF_ACK()) {
// first EOF ACK from receiver -> reset some state variables
printf("Source port: %d\nSource Address: %d\nCurrent Client Port: %d\nCurrent Client Ip: %d\n",
htons(cliAddr.sin_port),
cliAddr.sin_addr.s_addr,
currentClientPort,
currentClientIp);
if (htons(cliAddr.sin_port) == currentClientPort && cliAddr.sin_addr.s_addr == currentClientIp) {
free(fileContents);
fileLength = 0;
windowStart = 0;
currentClientPort = -1;
currentClientIp = -1;
eofPosition = -1;
}
Packet pkt(-1, EOF_ACK, NULL, 0);
printf("RETRANSMISSION: Sending EOF_ACK\n");
int serializedLength;
char* ackbuf = pkt.serialize(&serializedLength);
int bytesSent = sendto(sockfd, (void*)ackbuf, serializedLength, 0, (struct sockaddr *)&cliAddr, clilen);
free(ackbuf);
if (bytesSent < 0) {
error("ERROR on sending ack");
}
}
else if (rcvdPacket.isACK()) {
int ackNum = rcvdPacket.getAckNum();
if (ackNum > windowStart) {
printf("Received ACK packet with ACK number %d\n", rcvdPacket.getAckNum());
int stop;
if (eofPosition != -1)
stop = eofPosition;
else
stop = ackNum + WINDOW_SIZE;
gettimeofday(&timeVal, NULL);
t = timeVal.tv_usec/1000 + timeVal.tv_sec*1000;
for (int i = windowStart + WINDOW_SIZE; i < stop; i+=DATA_LEN) {
if (!send_pkt_with_seq_num(i, fileContents, fileLength, sockfd, cliAddr, stop))
break;
}
windowStart = ackNum;
printf("Window Start: %d\n", windowStart);
}
}
} /* end of while */
return 0; /* we never get here */
}