-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam13_tftp.c
540 lines (436 loc) · 14.9 KB
/
team13_tftp.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
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include "team13_struct.h"
#include <time.h>
#define MYPORT "5000" // the port users will be connecting to
#define MAXFILE 33554432
void senderror(int socket,short unsigned error_code,struct sockaddr_in* client_addr_ptr,socklen_t addr_len){
struct error_packet errorPacket;
short unsigned opcode;
//printf("the errorcode is %d\n.",error_code);
//error_code=htons(error_code);
errorPacket.error_code=htons(error_code);
opcode=5;
opcode=htons(opcode);
errorPacket.opcode=opcode;
//printf("1:%s\n",errormsg[error_code]);
strcpy(errorPacket.error_msg,errormsg[error_code]);
//printf("2:%s\n",errormsg[error_code]);
printf("send ERROR no.%d to the socket %d\n",error_code,socket);
sendto(socket,&errorPacket,sizeof(errorPacket),0,(struct sockaddr*)client_addr_ptr,addr_len);
}
int senddata(node* listTemp,int resultACK,socklen_t addr_len){
node* temp_ptr=listTemp;
int lastsize,block_no;// put lastsize into node
short unsigned opcode;
struct data_packet dataPacket;
opcode=3;//packet opcode
opcode=htons(opcode);
dataPacket.opcode=opcode;
/* lastsize=fread(&dataPacket.data,1,512,temp_ptr->fp_local); *///how many bytes have been read //packet data
if(!resultACK)//not a duplicate ACK
{
if(temp_ptr->lastsize<512){
close(temp_ptr->clientfd);//close the fd
printf("complete the transmission to the socket %d\n",temp_ptr->clientfd);
return temp_ptr->clientfd;//return the fd also as the boolean value of lastPacket
}
else{
lastsize=fread(dataPacket.data,1,512,temp_ptr->fp_local);
//printf("the packet size is %d\n",lastsize+4);
//printf("%s\n",dataPacket.data);
temp_ptr->lastsize=lastsize;//return lastsize to node
block_no=htons(temp_ptr->block_number);//packet block_no
dataPacket.block_number=block_no;
printf("send block no.%d to the socket %d\n",temp_ptr->block_number,temp_ptr->clientfd);
sendto(temp_ptr->clientfd,&dataPacket,lastsize+4,0,(struct sockaddr*)&(temp_ptr->client_addr),addr_len);//send a normal packet
temp_ptr->block_number+=1;
/* temp_ptr->position=ftell(temp_ptr->fp_local); */
return 0;
}
}
else{
//resend the former packet
fseek(temp_ptr->fp_local,-(temp_ptr->lastsize),1);//move the ptr to former block
temp_ptr->block_number-=1;
block_no=htons(temp_ptr->block_number);//packet block_no
dataPacket.block_number=block_no;
lastsize=fread(&dataPacket.data,1,512,temp_ptr->fp_local);
temp_ptr->lastsize=lastsize;//return lastsize to node
printf("RESEND block no.%d to the socket %d\n",temp_ptr->block_number,temp_ptr->clientfd);
sendto(temp_ptr->clientfd,&dataPacket,lastsize+4,0,(struct sockaddr*)&(temp_ptr->client_addr),addr_len);//resend last packet smaller than 512 byte
temp_ptr->block_number+=1;
return 0;
}
}
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa){
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
//campare two addresses, if same, return 0; otherwise return 1.
int addrCompare(struct sockaddr_in addr1,struct sockaddr_in addr2){
if((addr1.sin_port==addr2.sin_port)
&& (addr1.sin_addr.s_addr==addr2.sin_addr.s_addr))
return 0;
else
return 1;
}
//if found a node whose filename and address is same as that of the new one,
//it is a duplicate RQQ, return 1; otherwise is a new RQQ;
int rqqCheck(node* head,char* file_name,struct sockaddr_in client_address){
node* temp_ptr=head;
while(temp_ptr!=NULL){
if(!addrCompare(client_address,temp_ptr->client_addr)
&& (!strcmp(file_name,temp_ptr->filename))){
return 1;
}
temp_ptr=temp_ptr->following;
}
return 0;
}
//find the corresponding information for the current connection
node* find(node* listHead, int sockfd){
node* temp_ptr=listHead;
while(temp_ptr!=NULL){
if(temp_ptr->clientfd==sockfd){
return temp_ptr;
}
temp_ptr=temp_ptr->following;
}
return NULL;
}
//check whether the ack is duplicate, if same as the block_no,
//if perfect, return 0; otherwise packet lost, return 1;
int ackCheck(node* temp_ptr,int block_number){
if(temp_ptr->block_number-1==block_number){
return 0;
}
else{
return 1;
}
}
void listDelete(node* temp_ptr,node** headptr_addr,node** tailptr_addr){
//node *temp_ptr,*temp_front,*temp_tail;
if((*headptr_addr)==(*tailptr_addr)){
*headptr_addr=NULL;
*tailptr_addr=NULL;
}
else{
if(*headptr_addr==temp_ptr){
//it is the first nod
*headptr_addr=(*headptr_addr)->following;
(*headptr_addr)->previous=NULL;
}
else{//it is the tail or in the body
if(*tailptr_addr==temp_ptr){
*tailptr_addr=(*tailptr_addr)->previous;
(*tailptr_addr)->following=NULL;
}
else{//it is in the body
temp_ptr->previous->following=temp_ptr->following;
temp_ptr->following->previous=temp_ptr->previous;
}
}
}
free(temp_ptr);
}
struct timeval timeMinus(struct timeval time1, struct timeval time2){
//this program execute minus
struct timeval result;
int timeTotal;
timeTotal=1000000*(time1.tv_sec-time2.tv_sec)+time1.tv_usec-time2.tv_usec;
result.tv_sec=timeTotal/1000000;
result.tv_usec=timeTotal-1000000*result.tv_sec;
return result;
}
int main(void){
fd_set master; //the field to be supervised
fd_set read_fds; //the temp field to superviesd by select()
int fdmax; //record the max fd used in select()
int listener; //the file discriptor for listener
int newfd; //the file discriptor for new socket
int rv; //result of establishing IP address for getaddrinfo
int i; //index in for loop
int yes=1; //as a parameter to reuse port
int nbytes; //the numbers of bytes received
int resultCheck; //check the received message is expected
int lastPacket; //flag to indicate whether it is the last packet
int lastSize;
int client_portno;
char client_port[5];
FILE *fp=NULL;
int filesize;
//packet info
short unsigned opcode;
short unsigned block_number;
short unsigned error_code;
char msg_body[512];
char filename[512];
//address info
struct addrinfo hints, *servinfo, *clientinfo,*p;
struct sockaddr_in client_addr;
//paraments for list to record all clients information
node* listHead=NULL;
node* listEnd=NULL;
node* listTemp=NULL;
//container for different kinds of data packets
data_packet dataPacket;
ack_packet ackPacket;
error_packet errorPacket;
generic_packet rqqPacket;
//time used in select(); and record the time transmission for timeout
struct timeval tv,start_time,end_time,interval_time;
//temp container for data received or prepared to send
char buf[516];
socklen_t addr_len;
char remoterIP[INET6_ADDRSTRLEN];
//use time()
time_t rawtime;
struct tm * timeinfo;
FD_ZERO(&master);
FD_ZERO(&read_fds);
//seed to generate random port number
srand(time(NULL));
//
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE; // use my IP
if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((listener = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
perror("listener: socket");
continue;
}
setsockopt(listener,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int));
if (bind(listener, p->ai_addr, p->ai_addrlen) == -1) {
close(listener);
perror("listener: bind");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "listener: failed to bind socket\n");
return 2;
}
freeaddrinfo(servinfo);
printf("listener: waiting to recvfrom...\n");
addr_len = sizeof client_addr;
FD_SET(listener,&master);
fdmax=listener;
//initial to a large time period
tv.tv_sec=10;
tv.tv_usec=0;
for(;;){
read_fds=master;
if(select(fdmax+1,&read_fds,NULL,NULL,&tv)==-1){
perror("\nselect\n");
exit(4);
}
//if(!FD_ISSET(listener,&read_fds))
//printf("listener missiong\n");
for(i=0;i<=fdmax;i++){
if(FD_ISSET(i,&read_fds)){
//we got one connect
time ( &rawtime );
timeinfo = localtime ( &rawtime );
printf ( "\n%s =======================\n", asctime (timeinfo));
if(i==listener){
//it is a new connection
//printf("get a new connection\n");
memset(buf,'\0',sizeof buf);
if((nbytes=recvfrom(listener,buf,516,0,(struct sockaddr *)&client_addr,&addr_len))==-1){
//get a wrong packet,do noting
printf("get a wrong message!\n");
}
else{
//get a complete message
//printf("get a complete message\n");
memcpy(&opcode,buf,sizeof(opcode));
opcode=ntohs(opcode);
//printf("the opcode is %d\n",opcode);
if(opcode!=1){
//this port is for RQQ only
printf("this port is for RQQ only\n");
}
else{
//this is a RQQ expected
memset(filename,'\0',sizeof filename);
//memcpy(filename,buf+2,512);
strcpy(filename,buf+2);
//printf("the file name is %s\n",filename);
//check duplication,if duplicate return 1, pass return 0;
resultCheck=rqqCheck(listHead,filename,client_addr);
//printf("the resultCheck is %d\n",resultCheck);
if(resultCheck==1){
printf("it is a duplicate rqq\n");
}
else{
//create a socket
client_portno = rand();
client_portno=client_portno % 1000+5000;
memset(client_port,'\0',sizeof client_port);
sprintf(client_port,"%d",client_portno);
//printf("the client_port is %s\n",client_port);
if ((rv = getaddrinfo(NULL, client_port, &hints, &clientinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
return 1;
}
// loop through all the results and bind to the first we can
for(p = clientinfo; p != NULL; p = p->ai_next) {
if ((newfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
perror("listener: socket");
continue;
}
setsockopt(newfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int));
if (bind(newfd, p->ai_addr, p->ai_addrlen) == -1) {
close(newfd);
perror("newfd: bind");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "newfd: failed to bind socket\n");
return 2;
}
freeaddrinfo(clientinfo);
printf("set new socket %d successfull\n",newfd);
if((fp=fopen(filename,"rb"))!=NULL){
FILE *fp1=fp;
fseek(fp1,0L,2);
filesize=ftell(fp1);
//fclose(fp1);
printf("the file length is %d\n",filesize);
if(filesize > MAXFILE){
//send an error, file too large
senderror(newfd,ILLEGALOPERR,&client_addr,addr_len);
close(newfd);
}
else{
//printf("start to creat node\n");
//create a node in the list
listTemp=(node*)malloc(sizeof(node));
listTemp->clientfd=newfd;
listTemp->fp_local=fopen(filename,"rb");
listTemp->block_number=1;
listTemp->filesize=filesize;
listTemp->lastsize=512;
memset(listTemp->filename,'\0',sizeof filename);
strcpy(listTemp->filename,filename);
memcpy(&(listTemp->client_addr),&(client_addr),sizeof(client_addr));
gettimeofday(&(listTemp->sendtime),0);
listTemp->previous=NULL;
listTemp->following=NULL;
//add the node to the list
if(listHead==NULL){
listHead=listTemp;
listEnd=listTemp;
}
else{
listEnd->following=listTemp;
listTemp->previous=listEnd;
listEnd=listTemp;
}
//printf("insert information successfully\n");
//form a DATA to send
senddata(listTemp,0,addr_len);
printf("sent first data successfully\n");
FD_SET(newfd,&master);
if(newfd>fdmax){
fdmax=newfd;
}
}
}
else{
//form a ERROR to send
//printf("I can before senderror\n");
senderror(newfd,NOTFOUNDERR,&client_addr,addr_len);
//printf("I can behind senderror\n");
close(newfd);
}
}
}//end of complete RQQ
}//end of recerive a complete packet
}//end of the message is from listener
else{
//message from existing connection
memset(buf,'\0',sizeof buf);
if((nbytes=recvfrom(i,buf,516,0,(struct sockaddr *)&client_addr,&addr_len))==-1){
//get a wrong packet,do noting
printf("get a wrong message!\n");
}
else{
//get a complete packet
//printf("we can be here\n");
listTemp=find(listHead,i);
memcpy(&opcode,buf,sizeof(opcode));
opcode=ntohs(opcode);
if(opcode!=4){
//this port is for ACK only
printf("this port is for ACK only\n");
}
else{
//this is a ACK
memcpy(&block_number,buf+2,sizeof(block_number));
block_number=ntohs(block_number);
//check if the ACK is duplicate, if duplicate, return 1, else return 0;
resultCheck=ackCheck(listTemp,block_number);
gettimeofday(&(listTemp->sendtime),0);
//send DATA packet properly according to the resultCheck;
//if ack for last packet, close the connection,
//return socketfd,otherwise return 0
lastPacket=senddata(listTemp,resultCheck,addr_len);
if(lastPacket){
//last packet has been sent
listDelete(listTemp,&listHead,&listEnd);
FD_CLR(i,&master);
}
//else{
//listTemp->block_no++;
//}
}//end for ACK
}//end for complete packet
}//end for existing connection
}//end of FD_ISSET
}//end of small for loop
gettimeofday(&end_time,0);
//sent timeout equal to 5s
tv.tv_sec=5;
tv.tv_usec=0;
for(listTemp=listHead;listTemp!=NULL;listTemp=listTemp->following){
interval_time=timeMinus(end_time,listTemp->sendtime);
//timeout=5s=5000000us
//if time interval >timeout
if(interval_time.tv_sec * 1000000+interval_time.tv_usec > 5000000){
gettimeofday(&(listTemp->sendtime),0);
//resend
senddata(listTemp,1,addr_len);
}
else{
//record the minimal time
if(interval_time.tv_sec * 1000000+interval_time.tv_usec < tv.tv_sec * 1000000+tv.tv_usec){
tv.tv_sec=interval_time.tv_sec;
tv.tv_usec=interval_time.tv_usec;
}
}
}//end of the second small loop
}//and of big for loop
}