-
Notifications
You must be signed in to change notification settings - Fork 2
/
FTPserver2.cpp
349 lines (279 loc) · 11 KB
/
FTPserver2.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
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
/*
* FTPserver.cpp
*
* Created on: Mar 4, 2016
* Author: jeremy
*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <istream>
#include <sstream>
#include <fstream>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <ctime>
#include "packet.h"
//This is the line that displays if there are not 2 parameters input.
#define USAGE "Usage:\r\n [probability of packet corruption in int form] [probability of packet loss in int form]\r\n"
//This is the size of the Packet minus the size of the header.
#define BUFSIZE 249
#define PAKSIZE 256
//The port we will be communicating through
#define PORT 10038
#define ACK 0
#define TIMEOUT 10
#define NAK 1
using namespace std;
bool seqNum;
//The gremlin function is declared in this scope, at the bottom of the page.
bool gremlin(Packet * pack, int corruptProb, int lossProb);
//This method takes 3 arguments. The first is the number of the tux machine, the second is the prob
// of corruption to go in the gremlin function, and the final is the probability of a dropped packet
//for the gremlin function.
int main(int argc, char** argv) {
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 10000;
//This variable will hold the socket descriptor
int s = 0;
int rlen;
//If there correct amount of arguement are not input, the method will end.
if (argc != 3) {
cout << USAGE << endl;
return 1;
}
//Takes the 2 arguments and converts them into integers.
char * probCorruptStr = argv[1];
int probCorrupt = atoi(probCorruptStr);
char * probLossStr = argv[2];
int probLoss = atoi(probLossStr);
//Create socket address variable for the client and server.
struct sockaddr_in server;
struct sockaddr_in client;
socklen_t salen = sizeof(client);
//This may be used for the alternative to the hard coded Auburn addresses.
// struct hostent *h;
//If socket is not successful created the program ends.
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
cout << "Socket creation failed. (socket " << s << ")" << endl;
return 0;
}
//This initializes the server (computer running this program) information.
memset((char *) &server, 0, sizeof(server));
server.sin_family = AF_INET;
//INADDR_ANY is meant to get the address of the local machine.
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(PORT);
//binds this program to the socket.
if (bind(s, (struct sockaddr *) &server, sizeof(server)) < 0) {
cout << "Socket binding failed. (socket s, address server)" << endl;
return 0;
}
cout << endl;
/* Loop forever, waiting for messages from a client. */
cout << "Waiting on port " << PORT << "..." << endl;
for (;;) {
char filename[256];
rlen = recvfrom(s, filename, PAKSIZE, 0, (struct sockaddr *) &client,
&salen);
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
filename[rlen] = '\0';
cout << endl << "This is the name of the file received" << filename << endl;
const char * fn = reinterpret_cast<const char *>(filename);
//Open specific file for reading.
ifstream is(fn, ios::out);
//Create a buffer of the packet size without the header to use for reading.
unsigned char b[BUFSIZE];
char * file;
int length;
//if the file exists, read it into the file parameter for later use.
if (is) {
is.seekg(0, is.end);
length = is.tellg();
is.seekg(0, is.beg);
file = new char[length];
cout << "Reading " << length << " characters..." << endl;
is.read(file, length);
if (!is)
cout << "File reading failed. (filename " << filename
<< "). Only " << is.gcount() << " could be read.";
is.close();
}
//Makes a string from the file that was read in earlier.
string fstr = string(file);
//Prints out that file.
cout << "File: " << endl << fstr << endl << endl;
//Initalize the seqNum and dropPacket variables before getting ready to send packets.
seqNum = true;
bool dropPck = false;
//This for loop does the segmentation of the packet. It runs the size of the file divided by the total size
//of the buffer (249 bytes)
for (int x = 0; x <= length / BUFSIZE; x++) {
cout << endl;
cout << "=== TRANSMISSION START" << endl;
//gets the next 249 bytes of the file.
string mstr = fstr.substr(x * BUFSIZE, BUFSIZE);
//Marks where the file ends with the null character because if the last read went beyond the length of the
//file length - total already read will give you how much was left to be read.
if (x * BUFSIZE + BUFSIZE > length) {
mstr[length - (x * BUFSIZE)] = '\0';
}
//Create a packet with the string we just read from our file. The seqNum will alternate
//between 1 and 2 depending on if the packet gets dropped.
int startTime = clock();
Packet p;
createPacket(seqNum, mstr.c_str(), &p);
//Send the gremlin the probablities. If the packet gets dropped go to the next packet.
if ((dropPck = gremlin(&p, probCorrupt, probLoss)) == false) {
//If the packet does not get dropped send it.
if (sendto(s, str(p), BUFSIZE + 7, 0,
(struct sockaddr *) &client, sizeof(client)) < 0) {
cout
<< "Package sending failed. (socket s, client address client, message m)"
<< endl;
return 0;
}
}
rlen = 0;
rlen = recvfrom(s, b, BUFSIZE + 7, 0, (struct sockaddr *) &client, &salen);
cout << rlen << endl;
if (rlen < 0){
//Get the sequence number from the message
char * sns = new char[2];
memcpy(sns, &b[0], 1);
sns[1] = '\0';
//Get the checksum from the message
char * css = new char[6];
memcpy(css, &b[1], 6);
css[5] = '\0';
//get the data from the received message.
char * db = new char[BUFSIZE + 1];
memcpy(db, &b[7], BUFSIZE);
db[BUFSIZE] = '\0';
//Print out sequence number and checksum
cout << "Sequence number: " << sns << endl;
cout << "Checksum: " << css << endl;
cout << "timeout occurred." << endl;
Packet pk;
createPacket(0, db, &pk);
setSeqNum(atoi(sns), &pk);
setCkSum(atoi(css), &pk);
x--;
//reset the value of b.
memset(b, 0, BUFSIZE);
continue;
}
//Send the gremlin the probablities. If the packet gets dropped go to the next packet.
if ((dropPck = gremlin(&p, probCorrupt, probLoss)) == false) {
//If the packet does not get dropped send it.
if (sendto(s, str(p), BUFSIZE + 7, 0,
(struct sockaddr *) &client, sizeof(client)) < 0) {
cout << "Package sending failed. (socket s, client address client, message m)"<< endl;
return 0;
}
} else continue;
}
char * ack = new char[3];
b[rlen] = '\0';
cout << endl << "=== SERVER RESPONSE TEST" << endl;
cout << "Data: " << b << endl;
//Check the last spot in the header for the ack or nak.
if (b[6] == '0')
ack = (char *) "ACK";
else
ack = (char *) "NAK";
cout << "Response: " << ack << endl;
//If the file was corrupted.
if (b[6] != '0') { //if NAK
/* should say: if chksm(). chksm should be a function both client and server
* can see and use that returns a boolean: true if the checksum "checks out"
* (no bytes have been tampered with).
*/
//Get the sequence number from the message
char * sns = new char[2];
memcpy(sns, &b[0], 1);
sns[1] = '\0';
//Get the checksum from the message
char * css = new char[6];
memcpy(css, &b[1], 6);
css[5] = '\0';
//get the data from the received message.
char * db = new char[BUFSIZE + 1];
memcpy(db, &b[7], BUFSIZE);
db[BUFSIZE] = '\0';
//Print out sequence number and checksum
cout << "Sequence number: " << sns << endl;
cout << "Checksum: " << css << endl;
//Create new packet with old information.
Packet pk;
createPacket(0, db, &pk);
setSeqNum(atoi(sns), &pk);
setCkSum(atoi(css), &pk);
//If calculated checksums are the same resend the last packet.
if (ckSum(pk)) {
x--;
}
//If they are not the same send last two packets, unless this is the less than the 3rd packet, then
//start from 0.
else {
if (x - 2 > 0) {
x = x - 2;
} else {
x = 0;
}
}
delete css;
}
//reset the value of b.
memset(b, 0, BUFSIZE);
}
return 0;
}
bool gremlin(Packet *packet, int pCorr, int plost) {
bool drop = false;
int r = rand() % 100 + 1;
if (r <= plost) {
drop = true;
cout << "Packet has been dropped." << endl;
} else if (r <= pCorr) {
cout << "Packet has been corrupted." << endl;
int d = rand() % 101;
if (d <= 70) {
int c = rand() % 256;
packet->packet[c] = '\0';
} else if (d <= 90) {
int c = rand() % 256;
int d = rand() % 256;
packet->packet[c] = '\0';
while (d == c) {
d = rand() % 256;
}
packet->packet[d] = '\0';
} else {
int c = rand() % 256;
int d = rand() % 256;
int e = rand() % 256;
packet->packet[c] = '\0';
while (d == c) {
d = rand() % 256;
}
packet->packet[d] = '\0';
while (e == c || e == d) {
e = rand() % 256;
}
packet->packet[e] = '\0';
}
} else {
if (seqNum) {
seqNum = false;
} else {
seqNum = true;
}
cout << "Sequence number: " << packet->seqNum << endl;
cout << "Checksum: " << getCkSum(*packet) << endl;
cout << "Message: " << getData(*packet) << endl;
}
return drop;
}