-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclientsource.c
428 lines (364 loc) · 12.9 KB
/
clientsource.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include "packet.h"
#include "queue.h"
#include <errno.h>
#include <pthread.h>
#define PAYLOAD 512
void printsent(struct packet* message, int cwnd, int ssthresh){
printf("SEND %i %i %i %i%s%s%s%s\n", message->seqNum, message->ackNum, cwnd, ssthresh, message->ack ? " ACK" : "", message->syn ? " SYN" : "", message->fin ? " FIN" : "", message->dup ? " DUP" : "");
}
void printreceived(struct packet* message, int cwnd, int ssthresh){
printf("RECV %i %i %i %i%s%s%s%s\n", message->seqNum, message->ackNum, cwnd, ssthresh, message->ack ? " ACK" : "", message->syn ? " SYN" : "", message->fin ? " FIN" : "", message->dup ? " DUP" : "");
}
unsigned short max(unsigned short a, unsigned short b){
if (a > b)
return a;
else
return b;
}
unsigned short min(unsigned short a, unsigned short b){
if (a < b)
return a;
else
return b;
}
int fin(int sockfd, struct sockaddr* address, unsigned short* seqNum){
//use fin to close connection
struct packet message, ack;
struct sockaddr_in server;
socklen_t addressSize = sizeof(server);
int attempts;
memset(&message, 0, sizeof(message));
message.fin = 1;
message.seqNum = *seqNum;
for (attempts = 0; attempts < 3; attempts++){
//send fin
if (sendto(sockfd, (void*) &message, 12, 0, address, sizeof(*address)) == -1){
fprintf(stderr, "ERROR: Couldn't send fin to server, %s\n", strerror(errno));
return -1;
}
printsent(&message, 0, 0);
//receive finack
if (recvfrom(sockfd, (void*) &ack, 12, 0, NULL, NULL) == -1){
if (errno == EAGAIN)
continue;
fprintf(stderr, "ERROR: Couldn't receive finack from server, %s\n", strerror(errno));
return -1;
}
printreceived(&ack, 0, 0);
break;
}
struct timeval start, now;
gettimeofday(&start, 0);
do{
//recieve fins
if (recvfrom(sockfd, (void*) &message, 12, 0, (struct sockaddr*) &server, &addressSize) == -1){
if (errno == EAGAIN){
gettimeofday(&now, 0);
continue;
}
fprintf(stderr, "ERROR: Couldn't receive fin from server, %s\n", strerror(errno));
return -1;
}
printreceived(&message, 0, 0);
if (server.sin_addr.s_addr == ((struct sockaddr_in*)address)->sin_addr.s_addr && server.sin_port == ((struct sockaddr_in*)address)->sin_port && message.fin){
ack.ackNum = message.seqNum;
ack.seqNum = *seqNum + 1;
//send finacks
if (sendto(sockfd, (void*) &ack, 12, 0, address, sizeof(*address)) == -1){
fprintf(stderr, "ERROR: Couldn't send finack to server, %s\n", strerror(errno));
return -1;
}
printsent(&ack, 0, 0);
}
gettimeofday(&now, 0);
} while (now.tv_sec - start.tv_sec < 2);
return 0;
}
int transmit(int file, int sockfd, struct sockaddr* address, unsigned short* seqNum, unsigned short* ackNum){
//this function will be used to split the file into smaller sections and send to server
int numRead, cwnd = 512, ssthresh = 5120, duplicates = 0;
char first = 1, done = 0;
struct packet ack;
//queue contains all packets that have been sent but not yet acknowled
Queue window;
init(&window);
struct timeval latest, now;
gettimeofday(&latest, 0);
//main loop, alternates between sending and receiving loops
while(window.len != 0 || !done){
//handle timeouts
if (window.len > 0){
gettimeofday(&now, 0);
struct timeval diff = getTimer(&window);
if(now.tv_sec - latest.tv_sec >= 10){
fprintf(stderr, "Timeout\n");
return -1;
}
else if (diff.tv_sec > 0 || diff.tv_usec/1000 > 500){
struct packet* message = top(&window);
message->dup = 1;
if(sendto(sockfd, (void*) message, 12 + message->len, 0, address, sizeof(*address)) == -1){
fprintf(stderr, "ERROR: Couldn't send packet to server, %s\n", strerror(errno));
free(message);
delete(&window);
return -1;
}
printsent(message, cwnd, ssthresh);
resetTimer(&window);
ssthresh = max (cwnd / 2, 1024);
cwnd = 512;
}
}
//creates packet from file and sends
if(window.len < cwnd / 512 && !done){
//prepare message
struct packet* message = (struct packet*) malloc(sizeof(struct packet));
memset(message, 0, sizeof(*message));
if (first){
message->ack = 1;
first = 0;
}
message->seqNum = *seqNum;
message->ackNum = *ackNum;
numRead = read(file, message->message, PAYLOAD);
if (numRead == -1){
fprintf(stderr, "ERROR: Couldn't read file, %s\n", strerror(errno));
free(message);
delete(&window);
return -1;
} else if (numRead == 0){
done = 1;
break;
} else if (numRead < PAYLOAD)
done = 1;
message->len = numRead;
//send message
if(sendto(sockfd, (void*) message, 12 + numRead, 0, address, sizeof(*address)) == -1){
fprintf(stderr, "ERROR: Couldn't send packet to server, %s\n", strerror(errno));
free(message);
delete(&window);
return -1;
}
printsent(message, cwnd, ssthresh);
*seqNum = (*seqNum + numRead) % 25601;
push(&window, message);
}
//receive
//this loop continues when there are no more packets in the stream to read
//receive ack from server
if (recvfrom(sockfd, (void*) &ack, 12, 0, NULL, NULL) == -1){
if (errno == EAGAIN || errno == EWOULDBLOCK){//nothing to read
//should there be more?
continue;
}
else{
fprintf(stderr, "ERROR: Couldn't receive ack from server, %s\n", strerror(errno));
return -1;
}
}
gettimeofday(&latest, 0);
if(ack.ack == 0){
fprintf(stderr, "ERROR: Packet is not an ACK\n");
}
//check sender?
printreceived(&ack, cwnd, ssthresh);
resetTimer(&window);
//handle ackNums
if (window.len > 0){
if (ack.ackNum == getTopSeq(&window)){ //what if no ack arrives
duplicates++;
//fast retransmit
if (duplicates == 3){
ssthresh = max(cwnd/2, 1024);
cwnd = min(ssthresh + 1536, 10240);
top(&window)->dup = 1;
if(sendto(sockfd, (void*) top(&window), 12 + top(&window)->len, 0, address, sizeof(*address)) == -1){
fprintf(stderr, "ERROR: Couldn't send packet to server, %s\n", strerror(errno));
delete(&window);
return -1;
}
printsent(top(&window), cwnd, ssthresh);
}
else if(duplicates > 3 && cwnd < 10240){
cwnd = min(cwnd + 512, 10240);
}
}
else{
while(window.len > 0 && ack.ackNum != getTopSeq(&window)){
struct packet* message = pop(&window);
free(message);
//End of Fast Recovery
if(duplicates >= 3){
cwnd = min(ssthresh, 10240);
duplicates = 0;
}
//congestion avoidance
if (cwnd < 10240){
if (cwnd <= ssthresh)
cwnd = min(cwnd + 512, 10240);
else
cwnd = min(cwnd + (512 * 512) / cwnd, 10240);
}
}
duplicates = 0;
}
}
}
return 0;
}
int handshake(int sockfd, struct sockaddr* address, unsigned short* seqNum, unsigned short* ackNum){
//creates handshake with server specified by address parameter
*seqNum = rand() % 25601; //assigning random sequence number
struct packet syn, synack;
memset(&syn, 0, sizeof(syn));
syn.syn = 1;
syn.seqNum = *seqNum;
socklen_t size = sizeof(struct sockaddr_in);
//send syn
if (sendto(sockfd, (void*) &syn, 12, 0, address, size) == -1){
fprintf(stderr, "ERROR: Couldn't send SYN to server, %s\n", strerror(errno));
return -1;
}
printsent(&syn, 0, 0);
//wait for ack
if (recvfrom(sockfd, (void*) &synack, 12, 0, NULL, 0) == -1){
fprintf(stderr, "ERROR: Couldn't receive SYNACK from server, %s\n", strerror(errno));
return -1;
}
if (synack.syn != 1)
return -1;
printreceived(&synack, 0, 0);
*ackNum = synack.seqNum + 1;
if (synack.ackNum != *seqNum + 1){
fprintf(stderr, "synack returned incorrect ack number\n");
return -1;
}
(*seqNum)++;
return 0;
}
long iptolong(char* ip){
//converts string ip in format x.x.x.x to a long integer
int i = 0, a = 0;
long result = 0;
while (ip[i] != 0){
if (ip[i] == '.'){
result += a;
result *= 256;
a = 0;
} else if (ip[i] < '0'|| ip[i] > '9'){
fprintf(stderr, "ERROR: improper ip address\n");
return -1;
} else{
a *= 10;
a += ip[i] - '0';
}
i++;
}
result += a;
return result;
}
int stringtoint(char* string){
//converts a string to the int it represents
int i, size = strlen(string), number = 0;
for (i = 0; i < size; i++){
number *= 10;
if (string[i] > '9' || string[i] < '0')
return -1;
number += string[i] - '0';
}
return number;
}
int main(int argc, char* argv[]){
//handling command line arguments
long servername;
int port;
char* filename;
if (argc != 4){
fprintf(stderr, "ERROR: Incorrect number of arguments.\nusage: %s <HOSTNAME> <PORT> <FILENAME>\n", argv[0]);
exit(1);
}
if (strcmp(argv[1], "localhost") == 0)
servername = 0x7f000001;
else{
servername = iptolong(argv[1]);
if (servername == -1){
fprintf(stderr, "ERROR: Improper hostname\n");
exit(1);
}
}
port = stringtoint(argv[2]);
if (port < 0 || port > 65535){
fprintf(stderr, "ERROR: Improper port number\n");
exit(1);
}
filename = argv[3];
int filefd = open(filename, O_RDONLY);
if (filefd < 0){
fprintf(stderr, "ERROR: Couldn't open file %s: %s\n", filename, strerror(errno));
exit(1);
}
//setting up socket
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
unsigned short seqNum, ackNum;
struct sockaddr_in their_addr;
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(port);
their_addr.sin_addr.s_addr = htonl(servername);
memset(their_addr.sin_zero, '\0', sizeof(their_addr.sin_zero));
struct sockaddr* addr = (struct sockaddr*) &their_addr;
//timeout setting
struct timeval timeout = {2, 0};
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void*) &timeout, sizeof(timeout));
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFD, flags | O_NONBLOCK);
//handshake
int tries;
for(tries = 0; tries < 3; tries++){
if (handshake(sockfd, addr, &seqNum, &ackNum) == -1){
fprintf(stderr, "Handshake failed.\n");
}
else{
tries = 0;
break;
}
}
if(tries){
fprintf(stderr, "Max retries reached. Quitting program.\n");
close(sockfd);
close(filefd);
exit(1);
}
//timeout setting
timeout.tv_sec = 0;
timeout.tv_usec = 5000;
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void*) &timeout, sizeof(timeout));
//transmit file
if (transmit(filefd, sockfd, addr, &seqNum, &ackNum) == -1){
fprintf(stderr, "Transmission failed. Quitting program.\n");
close(sockfd);
close(filefd);
exit(1);
}
//timeout setting
timeout.tv_sec = 2;
timeout.tv_usec = 0;
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void*) &timeout, sizeof(timeout));
//fin
if (fin(sockfd, addr, &seqNum) == -1){
fprintf(stderr, "Fin failed. Quitting program.\n");
close(sockfd);
close(filefd);
exit(1);
}
//closing socket
close(sockfd);
close(filefd);
return 0;
}